Update and adds tests for Graph edge deletion

This commit is contained in:
2026-04-28 17:36:38 +02:00
parent f075501828
commit 8e0b3325f5
+131
View File
@@ -438,6 +438,31 @@ mod tests {
"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(), 10, "unexpected edge count after delete");
let expected_edges = [
(vertices[0], vertices[1]),
(vertices[1], vertices[3]),
(vertices[1], vertices[4]),
(vertices[3], vertices[6]),
(vertices[4], vertices[7]),
(vertices[4], vertices[8]),
(vertices[5], vertices[9]),
(vertices[6], vertices[9]),
(vertices[7], vertices[8]),
(vertices[7], vertices[9]),
];
for (v, u) in expected_edges {
assert!(
graph.are_adjacent(v, u),
"expected {v:?} and {u:?} to be adjacent after delete"
);
}
for v in [vertices[1], vertices[4], vertices[5], vertices[6]] {
assert!(
!graph.neighbors(v).any(|u| u == vertices[2]),
"unexpected adjacency of {v:?} to deleted vertex {:?}",
vertices[2]
);
}
} }
#[test] #[test]
@@ -452,6 +477,20 @@ mod tests {
"unexpected vertex count before delete" "unexpected vertex count before delete"
); );
assert_eq!(graph.edge_count(), 1, "unexpected edge count before delete"); assert_eq!(graph.edge_count(), 1, "unexpected edge count before delete");
assert!(
graph.are_adjacent(v1, v2),
"expected vertices to be adjacent before delete"
);
assert_eq!(
graph.degree(v1),
1,
"unexpected vertex degree before delete"
);
assert_eq!(
graph.degree(v2),
1,
"unexpected vertex degree before delete"
);
graph.delete_edge(e); graph.delete_edge(e);
assert_eq!( assert_eq!(
graph.vertex_count(), graph.vertex_count(),
@@ -459,6 +498,98 @@ mod tests {
"unexpected vertex count after delete" "unexpected vertex count after delete"
); );
assert_eq!(graph.edge_count(), 0, "unexpected edge count after delete"); assert_eq!(graph.edge_count(), 0, "unexpected edge count after delete");
assert!(
!graph.are_adjacent(v1, v2),
"unexpected adjacency after delete"
);
assert_eq!(graph.degree(v1), 0, "unexpected vertex degree after delete");
assert_eq!(graph.degree(v2), 0, "unexpected vertex degree after delete");
assert_ne!(
graph.add_edge(v1, v2),
e,
"unexpected duplicate edge after delete"
);
}
#[test]
fn delete_edge_loop() {
let mut graph = Graph::new();
let v = graph.add_vertex();
let e = graph.add_edge(v, v);
assert_eq!(
graph.vertex_count(),
1,
"unexpected vertex count before delete"
);
assert_eq!(graph.edge_count(), 1, "unexpected edge count before delete");
assert!(
graph.are_adjacent(v, v),
"expected vertex to be self-adjacent before delete"
);
assert_eq!(
graph.degree(v),
2,
"unexpected vertex degree before delete"
);
graph.delete_edge(e);
assert_eq!(
graph.vertex_count(),
1,
"unexpected vertex count after delete"
);
assert_eq!(graph.edge_count(), 0, "unexpected edge count after delete");
assert!(
!graph.are_adjacent(v, v),
"unexpected adjacency after delete"
);
assert_eq!(graph.degree(v), 0, "unexpected vertex degree after delete");
assert_ne!(
graph.add_edge(v, v),
e,
"unexpected duplicate edge after delete"
);
}
#[test]
fn delete_edge_multiple() {
let mut graph = Graph::new();
let v1 = graph.add_vertex();
let v2 = graph.add_vertex();
let e = graph.add_edge(v1, v2);
graph.add_edge(v1, v2);
assert_eq!(
graph.vertex_count(),
2,
"unexpected vertex count before delete"
);
assert_eq!(graph.edge_count(), 2, "unexpected edge count before delete");
assert!(
graph.are_adjacent(v1, v2),
"expected vertices to be adjacent before delete"
);
assert_eq!(
graph.degree(v1),
2,
"unexpected vertex degree before delete"
);
assert_eq!(
graph.degree(v2),
2,
"unexpected vertex degree before delete"
);
graph.delete_edge(e);
assert_eq!(
graph.vertex_count(),
2,
"unexpected vertex count after delete"
);
assert_eq!(graph.edge_count(), 1, "unexpected edge count after delete");
assert!(
graph.are_adjacent(v1, v2),
"expected vertices to be adjacent after delete"
);
assert_eq!(graph.degree(v1), 1, "unexpected vertex degree after delete");
assert_eq!(graph.degree(v2), 1, "unexpected vertex degree after delete");
assert_ne!( assert_ne!(
graph.add_edge(v1, v2), graph.add_edge(v1, v2),
e, e,