1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
|
#include "hs_tests.h"
static int testfunc1(lua_State *L) { return 0; }
static int testfunc2(lua_State *L) { return 0; }
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* tests for hs_create_table
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
TEST(table_correct_index)
{
int top_old = lua_gettop(L);
int index = hs_create_table(L);
mu_assert("the stack is unchanged!", lua_gettop(L) != top_old);
mu_assert("returned incorrect index!", index == lua_gettop(L));
return 0;
}
#define load_key(index, value, pushf) do { \
pushf(L, value); lua_gettable(L, index); \
mu_assert("value at key '" #value "' is nil!", \
!lua_isnil(L, -1)); \
} while(0)
#define loadkey_bool(index, value) \
load_key(index, value, lua_pushboolean)
#define loadkey_int(index, value) \
load_key(index, value, lua_pushinteger)
#define loadkey_num(index, value) \
load_key(index, value, lua_pushnumber)
#define loadkey_str(index, value) \
load_key(index, value, lua_pushstring)
#define loadkey_tbl(index, value) do { \
lua_pushvalue(L, value); lua_gettable(L, index); \
mu_assert("value at table (index '" #value "') is nil!", \
!lua_isnil(L, -1)); \
} while(0)
#define loadkey_func(index, value) do { \
lua_pushvalue(L, value); lua_gettable(L, index); \
mu_assert("value at function (index '" #value "') is nil!", \
!lua_isnil(L, -1)); \
} while(0)
#define loadkey_cfunc(index, value) \
load_key(index, value, lua_pushcfunction)
#define loadkey_user(index, value) do { \
lua_pushvalue(L, value); lua_gettable(L, index); \
mu_assert("value at userdata (index '" #value "') is nil!", \
!lua_isnil(L, -1)); \
} while(0)
#define loadkey_light(index, value) \
load_key(index, value, lua_pushlightuserdata)
#define check_value(is_type, typestring, type, conversion, comparison) \
do { \
mu_assert("value is not of type " typestring, is_type(L, -1)); \
type value = conversion(L, -1); lua_pop(L, 1); \
mu_assert("test " #comparison " failed!", (comparison)); \
lua_pop(L, 1); /* remove key */ \
} while(0)
#define checkval_bool(expected) \
check_value(lua_isboolean, "boolean", bool, lua_toboolean, value==expected)
#define checkval_int(expected) \
check_value(lua_isnumber, "integer", lua_Integer, lua_tointeger, value==expected)
#define checkval_num(expected) \
check_value(lua_isnumber, "number", lua_Number, lua_tonumber, value==expected)
#define checkval_str(expected) \
check_value(lua_isstring, "string", const char *, lua_tostring, strcmp(value, expected))
#define checkval_tbl(expected) \
check_value(lua_istable, "table", const void *, lua_topointer, value==lua_topointer(L, expected))
#define checkval_func(expected) \
check_value(lua_isfunction, "function", const void *, lua_topointer, value==lua_topointer(L, expected))
#define checkval_cfunc(expected) \
check_value(lua_iscfunction, "C function", lua_CFunction, lua_tocfunction, value==expected)
#define checkval_user(expected) \
check_value(lua_isuserdata, "userdata", void *, lua_touserdata, value==lua_topointer(L, expected))
#define checkval_light(expected) \
check_value(lua_islightuserdata, "light userdata", void *, lua_touserdata, value==expected)
#define store(name) \
lua_pushvalue(L, -1); \
int name ## _ref = luaL_ref(L, LUA_REGISTRYINDEX); \
int name = lua_gettop(L);
#define load(name) \
lua_rawgeti(L, LUA_REGISTRYINDEX, name ## _ref); \
name = lua_gettop(L);
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* boolean keys
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
TEST(create_table_bool_bool)
{
int oldtop = lua_gettop(L);
int index = hs_create_table
(L, hs_bool_bool(true, true), hs_bool_bool(false, true));
mu_assert_equal(oldtop + 1, lua_gettop(L));
loadkey_bool(index, true);
checkval_bool(true);
loadkey_bool(index, false);
checkval_bool(true);
return 0;
}
TEST(create_table_bool_int)
{
int oldtop = lua_gettop(L);
int index = hs_create_table
(L, hs_bool_int(true, 15), hs_bool_int(false, 25));
mu_assert_equal(oldtop + 1, lua_gettop(L));
loadkey_bool(index, true);
checkval_int(15);
loadkey_bool(index, false);
checkval_int(25);
return 0;
}
TEST(create_table_bool_num)
{
int oldtop = lua_gettop(L);
int index = hs_create_table
(L, hs_bool_num(true, 2.718), hs_bool_num(false, 1.618));
mu_assert_equal(oldtop + 1, lua_gettop(L));
loadkey_bool(index, true);
checkval_num(2.718);
loadkey_bool(index, false);
checkval_num(1.618);
return 0;
}
TEST(create_table_bool_str)
{
int oldtop = lua_gettop(L);
int index = hs_create_table
(L, hs_bool_str(true, "hello"), hs_bool_str(false, "world"));
mu_assert_equal(oldtop + 1, lua_gettop(L));
loadkey_bool(index, true);
checkval_str("hello");
loadkey_bool(index, false);
checkval_str("world");
return 0;
}
TEST(create_table_bool_tbl)
{
int oldtop = lua_gettop(L);
lua_newtable(L);
store(value1);
lua_newtable(L);
store(value2);
int index = hs_create_table
(L, hs_bool_tbl(true, value1), hs_bool_tbl(false, value2));
mu_assert_equal(oldtop + 1, lua_gettop(L));
load(value1);
load(value2);
loadkey_bool(index, true);
checkval_tbl(value1);
loadkey_bool(index, false);
checkval_tbl(value2);
return 0;
}
TEST(create_table_bool_func)
{
int oldtop = lua_gettop(L);
luaL_loadstring(L, "print('hello')");
store(value1);
luaL_loadstring(L, "print('hello')");
store(value2);
int index = hs_create_table
(L, hs_bool_func(true, value1), hs_bool_func(false, value2));
mu_assert_equal(oldtop + 1, lua_gettop(L));
load(value1);
load(value2);
loadkey_bool(index, true);
checkval_func(value1);
loadkey_bool(index, false);
checkval_func(value2);
return 0;
}
TEST(create_table_bool_cfunc)
{
int oldtop = lua_gettop(L);
int index = hs_create_table
(L, hs_bool_cfunc(true, testfunc1), hs_bool_cfunc(false, testfunc2));
mu_assert_equal(oldtop + 1, lua_gettop(L));
loadkey_bool(index, true);
checkval_cfunc(testfunc1);
loadkey_bool(index, false);
checkval_cfunc(testfunc2);
return 0;
}
TEST(create_table_bool_user)
{
int oldtop = lua_gettop(L);
lua_newuserdata(L, sizeof(char));
store(user1);
lua_newuserdata(L, sizeof(char));
store(user2);
int index = hs_create_table
(L, hs_bool_user(true, user1), hs_bool_user(false, user2));
mu_assert_equal(oldtop + 1, lua_gettop(L));
load(user1);
load(user2);
loadkey_bool(index, true);
checkval_user(user1);
loadkey_bool(index, false);
checkval_user(user2);
return 0;
}
TEST(create_table_bool_light)
{
int a1 = 5;
void *light1 = (void *) &a1;
int a2 = 6;
void *light2 = (void *) &a2;
int oldtop = lua_gettop(L);
int index = hs_create_table
(L, hs_bool_light(true, light1), hs_bool_light(false, light2));
mu_assert_equal(oldtop + 1, lua_gettop(L));
loadkey_bool(index, true);
checkval_light(light1);
loadkey_bool(index, false);
checkval_light(light2);
return 0;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* test suite
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
void hs_create_table_tests()
{
printf("running hs_create_table() tests...\n");
mu_run_test("return correct stack index", table_correct_index);
/* boolean keys */
mu_run_test("create table with boolean keys and boolean values",
create_table_bool_bool);
mu_run_test("create table with boolean keys and integer values", \
create_table_bool_int);
mu_run_test("create table with boolean keys and number values", \
create_table_bool_num);
mu_run_test("create table with boolean keys and string values", \
create_table_bool_str);
mu_run_test("create table with boolean keys and table values", \
create_table_bool_tbl);
mu_run_test("create table with boolean keys and function values", \
create_table_bool_func);
mu_run_test("create table with boolean keys and C function values", \
create_table_bool_cfunc);
mu_run_test("create table with boolean keys and userdata values", \
create_table_bool_user);
mu_run_test("create table with boolean keys and light userdata values", \
create_table_bool_light);
/* integer keys /
mu_run_test("create table with integer keys and boolean values",
create_table_int_bool);
mu_run_test("create table with integer keys and integer values", \
create_table_int_int);
mu_run_test("create table with integer keys and number values", \
create_table_int_num);
mu_run_test("create table with integer keys and string values", \
create_table_int_str);
mu_run_test("create table with integer keys and table values", \
create_table_int_tbl);
mu_run_test("create table with integer keys and function values", \
create_table_int_func);
mu_run_test("create table with integer keys and C function values", \
create_table_int_cfunc);
mu_run_test("create table with integer keys and userdata values", \
create_table_int_user);
mu_run_test("create table with integer keys and light userdata values", \
create_table_int_light);
/* number keys /
mu_run_test("create table with number keys and boolean values",
create_table_num_bool);
mu_run_test("create table with number keys and integer values", \
create_table_num_int);
mu_run_test("create table with number keys and number values", \
create_table_num_num);
mu_run_test("create table with number keys and string values", \
create_table_num_str);
mu_run_test("create table with number keys and table values", \
create_table_num_tbl);
mu_run_test("create table with number keys and function values", \
create_table_num_func);
mu_run_test("create table with number keys and C function values", \
create_table_num_cfunc);
mu_run_test("create table with number keys and userdata values", \
create_table_num_user);
mu_run_test("create table with number keys and light userdata values", \
create_table_num_light);
/* string keys /
mu_run_test("create table with string keys and boolean values",
create_table_str_bool);
mu_run_test("create table with string keys and integer values", \
create_table_str_int);
mu_run_test("create table with string keys and number values", \
create_table_str_num);
mu_run_test("create table with string keys and string values", \
create_table_str_str);
mu_run_test("create table with string keys and table values", \
create_table_str_tbl);
mu_run_test("create table with string keys and function values", \
create_table_str_func);
mu_run_test("create table with string keys and C function values", \
create_table_str_cfunc);
mu_run_test("create table with string keys and userdata values", \
create_table_str_user);
mu_run_test("create table with string keys and light userdata values", \
create_table_str_light);
/* table keys /
mu_run_test("create table with table keys and boolean values",
create_table_tbl_bool);
mu_run_test("create table with table keys and integer values", \
create_table_tbl_int);
mu_run_test("create table with table keys and number values", \
create_table_tbl_num);
mu_run_test("create table with table keys and string values", \
create_table_tbl_str);
mu_run_test("create table with table keys and table values", \
create_table_tbl_tbl);
mu_run_test("create table with table keys and function values", \
create_table_tbl_func);
mu_run_test("create table with table keys and C function values", \
create_table_tbl_cfunc);
mu_run_test("create table with table keys and userdata values", \
create_table_tbl_user);
mu_run_test("create table with table keys and light userdata values", \
create_table_tbl_light);
/**/
}
|