|
| 1 | +use petgraph::graph::Graph; |
| 2 | +use petgraph::visit::EdgeRef; |
| 3 | +use quickcheck::{quickcheck, TestResult}; |
| 4 | +use rustworkx_core::generators::heavy_square_graph; |
| 5 | +use std::collections::HashSet; |
| 6 | + |
| 7 | +#[test] |
| 8 | +fn prop_heavy_square_graph_structure() { |
| 9 | + fn prop(d: u8) -> TestResult { |
| 10 | + let d = ((d % 11) | 1) as usize; // ensure d is odd |
| 11 | + if d == 0 { |
| 12 | + return TestResult::discard(); |
| 13 | + } |
| 14 | + |
| 15 | + let g: Graph<(), ()> = match heavy_square_graph(d, || (), || (), false) { |
| 16 | + Ok(graph) => graph, |
| 17 | + Err(_) => return TestResult::error("Failed to generate heavy square graph"), |
| 18 | + }; |
| 19 | + |
| 20 | + let expected_nodes = 3 * d * d - 2 * d; |
| 21 | + let expected_edges = 4 * d * (d - 1); |
| 22 | + |
| 23 | + if g.node_count() != expected_nodes || g.edge_count() != expected_edges { |
| 24 | + return TestResult::failed(); |
| 25 | + } |
| 26 | + |
| 27 | + TestResult::passed() |
| 28 | + } |
| 29 | + |
| 30 | + quickcheck(prop as fn(u8) -> TestResult); |
| 31 | +} |
| 32 | + |
| 33 | +#[test] |
| 34 | +fn prop_heavy_square_bidirectionality() { |
| 35 | + fn prop(d: u8) -> TestResult { |
| 36 | + let d = ((d % 11) | 1) as usize; |
| 37 | + if d == 0 { |
| 38 | + return TestResult::discard(); |
| 39 | + } |
| 40 | + |
| 41 | + let g: Graph<(), ()> = match heavy_square_graph(d, || (), || (), true) { |
| 42 | + Ok(graph) => graph, |
| 43 | + Err(_) => return TestResult::error("Bidirectional graph generation failed"), |
| 44 | + }; |
| 45 | + |
| 46 | + let mut seen = HashSet::new(); |
| 47 | + for edge in g.edge_references() { |
| 48 | + let u = edge.source().index(); |
| 49 | + let v = edge.target().index(); |
| 50 | + if u == v { |
| 51 | + continue; |
| 52 | + } |
| 53 | + seen.insert((u.min(v), u.max(v))); |
| 54 | + } |
| 55 | + |
| 56 | + // Each edge should appear twice in bidirectional graph |
| 57 | + if g.edge_count() != 2 * seen.len() { |
| 58 | + return TestResult::failed(); |
| 59 | + } |
| 60 | + |
| 61 | + TestResult::passed() |
| 62 | + } |
| 63 | + |
| 64 | + quickcheck(prop as fn(u8) -> TestResult); |
| 65 | +} |
| 66 | + |
| 67 | +#[test] |
| 68 | +fn prop_heavy_square_even_distance_should_fail() { |
| 69 | + fn prop(d: u8) -> bool { |
| 70 | + let d = (d & !1) as usize; // make d even |
| 71 | + if d == 0 { |
| 72 | + return true; |
| 73 | + } |
| 74 | + heavy_square_graph::<Graph<(), ()>, (), _, _, ()>(d, || (), || (), false).is_err() |
| 75 | + } |
| 76 | + |
| 77 | + quickcheck(prop as fn(u8) -> bool); |
| 78 | +} |
0 commit comments