Add tests for some trivial graphs

This commit is contained in:
2025-10-25 00:51:48 +02:00
parent 6ff02f8c67
commit 50f3a150bb
+40 -2
View File
@@ -117,12 +117,31 @@ impl Graph {
mod tests { mod tests {
use super::*; 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] #[test]
fn vertex_count() { fn vertex_count() {
let (graph, _) = make_test_graph(); let (graph, _) = make_test_graph();
assert_eq!(graph.vertex_count(), 10, "unexpected vertex count"); 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] #[test]
fn edge_count() { fn edge_count() {
let (graph, _) = make_test_graph(); let (graph, _) = make_test_graph();
@@ -130,7 +149,14 @@ mod tests {
} }
#[test] #[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 (graph, vertices) = make_test_graph();
let expected_degrees = [1, 4, 4, 2, 4, 2, 3, 3, 2, 3]; let expected_degrees = [1, 4, 4, 2, 4, 2, 3, 3, 2, 3];
for i in 0..graph.vertex_count() { for i in 0..graph.vertex_count() {
@@ -144,7 +170,19 @@ mod tests {
} }
#[test] #[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(); let (graph, vertices) = make_test_graph();
assert!( assert!(
graph.are_adjacent(vertices[0], vertices[1]), graph.are_adjacent(vertices[0], vertices[1]),