Add Graph example test

This commit is contained in:
2025-10-05 23:53:06 +02:00
parent 58f330ef46
commit 5a1080783c
+53 -2
View File
@@ -1,4 +1,4 @@
#[derive(Copy, Clone, PartialEq, Eq)]
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
struct Vertex(usize);
#[derive(Copy, Clone, PartialEq, Eq)]
@@ -73,5 +73,56 @@ impl Graph {
}
fn main() {
println!("Hello, world!");
// TODO: Move this graph example into one or more tests.
let mut graph = Graph{
// TODO: The initializations should happen in an initializer or constructor
vertex_incidence_headers: vec![],
next_incidences: vec![],
incidence_vertices: vec![],
};
let vertices: [Vertex; 10] = core::array::from_fn(|_| graph.add_vertex());
graph.add_edge(vertices[0], vertices[1]);
graph.add_edge(vertices[1], vertices[2]);
graph.add_edge(vertices[1], vertices[3]);
graph.add_edge(vertices[1], vertices[4]);
graph.add_edge(vertices[2], vertices[4]);
graph.add_edge(vertices[2], vertices[5]);
graph.add_edge(vertices[2], vertices[6]);
graph.add_edge(vertices[3], vertices[6]);
graph.add_edge(vertices[4], vertices[7]);
graph.add_edge(vertices[4], vertices[8]);
graph.add_edge(vertices[5], vertices[9]);
graph.add_edge(vertices[6], vertices[9]);
graph.add_edge(vertices[7], vertices[8]);
graph.add_edge(vertices[7], vertices[9]);
assert_eq!(graph.vertex_count(), 10, "vertex count");
assert_eq!(graph.edge_count(), 14, "edge count");
let expected_degrees = [1, 4, 4, 2, 4, 2, 3, 3, 2, 3];
for i in 0..graph.vertex_count() {
assert_eq!(graph.degree(vertices[i]), expected_degrees[i],
"degree of {:?}", vertices[i]);
}
assert_eq!(graph.are_adjacent(vertices[0], vertices[1]), true,
"adjacency of {:?} and {:?}", vertices[0], vertices[1]);
assert_eq!(graph.are_adjacent(vertices[9], vertices[5]), true,
"adjacency of {:?} and {:?}", vertices[0], vertices[5]);
assert_eq!(graph.are_adjacent(vertices[9], vertices[3]), false,
"adjacency of {:?} and {:?}", vertices[9], vertices[3]);
for i in 0..vertices.len() {
let exp = match i {
2 => continue,
1 | 4 | 5 | 6 => true,
_ => false,
};
assert_eq!(graph.are_adjacent(vertices[2], vertices[i]), exp,
"adjacency of {:?} and {:?}", vertices[2], vertices[i]);
assert_eq!(graph.are_adjacent(vertices[i], vertices[2]), exp,
"adjacency of {:?} and {:?}", vertices[i], vertices[2]);
}
// TODO: test Dijkstra's algorithm starting at 0 and at another vertex.
let expected_distances_to_v0 = [0, 1, 2, 2, 2, 3, 3, 3, 3, 4];
}