Regularized unit testing framework.

This commit is contained in:
2021-01-12 15:49:05 -05:00
parent 25b9b4cb5d
commit 6bf1476e5e
14 changed files with 174 additions and 167 deletions

View File

@@ -1,6 +1,5 @@
#include <cmath>
#include <algorithm>
#include <cassert>
#include "luastack.hpp"
#include "planemap.hpp"
#include "util.hpp"
@@ -200,7 +199,7 @@ PlaneMap::EltVec PlaneMap::scan_radius(const std::string &plane, double x, doubl
return result;
}
LuaDefine(cunittests_planemap, "c") {
LuaDefine(unittests_planemap, "c") {
double SC = CELL_SCALE;
double E = CELL_SCALE * 0.4;
int LO = -CELL_LIMIT;
@@ -210,110 +209,110 @@ LuaDefine(cunittests_planemap, "c") {
PlaneMap::EltVec elts;
// Simple test.
assert(rect_cell_range(-7*SC, -15*SC, 87*SC, 21*SC).equal(-7, -15, 87, 21));
LuaAssert(L, rect_cell_range(-7*SC, -15*SC, 87*SC, 21*SC).equal(-7, -15, 87, 21));
// Adding an epsilon doesn't change result, if epsilon is less than half of cell scale.
assert(rect_cell_range(-7*SC+E, -15*SC+E, 87*SC-E, 21*SC-E).equal(-7, -15, 87, 21));
LuaAssert(L, rect_cell_range(-7*SC+E, -15*SC+E, 87*SC-E, 21*SC-E).equal(-7, -15, 87, 21));
// Rectangle that crosses the high end of the range.
assert(rect_cell_range((HI-7)*SC, (HI-5)*SC, (HI+3)*SC, (HI+6)*SC).equal(HI-7, HI-5, HI, HI));
LuaAssert(L, rect_cell_range((HI-7)*SC, (HI-5)*SC, (HI+3)*SC, (HI+6)*SC).equal(HI-7, HI-5, HI, HI));
// Rectangle that exceeds the high end of the range.
assert(rect_cell_range((HI+7)*SC, (HI+5)*SC, (HI+15)*SC, (HI+12)*SC).equal(HI+1, HI+1, HI, HI));
LuaAssert(L, rect_cell_range((HI+7)*SC, (HI+5)*SC, (HI+15)*SC, (HI+12)*SC).equal(HI+1, HI+1, HI, HI));
// Rectangle that crosses the low end of the range.
assert(rect_cell_range((LO-7)*SC, (LO-5)*SC, (LO+3)*SC, (LO+4)*SC).equal(LO, LO, LO+3, LO+4));
LuaAssert(L, rect_cell_range((LO-7)*SC, (LO-5)*SC, (LO+3)*SC, (LO+4)*SC).equal(LO, LO, LO+3, LO+4));
// Rectangle that exceeds the low end of the range.
assert(rect_cell_range((LO-15)*SC, (LO-17)*SC, (LO-7)*SC, (LO-5)*SC).equal(LO, LO, LO-1, LO-1));
LuaAssert(L, rect_cell_range((LO-15)*SC, (LO-17)*SC, (LO-7)*SC, (LO-5)*SC).equal(LO, LO, LO-1, LO-1));
// Simple test.
assert(point_cell_id(-7*SC, 15*SC) == cell_id(-7, 15));
LuaAssert(L, point_cell_id(-7*SC, 15*SC) == cell_id(-7, 15));
// Adding epsilon doesn't change the result if less than half cell scale.
assert(point_cell_id(-7*SC+E, 15*SC+E) == cell_id(-7, 15));
LuaAssert(L, point_cell_id(-7*SC+E, 15*SC+E) == cell_id(-7, 15));
// Right at the top edge of the range.
assert(point_cell_id(HI*SC, HI*SC) == cell_id(HI, HI));
LuaAssert(L, point_cell_id(HI*SC, HI*SC) == cell_id(HI, HI));
// Right at the bottom edge of the range.
assert(point_cell_id(LO*SC, LO*SC) == cell_id(LO, LO));
LuaAssert(L, point_cell_id(LO*SC, LO*SC) == cell_id(LO, LO));
// Beyond various edges.
assert(point_cell_id((LO-1)*SC, 0) == CELL_INVALID);
assert(point_cell_id((HI+1)*SC, 0) == CELL_INVALID);
assert(point_cell_id(0, (LO-1)*SC) == CELL_INVALID);
assert(point_cell_id(0, (HI+1)*SC) == CELL_INVALID);
LuaAssert(L, point_cell_id((LO-1)*SC, 0) == CELL_INVALID);
LuaAssert(L, point_cell_id((HI+1)*SC, 0) == CELL_INVALID);
LuaAssert(L, point_cell_id(0, (LO-1)*SC) == CELL_INVALID);
LuaAssert(L, point_cell_id(0, (HI+1)*SC) == CELL_INVALID);
// Test using the insert function.
pm.clear();
assert(pm.total_cells() == 0);
LuaAssert(L, pm.total_cells() == 0);
pm.insert("foo", 12345, &pia);
assert(pm.total_cells() == 1);
LuaAssert(L, pm.total_cells() == 1);
pm.insert("foo", 12345, &pib);
assert(pm.total_cells() == 1);
LuaAssert(L, pm.total_cells() == 1);
elts = pm.get_cell("foo", 12345);
assert(elts.size() == 2);
assert(elts[0] == &pia);
assert(elts[1] == &pib);
LuaAssert(L, elts.size() == 2);
LuaAssert(L, elts[0] == &pia);
LuaAssert(L, elts[1] == &pib);
// Test the remove function.
pm.remove("foo", 12345, &pia);
assert(pm.total_cells() == 1);
LuaAssert(L, pm.total_cells() == 1);
elts = pm.get_cell("foo", 12345);
assert(elts.size() == 1);
assert(elts[0] == &pib);
LuaAssert(L, elts.size() == 1);
LuaAssert(L, elts[0] == &pib);
pm.remove("foo", 12345, &pib);
assert(pm.total_cells() == 0);
LuaAssert(L, pm.total_cells() == 0);
// Test the insert function on the nowhere plane.
pm.clear();
pm.insert("nowhere", 12345, &pia);
pm.insert("nowhere", 12345, &pib);
assert(pm.total_cells() == 0);
LuaAssert(L, pm.total_cells() == 0);
// Test the insert function on an invalid cell.
pm.clear();
pm.insert("foo", CELL_INVALID, &pia);
pm.insert("foo", CELL_INVALID, &pib);
assert(pm.total_cells() == 0);
LuaAssert(L, pm.total_cells() == 0);
// Try moving a plane item around without it being connected to a grid.
pia.set_pos("foo", 3, 4, 5);
assert(pia.plane() == "foo");
assert(pia.x() == 3.0);
assert(pia.y() == 4.0);
assert(pia.z() == 5.0);
LuaAssert(L, pia.plane() == "foo");
LuaAssert(L, pia.x() == 3.0);
LuaAssert(L, pia.y() == 4.0);
LuaAssert(L, pia.z() == 5.0);
// Attach pia to the grid. This should record it.
pm.clear();
pm.track(&pia);
elts = pm.get_cell("foo", point_cell_id(3.0, 4.0));
assert(elts.size() == 1);
assert(elts[0] == &pia);
LuaAssert(L, elts.size() == 1);
LuaAssert(L, elts[0] == &pia);
// Unattach pia from the grid. This should unrecord it.
pia.untrack();
assert(pm.total_cells() == 0);
LuaAssert(L, pm.total_cells() == 0);
// Reattach pia to the grid, then move it.
pm.track(&pia);
assert(pm.total_cells() == 1);
LuaAssert(L, pm.total_cells() == 1);
pia.set_pos("bar", 1000.0, 1000.0, 0.0);
assert(pm.total_cells() == 1);
LuaAssert(L, pm.total_cells() == 1);
elts = pm.get_cell("bar", point_cell_id(1000.0, 1000.0));
assert(elts.size() == 1);
assert(elts[0] == &pia);
LuaAssert(L, elts.size() == 1);
LuaAssert(L, elts[0] == &pia);
// Insert the four elements, then test the scan function.
pm.track(&pib);
pib.set_pos("bar", 1100.0, 1000.0, 0.0);
elts = pm.scan_radius("bar", 1000.0, 1000.0, 1.0);
assert(elts.size() == 1);
LuaAssert(L, elts.size() == 1);
elts = pm.scan_radius("bar", 1000.0, 1000.0, 99.9);
assert(elts.size() == 1);
LuaAssert(L, elts.size() == 1);
elts = pm.scan_radius("bar", 1000.0, 1000.0, 100.0);
assert(elts.size() == 2);
LuaAssert(L, elts.size() == 2);
return 0;
}