Regularized unit testing framework.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user