From 85964de3517a15c3f18b75ebe84ca27a3373e9f3 Mon Sep 17 00:00:00 2001
From: raj_mathe <raj.dahya@math.uni-leipzig.de>
Date: Fri, 8 Apr 2022 17:07:36 +0200
Subject: [PATCH] =?UTF-8?q?master=20>=20master:=20code-rust=20-=20basic=20?=
 =?UTF-8?q?unit=20tests=20hinzugef=C3=BCgt?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 code/rust/tests/mod.rs                    |  5 ++
 code/rust/tests/test_core/mod.rs          |  1 +
 code/rust/tests/test_core/test_utils.rs   | 62 ++++++++++++++++++++++
 code/rust/tests/test_graphs/mod.rs        |  2 +
 code/rust/tests/test_stacks/mod.rs        |  1 +
 code/rust/tests/test_stacks/test_stack.rs | 63 +++++++++++++++++++++++
 6 files changed, 134 insertions(+)
 create mode 100644 code/rust/tests/mod.rs
 create mode 100644 code/rust/tests/test_core/mod.rs
 create mode 100644 code/rust/tests/test_core/test_utils.rs
 create mode 100644 code/rust/tests/test_graphs/mod.rs
 create mode 100644 code/rust/tests/test_stacks/mod.rs
 create mode 100644 code/rust/tests/test_stacks/test_stack.rs

diff --git a/code/rust/tests/mod.rs b/code/rust/tests/mod.rs
new file mode 100644
index 0000000..feb8881
--- /dev/null
+++ b/code/rust/tests/mod.rs
@@ -0,0 +1,5 @@
+extern crate ads2;
+
+pub mod test_core;
+pub mod test_stacks;
+pub mod test_graphs;
diff --git a/code/rust/tests/test_core/mod.rs b/code/rust/tests/test_core/mod.rs
new file mode 100644
index 0000000..681d26e
--- /dev/null
+++ b/code/rust/tests/test_core/mod.rs
@@ -0,0 +1 @@
+pub mod test_utils;
diff --git a/code/rust/tests/test_core/test_utils.rs b/code/rust/tests/test_core/test_utils.rs
new file mode 100644
index 0000000..96bc59a
--- /dev/null
+++ b/code/rust/tests/test_core/test_utils.rs
@@ -0,0 +1,62 @@
+// ----------------------------------------------------------------
+// IMPORTS
+// ----------------------------------------------------------------
+
+use ads2::core::utils;
+
+// ----------------------------------------------------------------
+// Test regex
+// ----------------------------------------------------------------
+
+#[test]
+fn test_regex() {
+    let re = utils::construct_regex("^a+(.*?)bc");
+    assert_eq!(re.is_match("aaa---bc"), true);
+    assert_eq!(re.is_match("aaa---b"), false);
+}
+
+// ----------------------------------------------------------------
+// Test min
+// ----------------------------------------------------------------
+
+#[test]
+fn test_min() {
+    assert_eq!(utils::min("beth", "alef"), "alef");
+    assert_eq!(utils::min("alef", "beth"), "alef");
+    assert_eq!(utils::min("alef", "ale"), "ale");
+    assert_eq!(utils::min(320, 24), 24);
+    assert_eq!(utils::min(0.03, 0.2), 0.03);
+}
+
+// ----------------------------------------------------------------
+// Test vector methods
+// ----------------------------------------------------------------
+
+#[test]
+pub fn test_restrict() {
+    let x: Vec<String> = vec![String::from("a"), String::from("b"), String::from("b"), String::from("c"), String::from("d"), String::from("e")];
+    assert_eq!(utils::restrict(&x, 0, 0), Vec::<String>::new());
+    assert_eq!(utils::restrict(&x, 2, 2), Vec::<String>::new());
+    assert_eq!(utils::restrict(&x, 0, 1), vec!["a"]);
+    assert_eq!(utils::restrict(&x, 2, 4), vec!["b", "c"]);
+    let x: Vec<i32> = vec![78, 100, -3, 1];
+    assert_eq!(utils::restrict(&x, 0, 0), Vec::<i32>::new());
+    assert_eq!(utils::restrict(&x, 4, 4), Vec::<i32>::new());
+    assert_eq!(utils::restrict(&x, 1, 3), vec![100, -3]);
+    let mut x_restr = utils::restrict(&x, 1, 3);
+    x_restr[0] = 58;
+    assert_ne!(x[1], 58, "Result should be a copy.");
+    assert_eq!(x[1], 100, "Result should be a copy.");
+}
+
+pub fn test_remove_first_last() {
+    let x: Vec<String> = vec!["merkur", "venus", "mars", "terra", "jupiter", "saturn", "uranus", "neptun"]
+            .iter()
+            .map(|s| {s.to_string()})
+            .collect::<Vec<String>>();
+    assert_eq!(utils::remove_first(&x), vec!["venus", "mars", "terra", "jupiter", "saturn", "uranus", "neptun"]);
+    assert_eq!(utils::remove_last(&x), vec!["merkur", "venus", "mars", "terra", "jupiter", "saturn", "uranus"]);
+    let x: Vec<f64> = vec![];
+    assert_eq!(utils::remove_first(&x), Vec::<f64>::new());
+    assert_eq!(utils::remove_last(&x), Vec::<f64>::new());
+}
diff --git a/code/rust/tests/test_graphs/mod.rs b/code/rust/tests/test_graphs/mod.rs
new file mode 100644
index 0000000..2a2c60d
--- /dev/null
+++ b/code/rust/tests/test_graphs/mod.rs
@@ -0,0 +1,2 @@
+pub mod test_graph;
+pub mod test_tarjan;
diff --git a/code/rust/tests/test_stacks/mod.rs b/code/rust/tests/test_stacks/mod.rs
new file mode 100644
index 0000000..33e9b4d
--- /dev/null
+++ b/code/rust/tests/test_stacks/mod.rs
@@ -0,0 +1 @@
+mod test_stack;
diff --git a/code/rust/tests/test_stacks/test_stack.rs b/code/rust/tests/test_stacks/test_stack.rs
new file mode 100644
index 0000000..de6ef42
--- /dev/null
+++ b/code/rust/tests/test_stacks/test_stack.rs
@@ -0,0 +1,63 @@
+// ----------------------------------------------------------------
+// IMPORTS
+// ----------------------------------------------------------------
+
+use std;
+extern crate closure;
+
+use ads2::stacks::stack;
+
+// ----------------------------------------------------------------
+// Test regex
+// ----------------------------------------------------------------
+
+// ----------------------------------------------------------------
+// Test stack
+// ----------------------------------------------------------------
+
+#[test]
+fn test_stack_initialisation() {
+    let stack = stack::Stack::<i32>::new();
+    assert_eq!(stack.len(), 0);
+}
+
+#[test]
+fn test_stack_push() {
+    let mut stack = stack::Stack::<String>::new();
+    assert_eq!(stack.len(), 0);
+    stack.push("hallo".to_string());
+    assert_eq!(stack.len(), 1);
+    stack.push("welt".to_string());
+    assert_eq!(stack.len(), 2);
+    stack.push("!".to_string());
+    assert_eq!(stack.len(), 3);
+}
+
+#[test]
+fn test_stack_no_error() {
+    let mut stack = stack::Stack::<String>::new();
+    stack.push("hallo".to_string());
+    stack.push("welt".to_string());
+    stack.push("!".to_string());
+    let result = std::panic::catch_unwind(closure::closure!(move mut stack, || {
+        stack.pop();
+        stack.pop();
+        stack.pop();
+    }));
+    assert!(result.is_ok());
+}
+
+#[test]
+fn test_stack_error_po() {
+    let mut stack = stack::Stack::<String>::new();
+    stack.push("hallo".to_string());
+    stack.push("welt".to_string());
+    stack.push("!".to_string());
+    let result = std::panic::catch_unwind(closure::closure!(move mut stack, || {
+        stack.pop();
+        stack.pop();
+        stack.pop();
+        stack.pop();
+    }));
+    assert!(result.is_err());
+}