improve portability of test
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
-- utility to give tables and functions uniform ids for testing
|
||||
ids = {}
|
||||
|
||||
-- return unique id for the value, independent of object id
|
||||
function id(obj)
|
||||
if not obj or type(obj) == 'number' or type(obj) == 'string' or type(obj) == 'boolean' then
|
||||
return obj
|
||||
@@ -12,3 +14,23 @@ function id(obj)
|
||||
ids[obj] = type(obj)..'.'..tostring(#ids)
|
||||
return ids[obj]
|
||||
end
|
||||
|
||||
-- return key-value pairs sorted by keys
|
||||
function spairs(unsorted)
|
||||
local keys = {}
|
||||
for k,v in pairs(unsorted) do
|
||||
table.insert(keys,k)
|
||||
end
|
||||
table.sort(keys)
|
||||
local lookup = {}
|
||||
for i,k in ipairs(keys) do
|
||||
lookup[k] = i;
|
||||
end
|
||||
local iterator = function(tbl,key)
|
||||
local i = lookup[key]
|
||||
local j,k = next(keys,i)
|
||||
if not j then return nil end
|
||||
return k,unsorted[k]
|
||||
end
|
||||
return iterator, table, nil
|
||||
end
|
||||
Reference in New Issue
Block a user