diff options
author | Philipp Janda <siffiejoe@gmx.net> | 2015-01-13 08:27:20 +0100 |
---|---|---|
committer | Philipp Janda <siffiejoe@gmx.net> | 2015-01-13 08:27:20 +0100 |
commit | 901f1bfdbadc7afa5526ca854a12bdfe8fcb88b3 (patch) | |
tree | 698870c642950c4287c7ab57c2dcab28b3efb6a6 /tests | |
parent | 46bbf75fbea482c19c4eb50bf97ef948f611e26a (diff) | |
download | lua-compat-5.3-901f1bfdbadc7afa5526ca854a12bdfe8fcb88b3.tar.gz lua-compat-5.3-901f1bfdbadc7afa5526ca854a12bdfe8fcb88b3.tar.bz2 lua-compat-5.3-901f1bfdbadc7afa5526ca854a12bdfe8fcb88b3.zip |
add initial implementation of c-api and license
Diffstat (limited to 'tests')
-rwxr-xr-x | tests/test.lua | 109 | ||||
-rw-r--r-- | tests/testmod.c | 246 |
2 files changed, 355 insertions, 0 deletions
diff --git a/tests/test.lua b/tests/test.lua new file mode 100755 index 0000000..cc07ee8 --- /dev/null +++ b/tests/test.lua | |||
@@ -0,0 +1,109 @@ | |||
1 | #!/usr/bin/env lua | ||
2 | |||
3 | local mod = require( "testmod" ) | ||
4 | local F | ||
5 | do | ||
6 | local type, unpack = type, table.unpack or unpack | ||
7 | function F( ... ) | ||
8 | local args, n = { ... }, select( '#', ... ) | ||
9 | for i = 1, n do | ||
10 | local t = type( args[ i ] ) | ||
11 | if t ~= "string" and t ~= "number" and t ~= "boolean" then | ||
12 | args[ i ] = t | ||
13 | end | ||
14 | end | ||
15 | return unpack( args, 1, n ) | ||
16 | end | ||
17 | end | ||
18 | |||
19 | |||
20 | print( mod.isinteger( 1 ) ) | ||
21 | print( mod.isinteger( 0 ) ) | ||
22 | print( mod.isinteger( 1234567 ) ) | ||
23 | print( mod.isinteger( 12.3 ) ) | ||
24 | print( mod.isinteger( math.huge ) ) | ||
25 | print( mod.isinteger( math.sqrt( -1 ) ) ) | ||
26 | |||
27 | |||
28 | print( mod.rotate( 1, 1, 2, 3, 4, 5, 6 ) ) | ||
29 | print( mod.rotate(-1, 1, 2, 3, 4, 5, 6 ) ) | ||
30 | print( mod.rotate( 4, 1, 2, 3, 4, 5, 6 ) ) | ||
31 | print( mod.rotate( -4, 1, 2, 3, 4, 5, 6 ) ) | ||
32 | |||
33 | |||
34 | print( mod.strtonum( "+123" ) ) | ||
35 | print( mod.strtonum( " 123 " ) ) | ||
36 | print( mod.strtonum( "-1.23" ) ) | ||
37 | print( mod.strtonum( " 123 abc" ) ) | ||
38 | print( mod.strtonum( "jkl" ) ) | ||
39 | |||
40 | |||
41 | local a, b, c = mod.requiref() | ||
42 | print( type( a ), type( b ), type( c ), | ||
43 | a.boolean, b.boolean, c.boolean, | ||
44 | type( requiref1 ), type( requiref2 ), type( requiref3 ) ) | ||
45 | |||
46 | local proxy, backend = {}, {} | ||
47 | setmetatable( proxy, { __index = backend, __newindex = backend } ) | ||
48 | print( rawget( proxy, 1 ), rawget( backend, 1 ) ) | ||
49 | print( mod.getseti( proxy, 1 ) ) | ||
50 | print( rawget( proxy, 1 ), rawget( backend, 1 ) ) | ||
51 | print( mod.getseti( proxy, 1 ) ) | ||
52 | print( rawget( proxy, 1 ), rawget( backend, 1 ) ) | ||
53 | |||
54 | -- tests for Lua 5.1 | ||
55 | print(mod.tonumber(12)) | ||
56 | print(mod.tonumber("12")) | ||
57 | print(mod.tonumber("0")) | ||
58 | print(mod.tonumber(false)) | ||
59 | print(mod.tonumber("error")) | ||
60 | |||
61 | print(mod.tointeger(12)) | ||
62 | print(mod.tointeger("12")) | ||
63 | print(mod.tointeger("0")) | ||
64 | print( "aaa" ) | ||
65 | print(mod.tointeger(math.pi)) | ||
66 | print( "bbb" ) | ||
67 | print(mod.tointeger(false)) | ||
68 | print(mod.tointeger("error")) | ||
69 | |||
70 | print(mod.len("123")) | ||
71 | print(mod.len({ 1, 2, 3})) | ||
72 | print(pcall(mod.len, true)) | ||
73 | local ud, meta = mod.newproxy() | ||
74 | meta.__len = function() return 5 end | ||
75 | print(mod.len(ud)) | ||
76 | meta.__len = function() return true end | ||
77 | print(pcall(mod.len, ud)) | ||
78 | |||
79 | print(mod.copy(true, "string", {}, 1)) | ||
80 | |||
81 | print(mod.rawxetp()) | ||
82 | print(mod.rawxetp("I'm back")) | ||
83 | |||
84 | print(F(mod.globals()), mod.globals() == _G) | ||
85 | |||
86 | local t = {} | ||
87 | print(F(mod.subtable(t))) | ||
88 | local x, msg = mod.subtable(t) | ||
89 | print(F(x, msg, x == t.xxx)) | ||
90 | |||
91 | print(F(mod.udata())) | ||
92 | print(mod.udata("nosuchtype")) | ||
93 | |||
94 | print(F(mod.uservalue())) | ||
95 | |||
96 | print(mod.getupvalues()) | ||
97 | |||
98 | print(mod.absindex("hi", true)) | ||
99 | |||
100 | print(mod.tolstring("string")) | ||
101 | local t = setmetatable({}, { | ||
102 | __tostring = function(v) return "mytable" end | ||
103 | }) | ||
104 | print(mod.tolstring( t ) ) | ||
105 | local t = setmetatable({}, { | ||
106 | __tostring = function(v) return nil end | ||
107 | }) | ||
108 | print(pcall(mod.tolstring, t)) | ||
109 | |||
diff --git a/tests/testmod.c b/tests/testmod.c new file mode 100644 index 0000000..a9c8e8d --- /dev/null +++ b/tests/testmod.c | |||
@@ -0,0 +1,246 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <lua.h> | ||
3 | #include <lauxlib.h> | ||
4 | #include "compat-5.3.h" | ||
5 | |||
6 | |||
7 | static int test_isinteger (lua_State *L) { | ||
8 | lua_pushboolean(L, lua_isinteger(L, 1)); | ||
9 | return 1; | ||
10 | } | ||
11 | |||
12 | |||
13 | static int test_rotate (lua_State *L) { | ||
14 | int r = luaL_checkint(L, 1); | ||
15 | int n = lua_gettop(L)-1; | ||
16 | luaL_argcheck(L, (r < 0 ? -r : r) <= n, 1, "not enough arguments"); | ||
17 | lua_rotate(L, 2, r); | ||
18 | return n; | ||
19 | } | ||
20 | |||
21 | |||
22 | static int test_str2num (lua_State *L) { | ||
23 | const char *s = luaL_checkstring(L, 1); | ||
24 | size_t len = lua_stringtonumber(L, s); | ||
25 | if (len == 0) | ||
26 | lua_pushnumber(L, 0); | ||
27 | lua_pushinteger(L, (lua_Integer)len); | ||
28 | return 2; | ||
29 | } | ||
30 | |||
31 | |||
32 | static int my_mod (lua_State *L ) { | ||
33 | lua_newtable(L); | ||
34 | lua_pushboolean(L, 1); | ||
35 | lua_setfield(L, -2, "boolean"); | ||
36 | return 1; | ||
37 | } | ||
38 | |||
39 | static int test_requiref (lua_State *L) { | ||
40 | lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); | ||
41 | lua_newtable(L); | ||
42 | lua_pushboolean(L, 0); | ||
43 | lua_setfield(L, -2, "boolean"); | ||
44 | lua_setfield(L, -2, "requiref3"); | ||
45 | lua_pop(L, 1); | ||
46 | luaL_requiref(L, "requiref1", my_mod, 0); | ||
47 | luaL_requiref(L, "requiref2", my_mod, 1); | ||
48 | luaL_requiref(L, "requiref3", my_mod, 1); | ||
49 | return 3; | ||
50 | } | ||
51 | |||
52 | static int test_getseti (lua_State *L) { | ||
53 | lua_Integer k = luaL_checkinteger(L, 2); | ||
54 | lua_Integer n = 0; | ||
55 | if (lua_geti(L, 1, k) == LUA_TNUMBER) { | ||
56 | n = lua_tointeger(L, -1); | ||
57 | } else { | ||
58 | lua_pop(L, 1); | ||
59 | lua_pushinteger(L, n); | ||
60 | } | ||
61 | lua_pushinteger(L, n+1); | ||
62 | lua_seti(L, 1, k); | ||
63 | return 1; | ||
64 | } | ||
65 | |||
66 | |||
67 | /* additional tests for Lua5.1 */ | ||
68 | #define NUP 3 | ||
69 | |||
70 | static int test_newproxy (lua_State *L) { | ||
71 | lua_settop(L, 0); | ||
72 | lua_newuserdata(L, 0); | ||
73 | lua_newtable(L); | ||
74 | lua_pushvalue(L, -1); | ||
75 | lua_pushboolean(L, 1); | ||
76 | lua_setfield(L, -2, "__gc"); | ||
77 | lua_setmetatable(L, -3); | ||
78 | return 2; | ||
79 | } | ||
80 | |||
81 | static int test_absindex (lua_State *L) { | ||
82 | int i = 1; | ||
83 | for (i = 1; i <= NUP; ++i) | ||
84 | lua_pushvalue(L, lua_absindex(L, lua_upvalueindex(i))); | ||
85 | lua_pushvalue(L, lua_absindex(L, LUA_REGISTRYINDEX)); | ||
86 | lua_pushstring(L, lua_typename(L, lua_type(L, lua_absindex(L, -1)))); | ||
87 | lua_replace(L, lua_absindex(L, -2)); | ||
88 | lua_pushvalue(L, lua_absindex(L, -2)); | ||
89 | lua_pushvalue(L, lua_absindex(L, -4)); | ||
90 | lua_pushvalue(L, lua_absindex(L, -6)); | ||
91 | i += 3; | ||
92 | lua_pushvalue(L, lua_absindex(L, 1)); | ||
93 | lua_pushvalue(L, lua_absindex(L, 2)); | ||
94 | lua_pushvalue(L, lua_absindex(L, 3)); | ||
95 | i += 3; | ||
96 | return i; | ||
97 | } | ||
98 | |||
99 | static int test_globals (lua_State *L) { | ||
100 | lua_pushglobaltable(L); | ||
101 | return 1; | ||
102 | } | ||
103 | |||
104 | static int test_tonumber (lua_State *L) { | ||
105 | int isnum = 0; | ||
106 | lua_Number n = lua_tonumberx(L, 1, &isnum); | ||
107 | if (!isnum) | ||
108 | lua_pushnil(L); | ||
109 | else | ||
110 | lua_pushnumber(L, n); | ||
111 | return 1; | ||
112 | } | ||
113 | |||
114 | static int test_tointeger (lua_State *L) { | ||
115 | int isnum = 0; | ||
116 | lua_Integer n = lua_tointegerx(L, 1, &isnum); | ||
117 | if (!isnum) | ||
118 | lua_pushnil(L); | ||
119 | else | ||
120 | lua_pushinteger(L, n); | ||
121 | return 1; | ||
122 | } | ||
123 | |||
124 | static int test_len (lua_State *L) { | ||
125 | luaL_checkany(L, 1); | ||
126 | lua_len(L, 1); | ||
127 | lua_pushinteger(L, luaL_len(L, 1)); | ||
128 | return 2; | ||
129 | } | ||
130 | |||
131 | static int test_copy (lua_State *L) { | ||
132 | int args = lua_gettop(L); | ||
133 | if (args >= 2) { | ||
134 | int i = 0; | ||
135 | for (i = args-1; i > 0; --i) | ||
136 | lua_copy(L, args, i); | ||
137 | } | ||
138 | return args; | ||
139 | } | ||
140 | |||
141 | /* need an address */ | ||
142 | static char const dummy = 0; | ||
143 | |||
144 | static int test_rawxetp (lua_State *L) { | ||
145 | if (lua_gettop(L) > 0) | ||
146 | lua_pushvalue(L, 1); | ||
147 | else | ||
148 | lua_pushliteral(L, "hello again"); | ||
149 | lua_rawsetp(L, LUA_REGISTRYINDEX, &dummy); | ||
150 | lua_settop(L, 0); | ||
151 | lua_rawgetp(L, LUA_REGISTRYINDEX, &dummy); | ||
152 | return 1; | ||
153 | } | ||
154 | |||
155 | static int test_udata (lua_State *L) { | ||
156 | const char *tname = luaL_optstring(L, 1, "utype1"); | ||
157 | void *u1 = lua_newuserdata(L, 1); | ||
158 | int u1pos = lua_gettop(L); | ||
159 | void *u2 = lua_newuserdata(L, 1); | ||
160 | int u2pos = lua_gettop(L); | ||
161 | luaL_newmetatable(L, "utype1"); | ||
162 | luaL_newmetatable(L, "utype2"); | ||
163 | lua_pop(L, 2); | ||
164 | luaL_setmetatable(L, "utype2"); | ||
165 | lua_pushvalue(L, u1pos); | ||
166 | luaL_setmetatable(L, "utype1"); | ||
167 | lua_pop(L, 1); | ||
168 | (void)u1; | ||
169 | (void)u2; | ||
170 | lua_pushlightuserdata(L, luaL_testudata(L, u1pos, tname)); | ||
171 | lua_pushlightuserdata(L, luaL_testudata(L, u2pos, tname)); | ||
172 | return 2; | ||
173 | } | ||
174 | |||
175 | static int test_subtable (lua_State *L) { | ||
176 | luaL_checktype(L, 1, LUA_TTABLE); | ||
177 | lua_settop(L, 1); | ||
178 | if (luaL_getsubtable(L, 1, "xxx")) { | ||
179 | lua_pushliteral(L, "oldtable"); | ||
180 | } else { | ||
181 | lua_pushliteral(L, "newtable"); | ||
182 | } | ||
183 | return 2; | ||
184 | } | ||
185 | |||
186 | static int test_uservalue (lua_State *L) { | ||
187 | void *udata = lua_newuserdata(L, 1); | ||
188 | int ui = lua_gettop(L); | ||
189 | lua_newtable(L); | ||
190 | lua_setuservalue(L, ui); | ||
191 | lua_getuservalue(L, ui); | ||
192 | (void)udata; | ||
193 | return 1; | ||
194 | } | ||
195 | |||
196 | static int test_upvalues (lua_State *L) { | ||
197 | int i = 1; | ||
198 | for (i = 1; i <= NUP; ++i) | ||
199 | lua_pushvalue(L, lua_upvalueindex(i)); | ||
200 | return NUP; | ||
201 | } | ||
202 | |||
203 | static int test_tolstring (lua_State *L) { | ||
204 | size_t len = 0; | ||
205 | luaL_tolstring(L, 1, &len); | ||
206 | lua_pushinteger(L, (int)len); | ||
207 | return 2; | ||
208 | } | ||
209 | |||
210 | |||
211 | static const luaL_Reg funcs[] = { | ||
212 | { "isinteger", test_isinteger }, | ||
213 | { "rotate", test_rotate }, | ||
214 | { "strtonum", test_str2num }, | ||
215 | { "requiref", test_requiref }, | ||
216 | { "getseti", test_getseti }, | ||
217 | { "newproxy", test_newproxy }, | ||
218 | { "tonumber", test_tonumber }, | ||
219 | { "tointeger", test_tointeger }, | ||
220 | { "len", test_len }, | ||
221 | { "copy", test_copy }, | ||
222 | { "rawxetp", test_rawxetp }, | ||
223 | { "subtable", test_subtable }, | ||
224 | { "udata", test_udata }, | ||
225 | { "uservalue", test_uservalue }, | ||
226 | { "globals", test_globals }, | ||
227 | { "tolstring", test_tolstring }, | ||
228 | { NULL, NULL } | ||
229 | }; | ||
230 | |||
231 | static const luaL_Reg more_funcs[] = { | ||
232 | { "getupvalues", test_upvalues }, | ||
233 | { "absindex", test_absindex }, | ||
234 | { NULL, NULL } | ||
235 | }; | ||
236 | |||
237 | |||
238 | int luaopen_testmod (lua_State *L) { | ||
239 | int i = 1; | ||
240 | luaL_newlib(L, funcs); | ||
241 | for (i = 1; i <= NUP; ++i) | ||
242 | lua_pushnumber(L, i); | ||
243 | luaL_setfuncs(L, more_funcs, NUP); | ||
244 | return 1; | ||
245 | } | ||
246 | |||