Add unit tests for GraphTopologyDeletion for Graph
This commit is contained in:
+101
-2
@@ -1,6 +1,6 @@
|
|||||||
use typed_generational_arena::{Arena, Index};
|
use typed_generational_arena::{Arena, Index};
|
||||||
|
|
||||||
use crate::traits::GraphTopology;
|
use crate::traits::{GraphTopology, GraphTopologyDeletion};
|
||||||
|
|
||||||
type Vertex = Index<VertexIncidenceHeader, usize, usize>;
|
type Vertex = Index<VertexIncidenceHeader, usize, usize>;
|
||||||
|
|
||||||
@@ -117,7 +117,16 @@ impl GraphTopology for Graph {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: impl GraphTopologyDeletion for Graph.
|
// TODO: Benchmark delete with storing "previous" in O(1) vs. linear lookup in O(degree).
|
||||||
|
impl GraphTopologyDeletion for Graph {
|
||||||
|
fn delete_vertex(&mut self, v: Self::Vertex) {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn delete_edge(&mut self, e: Self::Edge) {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
@@ -376,6 +385,96 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn delete_vertex() {
|
||||||
|
let mut graph = Graph::new();
|
||||||
|
let v = graph.add_vertex();
|
||||||
|
assert_eq!(
|
||||||
|
graph.vertex_count(),
|
||||||
|
1,
|
||||||
|
"unexpected vertex count before delete"
|
||||||
|
);
|
||||||
|
graph.delete_vertex(v);
|
||||||
|
assert_eq!(
|
||||||
|
graph.vertex_count(),
|
||||||
|
0,
|
||||||
|
"unexpected vertex count after delete"
|
||||||
|
);
|
||||||
|
assert_ne!(
|
||||||
|
graph.add_vertex(),
|
||||||
|
v,
|
||||||
|
"unexpected duplicate vertex after delete"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn delete_vertex_invalid_index() {
|
||||||
|
let mut graph = Graph::new();
|
||||||
|
let v = graph.add_vertex();
|
||||||
|
graph.delete_vertex(v);
|
||||||
|
let result = std::panic::catch_unwind(move || graph.delete_vertex(v));
|
||||||
|
assert!(result.is_err(), "second deletion should panic");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn delete_vertex_connected() {
|
||||||
|
let (mut graph, vertices) = make_test_graph();
|
||||||
|
assert_eq!(
|
||||||
|
graph.vertex_count(),
|
||||||
|
10,
|
||||||
|
"unexpected vertex count before delete"
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
graph.edge_count(),
|
||||||
|
14,
|
||||||
|
"unexpected edge count before delete"
|
||||||
|
);
|
||||||
|
graph.delete_vertex(vertices[2]);
|
||||||
|
assert_eq!(
|
||||||
|
graph.vertex_count(),
|
||||||
|
9,
|
||||||
|
"unexpected vertex count after delete"
|
||||||
|
);
|
||||||
|
assert_eq!(graph.edge_count(), 10, "unexpected edge count after delete");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn delete_edge() {
|
||||||
|
let mut graph = Graph::new();
|
||||||
|
let v1 = graph.add_vertex();
|
||||||
|
let v2 = graph.add_vertex();
|
||||||
|
let e = graph.add_edge(v1, v2);
|
||||||
|
assert_eq!(
|
||||||
|
graph.vertex_count(),
|
||||||
|
2,
|
||||||
|
"unexpected vertex count before delete"
|
||||||
|
);
|
||||||
|
assert_eq!(graph.edge_count(), 1, "unexpected edge count before delete");
|
||||||
|
graph.delete_edge(e);
|
||||||
|
assert_eq!(
|
||||||
|
graph.vertex_count(),
|
||||||
|
2,
|
||||||
|
"unexpected vertex count after delete"
|
||||||
|
);
|
||||||
|
assert_eq!(graph.edge_count(), 0, "unexpected edge count after delete");
|
||||||
|
assert_ne!(
|
||||||
|
graph.add_edge(v1, v2),
|
||||||
|
e,
|
||||||
|
"unexpected duplicate edge after delete"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn delete_edge_invalid_index() {
|
||||||
|
let mut graph = Graph::new();
|
||||||
|
let v1 = graph.add_vertex();
|
||||||
|
let v2 = graph.add_vertex();
|
||||||
|
let e = graph.add_edge(v1, v2);
|
||||||
|
graph.delete_edge(e);
|
||||||
|
let result = std::panic::catch_unwind(move || graph.delete_edge(e));
|
||||||
|
assert!(result.is_err(), "second deletion should panic");
|
||||||
|
}
|
||||||
|
|
||||||
fn make_test_graph() -> (Graph, [Vertex; 10]) {
|
fn make_test_graph() -> (Graph, [Vertex; 10]) {
|
||||||
let mut graph = Graph::new();
|
let mut graph = Graph::new();
|
||||||
let vertices: [Vertex; 10] = core::array::from_fn(|_| graph.add_vertex());
|
let vertices: [Vertex; 10] = core::array::from_fn(|_| graph.add_vertex());
|
||||||
|
|||||||
Reference in New Issue
Block a user