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 "idalloc.hpp"
#include <iostream>
#include <cassert>
LuaDefineType(IdGlobalPool);
LuaDefineType(IdPlayerPool);
@@ -136,113 +135,113 @@ void IdPlayerPool::prepare_thread(lua_State *L) {
lua_setnextid(L, get_batch());
}
LuaDefine(cunittests_idalloc, "c") {
LuaDefine(unittests_idalloc, "c") {
IdGlobalPool gp;
IdPlayerPool pp(&gp);
// Synchronous pools produce IDs starting at 0x001E000000000000
gp.init_synch(3);
assert(gp.get_one() == 0x001E000000000000);
assert(gp.get_one() == 0x001E000000000001);
assert(gp.get_one() == 0x001E000000000002);
LuaAssert(L, gp.get_one() == 0x001E000000000000);
LuaAssert(L, gp.get_one() == 0x001E000000000001);
LuaAssert(L, gp.get_one() == 0x001E000000000002);
// Master pools produce IDs starting at 0x0010000000000000
gp.init_master(3);
assert(gp.get_one() == 0x0010000000000000);
assert(gp.get_one() == 0x0010000000000001);
assert(gp.get_one() == 0x0010000000000002);
LuaAssert(L, gp.get_one() == 0x0010000000000000);
LuaAssert(L, gp.get_one() == 0x0010000000000001);
LuaAssert(L, gp.get_one() == 0x0010000000000002);
// Synchronous pools produce only null batches.
gp.init_synch(3);
assert(gp.get_batch() == 0);
assert(gp.get_batch() == 0);
LuaAssert(L, gp.get_batch() == 0);
LuaAssert(L, gp.get_batch() == 0);
gp.salvage(nthbatch(5));
assert(gp.get_batch() == 0);
LuaAssert(L, gp.get_batch() == 0);
// Simple fetch batches with a few salvages.
gp.init_master(3);
assert(gp.get_batch() == nthbatch(0));
assert(gp.get_batch() == nthbatch(1));
assert(gp.get_batch() == nthbatch(2));
LuaAssert(L, gp.get_batch() == nthbatch(0));
LuaAssert(L, gp.get_batch() == nthbatch(1));
LuaAssert(L, gp.get_batch() == nthbatch(2));
gp.salvage(nthbatch(182));
gp.salvage(nthbatch(183));
assert(gp.get_batch() == nthbatch(183));
assert(gp.get_batch() == nthbatch(182));
assert(gp.get_batch() == nthbatch(3));
LuaAssert(L, gp.get_batch() == nthbatch(183));
LuaAssert(L, gp.get_batch() == nthbatch(182));
LuaAssert(L, gp.get_batch() == nthbatch(3));
// Salvage of a zero-batch does nothing.
gp.init_master(3);
assert(gp.get_batch() == nthbatch(0));
assert(gp.get_batch() == nthbatch(1));
LuaAssert(L, gp.get_batch() == nthbatch(0));
LuaAssert(L, gp.get_batch() == nthbatch(1));
gp.salvage(0);
assert(gp.get_batch() == nthbatch(2));
LuaAssert(L, gp.get_batch() == nthbatch(2));
// Salvage of a partial batch.
gp.init_master(3);
assert(gp.get_batch() == nthbatch(0));
assert(gp.get_batch() == nthbatch(1));
LuaAssert(L, gp.get_batch() == nthbatch(0));
LuaAssert(L, gp.get_batch() == nthbatch(1));
gp.salvage(nthbatch(142) + 10);
assert(gp.get_batch() == nthbatch(142) + 10);
assert(gp.get_batch() == nthbatch(2));
LuaAssert(L, gp.get_batch() == nthbatch(142) + 10);
LuaAssert(L, gp.get_batch() == nthbatch(2));
// Salvage of a half-empty batch does nothing.
gp.init_master(3);
assert(gp.get_batch() == nthbatch(0));
assert(gp.get_batch() == nthbatch(1));
LuaAssert(L, gp.get_batch() == nthbatch(0));
LuaAssert(L, gp.get_batch() == nthbatch(1));
gp.salvage(nthbatch(142) + 145);
assert(gp.get_batch() == nthbatch(2));
LuaAssert(L, gp.get_batch() == nthbatch(2));
// In the synchronous model, refill should do nothing.
pp.purge();
gp.init_synch(3);
pp.refill();
assert(pp.size() == 0);
assert(pp.get_batch() == 0);
assert(pp.size() == 0);
assert(pp.get_batch() == 0);
LuaAssert(L, pp.size() == 0);
LuaAssert(L, pp.get_batch() == 0);
LuaAssert(L, pp.size() == 0);
LuaAssert(L, pp.get_batch() == 0);
// Test refill from master.
pp.purge();
gp.init_master(3);
pp.refill();
assert(ranges_equal(pp.ranges_, nthbatch(0), nthbatch(1), nthbatch(2)));
LuaAssert(L, ranges_equal(pp.ranges_, nthbatch(0), nthbatch(1), nthbatch(2)));
// Now test that get_batch keeps the pool filled from master.
assert(pp.get_batch() == nthbatch(0));
assert(ranges_equal(pp.ranges_, nthbatch(1), nthbatch(2), nthbatch(3)));
LuaAssert(L, pp.get_batch() == nthbatch(0));
LuaAssert(L, ranges_equal(pp.ranges_, nthbatch(1), nthbatch(2), nthbatch(3)));
// Test unqueueing the batches.
assert(gp.get_batch() == nthbatch(4));
assert(gp.get_batch() == nthbatch(5));
LuaAssert(L, gp.get_batch() == nthbatch(4));
LuaAssert(L, gp.get_batch() == nthbatch(5));
pp.unqueue();
assert(gp.get_batch() == nthbatch(3));
assert(gp.get_batch() == nthbatch(2));
assert(gp.get_batch() == nthbatch(1));
assert(gp.get_batch() == nthbatch(6));
LuaAssert(L, gp.get_batch() == nthbatch(3));
LuaAssert(L, gp.get_batch() == nthbatch(2));
LuaAssert(L, gp.get_batch() == nthbatch(1));
LuaAssert(L, gp.get_batch() == nthbatch(6));
// Try preparing a thread and salvaging a thread.
pp.purge();
gp.init_master(3);
lua_setnextid(L, 0);
pp.prepare_thread(L);
assert(lua_getnextid(L) == nthbatch(0));
LuaAssert(L, lua_getnextid(L) == nthbatch(0));
lua_setnextid(L, 0);
pp.prepare_thread(L);
assert(lua_getnextid(L) == nthbatch(1));
LuaAssert(L, lua_getnextid(L) == nthbatch(1));
// Try salvaging the pool from the thread.
pp.salvage_thread(L);
assert(lua_getnextid(L) == 0);
assert(gp.get_batch() == nthbatch(1));
LuaAssert(L, lua_getnextid(L) == 0);
LuaAssert(L, gp.get_batch() == nthbatch(1));
// Allocate IDs from inside a thread.
lua_setnextid(L, 0xFD);
gp.init_master(3);
assert(gp.alloc_id_for_thread(L) == 0xFD);
assert(gp.alloc_id_for_thread(L) == 0xFE);
assert(gp.alloc_id_for_thread(L) == 0xFF);
assert(gp.alloc_id_for_thread(L) == 0x0010000000000000);
assert(lua_getnextid(L) == 0);
LuaAssert(L, gp.alloc_id_for_thread(L) == 0xFD);
LuaAssert(L, gp.alloc_id_for_thread(L) == 0xFE);
LuaAssert(L, gp.alloc_id_for_thread(L) == 0xFF);
LuaAssert(L, gp.alloc_id_for_thread(L) == 0x0010000000000000);
LuaAssert(L, lua_getnextid(L) == 0);
return 0;
}

View File

@@ -124,7 +124,7 @@ class IdPlayerPool {
private:
IdGlobalPool *global_;
std::deque<int64_t> ranges_;
friend int cunittests_idalloc(lua_State *L);
friend int unittests_idalloc(lua_State *L);
public:
// Construct a player pool.

View File

@@ -1,7 +1,7 @@
/////////////////////////////////////////////////////////
//
//
// LuaStack
// LUASTACK
//
// Class LuaStack lets you create "lua local variables." These are
// variables that seem to store lua values. Class LuaStack also provides
@@ -117,14 +117,11 @@
// Like the other operations, they are strict.
//
//
/////////////////////////////////////////////////////////
// LUADEFINE
//
//
// LuaDefine
//
// LuaDefine is a macro that helps you define lua functions, but
// it also puts the function into a global function registry.
// You use it like so:
// LuaDefine is a macro that defines a C function which is
// exposed to lua. It creates a global registry of functions
// created with LuaDefine. You use it like so:
//
// LuaDefine(function_name, "modebits") {
// ...
@@ -146,7 +143,6 @@
//
// c - create a class, and put a function into it.
// f - create a global function not inside a class.
// a - autoexec - run this function automatically on model creation.
//
//
/////////////////////////////////////////////////////////
@@ -531,5 +527,7 @@ public:
LuaFunctionReg regt_##name("t", #name, LuaTypeTagValue<name>)
#define LuaStringify(x) #x
#define LuaAssert(L, x) if (!(x)) { luaL_error((L), "Assert failed: %s (file %s line %d)", LuaStringify(x), __FILE__, __LINE__); }
#endif // LUASTACK_HPP

View File

@@ -183,7 +183,7 @@ static int pmain(lua_State *L)
LuaStack::register_all_userdata(L);
// Initialize the builtins, then copy a snapshot of the
// builtins to the registry. This will allow us to restore
// builtins to the registry. The snapshot will allow us to restore
// the builtins during source_rebuild operations.
source_install_and_snapshot_builtins(L);
@@ -193,6 +193,9 @@ static int pmain(lua_State *L)
// Rebuild the global environment.
source_rebuild(L);
// Run unit tests.
source_run_unittests(L);
dotty(L);
return 0;
}

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;
}

View File

@@ -118,7 +118,7 @@ public:
private:
// unit testing stuff.
friend int cunittests_planemap(lua_State *L);
friend int unittests_planemap(lua_State *L);
EltVec get_cell(const std::string &plane, int64_t cell) const;
int total_cells() const;
void clear() { planes_.clear(); }

View File

@@ -2,6 +2,7 @@
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <sstream>
#include <fstream>
@@ -320,3 +321,38 @@ LuaDefine(source_rebuild, "c") {
return LS.result();
}
LuaDefine(source_run_unittests, "c") {
LuaVar unittests, name, func, err;
LuaRet rescode;
LuaStack LS(L, unittests, name, func, err, rescode);
LS.getfield(unittests, LuaGlobals, "unittests");
// Sort the unit test names.
std::set<std::string> names;
LS.set(name, LuaNil);
while (LS.next(unittests, name, func) != 0) {
if (LS.isfunction(func) && LS.isstring(name)) {
names.insert(LS.ckstring(name));
}
}
// Run the functions in order
bool any = false;
for (const std::string &name : names) {
std::cerr << "Running unittests." << name << std::endl;
LS.rawget(func, unittests, name);
lua_pushvalue(L, func.index());
if (traceback_pcall(L, 0, 0) != 0) {
lua_replace(L, err.index());
std::cerr << LS.ckstring(err);
any = true;
}
}
// Return 1 if any errors.
LS.set(rescode, any ? 1 : 0);
return LS.result();
}

View File

@@ -98,9 +98,22 @@
// So therefore, we have to provide a separate "safe" space for global data
// structures. That is provided elsewhere, in the module "globaldb".
//
//
// UNITTESTS
//
// We reserve the lua class name 'unittests' for storing unit tests. Any
// function placed into this class is considered a unit test. We don't
// separate unit tests from the rest of the code - they're compiled right
// into the main binary.
//
// Unit tests can be either lua functions, or Lua-registered C functions.
// Unit tests are executed by calling 'source_run_unittests'.
//
// Each unit test is run in a protected 'pcall' environment. Any errors
// are printed out to console. (At least for now).
//
////////////////////////////////////////////////////////////
#ifndef SOURCE_HPP
#define SOURCE_HPP
@@ -138,9 +151,19 @@ int source_update(lua_State *L);
//
// Rebuild the lua environment: clear it out, then reinstall all the
// functions that should be there. See above for more information.
// If an error exists in any of the source files, or when loading any
// of the closures, the error is (currently) printed. We'll come up
// with better error handling later.
//
int source_rebuild(lua_State *L);
// source_run_unittests
//
// Run all the unit tests. Print any errors to console. Pushes zero
// onto the lua stack if there are no errors, or one if there are.
//
int source_run_unittests(lua_State *L);
#endif // SOURCE_HPP