From 02cd1a0dea2f8394b23af80f5bfe39e477bd3783 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20M=C3=BCller?= Date: Thu, 23 Apr 2026 18:18:36 +0200 Subject: [PATCH] Fix several minor issues in AppendGraph::are_adjacent(), unit tests, and formatting --- src/models/append_graph.rs | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/src/models/append_graph.rs b/src/models/append_graph.rs index 4a76fe6..2423463 100644 --- a/src/models/append_graph.rs +++ b/src/models/append_graph.rs @@ -21,6 +21,7 @@ struct IncidenceEntry { next: Option, neighbor: Vertex, } + struct VertexNeighborIterator<'a> { graph: &'a AppendGraph, incidence: Option, @@ -85,7 +86,7 @@ impl GraphTopology for AppendGraph { } fn are_adjacent(&self, v1: Self::Vertex, v2: Self::Vertex) -> bool { - self.neighbors(v1).find(|&x| x == v2).is_some() + self.neighbors(v1).any(|x| x == v2) } fn vertices(&self) -> impl Iterator { @@ -183,9 +184,8 @@ mod tests { fn are_adjacent_vertex_self() { let mut graph = AppendGraph::new(); let v = graph.add_vertex(); - assert_eq!( - graph.are_adjacent(v, v), - false, + assert!( + !graph.are_adjacent(v, v), "should not be adjacent to itself" ); } @@ -290,11 +290,11 @@ mod tests { assert_eq!( graph.neighbors(vertices[4]).count(), 4, - "unexpected vertex count" + "unexpected neighbor count" ); // Expects each neighbor to appear exactly once. This will not work if there are multiple edges. - let neighbors: Vec = vec![vertices[1], vertices[2], vertices[7], vertices[8]]; + let neighbors = vec![vertices[1], vertices[2], vertices[7], vertices[8]]; for v in graph.neighbors(vertices[4]) { assert_eq!( neighbors.iter().filter(|&x| *x == v).count(), @@ -313,9 +313,8 @@ mod tests { assert_eq!(graph.vertex_count(), 1, "unexpected vertex count"); assert_eq!(graph.edge_count(), 1, "unexpected edge count"); assert_eq!(graph.degree(v), 2, "unexpected degree"); - assert_eq!( + assert!( graph.are_adjacent(v, v), - true, "vertex with loop edge should be adjacent to itself" ); let mut neighbors = graph.neighbors(v); @@ -334,10 +333,10 @@ mod tests { #[test] fn multiple_edges() { - let mult = 3; + let k = 3; let mut graph = AppendGraph::new(); let vertices = [graph.add_vertex(), graph.add_vertex()]; - for _ in 0..mult { + for _ in 0..k { graph.add_edge(vertices[0], vertices[1]); } assert_eq!( @@ -345,18 +344,17 @@ mod tests { vertices.len(), "unexpected vertex count" ); - assert_eq!(graph.edge_count(), mult, "unexpected edge count"); + assert_eq!(graph.edge_count(), k, "unexpected edge count"); for v in vertices { - assert_eq!(graph.degree(v), mult, "unexpected degree of {v:?}"); + assert_eq!(graph.degree(v), k, "unexpected degree of {v:?}"); } - assert_eq!( + assert!( graph.are_adjacent(vertices[0], vertices[1]), - true, "should be adjacent" ); for i in 0..2 { let mut neighbors = graph.neighbors(vertices[i]); - for j in 0..mult { + for j in 0..k { assert_eq!( neighbors.next(), Some(vertices[1 - i]),