Compare commits
3 Commits
17723e71a7
...
cadd869155
Author | SHA1 | Date | |
---|---|---|---|
cadd869155 | |||
0d8583dd0c | |||
cc297011b2 |
33
code/rust/Cargo.lock
generated
33
code/rust/Cargo.lock
generated
@ -12,6 +12,7 @@ dependencies = [
|
||||
"itertools",
|
||||
"numpy",
|
||||
"regex 1.5.5 (git+https://github.com/rust-lang/regex.git)",
|
||||
"rstest",
|
||||
"rust-embed",
|
||||
"textwrap",
|
||||
"yaml-rust",
|
||||
@ -383,6 +384,19 @@ name = "regex-syntax"
|
||||
version = "0.6.25"
|
||||
source = "git+https://github.com/rust-lang/regex.git#258bdf798a14f50529c1665e84cc8a3a9e2c90fc"
|
||||
|
||||
[[package]]
|
||||
name = "rstest"
|
||||
version = "0.12.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d912f35156a3f99a66ee3e11ac2e0b3f34ac85a07e05263d05a7e2c8810d616f"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"rustc_version",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rust-embed"
|
||||
version = "6.3.0"
|
||||
@ -417,6 +431,15 @@ dependencies = [
|
||||
"walkdir",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustc_version"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366"
|
||||
dependencies = [
|
||||
"semver",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "same-file"
|
||||
version = "1.0.6"
|
||||
@ -432,6 +455,12 @@ version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
|
||||
|
||||
[[package]]
|
||||
name = "semver"
|
||||
version = "1.0.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d65bd28f48be7196d222d95b9243287f48d27aca604e08497513019ff0502cc4"
|
||||
|
||||
[[package]]
|
||||
name = "sha2"
|
||||
version = "0.9.9"
|
||||
@ -470,9 +499,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "textwrap"
|
||||
version = "0.14.2"
|
||||
version = "0.15.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0066c8d12af8b5acd21e00547c3797fde4e8677254a7ee429176ccebbe93dd80"
|
||||
checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb"
|
||||
dependencies = [
|
||||
"smawk",
|
||||
"unicode-linebreak",
|
||||
|
@ -8,6 +8,9 @@ members = [
|
||||
".",
|
||||
]
|
||||
|
||||
[dev-dependencies]
|
||||
rstest = { version = "0.12.0" }
|
||||
|
||||
[dependencies]
|
||||
rust-embed = { version = "6.3.0" }
|
||||
closure = { version = "0.3.0" }
|
||||
@ -16,7 +19,7 @@ dyn-fmt = { version = "0.3.0" }
|
||||
argparse = { version = "0.2.2" }
|
||||
regex = { git = "https://github.com/rust-lang/regex.git", version = "1.5.4" }
|
||||
yaml-rust = { git = "https://github.com/chyh1990/yaml-rust.git", version = "0.4.4" }
|
||||
textwrap = { version = "0.14.2" }
|
||||
textwrap = { version = "0.15.0" }
|
||||
|
||||
itertools = { version = "0.10.3" }
|
||||
numpy = { version = "0.16.2" }
|
||||
|
@ -4,6 +4,10 @@
|
||||
|
||||
extern crate regex;
|
||||
|
||||
use std::hash::Hash;
|
||||
use std::collections::HashMap;
|
||||
use std::collections::HashSet;
|
||||
|
||||
use self::regex::Regex;
|
||||
|
||||
// ----------------------------------------------------------------
|
||||
@ -57,5 +61,29 @@ pub fn remove_first<T>(x: &Vec<T>) -> Vec<T>
|
||||
if n == 0 {
|
||||
return Vec::<T>::new();
|
||||
}
|
||||
return restrict::<T>(x, 0, n);
|
||||
return restrict::<T>(x, 1, n);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------
|
||||
// METHODS Vectors to sets
|
||||
// ----------------------------------------------------------------
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn vec_to_set<T>(x: &Vec<T>) -> HashSet<T>
|
||||
where T: Eq + Hash + Clone
|
||||
{
|
||||
return x.iter()
|
||||
.map(|u| {u.clone()})
|
||||
.collect();
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn vec_to_multiset<T>(x: &Vec<T>) -> HashMap<T, usize>
|
||||
where T: Eq + Hash + Copy
|
||||
{
|
||||
let mut counted = HashMap::<T, usize>::new();
|
||||
for u in x.iter() {
|
||||
*counted.entry(u.clone()).or_insert(0) += 1
|
||||
}
|
||||
return counted;
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
extern crate ads2;
|
||||
extern crate rstest;
|
||||
|
||||
pub mod test_core;
|
||||
pub mod test_stacks;
|
||||
|
@ -49,6 +49,7 @@ pub fn test_restrict() {
|
||||
assert_eq!(x[1], 100, "Result should be a copy.");
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_remove_first_last() {
|
||||
let x: Vec<String> = vec!["merkur", "venus", "mars", "terra", "jupiter", "saturn", "uranus", "neptun"]
|
||||
.iter()
|
||||
@ -60,3 +61,29 @@ pub fn test_remove_first_last() {
|
||||
assert_eq!(utils::remove_first(&x), Vec::<f64>::new());
|
||||
assert_eq!(utils::remove_last(&x), Vec::<f64>::new());
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------
|
||||
// Test vector-to-set-conversions
|
||||
// ----------------------------------------------------------------
|
||||
|
||||
#[test]
|
||||
pub fn test_vec_to_set() {
|
||||
let x = vec!["b", "a", "a", "d", "d", "c", "b", "a"];
|
||||
let y1 = vec!["a", "a", "a", "b", "b", "c", "d", "d"];
|
||||
let y2 = vec!["d", "c", "b", "a", "d"];
|
||||
let z = vec!["b", "e", "f"];
|
||||
assert_eq!(utils::vec_to_set(&x), utils::vec_to_set(&y1), "Should match when both lists have similar elements");
|
||||
assert_eq!(utils::vec_to_set(&x), utils::vec_to_set(&y2), "Should match when both lists have similar elements, regardless of counts");
|
||||
assert_ne!(utils::vec_to_set(&x), utils::vec_to_set(&z), "Should not match when lists different elements");
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_vec_to_multiset() {
|
||||
let x = vec!["b", "a", "a", "d", "d", "c", "b", "a"];
|
||||
let y1 = vec!["a", "a", "a", "b", "b", "c", "d", "d"];
|
||||
let y2 = vec!["d", "c", "b", "a", "d"];
|
||||
let z = vec!["b", "e", "f"];
|
||||
assert_eq!(utils::vec_to_multiset(&x), utils::vec_to_multiset(&y1), "Should match when both lists have similar elements with the same element counts");
|
||||
assert_ne!(utils::vec_to_multiset(&x), utils::vec_to_multiset(&y2), "Should not match when both lists have similar elements but different counts");
|
||||
assert_ne!(utils::vec_to_multiset(&x), utils::vec_to_multiset(&z), "Should not match when lists different elements");
|
||||
}
|
||||
|
73
code/rust/tests/test_graphs/test_graph.rs
Normal file
73
code/rust/tests/test_graphs/test_graph.rs
Normal file
@ -0,0 +1,73 @@
|
||||
// ----------------------------------------------------------------
|
||||
// IMPORTS
|
||||
// ----------------------------------------------------------------
|
||||
|
||||
use rstest::fixture;
|
||||
use rstest::rstest;
|
||||
|
||||
use ads2::core::utils;
|
||||
use ads2::graphs::graph;
|
||||
|
||||
// ----------------------------------------------------------------
|
||||
// Fixtures
|
||||
// ----------------------------------------------------------------
|
||||
|
||||
#[fixture]
|
||||
fn fixture_graph() -> graph::Graph<i32> {
|
||||
return graph::Graph::new(
|
||||
vec![1,2,3,4,5,6,7,8],
|
||||
vec![(1,2), (1, 3), (2,3), (3,4), (4, 5), (5, 6), (6, 2), (6, 7), (6, 8)]
|
||||
);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------
|
||||
// Test Graph
|
||||
// ----------------------------------------------------------------
|
||||
|
||||
#[test]
|
||||
fn test_graph_create_noerror() {
|
||||
let result = std::panic::catch_unwind(|| {
|
||||
let gph = graph::Graph::new(
|
||||
vec![5, 7, 8],
|
||||
vec![(5,7), (7, 8)]
|
||||
);
|
||||
assert_eq!(gph.len(), 3);
|
||||
});
|
||||
assert!(result.is_ok());
|
||||
let result = std::panic::catch_unwind(|| {
|
||||
let gph = graph::Graph::new(
|
||||
vec!["5", "7", "8", "10"],
|
||||
vec![("5", "7"), ("7", "8")]
|
||||
);
|
||||
assert_eq!(gph.len(), 4);
|
||||
});
|
||||
assert!(result.is_ok());
|
||||
let result = std::panic::catch_unwind(|| {
|
||||
let gph = graph::Graph::<f64>::new(Vec::new(), Vec::new());
|
||||
assert_eq!(gph.len(), 0);
|
||||
});
|
||||
assert!(result.is_ok());
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
fn test_graph_subgraph(fixture_graph: graph::Graph<i32>) {
|
||||
let sub_gph = fixture_graph.subgraph(vec![2,4,5,6,8]);
|
||||
assert_eq!(utils::vec_to_set(&sub_gph.edges), utils::vec_to_set(&vec![(6,2), (4,5), (5,6), (6,8)]));
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
fn test_graph_successors_and_predecessors(fixture_graph: graph::Graph<i32>) {
|
||||
let u = 1;
|
||||
assert_eq!(utils::vec_to_set(&fixture_graph.successors(&u)), utils::vec_to_set(&vec![2, 3]));
|
||||
let u = 8;
|
||||
assert_eq!(utils::vec_to_set(&fixture_graph.successors(&u)).len(), 0);
|
||||
let u = 6;
|
||||
assert_eq!(utils::vec_to_set(&fixture_graph.successors(&u)), utils::vec_to_set(&vec![2, 7, 8]));
|
||||
|
||||
let u = 1;
|
||||
assert_eq!(utils::vec_to_set(&fixture_graph.predecessors(&u)).len(), 0);
|
||||
let u = 8;
|
||||
assert_eq!(utils::vec_to_set(&fixture_graph.predecessors(&u)), utils::vec_to_set(&vec![6]));
|
||||
let u = 6;
|
||||
assert_eq!(utils::vec_to_set(&fixture_graph.predecessors(&u)), utils::vec_to_set(&vec![5]));
|
||||
}
|
80
code/rust/tests/test_graphs/test_tarjan.rs
Normal file
80
code/rust/tests/test_graphs/test_tarjan.rs
Normal file
@ -0,0 +1,80 @@
|
||||
// ----------------------------------------------------------------
|
||||
// IMPORTS
|
||||
// ----------------------------------------------------------------
|
||||
|
||||
use rstest::fixture;
|
||||
use rstest::rstest;
|
||||
use std::fmt::Display;
|
||||
use std::hash::Hash;
|
||||
|
||||
use ads2::core::utils;
|
||||
use ads2::graphs::graph;
|
||||
use ads2::graphs::tarjan;
|
||||
|
||||
// ----------------------------------------------------------------
|
||||
// Fixtures
|
||||
// ----------------------------------------------------------------
|
||||
|
||||
#[fixture]
|
||||
fn fixture_graph1() -> graph::Graph<i32> {
|
||||
return graph::Graph::<i32>::new(
|
||||
vec![1,2,3,4],
|
||||
vec![(1,2), (2,4), (4,2)]
|
||||
);
|
||||
}
|
||||
|
||||
#[fixture]
|
||||
fn fixture_graph2() -> graph::Graph<i32> {
|
||||
return graph::Graph::<i32>::new(
|
||||
vec![1,2,3,4,5,6,7],
|
||||
vec![(1,2), (1,3), (2,3), (3,4), (4,5), (5,2), (5,6), (5,7), (6,7)]
|
||||
);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------
|
||||
// Test Graph
|
||||
// ----------------------------------------------------------------
|
||||
|
||||
#[rstest]
|
||||
#[case(fixture_graph1(), vec![vec![1], vec![3], vec![2,4]])]
|
||||
#[case(fixture_graph2(), vec![vec![1], vec![6], vec![7], vec![2,3,4,5]])]
|
||||
fn test_tarjan<T>(#[case] gph: graph::Graph<T>, #[case] expected: Vec<Vec<T>>)
|
||||
where T: Eq + Hash + Clone + Display
|
||||
{
|
||||
assert_components_eq(
|
||||
&tarjan::tarjan_algorithm(&gph),
|
||||
&expected
|
||||
)
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------
|
||||
// AUXILIARY METHODS
|
||||
// ----------------------------------------------------------------
|
||||
|
||||
fn check_components_eq<T>(components1: &Vec<Vec<T>>, components2: &Vec<Vec<T>>) -> bool
|
||||
where T: Eq + Hash + Clone
|
||||
{
|
||||
if components1.len() != components2.len() {
|
||||
return false
|
||||
}
|
||||
|
||||
for component1 in components1 {
|
||||
let mut found = false;
|
||||
for component2 in components2 {
|
||||
if utils::vec_to_set(component1) == utils::vec_to_set(component2) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
fn assert_components_eq<T>(components1: &Vec<Vec<T>>, components2: &Vec<Vec<T>>)
|
||||
where T: Eq + Hash + Clone
|
||||
{
|
||||
assert!(check_components_eq(components1, components2));
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user