master > master: code-rust - basic unit tests hinzugefügt

This commit is contained in:
RD 2022-04-08 17:07:36 +02:00
parent 2f4032b64a
commit 85964de351
6 changed files with 134 additions and 0 deletions

5
code/rust/tests/mod.rs Normal file
View File

@ -0,0 +1,5 @@
extern crate ads2;
pub mod test_core;
pub mod test_stacks;
pub mod test_graphs;

View File

@ -0,0 +1 @@
pub mod test_utils;

View File

@ -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());
}

View File

@ -0,0 +1,2 @@
pub mod test_graph;
pub mod test_tarjan;

View File

@ -0,0 +1 @@
mod test_stack;

View File

@ -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());
}