From db81b925d776103326128bf629cbdda576a223e7 Mon Sep 17 00:00:00 2001 From: sanine Date: Sat, 16 Apr 2022 11:55:09 -0500 Subject: move 3rd-party librarys into libs/ and add built-in honeysuckle --- libs/honeysuckle/src/tests/colors.h | 22 + libs/honeysuckle/src/tests/hs_create_table_tests.c | 2239 ++++++++++++++++++++ libs/honeysuckle/src/tests/hs_parse_args_tests.c | 603 ++++++ .../src/tests/hs_parse_overloaded_tests.c | 188 ++ .../honeysuckle/src/tests/hs_process_table_tests.c | 511 +++++ libs/honeysuckle/src/tests/hs_pushstring_tests.c | 52 + libs/honeysuckle/src/tests/hs_tests.h | 63 + libs/honeysuckle/src/tests/hs_throw_error_tests.c | 57 + .../src/tests/hs_type_to_string_tests.c | 102 + libs/honeysuckle/src/tests/tests_main.c | 33 + 10 files changed, 3870 insertions(+) create mode 100644 libs/honeysuckle/src/tests/colors.h create mode 100644 libs/honeysuckle/src/tests/hs_create_table_tests.c create mode 100644 libs/honeysuckle/src/tests/hs_parse_args_tests.c create mode 100644 libs/honeysuckle/src/tests/hs_parse_overloaded_tests.c create mode 100644 libs/honeysuckle/src/tests/hs_process_table_tests.c create mode 100644 libs/honeysuckle/src/tests/hs_pushstring_tests.c create mode 100644 libs/honeysuckle/src/tests/hs_tests.h create mode 100644 libs/honeysuckle/src/tests/hs_throw_error_tests.c create mode 100644 libs/honeysuckle/src/tests/hs_type_to_string_tests.c create mode 100644 libs/honeysuckle/src/tests/tests_main.c (limited to 'libs/honeysuckle/src/tests') diff --git a/libs/honeysuckle/src/tests/colors.h b/libs/honeysuckle/src/tests/colors.h new file mode 100644 index 0000000..21451de --- /dev/null +++ b/libs/honeysuckle/src/tests/colors.h @@ -0,0 +1,22 @@ +#ifndef COLORS_H +#define COLORS_H + +#define RESET "\033[0m" +#define BLACK "\033[30m" /* Black */ +#define RED "\033[31m" /* Red */ +#define GREEN "\033[32m" /* Green */ +#define YELLOW "\033[33m" /* Yellow */ +#define BLUE "\033[34m" /* Blue */ +#define MAGENTA "\033[35m" /* Magenta */ +#define CYAN "\033[36m" /* Cyan */ +#define WHITE "\033[37m" /* White */ +#define BOLDBLACK "\033[1m\033[30m" /* Bold Black */ +#define BOLDRED "\033[1m\033[31m" /* Bold Red */ +#define BOLDGREEN "\033[1m\033[32m" /* Bold Green */ +#define BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */ +#define BOLDBLUE "\033[1m\033[34m" /* Bold Blue */ +#define BOLDMAGENTA "\033[1m\033[35m" /* Bold Magenta */ +#define BOLDCYAN "\033[1m\033[36m" /* Bold Cyan */ +#define BOLDWHITE "\033[1m\033[37m" /* Bold White */ + +#endif diff --git a/libs/honeysuckle/src/tests/hs_create_table_tests.c b/libs/honeysuckle/src/tests/hs_create_table_tests.c new file mode 100644 index 0000000..1adc947 --- /dev/null +++ b/libs/honeysuckle/src/tests/hs_create_table_tests.c @@ -0,0 +1,2239 @@ +#include "hs_tests.h" + +static int testfunc1(lua_State *L) { return 0; } +static int testfunc2(lua_State *L) { return 0; } + +static void print_stack(lua_State *L) +{ + printf("stack: %d [", lua_gettop(L)); + for (int i=0; i +#include +#include + +#include +#include +#include + +#include "../honeysuckle.h" +#include "colors.h" + +#define STR_IMPL(x) #x +#define STR(x) STR_IMPL(x) + +#define mu_indent " " + +/* minunit testing macros modified from those at + www.jera.com/techinfo/jtns/jtn002.html */ +#define mu_assert(message, test) do { \ + if (!(test)) return message \ + "\n" mu_indent MAGENTA " [" __FILE__ ":" STR(__LINE__) "]" RESET; \ + } while (0) +#define mu_assert_equal(a, b) mu_assert("'" #a "' is not equal to '" #b "'", (a) == (b)) +#define mu_assert_not_equal(a, b) mu_assert("'" #a "' is equal to '" #b "'", (a) != (b)) +#define mu_assert_str_equal(a, b) \ + mu_assert("'" #a "' is not equal to '" #b "'", strcmp((a), (b)) == 0) +#define mu_assert_str_not_equal(a, b) \ + mu_assert("'" #a "' is equal to '" #b "'", strcmp((a), (b)) != 0) + +#define mu_run_test(name, test) do { \ + lua_State *L = luaL_newstate(); \ + luaL_openlibs(L); \ + const char *message = test(L); \ + lua_close(L); \ + tests_run++; \ + if (message) { \ + printf(RED mu_indent "test '%s' failed:" RESET " %s\n", name, message); \ + tests_failed++; \ + } \ + } while (0) +#define TEST(name) static const char* name(lua_State *L) +#define mu_run_suite(suite) do { \ + tests_run_old = tests_run; \ + suite(); \ + printf(mu_indent "ran %d tests\n", tests_run - tests_run_old); \ + } while (0) + +extern int tests_run, tests_run_old, tests_failed; + +void hs_type_to_string_tests(); +void hs_parse_args_tests(); +void hs_parse_overloaded_tests(); +void hs_create_table_tests(); +void hs_create_enum_tests(); +void hs_process_table_tests(); +void hs_throw_error_tests(); +void hs_traceback_tests(); +void hs_call_tests(); +void hs_call_args_tests(); +void hs_pushstring_tests(); +void hs_rxx_tests(); diff --git a/libs/honeysuckle/src/tests/hs_throw_error_tests.c b/libs/honeysuckle/src/tests/hs_throw_error_tests.c new file mode 100644 index 0000000..7ae683c --- /dev/null +++ b/libs/honeysuckle/src/tests/hs_throw_error_tests.c @@ -0,0 +1,57 @@ +#include "hs_tests.h" + +char err_string[32] = ""; + +int set_err_string(lua_State *L) +{ + if (lua_isstring(L, -1)) + strcpy(err_string, lua_tostring(L, -1)); + return 0; +} + + +#define HS_ERROR_TEST(name, error_body, expectation) \ + int name ## _errfunc(lua_State *L) error_body; \ + TEST(name) { \ + lua_pushcfunction(L, set_err_string); \ + int pos = lua_gettop(L); \ + lua_pushcfunction(L, name ## _errfunc); \ + int result = lua_pcall(L, 0, 0, pos); \ + mu_assert_equal(result, LUA_ERRRUN); \ + mu_assert_str_not_equal(err_string, ""); \ + mu_assert_str_equal(err_string, expectation); \ + return 0; \ + } + + +HS_ERROR_TEST(hs_throw_error_constant, { + hs_throw_error(L, "a constant error"); + return 0; + }, "a constant error"); + + +HS_ERROR_TEST(hs_throw_error_format, { + hs_throw_error(L, "%s number %d", "error", 10); + return 0; + }, "error number 10"); + + +HS_ERROR_TEST(hs_throw_error_long, { + hs_throw_error(L, "%s is a %s is a %s", + "a very, very, very long string", + "a very, very, very long string", + "a very, very, very long string"); + return 0; + }, "a very, very, very long string is a a very, very, very long string is a a very, very, very long string"); + + +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +void hs_throw_error_tests() +{ + printf("running hs_throw_error() tests...\n"); + + mu_run_test("throw constant error string", hs_throw_error_constant); + mu_run_test("throw error with format string", hs_throw_error_format); + mu_run_test("throw error with very long string", hs_throw_error_long); +} diff --git a/libs/honeysuckle/src/tests/hs_type_to_string_tests.c b/libs/honeysuckle/src/tests/hs_type_to_string_tests.c new file mode 100644 index 0000000..3b158ab --- /dev/null +++ b/libs/honeysuckle/src/tests/hs_type_to_string_tests.c @@ -0,0 +1,102 @@ +#include "hs_tests.h" + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * + * tests for hs_type_to_string + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ + +TEST(hs_bool_to_string) +{ + mu_assert("HS_BOOL does not result in 'boolean'!", + strcmp(hs_type_to_string(HS_BOOL), "boolean") == 0); + return 0; +} + +TEST(hs_int_to_string) +{ + mu_assert("HS_INT does not result in 'integer'!", + strcmp(hs_type_to_string(HS_INT), "integer") == 0); + return 0; +} + +TEST(hs_num_to_string) +{ + mu_assert("HS_NUM does not result in 'number'!", + strcmp(hs_type_to_string(HS_NUM), "number") == 0); + return 0; +} + +TEST(hs_str_to_string) +{ + mu_assert("HS_STR does not result in 'string'!", + strcmp(hs_type_to_string(HS_STR), "string") == 0); + return 0; +} + +TEST(hs_tbl_to_string) +{ + mu_assert("HS_TBL does not result in 'table'!", + strcmp(hs_type_to_string(HS_TBL), "table") == 0); + return 0; +} + +TEST(hs_func_to_string) +{ + mu_assert("HS_FUNC does not result in 'function'!", + strcmp(hs_type_to_string(HS_FUNC), "function") == 0); + return 0; +} + +TEST(hs_cfunc_to_string) +{ + mu_assert("HS_CFUNC does not result in 'C function'!", + strcmp(hs_type_to_string(HS_CFUNC), "C function") == 0); + return 0; +} + +TEST(hs_user_to_string) +{ + mu_assert("HS_USER does not result in 'userdata'!", + strcmp(hs_type_to_string(HS_USER), "userdata") == 0); + return 0; +} + +TEST(hs_light_to_string) +{ + mu_assert("HS_LIGHT does not result in 'light userdata'!", + strcmp(hs_type_to_string(HS_LIGHT), "light userdata") == 0); + return 0; +} + +TEST(hs_nil_to_string) +{ + mu_assert("HS_NIL does not result in 'nil'!", + strcmp(hs_type_to_string(HS_NIL), "nil") == 0); + return 0; +} + +TEST(hs_any_to_string) +{ + mu_assert("HS_ANY does not result in 'any'!", + strcmp(hs_type_to_string(HS_ANY), "any") == 0); + return 0; +} + + +void hs_type_to_string_tests() +{ + printf("running hs_type_to_string() tests...\n"); + mu_run_test("bool to string", hs_bool_to_string); + mu_run_test("int to string", hs_int_to_string); + mu_run_test("num to string", hs_num_to_string); + mu_run_test("str to string", hs_str_to_string); + mu_run_test("tbl to string", hs_tbl_to_string); + mu_run_test("func to string", hs_func_to_string); + mu_run_test("cfunc to string", hs_cfunc_to_string); + mu_run_test("user to string", hs_user_to_string); + mu_run_test("light to string", hs_light_to_string); + mu_run_test("nil to string", hs_nil_to_string); + mu_run_test("any to string", hs_any_to_string); +} diff --git a/libs/honeysuckle/src/tests/tests_main.c b/libs/honeysuckle/src/tests/tests_main.c new file mode 100644 index 0000000..26af7cc --- /dev/null +++ b/libs/honeysuckle/src/tests/tests_main.c @@ -0,0 +1,33 @@ +#include "hs_tests.h" + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * + * RUN TESTS + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ + +int tests_run = 0; +int tests_run_old = 0; +int tests_failed = 0; + +int main() +{ + printf("================ start tests ================\n\n"); + + 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_enum_tests); + mu_run_suite(hs_process_table_tests); + mu_run_suite(hs_throw_error_tests); + + mu_run_suite(hs_pushstring_tests); + + printf("\n=============== tests finished ===============\n\n"); + + const char *color = tests_failed == 0 ? GREEN : RED; + printf("%sran %d tests, %d failed\n" RESET, color, tests_run, tests_failed); + return tests_failed; +} -- cgit v1.2.1