This large refactor allows future re-use of the existing tests for `FrozenGraph`, which does not implement `GraphTopologyAddition`, but instead has to convert from a different graph which does. - Fix tests for `GraphTopologyDeletion` hard-coding Graph instead of using the trait. - Fix some tests silently depending on macro call site uses. - Change `GraphTopologyAddition` tests to use `Default::default` instead of `new`. - Add new tests for `GraphTopology::vertex_map` and `edge_map` that also depend on `GraphTopologyAddition`, removing these dependencies from the existing tests. - Add new test macro `graph_topology_addition_deletion_tests` for tests that need deletion and addition.
165 lines
5.8 KiB
Rust
165 lines
5.8 KiB
Rust
use crate::traits::{GraphTopology, GraphTopologyAddition, Incidence};
|
|
|
|
pub trait MakeTestGraph: GraphTopology + Sized {
|
|
fn standard() -> (
|
|
Self,
|
|
[Self::Vertex; 10],
|
|
[(Self::Edge, Self::Vertex, Self::Vertex); 18],
|
|
[Vec<Incidence<Self::Vertex, Self::Edge>>; 10],
|
|
);
|
|
|
|
fn empty() -> Self;
|
|
|
|
fn single_vertex() -> (Self, Self::Vertex);
|
|
|
|
fn single_edge() -> (Self, [Self::Vertex; 2], Self::Edge);
|
|
|
|
fn disconnected() -> (Self, [Self::Vertex; 3]);
|
|
|
|
fn loop_edge() -> (Self, Self::Vertex, Self::Edge);
|
|
|
|
fn multiple_edges<const K: usize>() -> (Self, [Self::Vertex; 2], [Self::Edge; K]);
|
|
|
|
fn two_edge_path() -> (Self, [Self::Vertex; 3], [Self::Edge; 2]);
|
|
|
|
fn two_edge_path_with_loops() -> (Self, [Self::Vertex; 3], [Self::Edge; 4]);
|
|
}
|
|
|
|
impl<G: GraphTopologyAddition> MakeTestGraph for G {
|
|
fn standard() -> (
|
|
Self,
|
|
[Self::Vertex; 10],
|
|
[(Self::Edge, Self::Vertex, Self::Vertex); 18],
|
|
[Vec<Incidence<Self::Vertex, Self::Edge>>; 10],
|
|
) {
|
|
let mut graph = Self::default();
|
|
let vertices = core::array::from_fn(|_| graph.add_vertex());
|
|
let edges = [
|
|
(vertices[0], vertices[1]),
|
|
(vertices[0], vertices[1]),
|
|
(vertices[1], vertices[2]),
|
|
(vertices[1], vertices[3]),
|
|
(vertices[1], vertices[4]),
|
|
(vertices[2], vertices[2]),
|
|
(vertices[2], vertices[4]),
|
|
(vertices[2], vertices[4]),
|
|
(vertices[2], vertices[5]),
|
|
(vertices[2], vertices[6]),
|
|
(vertices[3], vertices[6]),
|
|
(vertices[4], vertices[4]),
|
|
(vertices[4], vertices[7]),
|
|
(vertices[4], vertices[8]),
|
|
(vertices[5], vertices[9]),
|
|
(vertices[6], vertices[9]),
|
|
(vertices[7], vertices[8]),
|
|
(vertices[7], vertices[9]),
|
|
]
|
|
.map(|(v1, v2)| (graph.add_edge(v1, v2), v1, v2));
|
|
let i = |vertex, edge| Incidence { vertex, edge };
|
|
let incidences = [
|
|
vec![i(vertices[1], edges[0].0), i(vertices[1], edges[1].0)],
|
|
vec![
|
|
i(vertices[0], edges[0].0),
|
|
i(vertices[0], edges[1].0),
|
|
i(vertices[2], edges[2].0),
|
|
i(vertices[3], edges[3].0),
|
|
i(vertices[4], edges[4].0),
|
|
],
|
|
vec![
|
|
i(vertices[2], edges[5].0),
|
|
i(vertices[2], edges[5].0),
|
|
i(vertices[1], edges[2].0),
|
|
i(vertices[4], edges[6].0),
|
|
i(vertices[4], edges[7].0),
|
|
i(vertices[5], edges[8].0),
|
|
i(vertices[6], edges[9].0),
|
|
],
|
|
vec![i(vertices[1], edges[3].0), i(vertices[6], edges[10].0)],
|
|
vec![
|
|
i(vertices[1], edges[4].0),
|
|
i(vertices[2], edges[6].0),
|
|
i(vertices[2], edges[7].0),
|
|
i(vertices[4], edges[11].0),
|
|
i(vertices[4], edges[11].0),
|
|
i(vertices[7], edges[12].0),
|
|
i(vertices[8], edges[13].0),
|
|
],
|
|
vec![i(vertices[2], edges[8].0), i(vertices[9], edges[14].0)],
|
|
vec![
|
|
i(vertices[2], edges[9].0),
|
|
i(vertices[3], edges[10].0),
|
|
i(vertices[9], edges[15].0),
|
|
],
|
|
vec![
|
|
i(vertices[4], edges[12].0),
|
|
i(vertices[8], edges[16].0),
|
|
i(vertices[9], edges[17].0),
|
|
],
|
|
vec![i(vertices[4], edges[13].0), i(vertices[7], edges[16].0)],
|
|
vec![
|
|
i(vertices[5], edges[14].0),
|
|
i(vertices[6], edges[15].0),
|
|
i(vertices[7], edges[17].0),
|
|
],
|
|
];
|
|
(graph, vertices, edges, incidences)
|
|
}
|
|
|
|
fn empty() -> Self {
|
|
Self::default()
|
|
}
|
|
|
|
fn single_vertex() -> (Self, Self::Vertex) {
|
|
let mut graph = Self::default();
|
|
let v = graph.add_vertex();
|
|
(graph, v)
|
|
}
|
|
|
|
fn single_edge() -> (Self, [Self::Vertex; 2], Self::Edge) {
|
|
let mut graph = Self::default();
|
|
let vertices = core::array::from_fn(|_| graph.add_vertex());
|
|
let e = graph.add_edge(vertices[0], vertices[1]);
|
|
(graph, vertices, e)
|
|
}
|
|
|
|
fn disconnected() -> (Self, [Self::Vertex; 3]) {
|
|
let mut graph = Self::default();
|
|
let vertices = core::array::from_fn(|_| graph.add_vertex());
|
|
graph.add_edge(vertices[1], vertices[2]);
|
|
(graph, vertices)
|
|
}
|
|
|
|
fn loop_edge() -> (Self, Self::Vertex, Self::Edge) {
|
|
let mut graph = Self::default();
|
|
let v = graph.add_vertex();
|
|
let e = graph.add_edge(v, v);
|
|
(graph, v, e)
|
|
}
|
|
|
|
fn multiple_edges<const K: usize>() -> (Self, [Self::Vertex; 2], [Self::Edge; K]) {
|
|
let mut graph = Self::default();
|
|
let vertices = core::array::from_fn(|_| graph.add_vertex());
|
|
let edges = core::array::from_fn(|_| graph.add_edge(vertices[0], vertices[1]));
|
|
(graph, vertices, edges)
|
|
}
|
|
|
|
fn two_edge_path() -> (Self, [Self::Vertex; 3], [Self::Edge; 2]) {
|
|
let mut graph = Self::default();
|
|
let vertices = core::array::from_fn(|_| graph.add_vertex());
|
|
let edges = core::array::from_fn(|i| graph.add_edge(vertices[i], vertices[i + 1]));
|
|
(graph, vertices, edges)
|
|
}
|
|
|
|
fn two_edge_path_with_loops() -> (Self, [Self::Vertex; 3], [Self::Edge; 4]) {
|
|
let mut graph = Self::default();
|
|
let vertices = core::array::from_fn(|_| graph.add_vertex());
|
|
// Path edges.
|
|
let e0 = graph.add_edge(vertices[0], vertices[1]);
|
|
let e1 = graph.add_edge(vertices[1], vertices[2]);
|
|
// Loops.
|
|
let e2 = graph.add_edge(vertices[0], vertices[0]);
|
|
let e3 = graph.add_edge(vertices[1], vertices[1]);
|
|
(graph, vertices, [e0, e1, e2, e3])
|
|
}
|
|
}
|