Extract asserts from Clone tests to be reusable
This commit is contained in:
@@ -338,3 +338,143 @@ where
|
||||
}
|
||||
path
|
||||
}
|
||||
|
||||
/// Asserts that the two graphs are isomorphic.
|
||||
///
|
||||
/// `vertices` must contain all vertices of the `original` graph, and `vertex_map` must for any
|
||||
/// vertex in the `original` graph return the corresponding vertex in the `other` graph.
|
||||
///
|
||||
/// `edges` must contain all edges and their endpoints of the `original` graph, and `edge_map` must
|
||||
/// for any edge in the `original` graph return the corresponding edge in the `other` graph.
|
||||
pub fn assert_isomorphic<G: GraphTopology, H: GraphTopology>(
|
||||
original: &G,
|
||||
other: &H,
|
||||
vertices: &[G::Vertex],
|
||||
vertex_map: impl Fn(G::Vertex) -> H::Vertex,
|
||||
edges: &[(G::Edge, G::Vertex, G::Vertex)],
|
||||
edge_map: impl Fn(G::Edge) -> H::Edge,
|
||||
) where
|
||||
G::Vertex: Debug,
|
||||
G::Edge: Debug,
|
||||
H::Vertex: Debug,
|
||||
H::Edge: Debug,
|
||||
{
|
||||
assert_eq!(
|
||||
original.vertex_count(),
|
||||
vertices.len(),
|
||||
"precondition: all vertices of the original must be provided"
|
||||
);
|
||||
assert_eq!(
|
||||
original.edge_count(),
|
||||
edges.len(),
|
||||
"precondition: all edges of the original must be provided"
|
||||
);
|
||||
assert_eq!(
|
||||
other.vertex_count(),
|
||||
original.vertex_count(),
|
||||
"other graph should have the same vertex count as the original"
|
||||
);
|
||||
assert_eq!(
|
||||
other.edge_count(),
|
||||
original.edge_count(),
|
||||
"other graph should have the same edge count as the original"
|
||||
);
|
||||
for &v in vertices.iter() {
|
||||
assert_eq!(
|
||||
other.degree(vertex_map(v)),
|
||||
original.degree(v),
|
||||
"vertex {v:?} should have the same degree in the other graph as in the original"
|
||||
);
|
||||
let mut expected: Vec<_> = original.incidences(v).collect();
|
||||
for incidence in other.incidences(vertex_map(v)) {
|
||||
let pos = expected
|
||||
.iter()
|
||||
.position(|x| {
|
||||
vertex_map(x.vertex) == incidence.vertex && edge_map(x.edge) == incidence.edge
|
||||
})
|
||||
.expect(&format!(
|
||||
"unexpected incidence {incidence:?} of vertex {v:?} in the other graph"
|
||||
));
|
||||
expected.swap_remove(pos);
|
||||
}
|
||||
assert!(
|
||||
expected.is_empty(),
|
||||
"expected incidences {expected:?} of vertex {v:?} are missing in the other graph"
|
||||
);
|
||||
}
|
||||
for &(e, u, v) in edges.iter() {
|
||||
assert_eq!(
|
||||
other.incident_vertices(edge_map(e)),
|
||||
(vertex_map(u), vertex_map(v)),
|
||||
"edge {e:?} should have the same incident vertices in the other graph as in the original"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Asserts that mutation of the two graphs happens independently.
|
||||
///
|
||||
/// `vertices` must contain all vertices of the `original` graph, which must not be empty, and
|
||||
/// `vertex_map` must for any vertex in the `original` graph return the corresponding vertex in the
|
||||
/// `other` graph.
|
||||
pub fn assert_mutation_independent<G: GraphTopologyAddition, H: GraphTopologyAddition>(
|
||||
original: &mut G,
|
||||
other: &mut H,
|
||||
vertices: &[G::Vertex],
|
||||
vertex_map: impl Fn(G::Vertex) -> H::Vertex,
|
||||
) {
|
||||
let edge_count = original.edge_count();
|
||||
assert_eq!(
|
||||
original.vertex_count(),
|
||||
vertices.len(),
|
||||
"precondition: all vertices of the original must be provided"
|
||||
);
|
||||
assert!(
|
||||
!vertices.is_empty(),
|
||||
"precondition: at least one vertex must be provided"
|
||||
);
|
||||
let u = other.add_vertex();
|
||||
other.add_edge(vertex_map(vertices[0]), u);
|
||||
assert_eq!(
|
||||
other.vertex_count(),
|
||||
vertices.len() + 1,
|
||||
"precondition: vertex count of other graph"
|
||||
);
|
||||
assert_eq!(
|
||||
other.edge_count(),
|
||||
edge_count + 1,
|
||||
"precondition: edge count of other graph"
|
||||
);
|
||||
assert_eq!(
|
||||
original.vertex_count(),
|
||||
vertices.len(),
|
||||
"mutating the other graph should not affect the vertex count of the original"
|
||||
);
|
||||
assert_eq!(
|
||||
original.edge_count(),
|
||||
edge_count,
|
||||
"mutating the other graph should not affect the edge count of the original"
|
||||
);
|
||||
|
||||
let v = original.add_vertex();
|
||||
original.add_edge(vertices[0], v);
|
||||
assert_eq!(
|
||||
original.vertex_count(),
|
||||
vertices.len() + 1,
|
||||
"precondition: vertex count of original"
|
||||
);
|
||||
assert_eq!(
|
||||
original.edge_count(),
|
||||
edge_count + 1,
|
||||
"precondition: edge count of original"
|
||||
);
|
||||
assert_eq!(
|
||||
other.vertex_count(),
|
||||
vertices.len() + 1,
|
||||
"mutating the original should not affect the vertex count of the other graph"
|
||||
);
|
||||
assert_eq!(
|
||||
other.edge_count(),
|
||||
edge_count + 1,
|
||||
"mutating the original should not affect the edge count of the other graph"
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user