|
| 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 | +} |
0 commit comments