Move common test helpers to fixtures module
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
use crate::maps::ElementMap;
|
||||
use crate::traits::{GraphTopology, GraphTopologyAddition, Incidence};
|
||||
use std::fmt::Debug;
|
||||
|
||||
pub trait MakeTestGraph: GraphTopology + Sized {
|
||||
fn standard() -> (
|
||||
@@ -162,3 +164,92 @@ impl<G: GraphTopologyAddition> MakeTestGraph for G {
|
||||
(graph, vertices, [e0, e1, e2, e3])
|
||||
}
|
||||
}
|
||||
|
||||
pub fn assert_standard_unweighted_distances_v0<V, F>(actual_for: F, vertices: &[V])
|
||||
where
|
||||
V: Debug + Copy,
|
||||
F: Fn(V) -> Option<u32>,
|
||||
{
|
||||
let expected = [0, 1, 2, 2, 2, 3, 3, 3, 3, 4];
|
||||
for (i, v) in vertices.iter().enumerate() {
|
||||
assert_eq!(
|
||||
actual_for(*v),
|
||||
Some(expected[i]),
|
||||
"unexpected distance from vertex {:?} to vertex {:?}",
|
||||
vertices[0],
|
||||
vertices[i]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn assert_standard_unweighted_predecessors_v0<V: Debug + Copy + PartialEq>(
|
||||
actual: &ElementMap<V, Option<V>>,
|
||||
vertices: &[V],
|
||||
) {
|
||||
let expected = [
|
||||
vec![None],
|
||||
vec![Some(vertices[0])],
|
||||
vec![Some(vertices[1])],
|
||||
vec![Some(vertices[1])],
|
||||
vec![Some(vertices[1])],
|
||||
vec![Some(vertices[2])],
|
||||
vec![Some(vertices[2]), Some(vertices[3])],
|
||||
vec![Some(vertices[4])],
|
||||
vec![Some(vertices[4])],
|
||||
vec![Some(vertices[5]), Some(vertices[6]), Some(vertices[7])],
|
||||
];
|
||||
for i in 0..10 {
|
||||
assert!(
|
||||
expected[i].contains(&actual[vertices[i]]),
|
||||
"unexpected predecessor {:?} of vertex {:?}",
|
||||
actual[vertices[i]],
|
||||
vertices[i]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Asserts that the path given as `path_option` is a valid path in `graph`
|
||||
///
|
||||
/// Walks the path and asserts that it is `Some`, that it is empty if and only if
|
||||
/// `source == target`, that its edges are pairwise incident in the order given, and that it starts
|
||||
/// at `source` and ends at `target`.
|
||||
pub fn assert_valid_path<G: GraphTopology>(
|
||||
graph: &G,
|
||||
path_option: Option<Vec<G::Edge>>,
|
||||
source: G::Vertex,
|
||||
targets: &[G::Vertex],
|
||||
) -> Vec<G::Edge>
|
||||
where
|
||||
G::Vertex: Debug,
|
||||
G::Edge: Debug,
|
||||
{
|
||||
let path = path_option.expect(&format!(
|
||||
"path should exist between source vertex {:?} and connected targets {:?}",
|
||||
source, targets
|
||||
));
|
||||
|
||||
if targets.contains(&source) {
|
||||
assert!(
|
||||
path.is_empty(),
|
||||
"path from source to itself should be empty"
|
||||
);
|
||||
} else {
|
||||
assert!(!path.is_empty(), "path should not be empty");
|
||||
// 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:?}");
|
||||
assert!(
|
||||
v1 == current || v2 == current,
|
||||
"path edge {e:?} (index {i}, vertices {v1:?} to {v2:?}) not incident to vertex {current:?}"
|
||||
);
|
||||
current = if v1 == current { v2 } else { v1 };
|
||||
}
|
||||
assert!(
|
||||
targets.contains(¤t),
|
||||
"path should end at a target vertex ({targets:?}), but ended at {current:?}"
|
||||
);
|
||||
}
|
||||
path
|
||||
}
|
||||
|
||||
+7
-85
@@ -1,9 +1,8 @@
|
||||
use std::fmt::Debug;
|
||||
|
||||
use grapherity::algorithms;
|
||||
use grapherity::maps::ElementMap;
|
||||
use grapherity::testing::fixtures;
|
||||
use grapherity::testing::fixtures::MakeTestGraph;
|
||||
use grapherity::traits::GraphTopology;
|
||||
|
||||
macro_rules! bfs_tests {
|
||||
($T:ty) => {
|
||||
@@ -96,8 +95,8 @@ where
|
||||
{
|
||||
let (graph, vertices, _, _) = G::standard();
|
||||
let result = algorithms::bfs(&graph, vertices[0]);
|
||||
assert_bfs_distances::<G>(&result.distances, &vertices);
|
||||
assert_bfs_predecessors::<G>(&result.predecessors, &vertices);
|
||||
fixtures::assert_standard_unweighted_distances_v0(|v| result.distances[v], &vertices);
|
||||
fixtures::assert_standard_unweighted_predecessors_v0(&result.predecessors, &vertices);
|
||||
}
|
||||
|
||||
fn bfs_distances_single_vertex<G: MakeTestGraph>() {
|
||||
@@ -135,7 +134,7 @@ where
|
||||
{
|
||||
let (graph, vertices, _, _) = G::standard();
|
||||
let distances = algorithms::bfs_distances(&graph, vertices[0]);
|
||||
assert_bfs_distances::<G>(&distances, &vertices);
|
||||
fixtures::assert_standard_unweighted_distances_v0(|v| distances[v], &vertices);
|
||||
}
|
||||
|
||||
fn bfs_find_source<G: MakeTestGraph>() {
|
||||
@@ -161,27 +160,10 @@ where
|
||||
G::Vertex: Debug,
|
||||
{
|
||||
let (graph, vertices, _, _) = G::standard();
|
||||
let expected_distances = [
|
||||
Some(0),
|
||||
Some(1),
|
||||
Some(2),
|
||||
Some(2),
|
||||
Some(2),
|
||||
Some(3),
|
||||
Some(3),
|
||||
Some(3),
|
||||
Some(3),
|
||||
Some(4),
|
||||
];
|
||||
for i in 0..10 {
|
||||
assert_eq!(
|
||||
algorithms::bfs_find(&graph, vertices[0], vertices[i]),
|
||||
expected_distances[i],
|
||||
"unexpected distance from {:?} to {:?}",
|
||||
vertices[0],
|
||||
vertices[i]
|
||||
fixtures::assert_standard_unweighted_distances_v0(
|
||||
|v| algorithms::bfs_find(&graph, vertices[0], v),
|
||||
&vertices,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn bfs_find_where_source_matches<G: MakeTestGraph>()
|
||||
@@ -245,63 +227,3 @@ where
|
||||
vertices[8]
|
||||
);
|
||||
}
|
||||
|
||||
fn assert_bfs_distances<G: GraphTopology>(
|
||||
distances: &ElementMap<G::Vertex, Option<u32>>,
|
||||
vertices: &[G::Vertex],
|
||||
) where
|
||||
G::Vertex: Debug,
|
||||
{
|
||||
let expected = [
|
||||
Some(0),
|
||||
Some(1),
|
||||
Some(2),
|
||||
Some(2),
|
||||
Some(2),
|
||||
Some(3),
|
||||
Some(3),
|
||||
Some(3),
|
||||
Some(3),
|
||||
Some(4),
|
||||
];
|
||||
for i in 0..10 {
|
||||
assert_eq!(
|
||||
distances[vertices[i]], expected[i],
|
||||
"unexpected distance from {:?} to {:?}",
|
||||
vertices[0], vertices[i]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn assert_bfs_predecessors<G: GraphTopology>(
|
||||
predecessors: &ElementMap<G::Vertex, Option<G::Vertex>>,
|
||||
vertices: &[G::Vertex],
|
||||
) where
|
||||
G::Vertex: Debug,
|
||||
{
|
||||
assert_eq!(
|
||||
predecessors[vertices[0]], None,
|
||||
"source should have no predecessor"
|
||||
);
|
||||
// Each non-source vertex's predecessor must be adjacent and at distance one less.
|
||||
let expected_predecessors = [
|
||||
vec![None],
|
||||
vec![Some(vertices[0])],
|
||||
vec![Some(vertices[1])],
|
||||
vec![Some(vertices[1])],
|
||||
vec![Some(vertices[1])],
|
||||
vec![Some(vertices[2])],
|
||||
vec![Some(vertices[2]), Some(vertices[3])],
|
||||
vec![Some(vertices[4])],
|
||||
vec![Some(vertices[4])],
|
||||
vec![Some(vertices[5]), Some(vertices[6]), Some(vertices[7])],
|
||||
];
|
||||
for i in 1..10 {
|
||||
assert!(
|
||||
expected_predecessors[i].contains(&predecessors[vertices[i]]),
|
||||
"unexpected predecessor {:?} of {:?}",
|
||||
predecessors[vertices[i]],
|
||||
vertices[i]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
+6
-45
@@ -2,6 +2,7 @@ use std::fmt::Debug;
|
||||
|
||||
use grapherity::algorithms;
|
||||
use grapherity::maps::ElementMap;
|
||||
use grapherity::testing::fixtures;
|
||||
use grapherity::testing::fixtures::MakeTestGraph;
|
||||
use grapherity::traits::GraphTopology;
|
||||
|
||||
@@ -257,12 +258,7 @@ where
|
||||
let (graph, vertices, e) = G::single_edge();
|
||||
let path = algorithms::dfs_find_path(&graph, vertices[0], vertices[1])
|
||||
.expect("path should exist between adjacent vertices");
|
||||
assert_eq!(
|
||||
path.len(),
|
||||
1,
|
||||
"unexpected path length between adjacent vertices"
|
||||
);
|
||||
assert_eq!(path[0], e, "path should use the connecting edge");
|
||||
assert_eq!(path, [e], "path should contain only the connecting edge");
|
||||
}
|
||||
|
||||
fn dfs_find_path<G: MakeTestGraph>()
|
||||
@@ -271,11 +267,8 @@ where
|
||||
G::Edge: Debug,
|
||||
{
|
||||
let (graph, vertices, _, _) = G::standard();
|
||||
let path = algorithms::dfs_find_path(&graph, vertices[0], vertices[9]).expect(&format!(
|
||||
"path should exist between connected vertices {:?} and {:?}",
|
||||
vertices[0], vertices[9]
|
||||
));
|
||||
assert_valid_path(&graph, &path, vertices[0], vertices[9]);
|
||||
let path = algorithms::dfs_find_path(&graph, vertices[0], vertices[9]);
|
||||
fixtures::assert_valid_path(&graph, path, vertices[0], &[vertices[9]]);
|
||||
}
|
||||
|
||||
fn dfs_find_path_where_source_matches<G: MakeTestGraph>()
|
||||
@@ -320,13 +313,8 @@ where
|
||||
G::Edge: Debug,
|
||||
{
|
||||
let (graph, vertices, _, _) = G::standard();
|
||||
let path = algorithms::dfs_find_path_where(&graph, vertices[0], |v| v == vertices[9]).expect(
|
||||
&format!(
|
||||
"path should exist between connected vertices {:?} and {:?}",
|
||||
vertices[0], vertices[9]
|
||||
),
|
||||
);
|
||||
assert_valid_path(&graph, &path, vertices[0], vertices[9]);
|
||||
let path = algorithms::dfs_find_path_where(&graph, vertices[0], |v| v == vertices[9]);
|
||||
_ = fixtures::assert_valid_path(&graph, path, vertices[0], &[vertices[9]]);
|
||||
}
|
||||
|
||||
fn assert_dfs_visited<G: GraphTopology>(
|
||||
@@ -369,30 +357,3 @@ fn assert_dfs_predecessors<G: GraphTopology>(
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn assert_valid_path<G: GraphTopology>(
|
||||
graph: &G,
|
||||
path: &[G::Edge],
|
||||
source: G::Vertex,
|
||||
target: G::Vertex,
|
||||
) where
|
||||
G::Vertex: Debug,
|
||||
G::Edge: Debug,
|
||||
{
|
||||
assert!(!path.is_empty(), "path should be non-empty");
|
||||
// Walks the path: tracks current vertex, confirm 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:?}");
|
||||
assert!(
|
||||
v1 == current || v2 == current,
|
||||
"path edge {e:?} (idx {i}, {v1:?} to {v2:?}) not incident to vertex {current:?}"
|
||||
);
|
||||
current = if v1 == current { v2 } else { v1 };
|
||||
}
|
||||
assert_eq!(
|
||||
current, target,
|
||||
"path should end at target {target:?}, but ended at {current:?}"
|
||||
);
|
||||
}
|
||||
|
||||
+21
-73
@@ -4,6 +4,7 @@ use std::hash::Hash;
|
||||
use grapherity::algorithms;
|
||||
use grapherity::algorithms::DijkstraResult;
|
||||
use grapherity::maps::ElementMap;
|
||||
use grapherity::testing::fixtures;
|
||||
use grapherity::testing::fixtures::MakeTestGraph;
|
||||
use grapherity::traits::{GraphTopology, Incidence};
|
||||
|
||||
@@ -94,18 +95,19 @@ fn dijkstra<G: MakeTestGraph>()
|
||||
where
|
||||
G::Vertex: Debug + Hash,
|
||||
{
|
||||
let (graph, vertices, _, _, weights) = make_test_graph_weighted::<G>();
|
||||
let (graph, vertices, _, _, weights) = make_standard_weighted::<G>();
|
||||
let result = algorithms::dijkstra(&graph, vertices[0], |e| weights[e]);
|
||||
assert_test_graph::<G>(&result, &vertices);
|
||||
assert_standard_weighted_distances_v0::<G>(&result.distances, &vertices);
|
||||
assert_standard_weighted_predecessors_v0::<G>(&result.predecessors, &vertices);
|
||||
}
|
||||
|
||||
fn dijkstra_distances<G: MakeTestGraph>()
|
||||
where
|
||||
G::Vertex: Debug + Hash,
|
||||
{
|
||||
let (graph, vertices, _, _, weights) = make_test_graph_weighted::<G>();
|
||||
let (graph, vertices, _, _, weights) = make_standard_weighted::<G>();
|
||||
let distances = algorithms::dijkstra_distances(&graph, vertices[0], |e| weights[e]);
|
||||
assert_distances_test_graph::<G>(&distances, &vertices);
|
||||
assert_standard_weighted_distances_v0::<G>(&distances, &vertices);
|
||||
}
|
||||
|
||||
fn dijkstra_unweighted_single_vertex<G: MakeTestGraph>()
|
||||
@@ -133,7 +135,8 @@ where
|
||||
{
|
||||
let (graph, vertices, _, _) = G::standard();
|
||||
let result = algorithms::dijkstra_unweighted(&graph, vertices[0]);
|
||||
assert_unweighted_test_graph::<G>(&result, &vertices);
|
||||
fixtures::assert_standard_unweighted_distances_v0(|v| result.distances[v], &vertices);
|
||||
fixtures::assert_standard_unweighted_predecessors_v0(&result.predecessors, &vertices);
|
||||
}
|
||||
|
||||
fn dijkstra_distances_unweighted_single_vertex<G: MakeTestGraph>()
|
||||
@@ -161,7 +164,7 @@ where
|
||||
{
|
||||
let (graph, vertices, _, _) = G::standard();
|
||||
let distances = algorithms::dijkstra_distances_unweighted(&graph, vertices[0]);
|
||||
assert_distances_unweighted_test_graph::<G>(&distances, &vertices);
|
||||
fixtures::assert_standard_unweighted_distances_v0(|v| distances[v], &vertices);
|
||||
}
|
||||
|
||||
fn assert_single_vertex<G: GraphTopology>(result: &DijkstraResult<G::Vertex>, v: G::Vertex)
|
||||
@@ -213,12 +216,13 @@ fn assert_distances_disconnected<G: GraphTopology>(
|
||||
}
|
||||
}
|
||||
|
||||
fn assert_test_graph<G: GraphTopology>(result: &DijkstraResult<G::Vertex>, vertices: &[G::Vertex])
|
||||
where
|
||||
fn assert_standard_weighted_predecessors_v0<G: GraphTopology>(
|
||||
actual: &ElementMap<G::Vertex, Option<G::Vertex>>,
|
||||
vertices: &[G::Vertex],
|
||||
) where
|
||||
G::Vertex: Debug,
|
||||
{
|
||||
assert_distances_test_graph::<G>(&result.distances, &vertices);
|
||||
let expected_predecessors_from_v0 = [
|
||||
let expected = [
|
||||
vec![None],
|
||||
vec![Some(vertices[0])],
|
||||
vec![Some(vertices[4])],
|
||||
@@ -232,21 +236,21 @@ where
|
||||
];
|
||||
for i in 0..10 {
|
||||
assert!(
|
||||
expected_predecessors_from_v0[i].contains(&result.predecessors[vertices[i]]),
|
||||
expected[i].contains(&actual[vertices[i]]),
|
||||
"unexpected predecessor {:?} of {:?}",
|
||||
result.predecessors[vertices[i]],
|
||||
actual[vertices[i]],
|
||||
vertices[i]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn assert_distances_test_graph<G: GraphTopology>(
|
||||
distances: &ElementMap<G::Vertex, Option<u32>>,
|
||||
fn assert_standard_weighted_distances_v0<G: GraphTopology>(
|
||||
actual: &ElementMap<G::Vertex, Option<u32>>,
|
||||
vertices: &[G::Vertex],
|
||||
) where
|
||||
G::Vertex: Debug,
|
||||
{
|
||||
let expected_distances_from_v0 = [
|
||||
let expected = [
|
||||
Some(0),
|
||||
Some(1),
|
||||
Some(65),
|
||||
@@ -260,70 +264,14 @@ fn assert_distances_test_graph<G: GraphTopology>(
|
||||
];
|
||||
for i in 0..10 {
|
||||
assert_eq!(
|
||||
distances[vertices[i]], expected_distances_from_v0[i],
|
||||
actual[vertices[i]], expected[i],
|
||||
"unexpected distance from {:?} to {:?}",
|
||||
vertices[0], vertices[i]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn assert_unweighted_test_graph<G: GraphTopology>(
|
||||
result: &DijkstraResult<G::Vertex>,
|
||||
vertices: &[G::Vertex],
|
||||
) where
|
||||
G::Vertex: Debug,
|
||||
{
|
||||
assert_distances_unweighted_test_graph::<G>(&result.distances, &vertices);
|
||||
let expected_predecessors_from_v0 = [
|
||||
vec![None],
|
||||
vec![Some(vertices[0])],
|
||||
vec![Some(vertices[1])],
|
||||
vec![Some(vertices[1])],
|
||||
vec![Some(vertices[1])],
|
||||
vec![Some(vertices[2])],
|
||||
vec![Some(vertices[2]), Some(vertices[3])],
|
||||
vec![Some(vertices[4])],
|
||||
vec![Some(vertices[4])],
|
||||
vec![Some(vertices[5]), Some(vertices[6]), Some(vertices[7])],
|
||||
];
|
||||
for i in 0..10 {
|
||||
assert!(
|
||||
expected_predecessors_from_v0[i].contains(&result.predecessors[vertices[i]]),
|
||||
"unexpected predecessor {:?} of {:?}",
|
||||
result.predecessors[vertices[i]],
|
||||
vertices[i]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn assert_distances_unweighted_test_graph<G: GraphTopology>(
|
||||
distances: &ElementMap<G::Vertex, Option<u32>>,
|
||||
vertices: &[G::Vertex],
|
||||
) where
|
||||
G::Vertex: Debug,
|
||||
{
|
||||
let expected_distances_from_v0 = [
|
||||
Some(0),
|
||||
Some(1),
|
||||
Some(2),
|
||||
Some(2),
|
||||
Some(2),
|
||||
Some(3),
|
||||
Some(3),
|
||||
Some(3),
|
||||
Some(3),
|
||||
Some(4),
|
||||
];
|
||||
for i in 0..10 {
|
||||
assert_eq!(
|
||||
distances[vertices[i]], expected_distances_from_v0[i],
|
||||
"unexpected distance from {:?} to {:?}",
|
||||
vertices[0], vertices[i]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn make_test_graph_weighted<G: MakeTestGraph>() -> (
|
||||
fn make_standard_weighted<G: MakeTestGraph>() -> (
|
||||
G,
|
||||
[G::Vertex; 10],
|
||||
[(G::Edge, G::Vertex, G::Vertex); 18],
|
||||
|
||||
Reference in New Issue
Block a user