Skip to content

Commit

Permalink
Added integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
FilippoFantinato committed Sep 16, 2024
1 parent 6aa9786 commit 684a1c2
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 13 deletions.
10 changes: 9 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
[package]
name = "algorithms-on-graphs"
name = "algorithms_on_graphs"
version = "0.1.0"
edition = "2021"

[[bin]]
name = "algorithms_on_graphs"
path = "./src/main.rs"

[lib]
name = "algorithms_on_graphs"
path = "./src/main.rs"

[dependencies]
clap = { version = "4.5.17", features = ["derive"] }
mockall = "0.13.0"
18 changes: 9 additions & 9 deletions src/graph/undirected_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,21 @@ impl Graph for UndirectedGraph {
self.adj_matrix.get_mut(u).unwrap().remove(v);
self.adj_matrix.get_mut(v).unwrap().remove(u);

if self.adj_matrix.get(u).unwrap().is_empty() {
self.adj_matrix.remove(u);
self.vertices.remove(u);
}

if self.adj_matrix.get(v).unwrap().is_empty() {
self.adj_matrix.remove(v);
self.vertices.remove(v);
}
clean_vertex(self, u);
clean_vertex(self, v);

self.edges.remove(&e);
});
}
}

fn clean_vertex(g: &mut UndirectedGraph, t: &Vertex) {
if g.adj_matrix.get(t).unwrap().is_empty() {
g.adj_matrix.remove(t);
g.vertices.remove(t);
}
}

mod tests {
use std::collections::{HashMap, HashSet};
use std::vec;
Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod algorithms;
mod cli;
mod graph;
pub mod algorithms;
pub mod cli;
pub mod graph;

use clap::Parser;
use cli::cli::Algorithm;
Expand Down
80 changes: 80 additions & 0 deletions tests/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use std::{path::PathBuf, str::FromStr};

use algorithms_on_graphs::cli::cli::{run_cli, Algorithm, Args};
use algorithms_on_graphs::graph::graph::Path;

#[test]
#[should_panic]
fn wrong_header_panic() {
let args = Args {
algorithm: Algorithm::IsAcyclic,
file: PathBuf::from_str("./tests/test_dataset/wrong_header.txt").unwrap(),
};
run_cli(&args);
}

#[test]
#[should_panic]
fn wrong_first_vertex_panic() {
let args = Args {
algorithm: Algorithm::IsAcyclic,
file: PathBuf::from_str("./tests/test_dataset/wrong_first_vertex.txt").unwrap(),
};
run_cli(&args);
}

#[test]
#[should_panic]
fn wrong_second_vertex_panic() {
let args = Args {
algorithm: Algorithm::IsAcyclic,
file: PathBuf::from_str("./tests/test_dataset/wrong_second_vertex.txt").unwrap(),
};
run_cli(&args);
}

#[test]
#[should_panic]
fn wrong_weight_panic() {
let args = Args {
algorithm: Algorithm::IsAcyclic,
file: PathBuf::from_str("./tests/test_dataset/wrong_weight.txt").unwrap(),
};
run_cli(&args);
}

#[test]
fn is_acylic_success() {
let args = Args {
algorithm: Algorithm::IsAcyclic,
file: PathBuf::from_str("./dataset/input_random_01_10.txt").unwrap(),
};
let res = run_cli(&args);

let expected = &true;
let current = res.downcast_ref::<bool>().unwrap();
assert_eq!(expected, current);
}

#[test]
fn kruskal_naive_success() {
let args = Args {
algorithm: Algorithm::KruskalNaive,
file: PathBuf::from_str("./dataset/input_random_01_10.txt").unwrap(),
};
let res = run_cli(&args);

let expected_path = &vec![
(6, 7, -7462),
(8, 9, -976),
(4, 5, -433),
(2, 3, 1392),
(1, 2, 4993),
(5, 6, 6590),
(7, 8, 6658),
(3, 4, 8856),
(9, 10, 9698),
];
let current_path = res.downcast_ref::<Path>().unwrap();
assert_eq!(expected_path, current_path);
}
40 changes: 40 additions & 0 deletions tests/is_acyclic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::{path::PathBuf, str::FromStr};

use algorithms_on_graphs::cli::cli::{run_cli, Algorithm, Args};
use algorithms_on_graphs::graph::graph::Path;

#[test]
fn is_acylic_success() {
let args = Args {
algorithm: Algorithm::IsAcyclic,
file: PathBuf::from_str("./dataset/input_random_01_10.txt").unwrap(),
};
let res = run_cli(&args);

let expected = &true;
let current = res.downcast_ref::<bool>().unwrap();
assert_eq!(expected, current);
}

#[test]
fn kruskal_naive_success() {
let args = Args {
algorithm: Algorithm::KruskalNaive,
file: PathBuf::from_str("./dataset/input_random_01_10.txt").unwrap(),
};
let res = run_cli(&args);

let expected_path = &vec![
(6, 7, -7462),
(8, 9, -976),
(4, 5, -433),
(2, 3, 1392),
(1, 2, 4993),
(5, 6, 6590),
(7, 8, 6658),
(3, 4, 8856),
(9, 10, 9698),
];
let current_path = res.downcast_ref::<Path>().unwrap();
assert_eq!(expected_path, current_path);
}
27 changes: 27 additions & 0 deletions tests/kruskal_naive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use std::{path::PathBuf, str::FromStr};

use algorithms_on_graphs::cli::cli::{run_cli, Algorithm, Args};
use algorithms_on_graphs::graph::graph::Path;

#[test]
fn kruskal_naive_success() {
let args = Args {
algorithm: Algorithm::KruskalNaive,
file: PathBuf::from_str("./dataset/input_random_01_10.txt").unwrap(),
};
let res = run_cli(&args);

let expected_path = &vec![
(6, 7, -7462),
(8, 9, -976),
(4, 5, -433),
(2, 3, 1392),
(1, 2, 4993),
(5, 6, 6590),
(7, 8, 6658),
(3, 4, 8856),
(9, 10, 9698),
];
let current_path = res.downcast_ref::<Path>().unwrap();
assert_eq!(expected_path, current_path);
}
10 changes: 10 additions & 0 deletions tests/test_dataset/input_random_01_10.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
10 9
1 2 4993
2 3 1392
3 4 8856
4 5 -433
5 6 6590
6 7 -7462
7 8 6658
8 9 -976
9 10 9698
10 changes: 10 additions & 0 deletions tests/test_dataset/wrong_first_vertex.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
10 9

2 3 1392
3 4 8856
4 5 -433
5 6 6590
6 7 -7462
7 8 6658
8 9 -976
9 10 9698
10 changes: 10 additions & 0 deletions tests/test_dataset/wrong_header.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

1 2 4993
2 3 1392
3 4 8856
4 5 -433
5 6 6590
6 7 -7462
7 8 6658
8 9 -976
9 10 9698
10 changes: 10 additions & 0 deletions tests/test_dataset/wrong_second_vertex.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
10 9
1
2 3 1392
3 4 8856
4 5 -433
5 6 6590
6 7 -7462
7 8 6658
8 9 -976
9 10 9698
10 changes: 10 additions & 0 deletions tests/test_dataset/wrong_weight.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
10 9
1 2
2 3 1392
3 4 8856
4 5 -433
5 6 6590
6 7 -7462
7 8 6658
8 9 -976
9 10 9698

0 comments on commit 684a1c2

Please sign in to comment.