From 785c4ab60c0bcdbe51d74ca28ce70ea23795ee86 Mon Sep 17 00:00:00 2001 From: sanine Date: Thu, 9 Sep 2021 16:29:59 -0500 Subject: add table nesting tests --- src/tests/hs_create_table_tests.c | 166 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 160 insertions(+), 6 deletions(-) (limited to 'src') 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); } -- cgit v1.2.1