diff options
Diffstat (limited to 'tests/testmod.c')
-rw-r--r-- | tests/testmod.c | 318 |
1 files changed, 318 insertions, 0 deletions
diff --git a/tests/testmod.c b/tests/testmod.c new file mode 100644 index 0000000..868136b --- /dev/null +++ b/tests/testmod.c | |||
@@ -0,0 +1,318 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <stdlib.h> | ||
3 | #include <lua.h> | ||
4 | #include <lauxlib.h> | ||
5 | #include "compat-5.3.h" | ||
6 | |||
7 | |||
8 | static int test_isinteger (lua_State *L) { | ||
9 | lua_pushboolean(L, lua_isinteger(L, 1)); | ||
10 | return 1; | ||
11 | } | ||
12 | |||
13 | |||
14 | static int test_rotate (lua_State *L) { | ||
15 | int r = luaL_checkint(L, 1); | ||
16 | int n = lua_gettop(L)-1; | ||
17 | luaL_argcheck(L, (r < 0 ? -r : r) <= n, 1, "not enough arguments"); | ||
18 | lua_rotate(L, 2, r); | ||
19 | return n; | ||
20 | } | ||
21 | |||
22 | |||
23 | static int test_str2num (lua_State *L) { | ||
24 | const char *s = luaL_checkstring(L, 1); | ||
25 | size_t len = lua_stringtonumber(L, s); | ||
26 | if (len == 0) | ||
27 | lua_pushnumber(L, 0); | ||
28 | lua_pushinteger(L, (lua_Integer)len); | ||
29 | return 2; | ||
30 | } | ||
31 | |||
32 | |||
33 | static int my_mod (lua_State *L ) { | ||
34 | lua_newtable(L); | ||
35 | lua_pushboolean(L, 1); | ||
36 | lua_setfield(L, -2, "boolean"); | ||
37 | return 1; | ||
38 | } | ||
39 | |||
40 | static int test_requiref (lua_State *L) { | ||
41 | lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); | ||
42 | lua_newtable(L); | ||
43 | lua_pushboolean(L, 0); | ||
44 | lua_setfield(L, -2, "boolean"); | ||
45 | lua_setfield(L, -2, "requiref3"); | ||
46 | lua_pop(L, 1); | ||
47 | luaL_requiref(L, "requiref1", my_mod, 0); | ||
48 | luaL_requiref(L, "requiref2", my_mod, 1); | ||
49 | luaL_requiref(L, "requiref3", my_mod, 1); | ||
50 | return 3; | ||
51 | } | ||
52 | |||
53 | static int test_getseti (lua_State *L) { | ||
54 | lua_Integer k = luaL_checkinteger(L, 2); | ||
55 | lua_Integer n = 0; | ||
56 | if (lua_geti(L, 1, k) == LUA_TNUMBER) { | ||
57 | n = lua_tointeger(L, -1); | ||
58 | } else { | ||
59 | lua_pop(L, 1); | ||
60 | lua_pushinteger(L, n); | ||
61 | } | ||
62 | lua_pushinteger(L, n+1); | ||
63 | lua_seti(L, 1, k); | ||
64 | return 1; | ||
65 | } | ||
66 | |||
67 | |||
68 | /* additional tests for Lua5.1 */ | ||
69 | #define NUP 3 | ||
70 | |||
71 | static int test_newproxy (lua_State *L) { | ||
72 | lua_settop(L, 0); | ||
73 | lua_newuserdata(L, 0); | ||
74 | lua_newtable(L); | ||
75 | lua_pushvalue(L, -1); | ||
76 | lua_pushboolean(L, 1); | ||
77 | lua_setfield(L, -2, "__gc"); | ||
78 | lua_setmetatable(L, -3); | ||
79 | return 2; | ||
80 | } | ||
81 | |||
82 | static int test_absindex (lua_State *L) { | ||
83 | int i = 1; | ||
84 | for (i = 1; i <= NUP; ++i) | ||
85 | lua_pushvalue(L, lua_absindex(L, lua_upvalueindex(i))); | ||
86 | lua_pushvalue(L, lua_absindex(L, LUA_REGISTRYINDEX)); | ||
87 | lua_pushstring(L, lua_typename(L, lua_type(L, lua_absindex(L, -1)))); | ||
88 | lua_replace(L, lua_absindex(L, -2)); | ||
89 | lua_pushvalue(L, lua_absindex(L, -2)); | ||
90 | lua_pushvalue(L, lua_absindex(L, -4)); | ||
91 | lua_pushvalue(L, lua_absindex(L, -6)); | ||
92 | i += 3; | ||
93 | lua_pushvalue(L, lua_absindex(L, 1)); | ||
94 | lua_pushvalue(L, lua_absindex(L, 2)); | ||
95 | lua_pushvalue(L, lua_absindex(L, 3)); | ||
96 | i += 3; | ||
97 | return i; | ||
98 | } | ||
99 | |||
100 | static int test_arith (lua_State *L) { | ||
101 | lua_settop(L, 2); | ||
102 | lua_pushvalue(L, 1); | ||
103 | lua_pushvalue(L, 2); | ||
104 | lua_arith(L, LUA_OPADD); | ||
105 | lua_pushvalue(L, 1); | ||
106 | lua_pushvalue(L, 2); | ||
107 | lua_arith(L, LUA_OPSUB); | ||
108 | lua_pushvalue(L, 1); | ||
109 | lua_pushvalue(L, 2); | ||
110 | lua_arith(L, LUA_OPMUL); | ||
111 | lua_pushvalue(L, 1); | ||
112 | lua_pushvalue(L, 2); | ||
113 | lua_arith(L, LUA_OPDIV); | ||
114 | lua_pushvalue(L, 1); | ||
115 | lua_pushvalue(L, 2); | ||
116 | lua_arith(L, LUA_OPMOD); | ||
117 | lua_pushvalue(L, 1); | ||
118 | lua_pushvalue(L, 2); | ||
119 | lua_arith(L, LUA_OPPOW); | ||
120 | lua_pushvalue(L, 1); | ||
121 | lua_arith(L, LUA_OPUNM); | ||
122 | return lua_gettop(L)-2; | ||
123 | } | ||
124 | |||
125 | static int test_compare (lua_State *L) { | ||
126 | luaL_checknumber(L, 1); | ||
127 | luaL_checknumber(L, 2); | ||
128 | lua_settop(L, 2); | ||
129 | lua_pushboolean(L, lua_compare(L, 1, 2, LUA_OPEQ)); | ||
130 | lua_pushboolean(L, lua_compare(L, 1, 2, LUA_OPLT)); | ||
131 | lua_pushboolean(L, lua_compare(L, 1, 2, LUA_OPLE)); | ||
132 | return 3; | ||
133 | } | ||
134 | |||
135 | static int test_globals (lua_State *L) { | ||
136 | lua_pushglobaltable(L); | ||
137 | return 1; | ||
138 | } | ||
139 | |||
140 | static int test_tonumber (lua_State *L) { | ||
141 | int isnum = 0; | ||
142 | lua_Number n = lua_tonumberx(L, 1, &isnum); | ||
143 | if (!isnum) | ||
144 | lua_pushnil(L); | ||
145 | else | ||
146 | lua_pushnumber(L, n); | ||
147 | return 1; | ||
148 | } | ||
149 | |||
150 | static int test_tointeger (lua_State *L) { | ||
151 | int isnum = 0; | ||
152 | lua_Integer n = lua_tointegerx(L, 1, &isnum); | ||
153 | if (!isnum) | ||
154 | lua_pushnil(L); | ||
155 | else | ||
156 | lua_pushinteger(L, n); | ||
157 | return 1; | ||
158 | } | ||
159 | |||
160 | static int test_len (lua_State *L) { | ||
161 | luaL_checkany(L, 1); | ||
162 | lua_len(L, 1); | ||
163 | lua_pushinteger(L, luaL_len(L, 1)); | ||
164 | return 2; | ||
165 | } | ||
166 | |||
167 | static int test_copy (lua_State *L) { | ||
168 | int args = lua_gettop(L); | ||
169 | if (args >= 2) { | ||
170 | int i = 0; | ||
171 | for (i = args-1; i > 0; --i) | ||
172 | lua_copy(L, args, i); | ||
173 | } | ||
174 | return args; | ||
175 | } | ||
176 | |||
177 | /* need an address */ | ||
178 | static char const dummy = 0; | ||
179 | |||
180 | static int test_rawxetp (lua_State *L) { | ||
181 | if (lua_gettop(L) > 0) | ||
182 | lua_pushvalue(L, 1); | ||
183 | else | ||
184 | lua_pushliteral(L, "hello again"); | ||
185 | lua_rawsetp(L, LUA_REGISTRYINDEX, &dummy); | ||
186 | lua_settop(L, 0); | ||
187 | lua_rawgetp(L, LUA_REGISTRYINDEX, &dummy); | ||
188 | return 1; | ||
189 | } | ||
190 | |||
191 | static int test_udata (lua_State *L) { | ||
192 | const char *tname = luaL_optstring(L, 1, "utype1"); | ||
193 | void *u1 = lua_newuserdata(L, 1); | ||
194 | int u1pos = lua_gettop(L); | ||
195 | void *u2 = lua_newuserdata(L, 1); | ||
196 | int u2pos = lua_gettop(L); | ||
197 | luaL_newmetatable(L, "utype1"); | ||
198 | luaL_newmetatable(L, "utype2"); | ||
199 | lua_pop(L, 2); | ||
200 | luaL_setmetatable(L, "utype2"); | ||
201 | lua_pushvalue(L, u1pos); | ||
202 | luaL_setmetatable(L, "utype1"); | ||
203 | lua_pop(L, 1); | ||
204 | (void)u1; | ||
205 | (void)u2; | ||
206 | lua_pushlightuserdata(L, luaL_testudata(L, u1pos, tname)); | ||
207 | lua_pushlightuserdata(L, luaL_testudata(L, u2pos, tname)); | ||
208 | luaL_getmetatable(L, "utype1"); | ||
209 | lua_getfield(L, -1, "__name"); | ||
210 | lua_replace(L, -2); | ||
211 | return 3; | ||
212 | } | ||
213 | |||
214 | static int test_subtable (lua_State *L) { | ||
215 | luaL_checktype(L, 1, LUA_TTABLE); | ||
216 | lua_settop(L, 1); | ||
217 | if (luaL_getsubtable(L, 1, "xxx")) { | ||
218 | lua_pushliteral(L, "oldtable"); | ||
219 | } else { | ||
220 | lua_pushliteral(L, "newtable"); | ||
221 | } | ||
222 | return 2; | ||
223 | } | ||
224 | |||
225 | static int test_uservalue (lua_State *L) { | ||
226 | void *udata = lua_newuserdata(L, 1); | ||
227 | int ui = lua_gettop(L); | ||
228 | lua_newtable(L); | ||
229 | lua_setuservalue(L, ui); | ||
230 | lua_getuservalue(L, ui); | ||
231 | (void)udata; | ||
232 | return 1; | ||
233 | } | ||
234 | |||
235 | static int test_upvalues (lua_State *L) { | ||
236 | int i = 1; | ||
237 | for (i = 1; i <= NUP; ++i) | ||
238 | lua_pushvalue(L, lua_upvalueindex(i)); | ||
239 | return NUP; | ||
240 | } | ||
241 | |||
242 | static int test_tolstring (lua_State *L) { | ||
243 | size_t len = 0; | ||
244 | luaL_tolstring(L, 1, &len); | ||
245 | lua_pushinteger(L, (int)len); | ||
246 | return 2; | ||
247 | } | ||
248 | |||
249 | static int test_pushstring (lua_State *L) { | ||
250 | lua_pushstring(L, lua_pushliteral(L, "abc")); | ||
251 | lua_pushstring(L, lua_pushlstring(L, "abc", 2)); | ||
252 | lua_pushstring(L, lua_pushlstring(L, NULL, 0)); | ||
253 | lua_pushstring(L, lua_pushstring(L, "abc")); | ||
254 | lua_pushboolean(L, NULL == lua_pushstring(L, NULL)); | ||
255 | return 10; | ||
256 | } | ||
257 | |||
258 | static int test_buffer (lua_State *L) { | ||
259 | luaL_Buffer b; | ||
260 | char *p = luaL_buffinitsize(L, &b, LUAL_BUFFERSIZE+1); | ||
261 | p[0] = 'a'; | ||
262 | p[1] = 'b'; | ||
263 | luaL_addsize(&b, 2); | ||
264 | luaL_addstring(&b, "c"); | ||
265 | lua_pushliteral(L, "d"); | ||
266 | luaL_addvalue(&b); | ||
267 | luaL_addchar(&b, 'e'); | ||
268 | luaL_pushresult(&b); | ||
269 | return 1; | ||
270 | } | ||
271 | |||
272 | static int test_exec (lua_State *L) { | ||
273 | const char *cmd = luaL_checkstring(L, 1); | ||
274 | return luaL_execresult(L, system(cmd)); | ||
275 | } | ||
276 | |||
277 | |||
278 | static const luaL_Reg funcs[] = { | ||
279 | { "isinteger", test_isinteger }, | ||
280 | { "rotate", test_rotate }, | ||
281 | { "strtonum", test_str2num }, | ||
282 | { "requiref", test_requiref }, | ||
283 | { "getseti", test_getseti }, | ||
284 | { "newproxy", test_newproxy }, | ||
285 | { "arith", test_arith }, | ||
286 | { "compare", test_compare }, | ||
287 | { "tonumber", test_tonumber }, | ||
288 | { "tointeger", test_tointeger }, | ||
289 | { "len", test_len }, | ||
290 | { "copy", test_copy }, | ||
291 | { "rawxetp", test_rawxetp }, | ||
292 | { "subtable", test_subtable }, | ||
293 | { "udata", test_udata }, | ||
294 | { "uservalue", test_uservalue }, | ||
295 | { "globals", test_globals }, | ||
296 | { "tolstring", test_tolstring }, | ||
297 | { "pushstring", test_pushstring }, | ||
298 | { "buffer", test_buffer }, | ||
299 | { "exec", test_exec }, | ||
300 | { NULL, NULL } | ||
301 | }; | ||
302 | |||
303 | static const luaL_Reg more_funcs[] = { | ||
304 | { "getupvalues", test_upvalues }, | ||
305 | { "absindex", test_absindex }, | ||
306 | { NULL, NULL } | ||
307 | }; | ||
308 | |||
309 | |||
310 | int luaopen_testmod (lua_State *L) { | ||
311 | int i = 1; | ||
312 | luaL_newlib(L, funcs); | ||
313 | for (i = 1; i <= NUP; ++i) | ||
314 | lua_pushnumber(L, i); | ||
315 | luaL_setfuncs(L, more_funcs, NUP); | ||
316 | return 1; | ||
317 | } | ||
318 | |||