aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorPhilipp Janda <siffiejoe@gmx.net>2015-01-13 08:27:20 +0100
committerPhilipp Janda <siffiejoe@gmx.net>2015-01-13 08:27:20 +0100
commit901f1bfdbadc7afa5526ca854a12bdfe8fcb88b3 (patch)
tree698870c642950c4287c7ab57c2dcab28b3efb6a6 /tests
parent46bbf75fbea482c19c4eb50bf97ef948f611e26a (diff)
downloadlua-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-xtests/test.lua109
-rw-r--r--tests/testmod.c246
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
3local mod = require( "testmod" )
4local F
5do
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
17end
18
19
20print( mod.isinteger( 1 ) )
21print( mod.isinteger( 0 ) )
22print( mod.isinteger( 1234567 ) )
23print( mod.isinteger( 12.3 ) )
24print( mod.isinteger( math.huge ) )
25print( mod.isinteger( math.sqrt( -1 ) ) )
26
27
28print( mod.rotate( 1, 1, 2, 3, 4, 5, 6 ) )
29print( mod.rotate(-1, 1, 2, 3, 4, 5, 6 ) )
30print( mod.rotate( 4, 1, 2, 3, 4, 5, 6 ) )
31print( mod.rotate( -4, 1, 2, 3, 4, 5, 6 ) )
32
33
34print( mod.strtonum( "+123" ) )
35print( mod.strtonum( " 123 " ) )
36print( mod.strtonum( "-1.23" ) )
37print( mod.strtonum( " 123 abc" ) )
38print( mod.strtonum( "jkl" ) )
39
40
41local a, b, c = mod.requiref()
42print( type( a ), type( b ), type( c ),
43 a.boolean, b.boolean, c.boolean,
44 type( requiref1 ), type( requiref2 ), type( requiref3 ) )
45
46local proxy, backend = {}, {}
47setmetatable( proxy, { __index = backend, __newindex = backend } )
48print( rawget( proxy, 1 ), rawget( backend, 1 ) )
49print( mod.getseti( proxy, 1 ) )
50print( rawget( proxy, 1 ), rawget( backend, 1 ) )
51print( mod.getseti( proxy, 1 ) )
52print( rawget( proxy, 1 ), rawget( backend, 1 ) )
53
54-- tests for Lua 5.1
55print(mod.tonumber(12))
56print(mod.tonumber("12"))
57print(mod.tonumber("0"))
58print(mod.tonumber(false))
59print(mod.tonumber("error"))
60
61print(mod.tointeger(12))
62print(mod.tointeger("12"))
63print(mod.tointeger("0"))
64print( "aaa" )
65print(mod.tointeger(math.pi))
66print( "bbb" )
67print(mod.tointeger(false))
68print(mod.tointeger("error"))
69
70print(mod.len("123"))
71print(mod.len({ 1, 2, 3}))
72print(pcall(mod.len, true))
73local ud, meta = mod.newproxy()
74meta.__len = function() return 5 end
75print(mod.len(ud))
76meta.__len = function() return true end
77print(pcall(mod.len, ud))
78
79print(mod.copy(true, "string", {}, 1))
80
81print(mod.rawxetp())
82print(mod.rawxetp("I'm back"))
83
84print(F(mod.globals()), mod.globals() == _G)
85
86local t = {}
87print(F(mod.subtable(t)))
88local x, msg = mod.subtable(t)
89print(F(x, msg, x == t.xxx))
90
91print(F(mod.udata()))
92print(mod.udata("nosuchtype"))
93
94print(F(mod.uservalue()))
95
96print(mod.getupvalues())
97
98print(mod.absindex("hi", true))
99
100print(mod.tolstring("string"))
101local t = setmetatable({}, {
102 __tostring = function(v) return "mytable" end
103})
104print(mod.tolstring( t ) )
105local t = setmetatable({}, {
106 __tostring = function(v) return nil end
107})
108print(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
7static int test_isinteger (lua_State *L) {
8 lua_pushboolean(L, lua_isinteger(L, 1));
9 return 1;
10}
11
12
13static 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
22static 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
32static 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
39static 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
52static 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
70static 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
81static 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
99static int test_globals (lua_State *L) {
100 lua_pushglobaltable(L);
101 return 1;
102}
103
104static 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
114static 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
124static 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
131static 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 */
142static char const dummy = 0;
143
144static 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
155static 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
175static 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
186static 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
196static 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
203static 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
211static 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
231static const luaL_Reg more_funcs[] = {
232 { "getupvalues", test_upvalues },
233 { "absindex", test_absindex },
234 { NULL, NULL }
235};
236
237
238int 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