Add tests for Graph::neighbors()
This commit is contained in:
@@ -214,6 +214,34 @@ mod tests {
|
||||
println!("vertices: {:?}", vertices);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn neighbors_empty() {
|
||||
let mut graph = Graph::new();
|
||||
let vertex = graph.add_vertex();
|
||||
assert_eq!(
|
||||
graph.neighbors(vertex).count(),
|
||||
0,
|
||||
"neighbor iterator of vertex with degree 0 should have no elements"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn neighbors() {
|
||||
let (graph, vertices) = make_test_graph();
|
||||
// Checks neighbors of vertex 4.
|
||||
assert_eq!(graph.neighbors(vertices[4]).count(), 4, "unexpected vertex count");
|
||||
|
||||
let mut neighbors: Vec<Vertex> = vec![vertices[1], vertices[2], vertices[7], vertices[8]];
|
||||
for v in graph.neighbors(vertices[4]) {
|
||||
if let Some(index) = neighbors.iter().position(|&x| x == v) {
|
||||
neighbors.swap_remove(index);
|
||||
} else {
|
||||
panic!("unexpected vertex of {v:?} from iterator");
|
||||
}
|
||||
}
|
||||
println!("vertices: {:?}", vertices);
|
||||
}
|
||||
|
||||
fn make_test_graph() -> (Graph, [Vertex; 10]) {
|
||||
let mut graph = Graph::new();
|
||||
let vertices: [Vertex; 10] = core::array::from_fn(|_| graph.add_vertex());
|
||||
|
||||
Reference in New Issue
Block a user