summaryrefslogtreecommitdiff
path: root/src/tests/hs_create_table_tests.c
blob: b2b3f3c645e9c3d3d86d56eee186d4e3e85b5d01 (plain)
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
#include "hs_tests.h"

static int testfunc(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_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", void *, lua_topointer, value==lua_topointer(L, expected)
#define checkval_func(expected)			\
   check_value(lua_isfunction, "function", 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_topointer, value==lua_topointer(L, expected))
#define checkval_light(expected)			\
   check_value(lua_islightuserdata, "light userdata", void *, lua_tolightuserdata, value==expected)

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

TEST(create_table_bool_bool)
{
   int index = hs_create_table
      (L, hs_bool_bool(true, true), hs_bool_bool(false, true));
   loadkey_bool(index, true);
   checkval_bool(true);
   loadkey_bool(index, false);
   checkval_bool(true);
   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);
    mu_run_test("create table with boolean keys and boolean values",
		create_table_bool_bool);
}