Skip to content

Commit acf34ab

Browse files
authored
Adding quickcheck for petersen graph (#1485)
Testing the following to ensure structural property are met: 1) nodes are 2n 2) edges are 3n 3) no self loop 4) nodes are within bounds
1 parent 101fa22 commit acf34ab

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

rustworkx-core/tests/quickcheck/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ mod hexagonal_lattice_graph;
1111
mod karate_club;
1212
mod lollipop_graph;
1313
mod path_graph;
14+
mod petersen_graph;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#[cfg(test)]
2+
mod tests {
3+
use petgraph::graph::UnGraph;
4+
use petgraph::visit::EdgeRef;
5+
use quickcheck::{quickcheck, TestResult};
6+
7+
use rustworkx_core::generators::petersen_graph;
8+
9+
#[test]
10+
fn prop_petersen_graph_structure() {
11+
fn prop(n_in: u8, k_in: u8) -> TestResult {
12+
let n = (n_in as usize % 97).max(3); // n >= 3
13+
let k = ((k_in as usize) % n).max(1); // k >= 1
14+
15+
// Discard invalid input: k must be < n/2
16+
if k >= n / 2 {
17+
return TestResult::discard();
18+
}
19+
20+
let g: UnGraph<(), ()> = match petersen_graph(n, k, || (), || ()) {
21+
Ok(graph) => graph,
22+
Err(_) => return TestResult::failed(),
23+
};
24+
25+
// node_count should be exactly 2n
26+
if g.node_count() != 2 * n {
27+
return TestResult::failed();
28+
}
29+
30+
// edge_count should be exactly 3n
31+
if g.edge_count() != 3 * n {
32+
return TestResult::failed();
33+
}
34+
35+
// No self-loops
36+
if g.edge_references().any(|e| e.source() == e.target()) {
37+
return TestResult::failed();
38+
}
39+
40+
// All node indices must be within bounds
41+
let max_node = g.node_count();
42+
for edge in g.edge_references() {
43+
if edge.source().index() >= max_node || edge.target().index() >= max_node {
44+
return TestResult::failed();
45+
}
46+
}
47+
48+
TestResult::passed()
49+
}
50+
51+
quickcheck(prop as fn(u8, u8) -> TestResult);
52+
}
53+
}

0 commit comments

Comments
 (0)