Skip to content

Commit 34ffae4

Browse files
Adding quickcheck for complete graph (#1471)
* Adding quickcheck for complete graph To verify the structural integrity of complete graph, we check the number of nodes, edges and whether all expected edges exist. * Add ignore for files temporarily * Address lint in test target as well --------- Co-authored-by: Ivan Carvalho <[email protected]>
1 parent 8cf854c commit 34ffae4

File tree

6 files changed

+82
-1
lines changed

6 files changed

+82
-1
lines changed

rustworkx-core/fuzz/fuzz_targets/test_fuzz_contraction.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![no_main]
2+
#![allow(clippy::uninlined_format_args)]
23

34
use arbitrary::{Arbitrary, Unstructured};
45
use libfuzzer_sys::fuzz_target;

rustworkx-core/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@
8282
//!
8383
//! <https://www.rustworkx.org/release_notes.html>
8484
85+
#![allow(clippy::uninlined_format_args)]
86+
8587
use std::convert::Infallible;
8688

8789
/// A convenient type alias that by default assumes no error can happen.

rustworkx-core/tests/graph_ext/contraction.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ where
253253
assert_eq!(*seen.entry('e').or_insert(e), e);
254254
assert_eq!(*seen.entry('m').or_insert(m), m);
255255
}
256-
(_, _, w) => panic!("Unexpected edge weight: {}", w),
256+
(_, _, w) => panic!("Unexpected edge weight: {w}"),
257257
}
258258
}
259259

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
use petgraph::graph::{DiGraph, UnGraph};
2+
use petgraph::visit::EdgeRef;
3+
use quickcheck::{quickcheck, TestResult};
4+
use rustworkx_core::generators::complete_graph;
5+
use std::collections::HashSet;
6+
7+
#[test]
8+
fn prop_complete_graph_structure_directed() {
9+
fn prop(n: usize) -> TestResult {
10+
let n = n % 100; // limit for runtime issues
11+
if n == 0 {
12+
return TestResult::discard();
13+
}
14+
15+
let g = match complete_graph::<DiGraph<(), ()>, (), _, _, ()>(Some(n), None, || (), || ()) {
16+
Ok(g) => g,
17+
Err(_) => return TestResult::error("Unexpected error in directed complete graph"),
18+
};
19+
20+
if g.node_count() != n || g.edge_count() != n * (n - 1) {
21+
return TestResult::failed();
22+
}
23+
24+
// Ensure that for every pair (i, j) with i != j, an edge exists exists
25+
let expected_edges: HashSet<_> = (0..n)
26+
.flat_map(|i| (0..n).filter(move |&j| j != i).map(move |j| (i, j)))
27+
.collect();
28+
29+
let actual_edges: HashSet<_> = g
30+
.edge_references()
31+
.map(|e| (e.source().index(), e.target().index()))
32+
.collect();
33+
34+
TestResult::from_bool(expected_edges == actual_edges)
35+
}
36+
37+
quickcheck(prop as fn(usize) -> TestResult);
38+
}
39+
40+
#[test]
41+
fn prop_complete_graph_structure_undirected() {
42+
fn prop(n: usize) -> TestResult {
43+
let n = n % 100;
44+
if n == 0 {
45+
return TestResult::discard();
46+
}
47+
48+
let g = match complete_graph::<UnGraph<(), ()>, (), _, _, ()>(Some(n), None, || (), || ()) {
49+
Ok(g) => g,
50+
Err(_) => return TestResult::error("Unexpected error in undirected complete graph"),
51+
};
52+
53+
if g.node_count() != n || g.edge_count() != n * (n - 1) / 2 {
54+
return TestResult::failed();
55+
}
56+
57+
// in undirected graphs, (i, j) and (j, i) are the same edge, so we only include one of them.
58+
let expected_edges: HashSet<_> = (0..n)
59+
.flat_map(|i| (i + 1..n).map(move |j| (i, j)))
60+
.collect();
61+
62+
let actual_edges: HashSet<_> = g
63+
.edge_references()
64+
.map(|e| {
65+
let u = e.source().index();
66+
let v = e.target().index();
67+
(u.min(v), u.max(v))
68+
})
69+
.collect();
70+
71+
TestResult::from_bool(expected_edges == actual_edges)
72+
}
73+
74+
quickcheck(prop as fn(usize) -> TestResult);
75+
}

rustworkx-core/tests/quickcheck/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod barbell_graph;
22
mod binomial_tree_graph;
3+
mod complete_graph;
34
mod full_rary_tree_graph;
45
mod grid_graph;
56
mod heavy_hex_graph;

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
// License for the specific language governing permissions and limitations
1111
// under the License.
1212

13+
#![allow(clippy::uninlined_format_args)]
14+
1315
mod bisimulation;
1416
mod cartesian_product;
1517
mod centrality;

0 commit comments

Comments
 (0)