summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2021-09-09 16:29:59 -0500
committersanine <sanine.not@pm.me>2021-09-09 16:29:59 -0500
commit785c4ab60c0bcdbe51d74ca28ce70ea23795ee86 (patch)
treec7e6d4aa632c523c633df71ad0e3feba94e60631
parentddc38c3e6524b27224b97faeb3abfc8663943d88 (diff)
add table nesting tests
-rw-r--r--src/tests/hs_create_table_tests.c166
1 files changed, 160 insertions, 6 deletions
diff --git a/src/tests/hs_create_table_tests.c b/src/tests/hs_create_table_tests.c
index a54a424..1adc947 100644
--- a/src/tests/hs_create_table_tests.c
+++ b/src/tests/hs_create_table_tests.c
@@ -1668,7 +1668,7 @@ TEST(create_table_light_bool)
int l2 = 6;
void *light1 = (void *) &l1;
void *light2 = (void *) &l2;
-
+
int index = hs_create_table
(L, hs_light_bool(light1, true), hs_light_bool(light2, true));
mu_assert_equal(oldtop + 1, lua_gettop(L));
@@ -1690,7 +1690,7 @@ TEST(create_table_light_int)
int l2 = 6;
void *light1 = (void *) &l1;
void *light2 = (void *) &l2;
-
+
int index = hs_create_table
(L, hs_light_int(light1, 15), hs_light_int(light2, 25));
mu_assert_equal(oldtop + 1, lua_gettop(L));
@@ -1712,7 +1712,7 @@ TEST(create_table_light_num)
int l2 = 6;
void *light1 = (void *) &l1;
void *light2 = (void *) &l2;
-
+
int index = hs_create_table
(L, hs_light_num(light1, 2.718), hs_light_num(light2, 1.618));
mu_assert_equal(oldtop + 1, lua_gettop(L));
@@ -1734,7 +1734,7 @@ TEST(create_table_light_str)
int l2 = 6;
void *light1 = (void *) &l1;
void *light2 = (void *) &l2;
-
+
int index = hs_create_table
(L, hs_light_str(light1, "hello"), hs_light_str(light2, "world"));
mu_assert_equal(oldtop + 1, lua_gettop(L));
@@ -1817,7 +1817,7 @@ TEST(create_table_light_cfunc)
int l2 = 6;
void *light1 = (void *) &l1;
void *light2 = (void *) &l2;
-
+
int index = hs_create_table
(L, hs_light_cfunc(light1, testfunc1), hs_light_cfunc(light2, testfunc2));
mu_assert_equal(oldtop + 1, lua_gettop(L));
@@ -1889,6 +1889,157 @@ TEST(create_table_light_light)
return 0;
}
+/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ *
+ * nested tables
+ *
+ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ */
+
+TEST(create_nested_table)
+{
+ int index = hs_create_table
+ (L,
+ hs_str_tbl("sub1", hs_create_table(L, hs_str_int("five", 5))),
+ hs_str_tbl("sub2", hs_create_table(L, hs_str_int("six", 6)))
+ );
+
+ mu_assert_equal(lua_gettop(L), 1);
+
+
+ lua_getfield(L, index, "sub1");
+ mu_assert("'sub1' is not a table!", lua_istable(L, -1));
+ lua_getfield(L, -1, "five");
+ mu_assert("'sub1.five' is not a number!", lua_isnumber(L, -1));
+ mu_assert_equal(lua_tointeger(L, -1), 5);
+
+ lua_pop(L, 2);
+
+ lua_getfield(L, index, "sub2");
+ mu_assert("'sub2' is not a table!", lua_istable(L, -1));
+ lua_getfield(L, -1, "six");
+ mu_assert("'sub2.six' is not a number!", lua_isnumber(L, -1));
+ mu_assert_equal(lua_tointeger(L, -1), 6);
+
+ return 0;
+}
+
+TEST(create_tree)
+{
+ /*
+ * { 0={ 0={ 0=0, 1=1},
+ * 1={ 0=0, 1=1},
+ * },
+ * 1={ 0={ 0=0, 1=1},
+ * 1={ 0=0, 1=1},
+ * },
+ * }
+ */
+ int index = hs_create_table
+ (L,
+ hs_int_tbl(0,
+ hs_create_table
+ (L,
+ hs_int_tbl(0,
+ hs_create_table
+ (L,
+ hs_int_int(0, 0),
+ hs_int_int(1, 1)
+ )),
+ hs_int_tbl(1,
+ hs_create_table
+ (L,
+ hs_int_int(0, 0),
+ hs_int_int(1, 1)
+ )),
+ )),
+ hs_int_tbl(1,
+ hs_create_table
+ (L,
+ hs_int_tbl(0,
+ hs_create_table
+ (L,
+ hs_int_int(0, 0),
+ hs_int_int(1, 1)
+ )),
+ hs_int_tbl(1,
+ hs_create_table
+ (L,
+ hs_int_int(0, 0),
+ hs_int_int(1, 1)
+ )),
+ ))
+ );
+
+ mu_assert_equal(lua_gettop(L), 1);
+
+ lua_rawgeti(L, index, 0);
+ mu_assert("tbl[0] is not a table!", lua_istable(L, -1));
+ lua_rawgeti(L, -1, 0);
+ mu_assert("tbl[0][0] is not a table!", lua_istable(L, -1));
+ lua_rawgeti(L, -1, 0);
+ mu_assert("tbl[0][0][0] is not a number!", lua_isnumber(L, -1));
+ lua_pop(L, 3);
+
+ lua_rawgeti(L, index, 0);
+ mu_assert("tbl[0] is not a table!", lua_istable(L, -1));
+ lua_rawgeti(L, -1, 0);
+ mu_assert("tbl[0][0] is not a table!", lua_istable(L, -1));
+ lua_rawgeti(L, -1, 1);
+ mu_assert("tbl[0][0][1] is not a number!", lua_isnumber(L, -1));
+ lua_pop(L, 3);
+
+ lua_rawgeti(L, index, 0);
+ mu_assert("tbl[0] is not a table!", lua_istable(L, -1));
+ lua_rawgeti(L, -1, 1);
+ mu_assert("tbl[0][1] is not a table!", lua_istable(L, -1));
+ lua_rawgeti(L, -1, 0);
+ mu_assert("tbl[0][1][0] is not a number!", lua_isnumber(L, -1));
+ lua_pop(L, 3);
+
+ lua_rawgeti(L, index, 0);
+ mu_assert("tbl[0] is not a table!", lua_istable(L, -1));
+ lua_rawgeti(L, -1, 1);
+ mu_assert("tbl[0][1] is not a table!", lua_istable(L, -1));
+ lua_rawgeti(L, -1, 1);
+ mu_assert("tbl[0][1][1] is not a number!", lua_isnumber(L, -1));
+ lua_pop(L, 3);
+
+ lua_rawgeti(L, index, 1);
+ mu_assert("tbl[1] is not a table!", lua_istable(L, -1));
+ lua_rawgeti(L, -1, 0);
+ mu_assert("tbl[1][0] is not a table!", lua_istable(L, -1));
+ lua_rawgeti(L, -1, 0);
+ mu_assert("tbl[1][0][0] is not a number!", lua_isnumber(L, -1));
+ lua_pop(L, 3);
+
+ lua_rawgeti(L, index, 1);
+ mu_assert("tbl[1] is not a table!", lua_istable(L, -1));
+ lua_rawgeti(L, -1, 0);
+ mu_assert("tbl[1][0] is not a table!", lua_istable(L, -1));
+ lua_rawgeti(L, -1, 1);
+ mu_assert("tbl[1][0][1] is not a number!", lua_isnumber(L, -1));
+ lua_pop(L, 3);
+
+ lua_rawgeti(L, index, 1);
+ mu_assert("tbl[1] is not a table!", lua_istable(L, -1));
+ lua_rawgeti(L, -1, 1);
+ mu_assert("tbl[1][1] is not a table!", lua_istable(L, -1));
+ lua_rawgeti(L, -1, 0);
+ mu_assert("tbl[1][1][0] is not a number!", lua_isnumber(L, -1));
+ lua_pop(L, 3);
+
+ lua_rawgeti(L, index, 1);
+ mu_assert("tbl[1] is not a table!", lua_istable(L, -1));
+ lua_rawgeti(L, -1, 1);
+ mu_assert("tbl[1][1] is not a table!", lua_istable(L, -1));
+ lua_rawgeti(L, -1, 1);
+ mu_assert("tbl[1][1][1] is not a number!", lua_isnumber(L, -1));
+ lua_pop(L, 3);
+
+ return 0;
+}
+
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
@@ -2042,7 +2193,7 @@ void hs_create_table_tests()
create_table_cfunc_user);
mu_run_test("create table with C function keys and light userdata values", \
create_table_cfunc_light);
-
+
/* userdata keys */
mu_run_test("create table with userdata keys and boolean values",
create_table_user_bool);
@@ -2082,4 +2233,7 @@ void hs_create_table_tests()
create_table_light_user);
mu_run_test("create table with light userdata keys and light userdata values", \
create_table_light_light);
+
+ mu_run_test("create nested tables", create_nested_table);
+ mu_run_test("create tree of nested tables", create_tree);
}