Skip to content

Commit 8cf854c

Browse files
authored
Adding Quickcheck for binomial tree graph. (#1470)
The quickcheck verifies the structure of binomial tree graph by checking number of node, number of edges and if they are bidirectional. I plan on adding quickchecks for all the deterministic graph generators and some other algos like bipartite colouring before moving forward.
1 parent 0311563 commit 8cf854c

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use petgraph::graph::UnGraph;
2+
use petgraph::visit::EdgeRef;
3+
use quickcheck::{quickcheck, TestResult};
4+
use rustworkx_core::generators::binomial_tree_graph;
5+
6+
#[test]
7+
fn prop_binomial_tree_structure() {
8+
fn prop(order: u32) -> TestResult {
9+
let order = order % 10; // upto 2^10 nodes due to memory constraints.
10+
if order >= 60 {
11+
return TestResult::discard();
12+
}
13+
14+
let g = match binomial_tree_graph::<UnGraph<(), ()>, (), _, _, ()>(
15+
order,
16+
None,
17+
|| (),
18+
|| (),
19+
true,
20+
) {
21+
Ok(g) => g,
22+
Err(_) => return TestResult::error("Unexpected error in graph generation"),
23+
};
24+
25+
let expected_nodes = 2_usize.pow(order);
26+
let expected_edges = 2 * (expected_nodes - 1);
27+
28+
if g.node_count() != expected_nodes || g.edge_count() != expected_edges {
29+
return TestResult::failed();
30+
}
31+
32+
// Check that for every edge, a reverse edge exists
33+
for edge in g.edge_references() {
34+
let u = edge.source();
35+
let v = edge.target();
36+
let reverse_exists = g.find_edge(v, u).is_some();
37+
if !reverse_exists {
38+
return TestResult::failed();
39+
}
40+
}
41+
42+
TestResult::passed()
43+
}
44+
45+
quickcheck(prop as fn(u32) -> TestResult);
46+
}

rustworkx-core/tests/quickcheck/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod barbell_graph;
2+
mod binomial_tree_graph;
23
mod full_rary_tree_graph;
34
mod grid_graph;
45
mod heavy_hex_graph;

0 commit comments

Comments
 (0)