#include "hs_tests.h" #define process() \ bool boolean = false; \ int integer = 0; \ float number = 0; \ const char *string = ""; \ hs_process_table(L, -1, \ "boolean", HS_BOOL, hs_pt_set_boolean, &boolean, \ "integer", HS_INT, hs_pt_set_integer, &integer, \ "number", HS_NUM, hs_pt_set_number, &number, \ "string", HS_STR, hs_pt_set_string, &string, \ HS_END); #define test_set_boolean() do { mu_assert("failed to set boolean!", boolean == true); } while(0); #define test_noset_boolean() do { mu_assert("incorrectly set boolean!", boolean == false); } while(0); #define test_set_integer() do { mu_assert("failed to set integer!", integer == 14); } while(0); #define test_noset_integer() do { mu_assert("incorrectly set integer!", integer == 0); } while(0); #define test_set_number() do { mu_assert("failed to set number!", number == 44.66); } while(0); #define test_noset_number() do { mu_assert("incorrectly set number!", number == 0); } while(0); #define test_set_string() \ do { mu_assert("failed to set string!", strcmp(string, "c: c: c:") == 0); } while(0); #define test_noset_string() \ do { mu_assert("incorrectly set string!", strcmp(string, "") == 0); } while(0); TEST(process_none) { lua_createtable(L, 0, 0); process(); test_noset_boolean(); test_noset_integer(); test_noset_number(); test_noset_string(); return 0; } TEST(process_bool) { lua_createtable(L, 0, 0); lua_pushboolean(L, true); lua_setfield(L, -2, "boolean"); process(); test_set_boolean(); test_noset_integer(); test_noset_number(); test_noset_string(); return 0; } TEST(process_int) { lua_createtable(L, 0, 0); lua_pushinteger(L, 14); lua_setfield(L, -2, "integer"); process(); test_noset_boolean(); test_set_integer(); test_noset_number(); test_noset_string(); return 0; } TEST(process_bool_int) { lua_createtable(L, 0, 0); lua_pushboolean(L, true); lua_setfield(L, -2, "boolean"); lua_pushinteger(L, 14); lua_setfield(L, -2, "integer"); process(); test_set_boolean(); test_set_integer(); test_noset_number(); test_noset_string(); return 0; } TEST(process_number) { lua_createtable(L, 0, 0); lua_pushnumber(L, 44.66); lua_setfield(L, -2, "number"); process(); test_noset_boolean(); test_noset_integer(); test_set_number(); test_noset_string(); return 0; } TEST(process_bool_number) { lua_createtable(L, 0, 0); lua_pushboolean(L, true); lua_setfield(L, -2, "boolean"); lua_pushnumber(L, 44.66); lua_setfield(L, -2, "number"); process(); test_set_boolean(); test_noset_integer(); test_set_number(); test_noset_string(); return 0; } TEST(process_int_number) { lua_createtable(L, 0, 0); lua_pushinteger(L, 14); lua_setfield(L, -2, "integer"); lua_pushnumber(L, 44.66); lua_setfield(L, -2, "number"); process(); test_noset_boolean(); test_set_integer(); test_set_number(); test_noset_string(); return 0; } TEST(process_bool_int_number) { lua_createtable(L, 0, 0); lua_pushboolean(L, true); lua_setfield(L, -2, "boolean"); lua_pushinteger(L, 14); lua_setfield(L, -2, "integer"); lua_pushnumber(L, 44.66); lua_setfield(L, -2, "number"); process(); test_set_boolean(); test_set_integer(); test_set_number(); test_noset_string(); return 0; } TEST(process_string) { lua_createtable(L, 0, 0); lua_pushstring(L, "c: c: c:"); lua_setfield(L, -2, "string"); process(); test_noset_boolean(); test_noset_integer(); test_noset_number(); test_set_string(); return 0; } TEST(process_bool_string) { lua_createtable(L, 0, 0); lua_pushboolean(L, true); lua_setfield(L, -2, "boolean"); lua_pushstring(L, "c: c: c:"); lua_setfield(L, -2, "string"); process(); test_set_boolean(); test_noset_integer(); test_noset_number(); test_set_string(); return 0; } TEST(process_int_string) { lua_createtable(L, 0, 0); lua_pushinteger(L, 14); lua_setfield(L, -2, "integer"); lua_pushstring(L, "c: c: c:"); lua_setfield(L, -2, "string"); process(); test_noset_boolean(); test_set_integer(); test_noset_number(); test_set_string(); return 0; } TEST(process_bool_int_string) { lua_createtable(L, 0, 0); lua_pushboolean(L, true); lua_setfield(L, -2, "boolean"); lua_pushinteger(L, 14); lua_setfield(L, -2, "integer"); lua_pushstring(L, "c: c: c:"); lua_setfield(L, -2, "string"); process(); test_set_boolean(); test_set_integer(); test_noset_number(); test_set_string(); return 0; } TEST(process_number_string) { lua_createtable(L, 0, 0); lua_pushnumber(L, 44.66); lua_setfield(L, -2, "number"); lua_pushstring(L, "c: c: c:"); lua_setfield(L, -2, "string"); process(); test_noset_boolean(); test_noset_integer(); test_set_number(); test_set_string(); return 0; } TEST(process_bool_number_string) { lua_createtable(L, 0, 0); lua_pushboolean(L, true); lua_setfield(L, -2, "boolean"); lua_pushnumber(L, 44.66); lua_setfield(L, -2, "number"); lua_pushstring(L, "c: c: c:"); lua_setfield(L, -2, "string"); process(); test_set_boolean(); test_noset_integer(); test_set_number(); test_set_string(); return 0; } TEST(process_int_number_string) { lua_createtable(L, 0, 0); lua_pushinteger(L, 14); lua_setfield(L, -2, "integer"); lua_pushnumber(L, 44.66); lua_setfield(L, -2, "number"); lua_pushstring(L, "c: c: c:"); lua_setfield(L, -2, "string"); process(); test_noset_boolean(); test_set_integer(); test_set_number(); test_set_string(); return 0; } TEST(process_all) { lua_createtable(L, 0, 0); lua_pushboolean(L, true); lua_setfield(L, -2, "boolean"); lua_pushinteger(L, 14); lua_setfield(L, -2, "integer"); lua_pushnumber(L, 44.66); lua_setfield(L, -2, "number"); lua_pushstring(L, "c: c: c:"); lua_setfield(L, -2, "string"); process(); test_set_boolean(); test_set_integer(); test_set_number(); test_set_string(); return 0; } /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * test suite * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ void hs_process_table_tests() { printf("running hs_process_table() tests...\n"); mu_run_test("process empty table", process_none); mu_run_test("process boolean only", process_bool); mu_run_test("process integer only", process_int); mu_run_test("process boolean and integer", process_bool_int); mu_run_test("process number only", process_number); mu_run_test("process boolean and number", process_bool_number); mu_run_test("process integer and number", process_int_number); mu_run_test("process boolean, integer, and number", process_bool_int_number); mu_run_test("process string only", process_string); mu_run_test("process boolean and string", process_bool_string); mu_run_test("process integer and string", process_int_string); mu_run_test("process boolean, integer, and string", process_bool_int_string); mu_run_test("process number and string", process_number_string); mu_run_test("process boolean, number, and string", process_bool_number_string); mu_run_test("process integer, number, and string", process_int_number_string); mu_run_test("process basic types", process_all); }