test: new graph tests

This commit is contained in:
2025-08-12 19:58:37 -05:00
parent 3a9381a56c
commit 8cd4a2daf6
2 changed files with 73 additions and 7 deletions

View File

@@ -192,14 +192,15 @@ impl Graph {
// Check if the edge already exists in this direction or to the same target
if let Some(err) = adjacency_list.edges().find_map(|e| {
// If we're not replacing the edge, we don't want to replace an edge that already exists in this direction
if !replace && e.direction == direction {
Some(Err("Edge already exists in this direction."))
} else if e.target == to {
Some(Err("Edge already exists."))
} else {
None
if !replace {
// If we're not replacing the edge, we don't want to replace an edge that already exists in this direction
if e.direction == direction {
return Some(Err("Edge already exists in this direction."));
} else if e.target == to {
return Some(Err("Edge already exists."));
}
}
None
}) {
return err;
}

View File

@@ -86,6 +86,71 @@ fn test_graph_edge_permissions() {
assert_eq!(edge.permissions, EdgePermissions::GhostsOnly);
}
#[test]
fn should_add_connected_node() {
let mut graph = Graph::new();
let node1 = graph.add_node(Node {
position: glam::Vec2::new(0.0, 0.0),
});
let node2 = graph
.add_connected(
node1,
Direction::Right,
Node {
position: glam::Vec2::new(16.0, 0.0),
},
)
.unwrap();
assert_eq!(graph.node_count(), 2);
let edge = graph.find_edge(node1, node2);
assert!(edge.is_some());
assert_eq!(edge.unwrap().direction, Direction::Right);
}
#[test]
fn should_error_on_negative_edge_distance() {
let mut graph = Graph::new();
let node1 = graph.add_node(Node {
position: glam::Vec2::new(0.0, 0.0),
});
let node2 = graph.add_node(Node {
position: glam::Vec2::new(16.0, 0.0),
});
let result = graph.add_edge(node1, node2, false, Some(-1.0), Direction::Right, EdgePermissions::All);
assert!(result.is_err());
}
#[test]
fn should_error_on_duplicate_edge_without_replace() {
let mut graph = create_test_graph();
let result = graph.add_edge(0, 1, false, None, Direction::Right, EdgePermissions::All);
assert!(result.is_err());
}
#[test]
fn should_allow_replacing_an_edge() {
let mut graph = create_test_graph();
let result = graph.add_edge(0, 1, true, Some(42.0), Direction::Right, EdgePermissions::All);
assert!(result.is_ok());
let edge = graph.find_edge(0, 1).unwrap();
assert_eq!(edge.distance, 42.0);
}
#[test]
fn should_find_edge_between_nodes() {
let graph = create_test_graph();
let edge = graph.find_edge(0, 1);
assert!(edge.is_some());
assert_eq!(edge.unwrap().target, 1);
let non_existent_edge = graph.find_edge(0, 99);
assert!(non_existent_edge.is_none());
}
#[test]
fn test_traverser_basic() {
let graph = create_test_graph();