From 50f3a150bbaa63098837d2b240c5d8f0b9ac710b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20M=C3=BCller?= Date: Sat, 25 Oct 2025 00:51:48 +0200 Subject: [PATCH] Add tests for some trivial graphs --- src/models.rs | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/src/models.rs b/src/models.rs index 64268af..58e25b0 100644 --- a/src/models.rs +++ b/src/models.rs @@ -117,12 +117,31 @@ impl Graph { mod tests { use super::*; + #[test] + fn add_vertex() { + let mut graph = Graph::new(); + let v = graph.add_vertex(); + assert_ne!(graph.add_vertex(), v, "unexpected duplicate vertex"); + } + + #[test] + fn vertex_count_empty() { + let graph = Graph::new(); + assert_eq!(graph.vertex_count(), 0, "unexpected vertex count"); + } + #[test] fn vertex_count() { let (graph, _) = make_test_graph(); assert_eq!(graph.vertex_count(), 10, "unexpected vertex count"); } + #[test] + fn edge_count_empty() { + let graph = Graph::new(); + assert_eq!(graph.edge_count(), 0, "unexpected edge count"); + } + #[test] fn edge_count() { let (graph, _) = make_test_graph(); @@ -130,7 +149,14 @@ mod tests { } #[test] - fn degrees() { + fn degree_zero() { + let mut graph = Graph::new(); + let v = graph.add_vertex(); + assert_eq!(graph.degree(v), 0, "unexpected non-zero degree"); + } + + #[test] + fn degree() { let (graph, vertices) = make_test_graph(); let expected_degrees = [1, 4, 4, 2, 4, 2, 3, 3, 2, 3]; for i in 0..graph.vertex_count() { @@ -144,7 +170,19 @@ mod tests { } #[test] - fn adjacencies() { + fn are_adjacent_single_edge() { + let mut graph = Graph::new(); + let v1 = graph.add_vertex(); + let v2 = graph.add_vertex(); + assert!(!graph.are_adjacent(v1, v2), "should not be adjacent"); + assert!(!graph.are_adjacent(v2, v1), "should not be adjacent"); + graph.add_edge(v1, v2); + assert!(graph.are_adjacent(v1, v2), "should be adjacent"); + assert!(graph.are_adjacent(v2, v1), "should be adjacent"); + } + + #[test] + fn are_adjacent() { let (graph, vertices) = make_test_graph(); assert!( graph.are_adjacent(vertices[0], vertices[1]),