diff --git a/src/models.rs b/src/models.rs index e31e264..325000a 100644 --- a/src/models.rs +++ b/src/models.rs @@ -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 = 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());