diff options
Diffstat (limited to 'tests/test.lua')
| -rwxr-xr-x | tests/test.lua | 116 |
1 files changed, 85 insertions, 31 deletions
diff --git a/tests/test.lua b/tests/test.lua index cc07ee8..ac415bc 100755 --- a/tests/test.lua +++ b/tests/test.lua | |||
| @@ -1,63 +1,106 @@ | |||
| 1 | #!/usr/bin/env lua | 1 | #!/usr/bin/env lua |
| 2 | 2 | ||
| 3 | local mod = require( "testmod" ) | 3 | local F, ___ |
| 4 | local F | ||
| 5 | do | 4 | do |
| 6 | local type, unpack = type, table.unpack or unpack | 5 | local type, unpack = type, table.unpack or unpack |
| 7 | function F( ... ) | 6 | function F(...) |
| 8 | local args, n = { ... }, select( '#', ... ) | 7 | local args, n = { ... }, select('#', ...) |
| 9 | for i = 1, n do | 8 | for i = 1, n do |
| 10 | local t = type( args[ i ] ) | 9 | local t = type(args[i]) |
| 11 | if t ~= "string" and t ~= "number" and t ~= "boolean" then | 10 | if t ~= "string" and t ~= "number" and t ~= "boolean" then |
| 12 | args[ i ] = t | 11 | args[i] = t |
| 13 | end | 12 | end |
| 14 | end | 13 | end |
| 15 | return unpack( args, 1, n ) | 14 | return unpack(args, 1, n) |
| 15 | end | ||
| 16 | local sep = ("="):rep(70) | ||
| 17 | function ___() | ||
| 18 | print(sep) | ||
| 16 | end | 19 | end |
| 17 | end | 20 | end |
| 18 | 21 | ||
| 22 | print( "testing Lua API ..." ) | ||
| 23 | package.path = "../?.lua;"..package.path | ||
| 24 | require("compat53") | ||
| 25 | ___'' | ||
| 26 | print("math.maxinteger", math.maxinteger+1 > math.maxinteger) | ||
| 27 | print("math.mininteger", math.mininteger-1 < math.mininteger) | ||
| 28 | |||
| 29 | |||
| 30 | ___'' | ||
| 31 | print("math.tointeger", math.tointeger(0)) | ||
| 32 | print("math.tointeger", math.tointeger(math.pi)) | ||
| 33 | print("math.tointeger", math.tointeger("hello")) | ||
| 34 | print("math.tointeger", math.tointeger(math.maxinteger+2.0)) | ||
| 35 | print("math.tointeger", math.tointeger(math.mininteger*2.0)) | ||
| 36 | |||
| 37 | |||
| 38 | ___'' | ||
| 39 | print("math.type", math.type(0)) | ||
| 40 | print("math.type", math.type(math.pi)) | ||
| 41 | print("math.type", math.type("hello")) | ||
| 42 | |||
| 43 | |||
| 44 | ___'' | ||
| 45 | print("math.ult", math.ult(1, 2), math.ult(2, 1)) | ||
| 46 | print("math.ult", math.ult(-1, 2), math.ult(2, -1)) | ||
| 47 | print("math.ult", math.ult(-1, -2), math.ult(-2, -1)) | ||
| 48 | print("math.ult", pcall(math.ult, "x", 2)) | ||
| 49 | print("math.ult", pcall(math.ult, 1, 2.1)) | ||
| 50 | ___'' | ||
| 51 | |||
| 52 | |||
| 19 | 53 | ||
| 20 | print( mod.isinteger( 1 ) ) | 54 | print("testing C API ...") |
| 21 | print( mod.isinteger( 0 ) ) | 55 | local mod = require("testmod") |
| 22 | print( mod.isinteger( 1234567 ) ) | 56 | ___'' |
| 23 | print( mod.isinteger( 12.3 ) ) | 57 | print(mod.isinteger(1)) |
| 24 | print( mod.isinteger( math.huge ) ) | 58 | print(mod.isinteger(0)) |
| 25 | print( mod.isinteger( math.sqrt( -1 ) ) ) | 59 | print(mod.isinteger(1234567)) |
| 60 | print(mod.isinteger(12.3)) | ||
| 61 | print(mod.isinteger(math.huge)) | ||
| 62 | print(mod.isinteger(math.sqrt(-1))) | ||
| 26 | 63 | ||
| 27 | 64 | ||
| 28 | print( mod.rotate( 1, 1, 2, 3, 4, 5, 6 ) ) | 65 | ___'' |
| 29 | print( mod.rotate(-1, 1, 2, 3, 4, 5, 6 ) ) | 66 | print(mod.rotate(1, 1, 2, 3, 4, 5, 6)) |
| 30 | print( mod.rotate( 4, 1, 2, 3, 4, 5, 6 ) ) | 67 | print(mod.rotate(-1, 1, 2, 3, 4, 5, 6)) |
| 31 | print( mod.rotate( -4, 1, 2, 3, 4, 5, 6 ) ) | 68 | print(mod.rotate(4, 1, 2, 3, 4, 5, 6)) |
| 69 | print(mod.rotate(-4, 1, 2, 3, 4, 5, 6)) | ||
| 32 | 70 | ||
| 33 | 71 | ||
| 34 | print( mod.strtonum( "+123" ) ) | 72 | ___'' |
| 35 | print( mod.strtonum( " 123 " ) ) | 73 | print(mod.strtonum("+123")) |
| 36 | print( mod.strtonum( "-1.23" ) ) | 74 | print(mod.strtonum(" 123 ")) |
| 37 | print( mod.strtonum( " 123 abc" ) ) | 75 | print(mod.strtonum("-1.23")) |
| 38 | print( mod.strtonum( "jkl" ) ) | 76 | print(mod.strtonum(" 123 abc")) |
| 77 | print(mod.strtonum("jkl")) | ||
| 39 | 78 | ||
| 40 | 79 | ||
| 80 | ___'' | ||
| 41 | local a, b, c = mod.requiref() | 81 | local a, b, c = mod.requiref() |
| 42 | print( type( a ), type( b ), type( c ), | 82 | print( type(a), type(b), type(c), |
| 43 | a.boolean, b.boolean, c.boolean, | 83 | a.boolean, b.boolean, c.boolean, |
| 44 | type( requiref1 ), type( requiref2 ), type( requiref3 ) ) | 84 | type(requiref1), type(requiref2), type(requiref3)) |
| 45 | 85 | ||
| 86 | ___'' | ||
| 46 | local proxy, backend = {}, {} | 87 | local proxy, backend = {}, {} |
| 47 | setmetatable( proxy, { __index = backend, __newindex = backend } ) | 88 | setmetatable(proxy, { __index = backend, __newindex = backend }) |
| 48 | print( rawget( proxy, 1 ), rawget( backend, 1 ) ) | 89 | print(rawget(proxy, 1), rawget(backend, 1)) |
| 49 | print( mod.getseti( proxy, 1 ) ) | 90 | print(mod.getseti(proxy, 1)) |
| 50 | print( rawget( proxy, 1 ), rawget( backend, 1 ) ) | 91 | print(rawget(proxy, 1), rawget(backend, 1)) |
| 51 | print( mod.getseti( proxy, 1 ) ) | 92 | print(mod.getseti(proxy, 1)) |
| 52 | print( rawget( proxy, 1 ), rawget( backend, 1 ) ) | 93 | print(rawget(proxy, 1), rawget(backend, 1)) |
| 53 | 94 | ||
| 54 | -- tests for Lua 5.1 | 95 | -- tests for Lua 5.1 |
| 96 | ___'' | ||
| 55 | print(mod.tonumber(12)) | 97 | print(mod.tonumber(12)) |
| 56 | print(mod.tonumber("12")) | 98 | print(mod.tonumber("12")) |
| 57 | print(mod.tonumber("0")) | 99 | print(mod.tonumber("0")) |
| 58 | print(mod.tonumber(false)) | 100 | print(mod.tonumber(false)) |
| 59 | print(mod.tonumber("error")) | 101 | print(mod.tonumber("error")) |
| 60 | 102 | ||
| 103 | ___'' | ||
| 61 | print(mod.tointeger(12)) | 104 | print(mod.tointeger(12)) |
| 62 | print(mod.tointeger("12")) | 105 | print(mod.tointeger("12")) |
| 63 | print(mod.tointeger("0")) | 106 | print(mod.tointeger("0")) |
| @@ -67,6 +110,7 @@ print( "bbb" ) | |||
| 67 | print(mod.tointeger(false)) | 110 | print(mod.tointeger(false)) |
| 68 | print(mod.tointeger("error")) | 111 | print(mod.tointeger("error")) |
| 69 | 112 | ||
| 113 | ___'' | ||
| 70 | print(mod.len("123")) | 114 | print(mod.len("123")) |
| 71 | print(mod.len({ 1, 2, 3})) | 115 | print(mod.len({ 1, 2, 3})) |
| 72 | print(pcall(mod.len, true)) | 116 | print(pcall(mod.len, true)) |
| @@ -76,34 +120,44 @@ print(mod.len(ud)) | |||
| 76 | meta.__len = function() return true end | 120 | meta.__len = function() return true end |
| 77 | print(pcall(mod.len, ud)) | 121 | print(pcall(mod.len, ud)) |
| 78 | 122 | ||
| 123 | ___'' | ||
| 79 | print(mod.copy(true, "string", {}, 1)) | 124 | print(mod.copy(true, "string", {}, 1)) |
| 80 | 125 | ||
| 126 | ___'' | ||
| 81 | print(mod.rawxetp()) | 127 | print(mod.rawxetp()) |
| 82 | print(mod.rawxetp("I'm back")) | 128 | print(mod.rawxetp("I'm back")) |
| 83 | 129 | ||
| 130 | ___'' | ||
| 84 | print(F(mod.globals()), mod.globals() == _G) | 131 | print(F(mod.globals()), mod.globals() == _G) |
| 85 | 132 | ||
| 133 | ___'' | ||
| 86 | local t = {} | 134 | local t = {} |
| 87 | print(F(mod.subtable(t))) | 135 | print(F(mod.subtable(t))) |
| 88 | local x, msg = mod.subtable(t) | 136 | local x, msg = mod.subtable(t) |
| 89 | print(F(x, msg, x == t.xxx)) | 137 | print(F(x, msg, x == t.xxx)) |
| 90 | 138 | ||
| 139 | ___'' | ||
| 91 | print(F(mod.udata())) | 140 | print(F(mod.udata())) |
| 92 | print(mod.udata("nosuchtype")) | 141 | print(mod.udata("nosuchtype")) |
| 93 | 142 | ||
| 143 | ___'' | ||
| 94 | print(F(mod.uservalue())) | 144 | print(F(mod.uservalue())) |
| 95 | 145 | ||
| 146 | ___'' | ||
| 96 | print(mod.getupvalues()) | 147 | print(mod.getupvalues()) |
| 97 | 148 | ||
| 149 | ___'' | ||
| 98 | print(mod.absindex("hi", true)) | 150 | print(mod.absindex("hi", true)) |
| 99 | 151 | ||
| 152 | ___'' | ||
| 100 | print(mod.tolstring("string")) | 153 | print(mod.tolstring("string")) |
| 101 | local t = setmetatable({}, { | 154 | local t = setmetatable({}, { |
| 102 | __tostring = function(v) return "mytable" end | 155 | __tostring = function(v) return "mytable" end |
| 103 | }) | 156 | }) |
| 104 | print(mod.tolstring( t ) ) | 157 | print(mod.tolstring(t)) |
| 105 | local t = setmetatable({}, { | 158 | local t = setmetatable({}, { |
| 106 | __tostring = function(v) return nil end | 159 | __tostring = function(v) return nil end |
| 107 | }) | 160 | }) |
| 108 | print(pcall(mod.tolstring, t)) | 161 | print(pcall(mod.tolstring, t)) |
| 162 | ___'' | ||
| 109 | 163 | ||
