diff options
Diffstat (limited to 'src/tests/hs_process_table_tests.c')
-rw-r--r-- | src/tests/hs_process_table_tests.c | 361 |
1 files changed, 330 insertions, 31 deletions
diff --git a/src/tests/hs_process_table_tests.c b/src/tests/hs_process_table_tests.c index eedf600..bf6e424 100644 --- a/src/tests/hs_process_table_tests.c +++ b/src/tests/hs_process_table_tests.c @@ -1,65 +1,347 @@ #include "hs_tests.h" -struct process_types { - bool boolean; - lua_Integer integer; - lua_Number number; - const char *string; -}; +#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); -void set_boolean(bool value, void *data) +#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) { - struct process_types *d = data; - d->boolean = value; + lua_createtable(L, 0, 0); + + process(); + + test_noset_boolean(); + test_noset_integer(); + test_noset_number(); + test_noset_string(); + + return 0; } -void set_integer(lua_Integer value, void *data) + + +TEST(process_bool) { - struct process_types *d = data; - d->integer = value; + 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; } -void set_number(lua_Number value, void *data) + +TEST(process_int) { - struct process_types *d = data; - d->number = value; + 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; } -void set_string(const char *value, void *data) +TEST(process_bool_int) { - struct process_types *d = data; - d->string = value; + 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_basic_types) +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"); - struct process_types data = { false, 0, 0, "" }; + process(); + + test_set_boolean(); + test_noset_integer(); + test_noset_number(); + test_set_string(); - hs_process_table(L, -1, &data, - "boolean", HS_BOOL, set_boolean, - "integer", HS_INT, set_integer, - "number", HS_NUM, set_number, - "string", HS_STR, set_string, - HS_END); + 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"); - mu_assert("failed to set boolean!", data.boolean == true); - mu_assert("failed to set integer!", data.integer == 14); - mu_assert("failed to set number!", data.number == 44.66); - mu_assert("failed to set string!", strcmp(data.string, "c: c: c:") == 0); + 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; } @@ -73,5 +355,22 @@ TEST(process_basic_types) void hs_process_table_tests() { printf("running hs_process_table() tests...\n"); - mu_run_test("process basic types", process_basic_types); + + 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); } |