Add reserve_vertices and reserve_edges to GraphTopologyAddition
This commit is contained in:
@@ -852,7 +852,6 @@ macro_rules! graph_topology_tests {
|
||||
#[test]
|
||||
fn incidence_cursor_copy() {
|
||||
use $crate::traits::{GraphTopology, IncidenceCursor};
|
||||
// Constructs a graph with two vertices connected to `v`.
|
||||
let mut graph = <$T>::new();
|
||||
let v = graph.add_vertex();
|
||||
for _ in 0..2 {
|
||||
@@ -909,6 +908,90 @@ macro_rules! graph_topology_tests {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reserve_vertices_increases_capacity() {
|
||||
use $crate::traits::GraphTopologyAddition;
|
||||
let mut graph = <$T>::new();
|
||||
let capacity_before = graph.vertex_capacity();
|
||||
graph.reserve_vertices(capacity_before + 10);
|
||||
assert!(
|
||||
graph.vertex_capacity() > capacity_before,
|
||||
"expected sufficient capacity increase after reserve"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reserve_vertices_prevents_reallocation_on_add() {
|
||||
use $crate::traits::GraphTopologyAddition;
|
||||
let mut graph = <$T>::new();
|
||||
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"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reserve_vertices_does_not_affect_edge_capacity() {
|
||||
use $crate::traits::GraphTopologyAddition;
|
||||
let mut graph = <$T>::new();
|
||||
let capacity_before = graph.edge_capacity();
|
||||
graph.reserve_vertices(10);
|
||||
assert_eq!(
|
||||
graph.edge_capacity(),
|
||||
capacity_before,
|
||||
"reserve_vertices must not change edge capacity"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reserve_edges_increases_capacity() {
|
||||
use $crate::traits::GraphTopologyAddition;
|
||||
let mut graph = <$T>::new();
|
||||
let capacity_before = graph.edge_capacity();
|
||||
graph.reserve_edges(capacity_before + 10);
|
||||
assert!(
|
||||
graph.edge_capacity() > capacity_before,
|
||||
"expected sufficient capacity increase after reserve"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reserve_edges_prevents_reallocation_on_add() {
|
||||
use $crate::traits::GraphTopologyAddition;
|
||||
let mut graph = <$T>::new();
|
||||
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"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reserve_edges_does_not_affect_vertex_capacity() {
|
||||
use $crate::traits::GraphTopologyAddition;
|
||||
let mut graph = <$T>::new();
|
||||
let capacity_before = graph.vertex_capacity();
|
||||
graph.reserve_edges(10);
|
||||
assert_eq!(
|
||||
graph.vertex_capacity(),
|
||||
capacity_before,
|
||||
"reserve_edges must not change vertex capacity"
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user