improve portability of test
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
-- utility to give tables and functions uniform ids for testing
|
-- utility to give tables and functions uniform ids for testing
|
||||||
ids = {}
|
ids = {}
|
||||||
|
|
||||||
|
-- return unique id for the value, independent of object id
|
||||||
function id(obj)
|
function id(obj)
|
||||||
if not obj or type(obj) == 'number' or type(obj) == 'string' or type(obj) == 'boolean' then
|
if not obj or type(obj) == 'number' or type(obj) == 'string' or type(obj) == 'boolean' then
|
||||||
return obj
|
return obj
|
||||||
@@ -12,3 +14,23 @@ function id(obj)
|
|||||||
ids[obj] = type(obj)..'.'..tostring(#ids)
|
ids[obj] = type(obj)..'.'..tostring(#ids)
|
||||||
return ids[obj]
|
return ids[obj]
|
||||||
end
|
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
|
||||||
@@ -1,9 +1,11 @@
|
|||||||
|
package.path = "?.lua;src/test/res/?.lua"
|
||||||
|
require 'ids'
|
||||||
|
|
||||||
t = { 11, 22, 33, you='one', me='two' }
|
t = { 11, 22, 33, you='one', me='two' }
|
||||||
|
|
||||||
print( "---------" )
|
print( "---------" )
|
||||||
for a,b in pairs(t) do
|
for a,b in pairs(t) do
|
||||||
print( a, b )
|
print( id(a), id(b) )
|
||||||
end
|
end
|
||||||
print( "----" )
|
print( "----" )
|
||||||
print( "t[2]", t[2] )
|
print( "t[2]", t[2] )
|
||||||
@@ -15,10 +17,12 @@ print( "me", me )
|
|||||||
print( "fred", fred )
|
print( "fred", fred )
|
||||||
print( "t[me]", t[me] )
|
print( "t[me]", t[me] )
|
||||||
print( "t[fred]", t[fred] )
|
print( "t[fred]", t[fred] )
|
||||||
|
function out(t,...)
|
||||||
|
print(type(t),...)
|
||||||
|
end
|
||||||
-- basic metatable setting
|
-- basic metatable setting
|
||||||
t = { 11, 22, 33, you='one', me='two' }
|
t = { 11, 22, 33, you='one', me='two' }
|
||||||
mt = { __index = print, __newindex = print }
|
mt = { __index = out, __newindex = out }
|
||||||
setmetatable(t,mt)
|
setmetatable(t,mt)
|
||||||
a = t[11]
|
a = t[11]
|
||||||
b = t.one
|
b = t.one
|
||||||
@@ -29,7 +33,7 @@ t[1] = 'pipe'
|
|||||||
print( a, b, c, d )
|
print( a, b, c, d )
|
||||||
print( "---------" )
|
print( "---------" )
|
||||||
for a,b in pairs(t) do
|
for a,b in pairs(t) do
|
||||||
print( a, b )
|
print( id(a), id(b) )
|
||||||
end
|
end
|
||||||
print( "----" )
|
print( "----" )
|
||||||
|
|
||||||
@@ -45,7 +49,7 @@ print( t.me )
|
|||||||
t[5] = 99
|
t[5] = 99
|
||||||
print( "---------" )
|
print( "---------" )
|
||||||
for a,b in pairs(s) do
|
for a,b in pairs(s) do
|
||||||
print( a, b )
|
print( id(a), id(b) )
|
||||||
end
|
end
|
||||||
print( "----" )
|
print( "----" )
|
||||||
|
|
||||||
@@ -65,30 +69,31 @@ function Vector:dot(v)
|
|||||||
end
|
end
|
||||||
|
|
||||||
v1 = Vector:new(3,4)
|
v1 = Vector:new(3,4)
|
||||||
print( "--------" )
|
print( "---------" )
|
||||||
for a,b in pairs(v1) do
|
for a,b in pairs(v1) do
|
||||||
print( a, b )
|
print( id(a), id(b) )
|
||||||
end
|
end
|
||||||
|
|
||||||
print( "----" )
|
print( "----" )
|
||||||
|
|
||||||
v2 = Vector:new(2,1)
|
v2 = Vector:new(2,1)
|
||||||
print( v2:dot(v1) )
|
print( v2:dot(v1) )
|
||||||
|
|
||||||
print( Vector )
|
print( id(Vector) )
|
||||||
|
|
||||||
print( "---------" )
|
print( "=======" )
|
||||||
for a,b in pairs(Vector) do
|
for a,b in spairs(Vector) do
|
||||||
print( a, b )
|
print( id(a), id(b) )
|
||||||
end
|
end
|
||||||
print( "----" )
|
print( "----" )
|
||||||
|
|
||||||
print( v1, v2 )
|
print( id(v1), id(v2) )
|
||||||
|
|
||||||
print( Vector_mt, getmetatable(v1), getmetatable(v2) )
|
print( id(Vector_mt), id(getmetatable(v1)), id(getmetatable(v2)) )
|
||||||
|
|
||||||
print( "---------" )
|
print( "---------" )
|
||||||
for a,b in pairs(Vector_mt) do
|
for a,b in pairs(Vector_mt) do
|
||||||
print( a, b )
|
print( id(a), id(b) )
|
||||||
end
|
end
|
||||||
print( "----" )
|
print( "----" )
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user