|
| 1 | +use petgraph::graph::{DiGraph, UnGraph}; |
| 2 | +use petgraph::visit::{EdgeRef, IntoNodeReferences, NodeRef}; |
| 3 | +use quickcheck::{quickcheck, TestResult}; |
| 4 | +use rustworkx_core::generators::{ |
| 5 | + hexagonal_lattice_graph, hexagonal_lattice_graph_weighted, InvalidInputError, |
| 6 | +}; |
| 7 | +use std::collections::HashSet; |
| 8 | + |
| 9 | +#[test] |
| 10 | +fn prop_hexagonal_lattice_node_and_edge_count() { |
| 11 | + fn prop(rows: u8, cols: u8, bidirectional: bool) -> TestResult { |
| 12 | + let (rows, cols) = (rows as usize % 5, cols as usize % 5); |
| 13 | + if rows == 0 || cols == 0 { |
| 14 | + return TestResult::discard(); |
| 15 | + } |
| 16 | + |
| 17 | + let expected = { |
| 18 | + let edge_factor = if bidirectional { 2 } else { 1 }; |
| 19 | + let rowlen = 2 * rows + 2; |
| 20 | + let collen = cols + 1; |
| 21 | + let num_nodes = rowlen * collen - 2; |
| 22 | + let num_edges = edge_factor * (3 * rows * cols + 2 * (rows + cols) - 1); |
| 23 | + (num_nodes, num_edges) |
| 24 | + }; |
| 25 | + |
| 26 | + let g: DiGraph<(), ()> = |
| 27 | + match hexagonal_lattice_graph(rows, cols, || (), || (), bidirectional, false) { |
| 28 | + Ok(g) => g, |
| 29 | + Err(_) => return TestResult::failed(), |
| 30 | + }; |
| 31 | + |
| 32 | + if g.node_count() != expected.0 || g.edge_count() != expected.1 { |
| 33 | + return TestResult::failed(); |
| 34 | + } |
| 35 | + |
| 36 | + TestResult::passed() |
| 37 | + } |
| 38 | + |
| 39 | + quickcheck(prop as fn(u8, u8, bool) -> TestResult); |
| 40 | +} |
| 41 | + |
| 42 | +#[test] |
| 43 | +fn prop_hexagonal_lattice_bidirectionality() { |
| 44 | + fn prop(rows: u8, cols: u8) -> TestResult { |
| 45 | + let (rows, cols) = (rows as usize % 4 + 1, cols as usize % 4 + 1); |
| 46 | + let g: DiGraph<(), ()> = |
| 47 | + match hexagonal_lattice_graph(rows, cols, || (), || (), true, false) { |
| 48 | + Ok(g) => g, |
| 49 | + Err(_) => return TestResult::failed(), |
| 50 | + }; |
| 51 | + |
| 52 | + let mut seen = HashSet::new(); |
| 53 | + for edge in g.edge_references() { |
| 54 | + let u = edge.source().index(); |
| 55 | + let v = edge.target().index(); |
| 56 | + seen.insert((u, v)); |
| 57 | + } |
| 58 | + |
| 59 | + for &(u, v) in &seen { |
| 60 | + if !seen.contains(&(v, u)) { |
| 61 | + return TestResult::failed(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + TestResult::passed() |
| 66 | + } |
| 67 | + |
| 68 | + quickcheck(prop as fn(u8, u8) -> TestResult); |
| 69 | +} |
| 70 | + |
| 71 | +#[test] |
| 72 | +fn prop_hexagonal_lattice_position_weights() { |
| 73 | + fn prop(rows: u8, cols: u8) -> TestResult { |
| 74 | + let (rows, cols) = (rows as usize % 4 + 1, cols as usize % 4 + 1); |
| 75 | + let g: UnGraph<(usize, usize), ()> = match hexagonal_lattice_graph_weighted( |
| 76 | + rows, |
| 77 | + cols, |
| 78 | + |u, v| (u, v), |
| 79 | + || (), |
| 80 | + false, |
| 81 | + false, |
| 82 | + ) { |
| 83 | + Ok(g) => g, |
| 84 | + Err(_) => return TestResult::failed(), |
| 85 | + }; |
| 86 | + |
| 87 | + for node in g.node_references() { |
| 88 | + let (u, v) = *node.weight(); |
| 89 | + if !g.node_weights().any(|&(x, y)| x == u && y == v) { |
| 90 | + return TestResult::failed(); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + TestResult::passed() |
| 95 | + } |
| 96 | + |
| 97 | + quickcheck(prop as fn(u8, u8) -> TestResult); |
| 98 | +} |
| 99 | + |
| 100 | +#[test] |
| 101 | +fn prop_hexagonal_lattice_invalid_periodic_rejected() { |
| 102 | + fn prop(rows: u8, cols: u8) -> TestResult { |
| 103 | + let rows = rows as usize % 4 + 1; |
| 104 | + let cols = (2 * (cols as usize % 2)) + 1; // make sure cols is odd |
| 105 | + |
| 106 | + let g = hexagonal_lattice_graph::<UnGraph<(), ()>, _, _, _, ()>( |
| 107 | + rows, |
| 108 | + cols, |
| 109 | + || (), |
| 110 | + || (), |
| 111 | + false, |
| 112 | + true, |
| 113 | + ); |
| 114 | + |
| 115 | + match g { |
| 116 | + Err(InvalidInputError) => TestResult::passed(), |
| 117 | + _ => TestResult::failed(), |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + quickcheck(prop as fn(u8, u8) -> TestResult); |
| 122 | +} |
0 commit comments