diff options
author | sanine <sanine.not@pm.me> | 2021-08-28 18:34:18 -0500 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2021-08-28 18:34:18 -0500 |
commit | e0ee4f7428db6d3ba60eef7b54cf65cc021daf90 (patch) | |
tree | e6eeb1550b8a0cf5da095fb7e1487e70d993d700 | |
parent | eedb9a17c548ae006d8d3df67c8c771b9dcc8b42 (diff) |
begin rewrite of hs_create_table tests
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/hs_create_table.c | 1 | ||||
-rw-r--r-- | src/tests/hs_create_table_tests.c | 180 | ||||
-rw-r--r-- | src/tests/tests_main.c | 2 |
4 files changed, 77 insertions, 108 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index e5ed893..50f3ede 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,7 +30,7 @@ set(TEST_SOURCES ${TEST_ROOT}/hs_type_to_string_tests.c ${TEST_ROOT}/hs_parse_args_tests.c ${TEST_ROOT}/hs_parse_overloaded_tests.c - # ${TEST_ROOT}/hs_create_table_tests.c + ${TEST_ROOT}/hs_create_table_tests.c # ${TEST_ROOT}/hs_process_table_tests.c ${TEST_ROOT}/hs_throw_error_tests.c # ${TEST_ROOT}/hs_traceback_tests.c diff --git a/src/hs_create_table.c b/src/hs_create_table.c index a394f6e..c1770e9 100644 --- a/src/hs_create_table.c +++ b/src/hs_create_table.c @@ -7,4 +7,5 @@ int hs_create_table_(lua_State *L, L=L; n_elements = n_elements; elements = elements; + return 0; } diff --git a/src/tests/hs_create_table_tests.c b/src/tests/hs_create_table_tests.c index 460832f..e2c8ca8 100644 --- a/src/tests/hs_create_table_tests.c +++ b/src/tests/hs_create_table_tests.c @@ -11,120 +11,88 @@ static int testfunc(lua_State *L) { return 0; } TEST(table_correct_index) { - int index = hs_create_table(L, HS_END); + int index = hs_create_table(L); 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", const 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) +#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) + +static inline compare_ptrs(lua_State *L, int index0, int index1) { - 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; + return lua_topointer(L, index0) == lua_topointer(L, index1); } - -TEST(table_nesting) +#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 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; + 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(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 @@ -136,6 +104,6 @@ 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); + mu_run_test("create table with boolean keys and boolean values", + create_table_bool_bool); } diff --git a/src/tests/tests_main.c b/src/tests/tests_main.c index 43a5dc9..bef18b5 100644 --- a/src/tests/tests_main.c +++ b/src/tests/tests_main.c @@ -18,7 +18,7 @@ int main() mu_run_suite(hs_type_to_string_tests); mu_run_suite(hs_parse_args_tests); mu_run_suite(hs_parse_overloaded_tests); - //mu_run_suite(hs_create_table_tests); + mu_run_suite(hs_create_table_tests); //mu_run_suite(hs_create_enum_tests); //mu_run_suite(hs_process_table_tests); mu_run_suite(hs_throw_error_tests); |