Refactor graph trait tests to minimize macro code, and split into separate files per macro
The tests only declare a list of tests in the macro, no longer the test functions themselves. This allows better tooling support (e.g. IDE and rustfmt).
This commit is contained in:
@@ -0,0 +1,152 @@
|
||||
use std::fmt::Debug;
|
||||
|
||||
use crate::traits::GraphTopologyAddition;
|
||||
|
||||
#[doc(hidden)]
|
||||
#[macro_export]
|
||||
macro_rules! graph_topology_addition_tests {
|
||||
($T:ty) => {
|
||||
$crate::graph_topology_addition_tests!(@wrap $T,
|
||||
reserve_vertices_increases_capacity,
|
||||
reserve_vertices_prevents_reallocation_on_add,
|
||||
reserve_vertices_does_not_affect_edge_capacity,
|
||||
reserve_edges_increases_capacity,
|
||||
reserve_edges_prevents_reallocation_on_add,
|
||||
reserve_edges_does_not_affect_vertex_capacity,
|
||||
add_vertex,
|
||||
add_edge,
|
||||
vertex_map_new_vertex,
|
||||
edge_map_new_edge,
|
||||
);
|
||||
};
|
||||
|
||||
(@wrap $T:ty, $($name:ident),* $(,)?) => {
|
||||
$(
|
||||
#[test]
|
||||
fn $name() {
|
||||
$crate::testing::graph_topology_addition_testing::$name::<$T>();
|
||||
}
|
||||
)*
|
||||
};
|
||||
}
|
||||
|
||||
pub(crate) fn reserve_vertices_increases_capacity<G: GraphTopologyAddition>() {
|
||||
let mut graph = G::default();
|
||||
let capacity_before = graph.vertex_capacity();
|
||||
graph.reserve_vertices(capacity_before + 10);
|
||||
assert!(
|
||||
graph.vertex_capacity() > capacity_before,
|
||||
"expected sufficient capacity increase after reserve"
|
||||
);
|
||||
}
|
||||
|
||||
pub(crate) fn reserve_vertices_prevents_reallocation_on_add<G: GraphTopologyAddition>() {
|
||||
let mut graph = G::default();
|
||||
graph.reserve_vertices(10);
|
||||
let capacity_before = graph.vertex_capacity();
|
||||
for _ in 0..10 {
|
||||
graph.add_vertex();
|
||||
}
|
||||
assert_eq!(
|
||||
graph.vertex_capacity(),
|
||||
capacity_before,
|
||||
"writes within reserved capacity should not reallocate"
|
||||
);
|
||||
}
|
||||
|
||||
pub(crate) fn reserve_vertices_does_not_affect_edge_capacity<G: GraphTopologyAddition>() {
|
||||
let mut graph = G::default();
|
||||
let capacity_before = graph.edge_capacity();
|
||||
graph.reserve_vertices(10);
|
||||
assert_eq!(
|
||||
graph.edge_capacity(),
|
||||
capacity_before,
|
||||
"reserve_vertices must not change edge capacity"
|
||||
);
|
||||
}
|
||||
|
||||
pub(crate) fn reserve_edges_increases_capacity<G: GraphTopologyAddition>() {
|
||||
let mut graph = G::default();
|
||||
let capacity_before = graph.edge_capacity();
|
||||
graph.reserve_edges(capacity_before + 10);
|
||||
assert!(
|
||||
graph.edge_capacity() > capacity_before,
|
||||
"expected sufficient capacity increase after reserve"
|
||||
);
|
||||
}
|
||||
|
||||
pub(crate) fn reserve_edges_prevents_reallocation_on_add<G: GraphTopologyAddition>() {
|
||||
let mut graph = G::default();
|
||||
let v1 = graph.add_vertex();
|
||||
let v2 = graph.add_vertex();
|
||||
graph.reserve_edges(10);
|
||||
let capacity_before = graph.edge_capacity();
|
||||
for _ in 0..10 {
|
||||
graph.add_edge(v1, v2);
|
||||
}
|
||||
assert_eq!(
|
||||
graph.edge_capacity(),
|
||||
capacity_before,
|
||||
"writes within reserved capacity should not reallocate"
|
||||
);
|
||||
}
|
||||
|
||||
pub(crate) fn reserve_edges_does_not_affect_vertex_capacity<G: GraphTopologyAddition>() {
|
||||
let mut graph = G::default();
|
||||
let capacity_before = graph.vertex_capacity();
|
||||
graph.reserve_edges(10);
|
||||
assert_eq!(
|
||||
graph.vertex_capacity(),
|
||||
capacity_before,
|
||||
"reserve_edges must not change vertex capacity"
|
||||
);
|
||||
}
|
||||
|
||||
pub(crate) fn add_vertex<G: GraphTopologyAddition>()
|
||||
where
|
||||
G::Vertex: Debug,
|
||||
{
|
||||
let mut graph = G::default();
|
||||
let v = graph.add_vertex();
|
||||
assert_ne!(graph.add_vertex(), v, "unexpected duplicate vertex");
|
||||
}
|
||||
|
||||
pub(crate) fn add_edge<G: GraphTopologyAddition>()
|
||||
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");
|
||||
}
|
||||
|
||||
pub(crate) fn vertex_map_new_vertex<G: GraphTopologyAddition>() {
|
||||
let mut graph = G::default();
|
||||
let mut map = graph.vertex_map(27);
|
||||
let v = graph.add_vertex();
|
||||
assert_eq!(
|
||||
map[v], 27,
|
||||
"unexpected value from default map read for new vertex"
|
||||
);
|
||||
map[v] = 9;
|
||||
assert_eq!(
|
||||
map[v], 9,
|
||||
"unexpected value from map read after write for new vertex"
|
||||
);
|
||||
}
|
||||
|
||||
pub(crate) fn edge_map_new_edge<G: GraphTopologyAddition>() {
|
||||
let mut graph = G::default();
|
||||
let v1 = graph.add_vertex();
|
||||
let v2 = graph.add_vertex();
|
||||
let mut map = graph.edge_map(28);
|
||||
let e = graph.add_edge(v1, v2);
|
||||
assert_eq!(map[e], 28);
|
||||
map[e] = 8;
|
||||
assert_eq!(
|
||||
map[e], 8,
|
||||
"unexpected value from map read after write for new edge"
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user