diff options
Diffstat (limited to 'tests/testmod.c')
| -rw-r--r-- | tests/testmod.c | 246 |
1 files changed, 246 insertions, 0 deletions
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 | |||
