feat(traits): add traits

This commit is contained in:
2024-12-26 18:15:46 +02:00
parent 434a23b15e
commit 91b1326bd4
14 changed files with 466 additions and 305 deletions

View File

@@ -29,7 +29,7 @@ fn valid_start_position(#[case] start_pos: Hex) {
.with_radius(3)
.with_start_position(start_pos)
.build());
assert_some!(maze.get_tile(&start_pos));
assert_some!(maze.get(&start_pos));
}
#[test]
@@ -67,13 +67,13 @@ fn maze_connectivity() {
let maze = assert_ok!(MazeBuilder::new().with_radius(3).build());
// Helper function to count accessible neighbors
fn count_accessible_neighbors(maze: &HexMaze, pos: Hex) -> usize {
fn count_accessible_neighbors(maze: &Maze, pos: Hex) -> usize {
hexx::EdgeDirection::ALL_DIRECTIONS
.iter()
.filter(|&&dir| {
let neighbor = pos + dir;
if let Some(walls) = maze.get_walls(&pos) {
!walls.contains(dir) && maze.get_tile(&neighbor).is_some()
!walls.contains(&dir) && maze.get(&neighbor).is_some()
} else {
false
}
@@ -107,7 +107,7 @@ fn maze_boundaries() {
let pos = Hex::new(q, r);
if q.abs() + r.abs() <= radius {
assert!(
maze.get_tile(&pos).is_some(),
maze.get(&pos).is_some(),
"Expected tile at {:?} to exist",
pos
);

View File

@@ -10,12 +10,12 @@ fn generator_type(
#[case] start_pos: Option<Hex>,
#[case] seed: Option<u64>,
) {
let mut maze = HexMaze::new();
let mut maze = Maze::new();
for q in -3..=3 {
for r in -3..=3 {
let hex = Hex::new(q, r);
if hex.length() <= 3 {
maze.add_tile(hex);
maze.insert(hex);
}
}
}
@@ -36,7 +36,7 @@ fn generator_type(
for dir in EdgeDirection::ALL_DIRECTIONS {
let neighbor = current + dir;
if let Some(walls) = maze.get_walls(&current) {
if !walls.contains(dir) && maze.get_tile(&neighbor).is_some() {
if !walls.contains(&dir) && maze.get(&neighbor).is_some() {
to_visit.push(neighbor);
}
}
@@ -57,7 +57,7 @@ fn generator_type(
#[test]
fn test_empty_maze() {
let mut maze = HexMaze::new();
let mut maze = Maze::new();
GeneratorType::RecursiveBacktracking.generate(&mut maze, None, None);
assert!(
maze.is_empty(),

View File

@@ -2,28 +2,28 @@ use hexlab::prelude::*;
#[test]
fn hex_maze_creation_and_basic_operations() {
let mut maze = HexMaze::new();
let mut maze = Maze::new();
assert!(maze.is_empty());
let center = Hex::ZERO;
maze.add_tile(center);
maze.insert(center);
assert_eq!(maze.len(), 1);
assert!(!maze.is_empty());
let tile = maze.get_tile(&center);
let tile = maze.get(&center);
assert!(tile.is_some());
assert_eq!(tile.unwrap().pos(), center);
}
#[test]
fn hex_maze_wall_operations() {
let mut maze = HexMaze::new();
let mut maze = Maze::new();
let center = Hex::ZERO;
maze.add_tile(center);
maze.insert(center);
// Add walls
for direction in EdgeDirection::ALL_DIRECTIONS {
maze.add_wall(center, direction);
let _ = maze.add_tile_wall(&center, direction);
}
let walls = maze.get_walls(&center).unwrap();
@@ -31,7 +31,7 @@ fn hex_maze_wall_operations() {
// Remove walls
for direction in EdgeDirection::ALL_DIRECTIONS {
maze.remove_tile_wall(&center, direction);
let _ = maze.remove_tile_wall(&center, direction);
}
let walls = maze.get_walls(&center).unwrap();
@@ -40,29 +40,29 @@ fn hex_maze_wall_operations() {
#[test]
fn hex_maze_multiple_tiles() {
let mut maze = HexMaze::new();
let mut maze = Maze::new();
let tiles = [Hex::ZERO, Hex::new(1, -1), Hex::new(0, 1), Hex::new(-1, 1)];
for &tile in &tiles {
maze.add_tile(tile);
maze.insert(tile);
}
assert_eq!(maze.len(), tiles.len());
for &tile in &tiles {
assert!(maze.get_tile(&tile).is_some());
assert!(maze.get(&tile).is_some());
}
}
#[test]
fn hex_maze_edge_cases() {
let mut maze = HexMaze::new();
let mut maze = Maze::new();
let non_existent = Hex::new(10, 10);
// Operations on non-existent tiles should not panic
maze.add_wall(non_existent, EdgeDirection::FLAT_NORTH);
maze.remove_tile_wall(&non_existent, EdgeDirection::FLAT_NORTH);
let _ = maze.add_tile_wall(&non_existent, EdgeDirection::FLAT_NORTH);
let _ = maze.remove_tile_wall(&non_existent, EdgeDirection::FLAT_NORTH);
assert!(maze.get_tile(&non_existent).is_none());
assert!(maze.get(&non_existent).is_none());
assert!(maze.get_walls(&non_existent).is_none());
}