Skip to content

Commit 101fa22

Browse files
authored
Adding quickcheck for path graph (#1484)
1 parent 62b5e6a commit 101fa22

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

rustworkx-core/tests/quickcheck/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ mod heavy_square_graph;
1010
mod hexagonal_lattice_graph;
1111
mod karate_club;
1212
mod lollipop_graph;
13+
mod path_graph;
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#[cfg(test)]
2+
mod tests {
3+
use petgraph::graph::DiGraph;
4+
use petgraph::visit::EdgeRef;
5+
use quickcheck::{quickcheck, TestResult};
6+
7+
use rustworkx_core::generators::path_graph;
8+
9+
#[test]
10+
fn prop_path_graph_structure() {
11+
fn prop(n: usize, bidirectional: bool) -> TestResult {
12+
let n = n % 100; // Prevent large test cases
13+
14+
let g: DiGraph<(), ()> = match path_graph(Some(n), None, || (), || (), bidirectional) {
15+
Ok(graph) => graph,
16+
Err(_) => return TestResult::failed(),
17+
};
18+
19+
// node_count
20+
if g.node_count() != n {
21+
return TestResult::failed();
22+
}
23+
24+
// edge_count
25+
let expected_edge_count = if n < 2 {
26+
0
27+
} else if bidirectional {
28+
2 * (n - 1)
29+
} else {
30+
n - 1
31+
};
32+
if g.edge_count() != expected_edge_count {
33+
return TestResult::failed();
34+
}
35+
36+
// No self-loops
37+
if g.edge_references().any(|e| e.source() == e.target()) {
38+
return TestResult::failed();
39+
}
40+
41+
// All edges connect to the next nodes (i to i+1)
42+
let mut expected_edges = vec![];
43+
for i in 0..n.saturating_sub(1) {
44+
expected_edges.push((i, i + 1));
45+
if bidirectional {
46+
expected_edges.push((i + 1, i));
47+
}
48+
}
49+
50+
let mut actual_edges: Vec<(usize, usize)> = g
51+
.edge_references()
52+
.map(|e| (e.source().index(), e.target().index()))
53+
.collect();
54+
55+
expected_edges.sort_unstable();
56+
actual_edges.sort_unstable();
57+
58+
if actual_edges != expected_edges {
59+
return TestResult::failed();
60+
}
61+
62+
TestResult::passed()
63+
}
64+
65+
quickcheck(prop as fn(usize, bool) -> TestResult);
66+
}
67+
}

0 commit comments

Comments
 (0)