-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap_tests.cpp
More file actions
154 lines (132 loc) · 5.74 KB
/
map_tests.cpp
File metadata and controls
154 lines (132 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <Eigen/Dense>
#include <cmath>
#include <iostream>
#include <stdexcept>
#include "Map.hpp"
namespace {
int tests_run = 0;
int tests_failed = 0;
void expect_true(bool condition, const char* test_name) {
++tests_run;
if (!condition) {
++tests_failed;
std::cerr << "[FAIL] " << test_name << std::endl;
} else {
std::cout << "[PASS] " << test_name << std::endl;
}
}
void expect_near(float actual, float expected, float tolerance, const char* test_name) {
++tests_run;
if (std::abs(actual - expected) > tolerance) {
++tests_failed;
std::cerr << "[FAIL] " << test_name << " (expected " << expected << ", got " << actual << ")" << std::endl;
} else {
std::cout << "[PASS] " << test_name << std::endl;
}
}
void expect_throws(auto func, const char* test_name) {
++tests_run;
try {
func();
++tests_failed;
std::cerr << "[FAIL] " << test_name << " (no exception thrown)" << std::endl;
} catch (...) {
std::cout << "[PASS] " << test_name << std::endl;
}
}
} // namespace
int main() {
// Create a simple 3x3 grid with no walls
Eigen::MatrixXi grid(3, 3);
grid.setZero();
float gridSize = 1.0f;
float mapHeight = 3.0f;
float maxRayDist = 10.0f;
Map map(grid, gridSize, mapHeight);
// Test 1: Basic horizontal raycast
{
Eigen::Vector3f position(0.5f, -0.5f, 1.5f);
Eigen::Vector3f direction(1.0f, 0.0f, 0.0f);
float distance = map.raycast(position, direction, maxRayDist);
expect_true(std::isfinite(distance), "basic horizontal raycast returns finite distance");
expect_true(distance >= 0.0f, "basic horizontal raycast returns non-negative distance");
expect_true(distance <= maxRayDist, "basic horizontal raycast respects max distance");
}
// Test 2: Zero direction vector should throw exception
{
Eigen::Vector3f position(0.5f, -0.5f, 1.5f);
Eigen::Vector3f zero_direction(0.0f, 0.0f, 0.0f);
expect_throws([&]() { map.raycast(position, zero_direction, maxRayDist); },
"zero direction vector throws exception");
}
// Test 3: Vertical raycast (floor collision)
{
Eigen::Vector3f position(0.5f, -0.5f, 1.5f);
Eigen::Vector3f direction(0.0f, 0.0f, -1.0f);
float distance = map.raycast(position, direction, maxRayDist);
expect_near(distance, 1.5f, 0.01f, "vertical raycast hits floor");
}
// Test 4: Vertical raycast (ceiling collision)
{
Eigen::Vector3f position(0.5f, -0.5f, 1.5f);
Eigen::Vector3f direction(0.0f, 0.0f, 1.0f);
float distance = map.raycast(position, direction, maxRayDist);
expect_near(distance, 1.5f, 0.01f, "vertical raycast hits ceiling");
}
// Test 5: Diagonal raycast in xy plane
{
Eigen::Vector3f position(0.5f, -0.5f, 1.5f);
Eigen::Vector3f direction(1.0f, 1.0f, 0.0f);
float distance = map.raycast(position, direction, maxRayDist);
expect_true(std::isfinite(distance), "diagonal xy raycast returns finite distance");
expect_true(distance >= 0.0f, "diagonal xy raycast returns non-negative distance");
}
// Test 6: Raycast with all components
{
Eigen::Vector3f position(0.5f, -0.5f, 1.5f);
Eigen::Vector3f direction(1.0f, 0.5f, 0.2f);
float distance = map.raycast(position, direction, maxRayDist);
expect_true(std::isfinite(distance), "3D raycast returns finite distance");
expect_true(distance >= 0.0f, "3D raycast returns non-negative distance");
expect_true(distance <= maxRayDist, "3D raycast respects max distance");
}
// Test 7: Create a map with walls
Eigen::MatrixXi grid_with_walls(3, 3);
grid_with_walls.setZero();
grid_with_walls(1, 1) = 2; // West wall in center cell
Map map_with_walls(grid_with_walls, gridSize, mapHeight);
{
Eigen::Vector3f position(0.5f, -0.5f, 1.5f);
Eigen::Vector3f direction(1.0f, 0.0f, 0.0f);
float distance = map_with_walls.raycast(position, direction, maxRayDist);
expect_true(std::isfinite(distance), "raycast with walls returns finite distance");
expect_true(distance >= 0.0f, "raycast with walls returns non-negative distance");
}
// Test 8: Negative direction components
{
Eigen::Vector3f position(1.5f, -1.5f, 1.5f);
Eigen::Vector3f direction(-1.0f, 0.0f, 0.0f);
float distance = map.raycast(position, direction, maxRayDist);
expect_true(std::isfinite(distance), "negative x direction returns finite distance");
expect_true(distance >= 0.0f, "negative x direction returns non-negative distance");
}
{
Eigen::Vector3f position(1.5f, -1.5f, 1.5f);
Eigen::Vector3f direction(0.0f, -1.0f, 0.0f);
float distance = map.raycast(position, direction, maxRayDist);
expect_true(std::isfinite(distance), "negative y direction returns finite distance");
expect_true(distance >= 0.0f, "negative y direction returns non-negative distance");
}
// Test 9: Non-normalized direction vector (should be normalized internally)
{
Eigen::Vector3f position(0.5f, -0.5f, 1.5f);
Eigen::Vector3f direction(5.0f, 0.0f, 0.0f); // Not normalized
float distance = map.raycast(position, direction, maxRayDist);
expect_true(std::isfinite(distance), "non-normalized direction returns finite distance");
expect_true(distance >= 0.0f, "non-normalized direction returns non-negative distance");
}
std::cout << "\n=================================\n";
std::cout << "Tests run: " << tests_run << ", failed: " << tests_failed << std::endl;
std::cout << "=================================\n";
return tests_failed == 0 ? 0 : 1;
}