From 6bf1476e5e5d247490d28982f6d9c63537547bb5 Mon Sep 17 00:00:00 2001 From: Josh Yelon Date: Tue, 12 Jan 2021 15:49:05 -0500 Subject: [PATCH] Regularized unit testing framework. --- luprex/syscpp/idalloc.cpp | 97 +++++++++++++++++------------------ luprex/syscpp/idalloc.hpp | 2 +- luprex/syscpp/luastack.hpp | 16 +++--- luprex/syscpp/main.cpp | 5 +- luprex/syscpp/planemap.cpp | 83 +++++++++++++++--------------- luprex/syscpp/planemap.hpp | 2 +- luprex/syscpp/source.cpp | 36 +++++++++++++ luprex/syscpp/source.hpp | 25 ++++++++- luprex/syslua/control.lst | 3 -- luprex/syslua/ut-cellgrid.lua | 7 --- luprex/syslua/ut-globaldb.lua | 6 +-- luprex/syslua/ut-misc.lua | 12 ----- luprex/syslua/ut-table.lua | 27 ++++------ luprex/syslua/utils.lua | 20 -------- 14 files changed, 174 insertions(+), 167 deletions(-) delete mode 100644 luprex/syslua/ut-cellgrid.lua delete mode 100644 luprex/syslua/ut-misc.lua delete mode 100644 luprex/syslua/utils.lua diff --git a/luprex/syscpp/idalloc.cpp b/luprex/syscpp/idalloc.cpp index 6022d197..22dac065 100644 --- a/luprex/syscpp/idalloc.cpp +++ b/luprex/syscpp/idalloc.cpp @@ -1,6 +1,5 @@ #include "idalloc.hpp" #include -#include 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; } diff --git a/luprex/syscpp/idalloc.hpp b/luprex/syscpp/idalloc.hpp index dbc5386b..c50a5ecc 100644 --- a/luprex/syscpp/idalloc.hpp +++ b/luprex/syscpp/idalloc.hpp @@ -124,7 +124,7 @@ class IdPlayerPool { private: IdGlobalPool *global_; std::deque ranges_; - friend int cunittests_idalloc(lua_State *L); + friend int unittests_idalloc(lua_State *L); public: // Construct a player pool. diff --git a/luprex/syscpp/luastack.hpp b/luprex/syscpp/luastack.hpp index 432b9022..1cb92776 100644 --- a/luprex/syscpp/luastack.hpp +++ b/luprex/syscpp/luastack.hpp @@ -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) +#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 diff --git a/luprex/syscpp/main.cpp b/luprex/syscpp/main.cpp index f3723409..1ff058d0 100644 --- a/luprex/syscpp/main.cpp +++ b/luprex/syscpp/main.cpp @@ -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; } diff --git a/luprex/syscpp/planemap.cpp b/luprex/syscpp/planemap.cpp index de6e48b2..54259b46 100644 --- a/luprex/syscpp/planemap.cpp +++ b/luprex/syscpp/planemap.cpp @@ -1,6 +1,5 @@ #include #include -#include #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; } diff --git a/luprex/syscpp/planemap.hpp b/luprex/syscpp/planemap.hpp index 1e40794a..9970e70b 100644 --- a/luprex/syscpp/planemap.hpp +++ b/luprex/syscpp/planemap.hpp @@ -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(); } diff --git a/luprex/syscpp/source.cpp b/luprex/syscpp/source.cpp index 4ac8b284..167cebc0 100644 --- a/luprex/syscpp/source.cpp +++ b/luprex/syscpp/source.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -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 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(); +} + diff --git a/luprex/syscpp/source.hpp b/luprex/syscpp/source.hpp index 337964ce..44ef7025 100644 --- a/luprex/syscpp/source.hpp +++ b/luprex/syscpp/source.hpp @@ -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 diff --git a/luprex/syslua/control.lst b/luprex/syslua/control.lst index 03b13f84..ac08a981 100644 --- a/luprex/syslua/control.lst +++ b/luprex/syslua/control.lst @@ -3,10 +3,7 @@ # in the order that they're supposed to be loaded. # -utils.lua inspect.lua -ut-misc.lua ut-table.lua ut-globaldb.lua -ut-cellgrid.lua diff --git a/luprex/syslua/ut-cellgrid.lua b/luprex/syslua/ut-cellgrid.lua deleted file mode 100644 index 259f90a1..00000000 --- a/luprex/syslua/ut-cellgrid.lua +++ /dev/null @@ -1,7 +0,0 @@ -local ut = {} - -function ut.cellgrid() -end - - -rununittests(ut) diff --git a/luprex/syslua/ut-globaldb.lua b/luprex/syslua/ut-globaldb.lua index 93ad0daa..9f476db6 100644 --- a/luprex/syslua/ut-globaldb.lua +++ b/luprex/syslua/ut-globaldb.lua @@ -1,7 +1,6 @@ +makeclass("unittests") -local ut = {} - -function ut.globaldb() +function unittests.globaldb() globaldb.enable() local g1a = global("unittest-g1") local g2a = global("unittest-g2") @@ -13,4 +12,3 @@ function ut.globaldb() assert(g2a.__global == "unittest-g2") end -rununittests(ut) diff --git a/luprex/syslua/ut-misc.lua b/luprex/syslua/ut-misc.lua deleted file mode 100644 index 6fa49e78..00000000 --- a/luprex/syslua/ut-misc.lua +++ /dev/null @@ -1,12 +0,0 @@ - -local ut = {} - -function ut.idalloc() - cunittests.idalloc() -end - -function ut.planemap() - cunittests.planemap() -end - -rununittests(ut) diff --git a/luprex/syslua/ut-table.lua b/luprex/syslua/ut-table.lua index 93145dc1..1ea3ec4f 100644 --- a/luprex/syslua/ut-table.lua +++ b/luprex/syslua/ut-table.lua @@ -1,27 +1,25 @@ -local ut = {} +makeclass("unittests") -function ut.table_count() +function unittests.tables() + -- check table.count assert(table.count({}) == 0) assert(table.count({a=1,b=2}) == 2) assert(table.count({[2]=5,[5]=3}) == 2) -end -function ut.table_clear() + -- check table.clear local t = { a = 1, b = 2 } table.clear(t) assert(t.a == nil) assert(t.b == nil) assert(table.count(t) == 0) -end -function ut.table_empty() + -- check table.empty assert(table.empty({}) == true) assert(table.empty({1}) == false) assert(table.empty({a=1}) == false) -end -function ut.table_equal() + -- check table.equal assert(table.equal({},{})) assert(not table.equal({}, {1})) assert(not table.equal({1}, {})) @@ -31,9 +29,8 @@ function ut.table_equal() assert(not table.equal({1,2,3}, {1,2})) assert(table.equal({a=1,b=2},{a=1,b=2})) assert(not table.equal({a=1,b=3},{a=1,b=2})) -end -function ut.table_push() + -- check table.push t = {} table.push(t, 1) assert(table.equal(t, {1})) @@ -41,9 +38,8 @@ function ut.table_push() assert(table.equal(t, {1,2})) table.push(t, 3) assert(table.equal(t, {1,2,3})) -end - -function ut.table_findremove() + + -- check table.findremove t = {1,2,3,4,5,1,2,3,4,5} table.findremove(t, 2) assert(table.equal(t, {1,3,4,5,1,3,4,5})) @@ -53,7 +49,7 @@ function ut.table_findremove() assert(table.equal(t, {3,4,3,4})) end -function ut.queues() +function unittests.queues() local q = queue.create() assert(q.head == 1000000) assert(q.tail == 1000000) @@ -78,6 +74,3 @@ function ut.queues() assert(q.tail == 1000002) assert(queue.size(q) == 0) end - -rununittests(ut) - diff --git a/luprex/syslua/utils.lua b/luprex/syslua/utils.lua deleted file mode 100644 index 8ca5c813..00000000 --- a/luprex/syslua/utils.lua +++ /dev/null @@ -1,20 +0,0 @@ - - -function rununittests(tab) - for k, v in pairs(tab) do - v() - end -end - -function crash2() - local tab = nil - tab[3] = 1 -end - -function crash1() - crash2() -end - -function doyield() - coroutine.yield() -end