mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-06 11:15:46 -06:00
136 lines
3.7 KiB
Rust
136 lines
3.7 KiB
Rust
use pacman::map::direction::Direction;
|
|
use pacman::map::graph::{Graph, Node, TraversalFlags};
|
|
|
|
mod common;
|
|
|
|
#[test]
|
|
fn test_graph_basic_operations() {
|
|
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),
|
|
});
|
|
|
|
assert_eq!(graph.nodes().count(), 2);
|
|
assert!(graph.get_node(node1).is_some());
|
|
assert!(graph.get_node(node2).is_some());
|
|
assert!(graph.get_node(999).is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn test_graph_connect() {
|
|
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),
|
|
});
|
|
|
|
assert!(graph.connect(node1, node2, false, None, Direction::Right).is_ok());
|
|
|
|
let edge1 = graph.find_edge_in_direction(node1, Direction::Right);
|
|
let edge2 = graph.find_edge_in_direction(node2, Direction::Left);
|
|
|
|
assert!(edge1.is_some());
|
|
assert!(edge2.is_some());
|
|
assert_eq!(edge1.unwrap().target, node2);
|
|
assert_eq!(edge2.unwrap().target, node1);
|
|
}
|
|
|
|
#[test]
|
|
fn test_graph_connect_errors() {
|
|
let mut graph = Graph::new();
|
|
let node1 = graph.add_node(Node {
|
|
position: glam::Vec2::new(0.0, 0.0),
|
|
});
|
|
|
|
assert!(graph.connect(node1, 999, false, None, Direction::Right).is_err());
|
|
assert!(graph.connect(999, node1, false, None, Direction::Right).is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn test_graph_edge_permissions() {
|
|
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),
|
|
});
|
|
|
|
graph
|
|
.add_edge(node1, node2, false, None, Direction::Right, TraversalFlags::GHOST)
|
|
.unwrap();
|
|
|
|
let edge = graph.find_edge_in_direction(node1, Direction::Right).unwrap();
|
|
assert_eq!(edge.traversal_flags, TraversalFlags::GHOST);
|
|
}
|
|
|
|
#[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.nodes().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, TraversalFlags::ALL);
|
|
assert!(result.is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn should_error_on_duplicate_edge_without_replace() {
|
|
let mut graph = common::create_test_graph();
|
|
let result = graph.add_edge(0, 1, false, None, Direction::Right, TraversalFlags::ALL);
|
|
assert!(result.is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn should_allow_replacing_an_edge() {
|
|
let mut graph = common::create_test_graph();
|
|
let result = graph.add_edge(0, 1, true, Some(42.0), Direction::Right, TraversalFlags::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 = common::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());
|
|
}
|