|
| 1 | +#[cfg(test)] |
| 2 | +mod tests { |
| 3 | + use petgraph::graph::DiGraph; |
| 4 | + use petgraph::visit::EdgeRef; |
| 5 | + use quickcheck::{quickcheck, TestResult}; |
| 6 | + |
| 7 | + use rustworkx_core::generators::star_graph; |
| 8 | + |
| 9 | + #[test] |
| 10 | + fn prop_star_graph_structure() { |
| 11 | + fn prop(n: usize, bidirectional: bool, inward: bool) -> TestResult { |
| 12 | + let n = n % 50; // prevent overly large graphs |
| 13 | + |
| 14 | + let g: DiGraph<(), ()> = |
| 15 | + match star_graph(Some(n), None, || (), || (), inward, bidirectional) { |
| 16 | + Ok(graph) => graph, |
| 17 | + Err(_) => return TestResult::failed(), |
| 18 | + }; |
| 19 | + |
| 20 | + // Number of nodes should be exactly n |
| 21 | + if g.node_count() != n { |
| 22 | + return TestResult::failed(); |
| 23 | + } |
| 24 | + |
| 25 | + // Star graph has one central node (0), all others connected to/from it |
| 26 | + let expected_edge_count = if n < 2 { |
| 27 | + 0 |
| 28 | + } else if bidirectional { |
| 29 | + 2 * (n - 1) |
| 30 | + } else { |
| 31 | + n - 1 |
| 32 | + }; |
| 33 | + if g.edge_count() != expected_edge_count { |
| 34 | + return TestResult::failed(); |
| 35 | + } |
| 36 | + |
| 37 | + // No self-loops |
| 38 | + if g.edge_references().any(|e| e.source() == e.target()) { |
| 39 | + return TestResult::failed(); |
| 40 | + } |
| 41 | + |
| 42 | + // All edges must involve the center node (index 0) |
| 43 | + for edge in g.edge_references() { |
| 44 | + let src = edge.source().index(); |
| 45 | + let dst = edge.target().index(); |
| 46 | + if src != 0 && dst != 0 { |
| 47 | + return TestResult::failed(); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + TestResult::passed() |
| 52 | + } |
| 53 | + |
| 54 | + quickcheck(prop as fn(usize, bool, bool) -> TestResult); |
| 55 | + } |
| 56 | +} |
0 commit comments