From fc93661728c2f6d63f448c45cf7c872ac592dd3c Mon Sep 17 00:00:00 2001 From: sanine-a Date: Sun, 4 Jul 2021 13:01:01 -0500 Subject: add tests for all sample table combinations for hs_process_table --- src/honeysuckle.c | 5 + src/honeysuckle.h | 7 + src/tests/hs_process_table_tests.c | 361 +++++++++++++++++++++++++++++++++---- 3 files changed, 342 insertions(+), 31 deletions(-) diff --git a/src/honeysuckle.c b/src/honeysuckle.c index 11f3f53..97c60c4 100644 --- a/src/honeysuckle.c +++ b/src/honeysuckle.c @@ -41,6 +41,11 @@ void hs_process_table(lua_State *L, int table_index, void *data, ...) data = data; } +void hs_pt_set_boolean(bool value, void *data) { value=value; data=data; } +void hs_pt_set_integer(lua_Integer value, void *data) { value=value; data=data; } +void hs_pt_set_number(lua_Number value, void *data) { value=value; data=data; } +void hs_pt_set_string(const char *value, void *data) { value=value; data=data; } + void hs_pushstring(lua_State *L, const char *format_string, ...) { diff --git a/src/honeysuckle.h b/src/honeysuckle.h index 43e5f51..1890c40 100644 --- a/src/honeysuckle.h +++ b/src/honeysuckle.h @@ -1,6 +1,8 @@ #ifndef HONEYSUCKLE_H #define HONEYSUCKLE_H +#include + #include #include #include @@ -29,6 +31,11 @@ int hs_create_table(lua_State *L, ...); int hs_create_enum(lua_State *L, ...); void hs_process_table(lua_State *L, int table_index, void *data, ...); +// default processors +void hs_pt_set_boolean(bool value, void *data); +void hs_pt_set_integer(lua_Integer value, void *data); +void hs_pt_set_number(lua_Number value, void *data); +void hs_pt_set_string(const char *value, void *data); void hs_throw_error(lua_State *L, const char *format_string, ...); int hs_traceback(lua_State *L); 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); } -- cgit v1.2.1