Skip to content

Commit 46ff474

Browse files
authored
Adding quickcheck for hex lattice graph (#1477)
This test verifies: 1. Number of nodes and edges generated matches the expected formula based on rows, cols, and whether the graph is bidirectional and non-periodic. 2. For every edge, the reverse edge exists if bidirectional == true. 3. Each node in a hexagonal_lattice_graph_weighted graph has the correct (u, v) weight based on its intended lattice position. 4. Invalid periodic graphs (with odd cols or small rows) are correctly rejected with an error.
1 parent d1b1b92 commit 46ff474

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
}

rustworkx-core/tests/quickcheck/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ mod full_rary_tree_graph;
77
mod grid_graph;
88
mod heavy_hex_graph;
99
mod heavy_square_graph;
10+
mod hexagonal_lattice_graph;
1011
mod lollipop_graph;

0 commit comments

Comments
 (0)