Skip to content

Commit 949d30b

Browse files
authored
Adding quickcheck for star graph (#1486)
Checking the number of nodes, edges, no self loops and all the edges pass through the central node.
1 parent acf34ab commit 949d30b

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

rustworkx-core/tests/quickcheck/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ mod karate_club;
1212
mod lollipop_graph;
1313
mod path_graph;
1414
mod petersen_graph;
15+
mod star_graph;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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::star_graph;
8+
9+
#[test]
10+
fn prop_star_graph_structure() {
11+
fn prop(n: usize, bidirectional: bool, inward: bool) -> TestResult {
12+
let n = n % 50; // prevent overly large graphs
13+
14+
let g: DiGraph<(), ()> =
15+
match star_graph(Some(n), None, || (), || (), inward, bidirectional) {
16+
Ok(graph) => graph,
17+
Err(_) => return TestResult::failed(),
18+
};
19+
20+
// Number of nodes should be exactly n
21+
if g.node_count() != n {
22+
return TestResult::failed();
23+
}
24+
25+
// Star graph has one central node (0), all others connected to/from it
26+
let expected_edge_count = if n < 2 {
27+
0
28+
} else if bidirectional {
29+
2 * (n - 1)
30+
} else {
31+
n - 1
32+
};
33+
if g.edge_count() != expected_edge_count {
34+
return TestResult::failed();
35+
}
36+
37+
// No self-loops
38+
if g.edge_references().any(|e| e.source() == e.target()) {
39+
return TestResult::failed();
40+
}
41+
42+
// All edges must involve the center node (index 0)
43+
for edge in g.edge_references() {
44+
let src = edge.source().index();
45+
let dst = edge.target().index();
46+
if src != 0 && dst != 0 {
47+
return TestResult::failed();
48+
}
49+
}
50+
51+
TestResult::passed()
52+
}
53+
54+
quickcheck(prop as fn(usize, bool, bool) -> TestResult);
55+
}
56+
}

0 commit comments

Comments
 (0)