Move capacity and add methods from GraphTopology trait to new GraphTopologyAddition trait
116 lines
3.9 KiB
Rust
116 lines
3.9 KiB
Rust
#[doc(hidden)]
|
|
#[macro_export]
|
|
macro_rules! entity_map_tests {
|
|
($T:ty) => {
|
|
#[test]
|
|
fn initial_values_are_default() {
|
|
use $crate::traits::{GraphTopology, GraphTopologyAddition};
|
|
let mut graph = <$T>::new();
|
|
let v1 = graph.add_vertex();
|
|
let v2 = graph.add_vertex();
|
|
let map = graph.vertex_map(42);
|
|
assert_eq!(map[v1], 42);
|
|
assert_eq!(map[v2], 42);
|
|
}
|
|
|
|
#[test]
|
|
fn write_and_read() {
|
|
use $crate::traits::{GraphTopology, GraphTopologyAddition};
|
|
let mut graph = <$T>::new();
|
|
let v1 = graph.add_vertex();
|
|
let v2 = graph.add_vertex();
|
|
let mut map = graph.vertex_map(0);
|
|
map[v1] = 7;
|
|
assert_eq!(map[v1], 7);
|
|
assert_eq!(map[v2], 0);
|
|
}
|
|
|
|
#[test]
|
|
fn lazy_growth_on_read() {
|
|
use $crate::traits::{GraphTopology, GraphTopologyAddition};
|
|
let mut graph = <$T>::new();
|
|
graph.add_vertex();
|
|
let map = graph.vertex_map(99);
|
|
let v = graph.add_vertex();
|
|
assert_eq!(map[v], 99);
|
|
}
|
|
|
|
#[test]
|
|
fn lazy_growth_on_write() {
|
|
use $crate::traits::{GraphTopology, GraphTopologyAddition};
|
|
let mut graph = <$T>::new();
|
|
let v1 = graph.add_vertex();
|
|
let mut map = graph.vertex_map(0);
|
|
let v2 = graph.add_vertex();
|
|
map[v2] = 7;
|
|
assert_eq!(map[v1], 0);
|
|
assert_eq!(map[v2], 7);
|
|
}
|
|
|
|
#[test]
|
|
fn expand_to_new_vertices() {
|
|
use $crate::traits::{GraphTopology, GraphTopologyAddition};
|
|
let mut graph = <$T>::new();
|
|
graph.add_vertex();
|
|
let mut map = graph.vertex_map(42);
|
|
let capacity_before = map.capacity();
|
|
while graph.vertex_capacity() <= capacity_before {
|
|
graph.add_vertex();
|
|
}
|
|
assert!(
|
|
map.capacity() < graph.vertex_capacity(),
|
|
"precondition: map is stale before expand"
|
|
);
|
|
map.expand(graph.vertex_capacity());
|
|
assert_eq!(map.capacity(), graph.vertex_capacity());
|
|
}
|
|
|
|
#[test]
|
|
fn expand_does_not_overwrite_existing_values() {
|
|
use $crate::traits::{GraphTopology, GraphTopologyAddition};
|
|
let mut graph = <$T>::new();
|
|
let v = graph.add_vertex();
|
|
let mut map = graph.vertex_map(0);
|
|
map[v] = 5;
|
|
graph.add_vertex();
|
|
map.expand(graph.vertex_capacity());
|
|
assert_eq!(map[v], 5);
|
|
}
|
|
};
|
|
}
|
|
|
|
#[doc(hidden)]
|
|
#[macro_export]
|
|
macro_rules! entity_map_deletion_tests {
|
|
($T:ty) => {
|
|
#[test]
|
|
fn surviving_vertex_readable_after_delete() {
|
|
use $crate::traits::{GraphTopology, GraphTopologyAddition, GraphTopologyDeletion};
|
|
let mut graph = <$T>::new();
|
|
let v1 = graph.add_vertex();
|
|
let v2 = graph.add_vertex();
|
|
let mut map = graph.vertex_map(0);
|
|
map[v1] = 1;
|
|
map[v2] = 2;
|
|
graph.delete_vertex(v2);
|
|
assert_eq!(map[v1], 1);
|
|
}
|
|
|
|
#[test]
|
|
fn reused_slot_returns_old_value() {
|
|
use $crate::traits::{GraphTopology, GraphTopologyAddition, GraphTopologyDeletion};
|
|
let mut graph = <$T>::new();
|
|
graph.add_vertex();
|
|
let v1 = graph.add_vertex();
|
|
let mut map = graph.vertex_map(0);
|
|
map[v1] = 99;
|
|
graph.delete_vertex(v1);
|
|
let v2 = graph.add_vertex();
|
|
// EntityMap uses raw indices, not vertex identity. A new vertex v2 reusing the slot
|
|
// of previously deleted v1 sees the old value. Callers must reinitialize stale slots
|
|
// after deletion.
|
|
assert_eq!(map[v2], 99);
|
|
}
|
|
};
|
|
}
|