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

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * tests for hs_create_table
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 */

TEST(table_correct_index)
{
    int index = hs_create_table(L, HS_END);
    mu_assert("returned incorrect index!", index == lua_gettop(L));
    return 0;
}


#define check_table_element(field, is_type, typestring, type, conversion, comparison) \
    do {								\
    lua_getfield(L, -1, field);						\
    if (!is_type(L, -1)) { lua_pop(L, 1); return "field '" field "' is not type " typestring "!"; } \
    type value = conversion(L, -1);					\
    lua_pop(L, 1);							\
    mu_assert("field '" field "' does not match the expected value!", comparison); \
    } while(0)

#define check_bool(field, expected) check_table_element(field, lua_isboolean, "boolean", bool, lua_toboolean, value == expected)
#define check_int(field, expected) check_table_element(field, lua_isnumber, "integer", int, lua_tointeger, value == expected)
#define check_num(field, expected) check_table_element(field, lua_isnumber, "number", float, lua_tonumber, value == expected)
#define check_string(field, expected) check_table_element(field, lua_isstring, "string", char*, lua_tostring, strcmp(value, expected))
#define check_cfunc(field, expected) check_table_element(field, lua_iscfunction, "C function", lua_CFunction, lua_tocfunction, value == expected)
#define check_user(field, expected) check_table_element(field, lua_isuserdata, "userdata", void*, lua_touserdata, value == expected)
#define check_light(field, expected) check_table_element(field, lua_islightuserdata, "light userdata", void*, lua_touserdata, value == expected)

TEST(table_create_basic_types)
{
    void *userdata = lua_newuserdata(L, sizeof(char));
    int user_index = lua_gettop(L);
    char lightuserdata = 'F';
    
    hs_create_table
	(L,
	 "boolValue", HS_BOOL, false,
	 "intValue", HS_INT, 15,
	 "numValue", HS_NUM, 33.66,
	 "stringValue", HS_STR, "goober",
	 "cfuncValue", HS_CFUNC, testfunc,
	 "userValue", HS_USER, user_index,
	 "lightValue", HS_LIGHT, &lightuserdata,
	 HS_END);

    check_bool("boolValue", false);
    check_int("intValue", 15);
    check_num("numValue", 33.66);
    check_string("stringValue", "goober");
    check_cfunc("cfuncValue", testfunc);
    check_user("userValue", userdata);
    check_light("lightValue", &lightuserdata);
    return 0;
}


TEST(table_nesting)
{
    int top_init = lua_gettop(L);
    
    hs_create_table
	(L,
	 "subtable1", HS_TBL,
	 hs_create_table(L,
			 "subsubtable1", HS_TBL,
			 hs_create_table(L,
					 "fourteen", HS_INT, 14,
					 "phi", HS_NUM, 1.61803f,
					 HS_END),
			 HS_END),
	 "subtable2", HS_TBL,
	 hs_create_table(L,
			 "subsubtable2", HS_TBL,
			 hs_create_table(L,
					 "subsubsubtable", HS_TBL,
					 hs_create_table(L, HS_END),
					 HS_END),
			 HS_END),
	 HS_END);

    lua_getfield(L, -1, "subtable1");
    mu_assert("subtable1 is not a table!", lua_istable(L, -1));
    lua_getfield(L, -1, "subsubtable1");
    mu_assert("subsubtable1 is not a table!", lua_istable(L, -1));
    check_int("fourteen", 14);
    check_num("phi", 1.61803f);
    lua_pop(L, 2);

    lua_getfield(L, -1, "subtable2");
    mu_assert("subtable2 is not a table!", lua_istable(L, -1));
    lua_getfield(L, -1, "subsubtable2");
    mu_assert("subsubtable2 is not a table!", lua_istable(L, -1));
    lua_getfield(L, -1, "subsubsubtable");
    mu_assert("subsubsubtable is not a table!", lua_istable(L, -1));
    lua_pop(L, 4);

    mu_assert("more than one new item on stack!",
	      lua_gettop(L) == top_init + 1);
    return 0;    
}


TEST(table_pop_indices)
{
    int top_init = lua_gettop(L);
    int subtable1_index = hs_create_table(L, HS_END);
    int subtable2_index = hs_create_table(L, HS_END);

    hs_create_table(L,
		    "sub1", HS_TBL, subtable1_index,
		    "sub2", HS_TBL, subtable2_index,
		    HS_END);

    mu_assert("failed to remove subtables from the stack!",
	      lua_gettop(L) == top_init + 1);
    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("check creation of basic types", table_create_basic_types);
    mu_run_test("check table nesting", table_nesting);
}