From 4c6a8a43e4dfdba7eae13d4ffebd8d59a02c28df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20M=C3=BCller?= Date: Fri, 4 Sep 2026 09:28:00 +0200 Subject: [PATCH] Refactor tests: use MakeTestGraph in model-specific tests, deconstruct vertex arrays where useful, adjust local variable names --- src/models/append_graph.rs | 28 +++---- src/models/frozen_graph.rs | 16 ++-- src/models/graph.rs | 69 ++++++++--------- src/testing/fixtures.rs | 12 +-- ...raph_topology_addition_deletion_testing.rs | 4 +- .../graph_topology_addition_testing.rs | 20 ++--- .../graph_topology_deletion_testing.rs | 44 +++++------ src/testing/graph_topology_testing.rs | 75 +++++++------------ src/traits.rs | 12 +-- tests/bfs.rs | 31 ++++---- tests/dfs.rs | 37 +++++---- 11 files changed, 153 insertions(+), 195 deletions(-) diff --git a/src/models/append_graph.rs b/src/models/append_graph.rs index dda99a1..c20ed1d 100644 --- a/src/models/append_graph.rs +++ b/src/models/append_graph.rs @@ -90,10 +90,10 @@ impl IncidenceCursor for AppendGraphIncidenceCursor { /// use grapherity::models::AppendGraph; /// /// let mut graph = AppendGraph::new(); -/// let v1 = graph.add_vertex(); -/// let v2 = graph.add_vertex(); -/// let e = graph.add_edge(v1, v2); -/// assert!(graph.are_adjacent(v1, v2)); +/// let u = graph.add_vertex(); +/// let v = graph.add_vertex(); +/// let e = graph.add_edge(u, v); +/// assert!(graph.are_adjacent(u, v)); /// ``` /// /// # Time and space complexity @@ -122,13 +122,13 @@ impl AppendGraph { /// Adds a single incidence of an edge, which is composed by two such incidences, to the /// incidences vector. - fn add_incidence(&mut self, v1: Vertex, v2: Vertex) { + fn add_incidence(&mut self, u: Vertex, v: Vertex) { self.incidences.push(IncidenceEntry { - next: self.vertices[v1.0].first_incidence.take(), - adjacent: v2, + next: self.vertices[u.0].first_incidence.take(), + adjacent: v, }); - self.vertices[v1.0].incidence_count += 1; - self.vertices[v1.0].first_incidence = Some(Edge::new(self.incidences.len() - 1)); + self.vertices[u.0].incidence_count += 1; + self.vertices[u.0].first_incidence = Some(Edge::new(self.incidences.len() - 1)); } fn raw_incidences(&self, v: Vertex) -> impl Iterator { @@ -177,8 +177,8 @@ impl GraphTopology for AppendGraph { self.vertices[v.0].incidence_count } - fn are_adjacent(&self, v1: Self::Vertex, v2: Self::Vertex) -> bool { - self.adjacent_vertices(v1).any(|x| x == v2) + fn are_adjacent(&self, u: Self::Vertex, v: Self::Vertex) -> bool { + self.adjacent_vertices(u).any(|x| x == v) } fn vertices(&self) -> impl Iterator { @@ -244,9 +244,9 @@ impl GraphTopologyAddition for AppendGraph { Vertex(self.vertices.len() - 1) } - fn add_edge(&mut self, v1: Self::Vertex, v2: Self::Vertex) -> Self::Edge { - self.add_incidence(v1, v2); - self.add_incidence(v2, v1); + fn add_edge(&mut self, u: Self::Vertex, v: Self::Vertex) -> Self::Edge { + self.add_incidence(u, v); + self.add_incidence(v, u); Edge::new(self.incidences.len() - 2) } } diff --git a/src/models/frozen_graph.rs b/src/models/frozen_graph.rs index 978e335..5b62182 100644 --- a/src/models/frozen_graph.rs +++ b/src/models/frozen_graph.rs @@ -74,13 +74,13 @@ impl IncidenceCursor for FrozenGraphIncidenceCursor { /// /// // Constructs a FrozenGraph instance via AppendGraph. /// let mut graph = AppendGraph::new(); -/// let v1 = graph.add_vertex(); -/// let v2 = graph.add_vertex(); -/// let e = graph.add_edge(v1, v2); -/// let (graph, vertices, edges) = FrozenGraph::from_graph(&graph); +/// let u = graph.add_vertex(); +/// let v = graph.add_vertex(); +/// let e = graph.add_edge(u, v); +/// let (graph, vertex_map, edge_map) = FrozenGraph::from_graph(&graph); /// -/// // Queries the FrozenGraph instance. -/// assert!(graph.are_adjacent(vertices[v1], vertices[v2])); +/// // Queries the FrozenGraph instance via the returned map. +/// assert!(graph.are_adjacent(vertex_map[u], vertex_map[v])); /// ``` /// /// # Time and space complexity @@ -210,8 +210,8 @@ impl GraphTopology for FrozenGraph { self.vertices[v.0 + 1] - self.vertices[v.0] } - fn are_adjacent(&self, v1: Self::Vertex, v2: Self::Vertex) -> bool { - self.adjacent_vertices(v1).any(|x| x == v2) + fn are_adjacent(&self, u: Self::Vertex, v: Self::Vertex) -> bool { + self.adjacent_vertices(u).any(|x| x == v) } fn vertices(&self) -> impl Iterator { diff --git a/src/models/graph.rs b/src/models/graph.rs index 37b1a75..3895d80 100644 --- a/src/models/graph.rs +++ b/src/models/graph.rs @@ -152,13 +152,13 @@ impl Graph { /// Adds a single incidence of an edge, which is composed by two such incidences, to the /// incidences arena, and returns its index. - fn add_incidence(&mut self, v1: Vertex, v2: Vertex) -> Edge { + fn add_incidence(&mut self, u: Vertex, v: Vertex) -> Edge { let edge = self.incidences.insert(IncidenceEntry { - next: self.vertices[v1].first_incidence.take(), - adjacent: VertexSlot(v2.arr_idx()), + next: self.vertices[u].first_incidence.take(), + adjacent: VertexSlot(v.arr_idx()), }); - self.vertices[v1].incidence_count += 1; - self.vertices[v1].first_incidence = Some(IncidenceSlot::new(edge.arr_idx())); + self.vertices[u].incidence_count += 1; + self.vertices[u].first_incidence = Some(IncidenceSlot::new(edge.arr_idx())); edge } @@ -270,8 +270,8 @@ impl GraphTopology for Graph { self.vertices[v].incidence_count } - fn are_adjacent(&self, v1: Self::Vertex, v2: Self::Vertex) -> bool { - self.adjacent_vertices(v1).any(|x| x == v2) + fn are_adjacent(&self, u: Self::Vertex, v: Self::Vertex) -> bool { + self.adjacent_vertices(u).any(|x| x == v) } fn vertices(&self) -> impl Iterator { @@ -284,16 +284,16 @@ impl GraphTopology for Graph { } fn incident_vertices(&self, e: Self::Edge) -> (Self::Vertex, Self::Vertex) { - let v2 = self + let v = self .vertices .get_idx(self.incidences[e].adjacent.0) .unwrap(); let f = self.incidences.get_idx(e.arr_idx() ^ 1).unwrap(); - let v1 = self + let u = self .vertices .get_idx(self.incidences[f].adjacent.0) .unwrap(); - (v1, v2) + (u, v) } fn edges(&self) -> impl Iterator { @@ -345,9 +345,9 @@ impl GraphTopologyAddition for Graph { }) } - fn add_edge(&mut self, v1: Self::Vertex, v2: Self::Vertex) -> Self::Edge { - let first = self.add_incidence(v1, v2); - self.add_incidence(v2, v1); + fn add_edge(&mut self, u: Self::Vertex, v: Self::Vertex) -> Self::Edge { + let first = self.add_incidence(u, v); + self.add_incidence(v, u); first } } @@ -403,30 +403,25 @@ mod trait_tests { #[cfg(test)] mod tests { use super::*; + use crate::testing::fixtures::MakeTestGraph; #[test] fn incident_vertices_paired_index() { - let mut graph = Graph::new(); - let v1 = graph.add_vertex(); - let v2 = graph.add_vertex(); - let e = graph.add_edge(v1, v2); + let (graph, [v0, v1], e) = Graph::single_edge(); let f = graph .incidences .get_idx(e.arr_idx() + 1) .expect("paired index should be valid"); - let (u1, u2) = graph.incident_vertices(f); + let (u0, u1) = graph.incident_vertices(f); assert!( - (u1 == v1 && u2 == v2) || (u1 == v2 && u2 == v1), - "unexpected incident vertices {u1:?} and {u2:?} for edge {f:?}" + (u0 == v0 && u1 == v1) || (u0 == v1 && u1 == v0), + "unexpected incident vertices {u0:?} and {u1:?} for edge {f:?}" ); } #[test] fn delete_edge_paired_index() { - let mut graph = Graph::new(); - let v1 = graph.add_vertex(); - let v2 = graph.add_vertex(); - let e = graph.add_edge(v1, v2); + let (mut graph, _, e) = Graph::single_edge(); let f = graph .incidences .get_idx(e.arr_idx() + 1) @@ -437,9 +432,7 @@ mod tests { #[test] fn delete_edge_loop_paired_index() { - let mut graph = Graph::new(); - let v = graph.add_vertex(); - let e = graph.add_edge(v, v); + let (mut graph, _, e) = Graph::loop_edge(); let f = graph .incidences .get_idx(e.arr_idx() + 1) @@ -450,23 +443,21 @@ mod tests { #[test] fn reused_slot_returns_old_value() { - let mut graph = Graph::new(); - graph.add_vertex(); - let v1 = graph.add_vertex(); + let (mut graph, [_, v], _) = Graph::single_edge(); let mut map = graph.vertex_map(0); - map[v1] = 99; - graph.delete_vertex(v1); - let v2 = graph.add_vertex(); + map[v] = 99; + graph.delete_vertex(v); + let u = graph.add_vertex(); assert_eq!( - v1.arr_idx(), - v2.arr_idx(), - "precondition: new vertex {v1:?} should reuse slot of deleted vertex {v2:?}" + v.arr_idx(), + u.arr_idx(), + "precondition: new vertex {v:?} should reuse slot of deleted vertex {u:?}" ); - // ElementMap uses raw indices, not vertex identity. A new vertex v2 reusing the slot - // of previously deleted v1 sees the old value. Callers must reinitialize stale slots + // ElementMap uses raw indices, not vertex identity. A new vertex u reusing the slot + // of previously deleted v sees the old value. Callers must reinitialize stale slots // after deletion. assert_eq!( - map[v2], 99, + map[u], 99, "new vertex reusing slot of deleted vertex should return old value" ); } diff --git a/src/testing/fixtures.rs b/src/testing/fixtures.rs index 3b02869..0701f91 100644 --- a/src/testing/fixtures.rs +++ b/src/testing/fixtures.rs @@ -58,7 +58,7 @@ impl MakeTestGraph for G { (vertices[7], vertices[8]), (vertices[7], vertices[9]), ] - .map(|(v1, v2)| (graph.add_edge(v1, v2), v1, v2)); + .map(|(u, v)| (graph.add_edge(u, v), u, v)); let i = |vertex, edge| Incidence { vertex, edge }; let incidences = [ vec![i(vertices[1], edges[0].0), i(vertices[1], edges[1].0)], @@ -323,13 +323,13 @@ where // Walks the path: tracks current vertex, confirms each edge is incident to it. let mut current = source; for (i, &e) in path.iter().enumerate() { - let (v1, v2) = graph.incident_vertices(e); - assert_ne!(v1, v2, "path should not contain loop edge {e:?}"); + let (u, v) = graph.incident_vertices(e); + assert_ne!(u, v, "path should not contain loop edge {e:?}"); assert!( - v1 == current || v2 == current, - "path edge {e:?} (index {i}, vertices {v1:?} to {v2:?}) not incident to vertex {current:?}" + u == current || v == current, + "path edge {e:?} (index {i}, vertices {u:?} to {v:?}) not incident to vertex {current:?}" ); - current = if v1 == current { v2 } else { v1 }; + current = if u == current { v } else { u }; } assert!( targets.contains(¤t), diff --git a/src/testing/graph_topology_addition_deletion_testing.rs b/src/testing/graph_topology_addition_deletion_testing.rs index a7b9b72..a6e6643 100644 --- a/src/testing/graph_topology_addition_deletion_testing.rs +++ b/src/testing/graph_topology_addition_deletion_testing.rs @@ -50,11 +50,11 @@ pub fn delete_edge_add_edge() where G::Edge: Debug, { - let (mut graph, vertices, e) = G::single_edge(); + let (mut graph, [v0, v1], e) = G::single_edge(); graph.delete_edge(e); assert_eq!(graph.edge_count(), 0, "unexpected edge count after delete"); assert_ne!( - graph.add_edge(vertices[0], vertices[1]), + graph.add_edge(v0, v1), e, "unexpected duplicate edge after re-add" ); diff --git a/src/testing/graph_topology_addition_testing.rs b/src/testing/graph_topology_addition_testing.rs index e48b7c7..77f68b1 100644 --- a/src/testing/graph_topology_addition_testing.rs +++ b/src/testing/graph_topology_addition_testing.rs @@ -77,12 +77,12 @@ pub fn reserve_edges_increases_capacity() { pub fn reserve_edges_prevents_reallocation_on_add() { let mut graph = G::default(); - let v1 = graph.add_vertex(); - let v2 = graph.add_vertex(); + let u = graph.add_vertex(); + let v = graph.add_vertex(); graph.reserve_edges(10); let capacity_before = graph.edge_capacity(); for _ in 0..10 { - graph.add_edge(v1, v2); + graph.add_edge(u, v); } assert_eq!( graph.edge_capacity(), @@ -116,10 +116,10 @@ where G::Edge: Debug, { let mut graph = G::default(); - let v1 = graph.add_vertex(); - let v2 = graph.add_vertex(); - let e = graph.add_edge(v1, v2); - assert_ne!(graph.add_edge(v1, v2), e, "unexpected duplicate edge"); + let u = graph.add_vertex(); + let v = graph.add_vertex(); + let e = graph.add_edge(u, v); + assert_ne!(graph.add_edge(u, v), e, "unexpected duplicate edge"); } pub fn vertex_map_new_vertex() { @@ -139,10 +139,10 @@ pub fn vertex_map_new_vertex() { pub fn edge_map_new_edge() { let mut graph = G::default(); - let v1 = graph.add_vertex(); - let v2 = graph.add_vertex(); + let u = graph.add_vertex(); + let v = graph.add_vertex(); let mut map = graph.edge_map(28); - let e = graph.add_edge(v1, v2); + let e = graph.add_edge(u, v); assert_eq!(map[e], 28); map[e] = 8; assert_eq!( diff --git a/src/testing/graph_topology_deletion_testing.rs b/src/testing/graph_topology_deletion_testing.rs index c51b42a..d1d82e7 100644 --- a/src/testing/graph_topology_deletion_testing.rs +++ b/src/testing/graph_topology_deletion_testing.rs @@ -136,7 +136,7 @@ where } pub fn delete_edge() { - let (mut graph, vertices, e) = G::single_edge(); + let (mut graph, [v0, v1], e) = G::single_edge(); assert_eq!( graph.vertex_count(), 2, @@ -144,16 +144,16 @@ pub fn delete_edge() { ); assert_eq!(graph.edge_count(), 1, "unexpected edge count before delete"); assert!( - graph.are_adjacent(vertices[0], vertices[1]), + graph.are_adjacent(v0, v1), "expected vertices to be adjacent before delete" ); assert_eq!( - graph.degree(vertices[0]), + graph.degree(v0), 1, "unexpected vertex degree before delete" ); assert_eq!( - graph.degree(vertices[1]), + graph.degree(v1), 1, "unexpected vertex degree before delete" ); @@ -165,19 +165,11 @@ pub fn delete_edge() { ); assert_eq!(graph.edge_count(), 0, "unexpected edge count after delete"); assert!( - !graph.are_adjacent(vertices[0], vertices[1]), + !graph.are_adjacent(v0, v1), "unexpected adjacency after delete" ); - assert_eq!( - graph.degree(vertices[0]), - 0, - "unexpected vertex degree after delete" - ); - assert_eq!( - graph.degree(vertices[1]), - 0, - "unexpected vertex degree after delete" - ); + assert_eq!(graph.degree(v0), 0, "unexpected vertex degree after delete"); + assert_eq!(graph.degree(v1), 0, "unexpected vertex degree after delete"); } pub fn delete_edge_loop() { @@ -209,7 +201,7 @@ pub fn delete_edge_loop() { pub fn delete_edge_multiple() { const K: usize = 2; - let (mut graph, vertices, edges) = G::multiple_edges::(); + let (mut graph, [v0, v1], edges) = G::multiple_edges::(); assert_eq!( graph.vertex_count(), 2, @@ -217,16 +209,16 @@ pub fn delete_edge_multiple() ); assert_eq!(graph.edge_count(), K, "unexpected edge count before delete"); assert!( - graph.are_adjacent(vertices[0], vertices[1]), + graph.are_adjacent(v0, v1), "expected vertices to be adjacent before delete" ); assert_eq!( - graph.degree(vertices[0]), + graph.degree(v0), K, "unexpected vertex degree before delete" ); assert_eq!( - graph.degree(vertices[1]), + graph.degree(v1), K, "unexpected vertex degree before delete" ); @@ -242,16 +234,16 @@ pub fn delete_edge_multiple() "unexpected edge count after delete" ); assert!( - graph.are_adjacent(vertices[0], vertices[1]), + graph.are_adjacent(v0, v1), "expected vertices to be adjacent after delete" ); assert_eq!( - graph.degree(vertices[0]), + graph.degree(v0), K - 1, "unexpected vertex degree after delete" ); assert_eq!( - graph.degree(vertices[1]), + graph.degree(v1), K - 1, "unexpected vertex degree after delete" ); @@ -305,11 +297,11 @@ where let (mut graph, _, edges, _) = G::standard(); let e = edges[2].0; graph.delete_edge(e); - for &(f, v1, v2) in edges.iter().filter(|&&(f, _, _)| f != e) { - let (u1, u2) = graph.incident_vertices(f); + for &(f, v0, v1) in edges.iter().filter(|&&(f, _, _)| f != e) { + let (u0, u1) = graph.incident_vertices(f); assert!( - (u1 == v1 && u2 == v2) || (u1 == v2 && u2 == v1), - "unexpected incident vertices {u1:?} and {u2:?} for edge {f:?} after delete" + (u0 == v0 && u1 == v1) || (u0 == v1 && u1 == v0), + "unexpected incident vertices {u0:?} and {u1:?} for edge {f:?} after delete" ); } } diff --git a/src/testing/graph_topology_testing.rs b/src/testing/graph_topology_testing.rs index b3bb449..415fc52 100644 --- a/src/testing/graph_topology_testing.rs +++ b/src/testing/graph_topology_testing.rs @@ -135,25 +135,12 @@ where G::Vertex: Debug, { const K: usize = 3; - let (graph, vertices, _) = G::multiple_edges::(); + let (graph, [v0, v1], _) = G::multiple_edges::(); assert_eq!(graph.vertex_count(), 2, "unexpected vertex count"); assert_eq!(graph.edge_count(), K, "unexpected edge count"); - assert_eq!( - graph.degree(vertices[0]), - K, - "unexpected degree of vertex {:?}", - vertices[0] - ); - assert_eq!( - graph.degree(vertices[1]), - K, - "unexpected degree of vertex {:?}", - vertices[1] - ); - assert!( - graph.are_adjacent(vertices[0], vertices[1]), - "should be adjacent" - ); + assert_eq!(graph.degree(v0), K, "unexpected degree of vertex {v0:?}"); + assert_eq!(graph.degree(v1), K, "unexpected degree of vertex {v1:?}",); + assert!(graph.are_adjacent(v0, v1), "should be adjacent"); } pub fn are_adjacent_vertex_self() { @@ -165,15 +152,9 @@ pub fn are_adjacent_vertex_self() { } pub fn are_adjacent_single_edge() { - let (graph, vertices, _) = G::single_edge(); - assert!( - graph.are_adjacent(vertices[0], vertices[1]), - "should be adjacent" - ); - assert!( - graph.are_adjacent(vertices[1], vertices[0]), - "should be adjacent" - ); + let (graph, [v0, v1], _) = G::single_edge(); + assert!(graph.are_adjacent(v0, v1), "should be adjacent"); + assert!(graph.are_adjacent(v1, v0), "should be adjacent"); } pub fn are_adjacent() @@ -341,11 +322,11 @@ where G::Vertex: Debug, G::Edge: Debug, { - let (graph, vertices, e) = G::single_edge(); - let (u, v) = graph.incident_vertices(e); + let (graph, [v0, v1], e) = G::single_edge(); + let (u0, u1) = graph.incident_vertices(e); assert!( - (u == vertices[0] && v == vertices[1]) || (u == vertices[1] && v == vertices[0]), - "unexpected incident vertices {u:?} and {v:?} for edge {e:?}" + (u0 == v0 && u1 == v1) || (u0 == v1 && u1 == v0), + "unexpected incident vertices {u0:?} and {u1:?} for edge {e:?}" ); } @@ -367,20 +348,16 @@ where G::Vertex: Debug, G::Edge: Debug, { - let (graph, vertices, edges) = G::multiple_edges::<2>(); + let (graph, [v0, v1], edges) = G::multiple_edges::<2>(); assert_ne!(edges[0], edges[1], "edges should be distinct"); - let (u, v) = graph.incident_vertices(edges[0]); - assert!( - (u == vertices[0] && v == vertices[1]) || (u == vertices[1] && v == vertices[0]), - "unexpected incident vertices {u:?} and {v:?} for first multi-edge {:?}", - edges[0] - ); - let (u, v) = graph.incident_vertices(edges[1]); - assert!( - (u == vertices[0] && v == vertices[1]) || (u == vertices[1] && v == vertices[0]), - "unexpected incident vertices {u:?} and {v:?} for second multi-edge {:?}", - edges[1] - ); + for i in 0..2 { + let (u0, u1) = graph.incident_vertices(edges[i]); + assert!( + (u0 == v0 && u1 == v1) || (u0 == v1 && u1 == v0), + "unexpected incident vertices {u0:?} and {u1:?} for multi-edge {:?} (index {i})", + edges[i] + ); + } } pub fn incident_vertices() @@ -389,11 +366,11 @@ where G::Edge: Debug, { let (graph, _, edges, _) = G::standard(); - for &(e, v1, v2) in edges.iter() { - let (u1, u2) = graph.incident_vertices(e); + for &(e, v0, v1) in edges.iter() { + let (u0, u1) = graph.incident_vertices(e); assert!( - (u1 == v1 && u2 == v2) || (u1 == v2 && u2 == v1), - "unexpected incident vertices {u1:?} and {u2:?} for edge {e:?}" + (u0 == v0 && u1 == v1) || (u0 == v1 && u1 == v0), + "unexpected incident vertices {u0:?} and {u1:?} for edge {e:?}" ); } } @@ -741,8 +718,8 @@ where } pub fn incidence_cursor_copy() { - let (graph, vertices, _) = G::two_edge_path(); - let mut c1 = graph.incidence_cursor(vertices[1]); + let (graph, [_, v, _], _) = G::two_edge_path(); + let mut c1 = graph.incidence_cursor(v); assert!(c1.next(&graph).is_some(), "expected first incidence"); let mut c2 = c1; assert!( diff --git a/src/traits.rs b/src/traits.rs index fda781a..9fdfccd 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -101,14 +101,14 @@ pub trait GraphTopology { /// Panics if `v` is not a valid vertex of this graph. fn degree(&self, v: Self::Vertex) -> usize; - /// Returns `true` if there is at least one edge between `v1` and `v2`, and `false` otherwise. + /// Returns `true` if there is at least one edge between `u` and `v`, and `false` otherwise. /// /// A vertex is adjacent to itself if and only if it has a loop edge. /// /// # Panics /// - /// Panics if `v1` or `v2` is not a valid vertex of this graph. - fn are_adjacent(&self, v1: Self::Vertex, v2: Self::Vertex) -> bool; + /// Panics if `u` or `v` is not a valid vertex of this graph. + fn are_adjacent(&self, u: Self::Vertex, v: Self::Vertex) -> bool; /// Returns an iterator over all vertices in the graph. fn vertices(&self) -> impl Iterator; @@ -206,12 +206,12 @@ pub trait GraphTopologyAddition: GraphTopology + Default { /// Adds a new isolated vertex and returns its handle. fn add_vertex(&mut self) -> Self::Vertex; - /// Adds a new edge between `v1` and `v2` and returns its handle. + /// Adds a new edge between `u` and `v` and returns its handle. /// /// # Panics /// - /// Panics if `v1` or `v2` is not a valid vertex of this graph. - fn add_edge(&mut self, v1: Self::Vertex, v2: Self::Vertex) -> Self::Edge; + /// Panics if `u` or `v` is not a valid vertex of this graph. + fn add_edge(&mut self, u: Self::Vertex, v: Self::Vertex) -> Self::Edge; } /// A trait that adds deletion operations to an undirected graph topology. diff --git a/tests/bfs.rs b/tests/bfs.rs index 53a1dfe..72cec83 100644 --- a/tests/bfs.rs +++ b/tests/bfs.rs @@ -161,9 +161,9 @@ fn bfs_find_source() { } fn bfs_find_disconnected() { - let (graph, vertices) = G::disconnected(); + let (graph, [v0, v1, _]) = G::disconnected(); assert_eq!( - algorithms::bfs_find(&graph, vertices[0], vertices[1]), + algorithms::bfs_find(&graph, v0, v1), None, "disconnected target should not be found" ); @@ -196,12 +196,11 @@ fn bfs_find_where_disconnected() where G::Vertex: Debug, { - let (graph, vertices) = G::disconnected(); + let (graph, [v0, v1, _]) = G::disconnected(); assert_eq!( - algorithms::bfs_find_where(&graph, vertices[0], |v| v == vertices[1]), + algorithms::bfs_find_where(&graph, v0, |v| v == v1), None, - "disconnected vertex {:?} should not be found", - vertices[1] + "disconnected vertex {v1:?} should not be found" ); } @@ -209,9 +208,9 @@ fn bfs_find_where_no_match() where G::Vertex: Debug, { - let (graph, vertices, _, _) = G::standard(); + let (graph, [v0, ..], _, _) = G::standard(); assert_eq!( - algorithms::bfs_find_where(&graph, vertices[0], |_| false), + algorithms::bfs_find_where(&graph, v0, |_| false), None, "no vertex should match an always-false predicate" ); @@ -258,9 +257,9 @@ fn bfs_find_path_disconnected() where G::Edge: Debug, { - let (graph, vertices) = G::disconnected(); + let (graph, [v0, v1, _]) = G::disconnected(); assert_eq!( - algorithms::bfs_find_path(&graph, vertices[0], vertices[1]), + algorithms::bfs_find_path(&graph, v0, v1), None, "no path should exist to disconnected vertex" ); @@ -270,8 +269,8 @@ fn bfs_find_path_adjacent() where G::Edge: Debug, { - let (graph, vertices, e) = G::single_edge(); - let path = algorithms::bfs_find_path(&graph, vertices[0], vertices[1]) + let (graph, [v0, v1], e) = G::single_edge(); + let path = algorithms::bfs_find_path(&graph, v0, v1) .expect("path should exist between adjacent vertices"); assert_eq!(path, [e], "path should contain only the connecting edge"); } @@ -308,9 +307,9 @@ fn bfs_find_path_where_disconnected() where G::Edge: Debug, { - let (graph, vertices) = G::disconnected(); + let (graph, [v0, v1, _]) = G::disconnected(); assert_eq!( - algorithms::bfs_find_path_where(&graph, vertices[0], |v| v == vertices[1]), + algorithms::bfs_find_path_where(&graph, v0, |v| v == v1), None, "no path should exist to disconnected vertex" ); @@ -320,9 +319,9 @@ fn bfs_find_path_where_no_match() where G::Edge: Debug, { - let (graph, vertices, _, _) = G::standard(); + let (graph, [v0, ..], _, _) = G::standard(); assert_eq!( - algorithms::bfs_find_path_where(&graph, vertices[0], |_| false), + algorithms::bfs_find_path_where(&graph, v0, |_| false), None, "no path should exist when predicate never matches" ); diff --git a/tests/dfs.rs b/tests/dfs.rs index d49bbcb..25fa58b 100644 --- a/tests/dfs.rs +++ b/tests/dfs.rs @@ -150,9 +150,9 @@ fn dfs_find_source() { } fn dfs_find_disconnected() { - let (graph, vertices) = G::disconnected(); + let (graph, [v0, v1, _]) = G::disconnected(); assert!( - !algorithms::dfs_find(&graph, vertices[0], vertices[1]), + !algorithms::dfs_find(&graph, v0, v1), "disconnected target should not be found" ); } @@ -188,12 +188,11 @@ fn dfs_find_where_disconnected() where G::Vertex: Debug, { - let (graph, vertices) = G::disconnected(); + let (graph, [v0, v1, _]) = G::disconnected(); assert_eq!( - algorithms::dfs_find_where(&graph, vertices[0], |v| v == vertices[1]), + algorithms::dfs_find_where(&graph, v0, |v| v == v1), None, - "disconnected vertex {:?} should not be found", - vertices[1] + "disconnected vertex {v1:?} should not be found" ); } @@ -201,9 +200,9 @@ fn dfs_find_where_no_match() where G::Vertex: Debug, { - let (graph, vertices, _, _) = G::standard(); + let (graph, [v0, ..], _, _) = G::standard(); assert_eq!( - algorithms::dfs_find_where(&graph, vertices[0], |_| false), + algorithms::dfs_find_where(&graph, v0, |_| false), None, "no vertex should match an always-false predicate" ); @@ -213,10 +212,10 @@ fn dfs_find_where_adjacent() where G::Vertex: Debug, { - let (graph, vertices, _, _) = G::standard(); + let (graph, [v0, v1, ..], _, _) = G::standard(); assert_eq!( - algorithms::dfs_find_where(&graph, vertices[0], |v| v == vertices[1]), - Some(vertices[1]), + algorithms::dfs_find_where(&graph, v0, |v| v == v1), + Some(v1), "expected to find adjacent vertex" ); } @@ -249,9 +248,9 @@ fn dfs_find_path_disconnected() where G::Edge: Debug, { - let (graph, vertices) = G::disconnected(); + let (graph, [v0, v1, _]) = G::disconnected(); assert_eq!( - algorithms::dfs_find_path(&graph, vertices[0], vertices[1]), + algorithms::dfs_find_path(&graph, v0, v1), None, "no path should exist to disconnected vertex" ); @@ -261,8 +260,8 @@ fn dfs_find_path_adjacent() where G::Edge: Debug, { - let (graph, vertices, e) = G::single_edge(); - let path = algorithms::dfs_find_path(&graph, vertices[0], vertices[1]) + let (graph, [v0, v1], e) = G::single_edge(); + let path = algorithms::dfs_find_path(&graph, v0, v1) .expect("path should exist between adjacent vertices"); assert_eq!(path, [e], "path should contain only the connecting edge"); } @@ -293,9 +292,9 @@ fn dfs_find_path_where_disconnected() where G::Edge: Debug, { - let (graph, vertices) = G::disconnected(); + let (graph, [v0, v1, _]) = G::disconnected(); assert_eq!( - algorithms::dfs_find_path_where(&graph, vertices[0], |v| v == vertices[1]), + algorithms::dfs_find_path_where(&graph, v0, |v| v == v1), None, "no path should exist to disconnected vertex" ); @@ -305,9 +304,9 @@ fn dfs_find_path_where_no_match() where G::Edge: Debug, { - let (graph, vertices, _, _) = G::standard(); + let (graph, [v0, ..], _, _) = G::standard(); assert_eq!( - algorithms::dfs_find_path_where(&graph, vertices[0], |_| false), + algorithms::dfs_find_path_where(&graph, v0, |_| false), None, "no path should exist when predicate never matches" );