Skip to content

Commit 62b5e6a

Browse files
authored
Adding quickcheck for karate club (#1483)
Added quick to check the structural property of karate_club_graph, by ensuring the number of nodes, edges, there are no self loops and all nodes referenced are valid.
1 parent 27d6022 commit 62b5e6a

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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::karate_club_graph;
8+
9+
#[test]
10+
fn prop_karate_club_basic_structure() {
11+
fn prop(x: u8, y: u16) -> TestResult {
12+
let g: UnGraph<u8, u16> = karate_club_graph(
13+
|is_hi| if is_hi { x } else { x.wrapping_add(1) },
14+
|strength| y.wrapping_add(strength as u16),
15+
);
16+
17+
// Always 34 nodes
18+
if g.node_count() != 34 {
19+
return TestResult::failed();
20+
}
21+
22+
// Always 78 edges
23+
if g.edge_count() != 78 {
24+
return TestResult::failed();
25+
}
26+
27+
// No self-loops
28+
if g.edge_references().any(|e| e.source() == e.target()) {
29+
return TestResult::failed();
30+
}
31+
32+
// All nodes referenced are valid
33+
let max_node = g.node_count();
34+
for edge in g.edge_references() {
35+
if edge.source().index() >= max_node || edge.target().index() >= max_node {
36+
return TestResult::failed();
37+
}
38+
}
39+
40+
TestResult::passed()
41+
}
42+
43+
quickcheck(prop as fn(u8, u16) -> TestResult);
44+
}
45+
}

rustworkx-core/tests/quickcheck/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ mod grid_graph;
88
mod heavy_hex_graph;
99
mod heavy_square_graph;
1010
mod hexagonal_lattice_graph;
11+
mod karate_club;
1112
mod lollipop_graph;

0 commit comments

Comments
 (0)