Skip to content

Commit 041cb69

Browse files
Krishn1412SILIZ4
authored andcommitted
Adding quickcheck for grid graph (Qiskit#1465)
* Adding quickcheck for grid graph This property test checks structural correctness of grid_graph. It verifies that the total number of nodes is exactly rows * cols. It confirms the number of edges matches expectations based on grid dimensions and directionality. It ensures all edge endpoints refer to valid node indices. It checks that if the graph is bidirectional, it is weakly connected using BFS traversal. * Lint issues
1 parent 1b091df commit 041cb69

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use petgraph::graph::DiGraph;
2+
use petgraph::visit::{Bfs, EdgeRef, NodeIndexable};
3+
use quickcheck::{quickcheck, TestResult};
4+
use rustworkx_core::generators::grid_graph;
5+
6+
#[test]
7+
fn prop_grid_graph_validity() {
8+
fn prop(rows: usize, cols: usize, bidirectional_flag: bool) -> TestResult {
9+
let rows = rows % 32 + 1; // Ensure >= 1
10+
let cols = cols % 32 + 1;
11+
12+
let graph = match grid_graph::<DiGraph<(), ()>, (), _, _, ()>(
13+
Some(rows),
14+
Some(cols),
15+
None,
16+
|| (),
17+
|| (),
18+
bidirectional_flag,
19+
) {
20+
Ok(g) => g,
21+
Err(_) => return TestResult::error("Graph generation failed"),
22+
};
23+
24+
let expected_nodes = rows * cols;
25+
let base_edges = (rows - 1) * cols + (cols - 1) * rows;
26+
let expected_edges = if bidirectional_flag {
27+
base_edges * 2
28+
} else {
29+
base_edges
30+
};
31+
32+
// Property 1: Node count
33+
if graph.node_count() != expected_nodes {
34+
return TestResult::failed();
35+
}
36+
37+
// Property 2: Edge count
38+
if graph.edge_count() != expected_edges {
39+
return TestResult::failed();
40+
}
41+
42+
// Property3: All edge indices are in bounds
43+
let max_index = graph.node_bound();
44+
for edge in graph.edge_references() {
45+
let src = edge.source().index();
46+
let tgt = edge.target().index();
47+
if src >= max_index || tgt >= max_index {
48+
return TestResult::failed();
49+
}
50+
}
51+
52+
// Property 4: Weak connectivity (if bidirectional, should be connected)
53+
if bidirectional_flag {
54+
let mut bfs = Bfs::new(&graph, graph.from_index(0));
55+
let mut visited = 0;
56+
while bfs.next(&graph).is_some() {
57+
visited += 1;
58+
}
59+
if visited != expected_nodes {
60+
return TestResult::failed();
61+
}
62+
}
63+
64+
TestResult::passed()
65+
}
66+
67+
quickcheck(prop as fn(usize, usize, bool) -> TestResult);
68+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
mod special_graph;
1+
mod grid_graph;
2+
mod lollipop_graph;

0 commit comments

Comments
 (0)