From e06165088c13cd31819a7b1bb1a2ba9e5aaf689e Mon Sep 17 00:00:00 2001 From: sanine-a Date: Sun, 23 May 2021 16:47:59 -0500 Subject: create initial files and write three hs_pushstring() tests --- src/honeysuckle.c | 7 +++++ src/honeysuckle.h | 27 +++++++++++++++++++ src/test.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 src/honeysuckle.c create mode 100644 src/honeysuckle.h create mode 100644 src/test.c (limited to 'src') diff --git a/src/honeysuckle.c b/src/honeysuckle.c new file mode 100644 index 0000000..423fa1b --- /dev/null +++ b/src/honeysuckle.c @@ -0,0 +1,7 @@ +#include "honeysuckle.h" + +void hs_pushstring(lua_State *L, const char *format_string, ...) +{ + lua_pushnumber(L, 0.4); + format_string = format_string; +} diff --git a/src/honeysuckle.h b/src/honeysuckle.h new file mode 100644 index 0000000..7538791 --- /dev/null +++ b/src/honeysuckle.h @@ -0,0 +1,27 @@ +#ifndef HONEYSUCKLE_H +#define HONEYSUCKLE_H + +#include +#include +#include + + +#define HS_END 0 + +/* type constants */ +#define HS_BOOL 1 +#define HS_INT 2 +#define HS_NUM 3 +#define HS_STR 4 +#define HS_TBL 5 +#define HS_FUNC 6 +#define HS_CFUNC 7 +#define HS_USER 8 +#define HS_LIGHT 9 +#define HS_NIL 10 +#define HS_ANY 11 + + +void hs_pushstring(lua_State *L, const char *format_string, ...); + +#endif diff --git a/src/test.c b/src/test.c new file mode 100644 index 0000000..5275d4d --- /dev/null +++ b/src/test.c @@ -0,0 +1,80 @@ +#include +#include + +#include +#include +#include + +#include "honeysuckle.h" + +/* minunit testing macros modified from those at www.jera.com/techinfo/jtns/jtn002.html */ +#define mu_assert(message, test) do { if (!(test)) return message; } while (0) +#define mu_run_test(name, test) do { \ + lua_State *L = luaL_newstate(); \ + luaL_openlibs(L); \ + char *message = test(L); \ + lua_close(L); \ + tests_run++; \ + if (message) { \ + printf("test '%s' failed: %s\n", name, message); \ + tests_failed++; \ + } \ + } while (0) + +int tests_run = 0; +int tests_failed = 0; + + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * + * tests for hs_pushstring + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ + +static char* push_noformat(lua_State *L) +{ + hs_pushstring(L, "a string"); + mu_assert("no string at top of stack!", lua_isstring(L, -1)); + const char *string = lua_tostring(L, -1); + mu_assert("string != 'a string'", strcmp(string, "a string") == 0); + return 0; +} + +static char* push_formatint(lua_State *L) +{ + hs_pushstring(L, "%d is 5", 5); + mu_assert("no string at top of stack!", lua_isstring(L, -1)); + const char *string = lua_tostring(L, -1); + mu_assert("string != '5 is 5'", strcmp(string, "5 is 5") == 0); + return 0; +} + +static char* push_formatstring(lua_State *L) +{ + hs_pushstring(L, "%s is 'hello'", "hello"); + mu_assert("no string at top of stack!", lua_isstring(L, -1)); + const char *string = lua_tostring(L, -1); + mu_assert("string != 'hello is 'hello''", + strcmp(string, "hello is 'hello'") == 0); + return 0; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * + * RUN TESTS + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ + +int main() +{ + printf("================ start tests ================\n\n"); + printf("running hs_pushstring() tests...\n"); + mu_run_test("hs_pushstring (no printf formatting)", push_noformat); + mu_run_test("hs_pushstring (integer formatting)", push_formatint); + mu_run_test("hs_pushstring (string formatting)", push_formatstring); + printf("\n=============== tests finished ===============\n\n"); + printf("ran %d tests, %d failed\n", tests_run, tests_failed); + return 0; +} -- cgit v1.2.1