diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/honeysuckle.h | 37 | ||||
-rw-r--r-- | src/hs_process_table.c | 15 | ||||
-rw-r--r-- | src/tests/hs_process_table_tests.c | 502 | ||||
-rw-r--r-- | src/tests/tests_main.c | 2 |
4 files changed, 52 insertions, 504 deletions
diff --git a/src/honeysuckle.h b/src/honeysuckle.h index 378e0a1..871384c 100644 --- a/src/honeysuckle.h +++ b/src/honeysuckle.h @@ -365,7 +365,12 @@ int hs_create_table_(lua_State *L, int n_elements, struct hs_tbl_entry *elements * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ -void hs_process_table(lua_State *L, int table_index, void *data, ...); +typedef void (*hs_pt_bool_callback)(bool, void *); +typedef void (*hs_pt_int_callback)(lua_Integer, void *); +typedef void (*hs_pt_num_callback)(lua_Number, void *); +typedef void (*hs_pt_str_callback)(const char *, void *); + + // default processors void hs_pt_set_boolean(bool value, void *data); void hs_pt_set_integer(lua_Integer value, void *data); @@ -373,6 +378,36 @@ void hs_pt_set_number(lua_Number value, void *data); void hs_pt_set_string(const char *value, void *data); +struct hs_table_processor { + const char *key; + hs_type type; + union { + hs_pt_bool_callback boolean; + hs_pt_int_callback integer; + hs_pt_num_callback number; + hs_pt_str_callback string; + } func; + void *data; +}; + +#define hs_process_bool(str, f, d) \ + { .key=(str), .type=HS_BOOL, .func.boolean=(f), .data=(d) } +#define hs_process_int(str, f, d) \ + { .key=(str), .type=HS_INT, .func.integer=(f), .data=(d) } +#define hs_process_num(str, f, d) \ + { .key=(str), .type=HS_NUM, .func.number=(f), .data=(d) } +#define hs_process_str(str, f, d) \ + { .key=(str), .type=HS_STR, .func.string=(f), .data=(d) } + +void hs_process_table_(lua_State *L, + int table_index, + int n_processors, + struct hs_table_processor *processors); + +#define hs_process_table(L, table_index, ...) \ + hs_process_table(L, table_index, VA_NARGS(__VA_ARGS__)/4, {__VA_ARGS__}) + + /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * hs_pushstring diff --git a/src/hs_process_table.c b/src/hs_process_table.c new file mode 100644 index 0000000..9d72253 --- /dev/null +++ b/src/hs_process_table.c @@ -0,0 +1,15 @@ +#include "honeysuckle.h" + +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_process_table_(lua_State *L, + int table_index, + int n_processors, + struct hs_table_processor *processors) +{ + +} diff --git a/src/tests/hs_process_table_tests.c b/src/tests/hs_process_table_tests.c index fc13b37..f2c6a31 100644 --- a/src/tests/hs_process_table_tests.c +++ b/src/tests/hs_process_table_tests.c @@ -1,484 +1,5 @@ #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); - - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - * - * tests for all 16 possible combinations of one table containing one - * boolean, integer, number, and string key each. - * - * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - */ - - -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; -} - - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - * - * tests for four keys of the same type - * - * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - */ - -TEST(process_four_bools) -{ - lua_createtable(L, 0, 4); - - lua_pushboolean(L, true); - lua_setfield(L, -2, "b1"); - - lua_pushboolean(L, false); - lua_setfield(L, -2, "b2"); - - lua_pushboolean(L, false); - lua_setfield(L, -2, "b3"); - - lua_pushboolean(L, true); - lua_setfield(L, -2, "b4"); - - bool b1 = false; - bool b2 = true; - bool b3 = true; - bool b4 = false; - - hs_process_table(L, -1, - "b1", HS_BOOL, hs_pt_set_boolean, &b1, - "b2", HS_BOOL, hs_pt_set_boolean, &b2, - "b3", HS_BOOL, hs_pt_set_boolean, &b3, - "b4", HS_BOOL, hs_pt_set_boolean, &b4, - HS_END); - - mu_assert("b1 was not correctly set!", b1 == true); - mu_assert("b2 was not correctly set!", b2 == false); - mu_assert("b3 was not correctly set!", b3 == false); - mu_assert("b4 was not correctly set!", b4 == true); - return 0; -} -TEST(process_four_ints) -{ - lua_createtable(L, 0, 4); - - lua_pushinteger(L, 2); - lua_setfield(L, -2, "j1"); - - lua_pushinteger(L, 1); - lua_setfield(L, -2, "j2"); - - lua_pushinteger(L, 3); - lua_setfield(L, -2, "j3"); - - lua_pushinteger(L, 4); - lua_setfield(L, -2, "j4"); - - lua_Integer j1 = 0; - lua_Integer j2 = 0; - lua_Integer j3 = 0; - lua_Integer j4 = 0; - - hs_process_table(L, -1, - "j1", HS_INT, hs_pt_set_integer, &j1, - "j2", HS_INT, hs_pt_set_integer, &j2, - "j3", HS_INT, hs_pt_set_integer, &j3, - "j4", HS_INT, hs_pt_set_integer, &j4, - HS_END); - - mu_assert("j1 was not correctly set!", j1 == 2); - mu_assert("j2 was not correctly set!", j2 == 1); - mu_assert("j3 was not correctly set!", j3 == 3); - mu_assert("j4 was not correctly set!", j4 == 4); - return 0; -} -TEST(process_four_numbers) -{ - lua_createtable(L, 0, 4); - - lua_pushnumber(L, 3.141); - lua_setfield(L, -2, "n1"); - - lua_pushnumber(L, 2.718); - lua_setfield(L, -2, "n2"); - - lua_pushnumber(L, 1.618); - lua_setfield(L, -2, "n3"); - - lua_pushnumber(L, 4.669); - lua_setfield(L, -2, "n4"); - - lua_Number n1 = 0; - lua_Number n2 = 0; - lua_Number n3 = 0; - lua_Number n4 = 0; - - hs_process_table(L, -1, - "n1", HS_NUM, hs_pt_set_number, &n1, - "n2", HS_NUM, hs_pt_set_number, &n2, - "n3", HS_NUM, hs_pt_set_number, &n3, - "n4", HS_NUM, hs_pt_set_number, &n4, - HS_END); - - mu_assert("n1 was not correctly set!", n1 == 3.141); - mu_assert("n2 was not correctly set!", n2 == 2.718); - mu_assert("n3 was not correctly set!", n3 == 1.618); - mu_assert("n4 was not correctly set!", n4 == 4.669); - return 0; -} -TEST(process_four_strings) -{ - lua_createtable(L, 0, 4); - - lua_pushstring(L, "When meditation is mastered, The mind is unwavering like the Flame of a lamp in a windless place." ); - lua_setfield(L, -2, "s1"); - - lua_pushstring(L, "In the still mind, In the depths of meditation, The Self reveals itself."); - lua_setfield(L, -2, "s2"); - - lua_pushstring(L, "Beholding the Self By means of the Self, An aspirant knows the Joy and peace of complete fulfillment."); - lua_setfield(L, -2, "s3"); - - lua_pushstring(L, "Having attained that Abiding joy beyond the senses, Revealed in the stilled mind, He never swerves from the eternal truth."); - lua_setfield(L, -2, "s4"); - - const char *s1 = ""; - const char *s2 = ""; - const char *s3 = ""; - const char *s4 = ""; - - hs_process_table(L, -1, - "s1", HS_STR, hs_pt_set_string, &s1, - "s2", HS_STR, hs_pt_set_string, &s2, - "s3", HS_STR, hs_pt_set_string, &s3, - "s4", HS_STR, hs_pt_set_string, &s4, - HS_END); - - mu_assert("s1 was not correctly set!", - strcmp(s1, "When meditation is mastered, The mind is unwavering like the Flame of a lamp in a windless place.") == 0); - mu_assert("s2 was not correctly set!", - strcmp(s2, "In the still mind, In the depths of meditation, The Self reveals itself.") == 0); - mu_assert("s3 was not correctly set!", - strcmp(s3, "Beholding the Self By means of the Self, An aspirant knows the Joy and peace of complete fulfillment.") == 0); - mu_assert("s4 was not correctly set!", - strcmp(s4, "Having attained that Abiding joy beyond the senses, Revealed in the stilled mind, He never swerves from the eternal truth.") == 0); - return 0; -} - - - /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * test suite @@ -489,27 +10,4 @@ TEST(process_four_strings) 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); - - mu_run_test("process four booleans", process_four_bools); - mu_run_test("process four integers", process_four_ints); - mu_run_test("process four numbers", process_four_numbers); - mu_run_test("process four strings", process_four_strings); } diff --git a/src/tests/tests_main.c b/src/tests/tests_main.c index bef18b5..26af7cc 100644 --- a/src/tests/tests_main.c +++ b/src/tests/tests_main.c @@ -20,7 +20,7 @@ int main() mu_run_suite(hs_parse_overloaded_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_process_table_tests); mu_run_suite(hs_throw_error_tests); mu_run_suite(hs_pushstring_tests); |