summaryrefslogtreecommitdiff
path: root/src/tests/hs_create_table_tests.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/tests/hs_create_table_tests.c')
-rw-r--r--src/tests/hs_create_table_tests.c139
1 files changed, 139 insertions, 0 deletions
diff --git a/src/tests/hs_create_table_tests.c b/src/tests/hs_create_table_tests.c
new file mode 100644
index 0000000..feca8f5
--- /dev/null
+++ b/src/tests/hs_create_table_tests.c
@@ -0,0 +1,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);
+}