Add loops and multi-edges to the test graph, fix neighbors() test

This commit is contained in:
2026-05-05 19:21:48 +02:00
parent 8324f93ebd
commit feaca335b1
2 changed files with 35 additions and 17 deletions
-1
View File
@@ -267,5 +267,4 @@ mod tests {
graph.delete_edge(f); graph.delete_edge(f);
assert_eq!(graph.edge_count(), 0, "unexpected edge count after delete"); assert_eq!(graph.edge_count(), 0, "unexpected edge count after delete");
} }
} }
+33 -14
View File
@@ -7,13 +7,17 @@ macro_rules! graph_topology_test_fixtures {
let vertices: [<$T as $crate::traits::GraphTopology>::Vertex; 10] = let vertices: [<$T as $crate::traits::GraphTopology>::Vertex; 10] =
core::array::from_fn(|_| graph.add_vertex()); core::array::from_fn(|_| graph.add_vertex());
graph.add_edge(vertices[0], vertices[1]); graph.add_edge(vertices[0], vertices[1]);
graph.add_edge(vertices[0], vertices[1]);
graph.add_edge(vertices[1], vertices[2]); graph.add_edge(vertices[1], vertices[2]);
graph.add_edge(vertices[1], vertices[3]); graph.add_edge(vertices[1], vertices[3]);
graph.add_edge(vertices[1], vertices[4]); graph.add_edge(vertices[1], vertices[4]);
graph.add_edge(vertices[2], vertices[2]);
graph.add_edge(vertices[2], vertices[4]);
graph.add_edge(vertices[2], vertices[4]); graph.add_edge(vertices[2], vertices[4]);
graph.add_edge(vertices[2], vertices[5]); graph.add_edge(vertices[2], vertices[5]);
graph.add_edge(vertices[2], vertices[6]); graph.add_edge(vertices[2], vertices[6]);
graph.add_edge(vertices[3], vertices[6]); graph.add_edge(vertices[3], vertices[6]);
graph.add_edge(vertices[4], vertices[4]);
graph.add_edge(vertices[4], vertices[7]); graph.add_edge(vertices[4], vertices[7]);
graph.add_edge(vertices[4], vertices[8]); graph.add_edge(vertices[4], vertices[8]);
graph.add_edge(vertices[5], vertices[9]); graph.add_edge(vertices[5], vertices[9]);
@@ -66,7 +70,7 @@ macro_rules! graph_topology_tests {
#[test] #[test]
fn edge_count() { fn edge_count() {
let (graph, _) = make_test_graph(); let (graph, _) = make_test_graph();
assert_eq!(graph.edge_count(), 14, "unexpected edge count"); assert_eq!(graph.edge_count(), 18, "unexpected edge count");
} }
#[test] #[test]
@@ -79,7 +83,7 @@ macro_rules! graph_topology_tests {
#[test] #[test]
fn degree() { fn degree() {
let (graph, vertices) = make_test_graph(); let (graph, vertices) = make_test_graph();
let expected_degrees = [1, 4, 4, 2, 4, 2, 3, 3, 2, 3]; let expected_degrees = [2, 5, 7, 2, 7, 2, 3, 3, 2, 3];
for i in 0..graph.vertex_count() { for i in 0..graph.vertex_count() {
assert_eq!( assert_eq!(
graph.degree(vertices[i]), graph.degree(vertices[i]),
@@ -136,8 +140,7 @@ macro_rules! graph_topology_tests {
for i in 0..graph.vertex_count() { for i in 0..graph.vertex_count() {
let exp = match i { let exp = match i {
2 => continue, 1 | 2 | 4 | 5 | 6 => true,
1 | 4 | 5 | 6 => true,
_ => false, _ => false,
}; };
assert_eq!( assert_eq!(
@@ -199,20 +202,35 @@ macro_rules! graph_topology_tests {
// Checks neighbors of vertex 4. // Checks neighbors of vertex 4.
assert_eq!( assert_eq!(
graph.neighbors(vertices[4]).count(), graph.neighbors(vertices[4]).count(),
4, 7,
"unexpected neighbor count" "unexpected neighbor count"
); );
let mut expected_neighbors = vec![
// Expects each neighbor to appear exactly once. This will not work if there are multiple edges. vertices[1],
let neighbors = vec![vertices[1], vertices[2], vertices[7], vertices[8]]; vertices[2],
vertices[2],
vertices[4],
vertices[4],
vertices[7],
vertices[8],
];
for v in graph.neighbors(vertices[4]) { for v in graph.neighbors(vertices[4]) {
assert_eq!( let i = expected_neighbors
neighbors.iter().filter(|&x| *x == v).count(), .iter()
1, .position(|w| *w == v)
.expect(&format!(
"unexpected neighbor {v:?} of {:?} from the iterator", "unexpected neighbor {v:?} of {:?} from the iterator",
vertices[4] vertices[4]
); ));
expected_neighbors.swap_remove(i);
} }
assert_eq!(
expected_neighbors.len(),
0,
"expected neighbors {:?} of {:?} were not matched by the iterator",
expected_neighbors,
vertices[4]
);
} }
#[test] #[test]
@@ -359,7 +377,7 @@ macro_rules! graph_topology_deletion_tests {
); );
assert_eq!( assert_eq!(
graph.edge_count(), graph.edge_count(),
14, 18,
"unexpected edge count before delete" "unexpected edge count before delete"
); );
graph.delete_vertex(vertices[2]); graph.delete_vertex(vertices[2]);
@@ -368,12 +386,13 @@ macro_rules! graph_topology_deletion_tests {
9, 9,
"unexpected vertex count after delete" "unexpected vertex count after delete"
); );
assert_eq!(graph.edge_count(), 10, "unexpected edge count after delete"); assert_eq!(graph.edge_count(), 12, "unexpected edge count after delete");
let expected_edges = [ let expected_edges = [
(vertices[0], vertices[1]), (vertices[0], vertices[1]),
(vertices[1], vertices[3]), (vertices[1], vertices[3]),
(vertices[1], vertices[4]), (vertices[1], vertices[4]),
(vertices[3], vertices[6]), (vertices[3], vertices[6]),
(vertices[4], vertices[4]),
(vertices[4], vertices[7]), (vertices[4], vertices[7]),
(vertices[4], vertices[8]), (vertices[4], vertices[8]),
(vertices[5], vertices[9]), (vertices[5], vertices[9]),