96 lines
2.5 KiB
Lua
96 lines
2.5 KiB
Lua
|
|
local ut = {}
|
|
|
|
local function nthbatch(i)
|
|
return 0x0001000000000000 + 256 * (i - 1)
|
|
end
|
|
|
|
local function equalbatches(q, batches)
|
|
if queue.size(q) ~= #batches then
|
|
return false
|
|
end
|
|
for i = 1, #batches do
|
|
if queue.nth(q, i) ~= nthbatch(batches[i]) then
|
|
return false
|
|
end
|
|
end
|
|
return true
|
|
end
|
|
|
|
function ut.idalloc_synch()
|
|
local allocator = {}
|
|
idalloc.initsynch(allocator, 10)
|
|
assert(allocator.id_salvaged == nil)
|
|
assert(allocator.id_nextbatch == nil)
|
|
assert(allocator.id_nextid == 0x001E000000000000)
|
|
assert(allocator.id_queuefill == 10)
|
|
|
|
local q1 = queue.create()
|
|
idalloc.refill(allocator, q1)
|
|
assert(queue.size(q1) == 0)
|
|
end
|
|
|
|
function ut.idalloc_master()
|
|
local allocator = {}
|
|
idalloc.initmaster(allocator, 3)
|
|
assert(queue.size(allocator.id_salvaged) == 0)
|
|
assert(allocator.id_nextbatch == nthbatch(1))
|
|
assert(allocator.id_nextid == 0x0010000000000000)
|
|
assert(allocator.id_queuefill == 3)
|
|
|
|
local q1 = queue.create()
|
|
local q2 = queue.create()
|
|
local q3 = queue.create()
|
|
local salv = allocator.id_salvaged
|
|
|
|
idalloc.refill(allocator, q1)
|
|
idalloc.refill(allocator, q2)
|
|
assert(equalbatches(q1, { 1, 2, 3 }))
|
|
assert(equalbatches(q2, { 4, 5, 6 }))
|
|
idalloc.unqueue(allocator, q1)
|
|
assert(equalbatches(salv, { 1, 2, 3 }))
|
|
queue.pop(q2)
|
|
assert(equalbatches(q2, { 5, 6 }))
|
|
idalloc.refill(allocator, q2)
|
|
assert(equalbatches(q2, { 5, 6, 1 }))
|
|
assert(equalbatches(salv, { 2, 3 }))
|
|
idalloc.refill(allocator, q3)
|
|
assert(equalbatches(q3, { 2, 3, 7 }))
|
|
end
|
|
|
|
function ut.idalloc_thread()
|
|
local allocator = {}
|
|
idalloc.initmaster(allocator, 3)
|
|
local salv = allocator.id_salvaged
|
|
local q = queue.create()
|
|
idalloc.refill(allocator, q)
|
|
|
|
local id = 0
|
|
local function useids()
|
|
for i = 1,10000 do
|
|
id = idalloc.allocid(allocator)
|
|
coroutine.yield()
|
|
end
|
|
end
|
|
|
|
local co = coroutine.create(useids)
|
|
coroutine.resume(co)
|
|
assert(id == 0x0010000000000000)
|
|
coroutine.resume(co)
|
|
assert(id == 0x0010000000000001)
|
|
idalloc.preparethread(q, co)
|
|
coroutine.resume(co)
|
|
assert(id == nthbatch(1) + 0)
|
|
coroutine.resume(co)
|
|
assert(id == nthbatch(1) + 1)
|
|
idalloc.salvagethread(allocator, co)
|
|
coroutine.resume(co)
|
|
assert(id == 0x0010000000000002)
|
|
|
|
assert(queue.size(salv) == 1)
|
|
assert(queue.nth(salv, 1) == nthbatch(1) + 2)
|
|
end
|
|
|
|
rununittests(ut)
|
|
|