From 253f1d1ca8b4b81f206e4aeb20afe440a6dae8be Mon Sep 17 00:00:00 2001 From: sanine Date: Thu, 18 Aug 2022 22:03:13 -0500 Subject: add glfwInit and glfwTerminate bindings --- src/gl/window.test.c | 446 +++++++-------------------------------------------- 1 file changed, 62 insertions(+), 384 deletions(-) (limited to 'src/gl/window.test.c') diff --git a/src/gl/window.test.c b/src/gl/window.test.c index 6b8e091..b4fb5d4 100644 --- a/src/gl/window.test.c +++ b/src/gl/window.test.c @@ -4,433 +4,111 @@ #include #include "test/honey-test.h" -int mock_glfwInit(); -int mock_hs_throw_error(lua_State *L, const char *format_string, ...); -#define glfwInit mock_glfwInit -#define hs_throw_error mock_hs_throw_error + +int mock_glfwInit_(void); +int mock_hs_throw_error_(lua_State *L, const char *str); +void mock_glfwTerminate_(); + +#define glfwInit mock_glfwInit_ +#define hs_throw_error mock_hs_throw_error_ +#define glfwTerminate mock_glfwTerminate_ #include "gl/window.c" -#undef glfwInit +#undef glfwTerminate #undef hs_throw_error +#undef glfwInit -lily_mock_t *mock_glfwInit_data = NULL; -int mock_glfwInit() +lily_mock_t *mock_glfwInit = NULL; +int mock_glfwInit_() { - lily_mock_call(mock_glfwInit_data, NULL); + struct lily_mock_arg_t args[] = {}; + lily_mock_store_call(mock_glfwInit, args); int result; - mock_dequeue(mock_glfwInit, int, &result); + lily_get_value(mock_glfwInit, int, &result); return result; } -lily_mock_t *mock_hs_throw_error_data = NULL; -int mock_hs_throw_error(lua_State *L, const char *format_string, ...) -{ - /* to avoid basically just re-implementing printf parsing here, - i am limiting this function to be able to receive strings only */ - - /* count format specifiers */ - char *ptr = strchr(format_string, '%'); - int n_args = 0; - while (ptr != NULL) { - n_args += 1; - ptr = strchr(ptr+1, '%'); - } - /* store arguments */ +lily_mock_t *mock_hs_throw_error = NULL; +int mock_hs_throw_error_(lua_State *L, const char *str) +{ struct lily_mock_arg_t args[] = { - { sizeof(const char*), &format_string }, - { sizeof(int), &n_args }, + { sizeof(const char *), &str } }; - lily_mock_call(mock_hs_throw_error_data, args); + lily_mock_store_call(mock_hs_throw_error, args); - /* store format arguments */ - va_list vl; - va_start(vl, format_string); - for (int i=0; in_calls, 1); - lily_assert_int_equal(mock_hs_throw_error_data->n_calls, 0); -} - - -void gl_init_fail_glfwInit() -{ - USE_MOCK(mock_glfwInit); - USE_MOCK(mock_hs_throw_error); - - /* queue failure */ - mock_enqueue(mock_glfwInit, int, GLFW_FALSE); - gl_init(NULL); - - lily_assert_int_equal(mock_glfwInit_data->n_calls, 1); - lily_assert_int_equal(mock_hs_throw_error_data->n_calls, 1); - - const char *fmt; int argc; - struct lily_mock_arg_t args[] = { - { sizeof(const char*), &fmt }, - { sizeof(int), &argc }, - }; - lily_get_call(mock_hs_throw_error_data, args, 0); + lua_State *L = luaL_newstate(); + lily_store_value(mock_glfwInit, int, 1); + lua_pushcfunction(L, gl_init); + int err = lua_pcall(L, 0, 0, 0); + lua_close(L); - lily_assert_string_equal((char*) fmt, "failed to initialize GLFW"); + lily_assert_int_equal(err, 0); + lily_assert_int_equal(mock_glfwInit->n_calls, 1); + lily_assert_int_equal(mock_hs_throw_error->n_calls, 0); } -int get_int(lua_State *L, int table_index, const char *key) +void gl_init_fails() { - lua_getfield(L, table_index, key); - lily_assert_msg(lua_isnumber(L, -1), LILY_LOCATION, "key %s is not a number", key); - int n = lua_tointeger(L, -1); - lua_pop(L, 1); - return n; -} - -#define CHECK_VALUE(str, num) \ - do { \ - int value = get_int(L, 1, str); \ - lily_assert_int_equal(value, num); \ - } while(0) + lily_mock_use(&mock_glfwInit); + lily_mock_use(&mock_hs_throw_error); -void glfw_window_hints_table() -{ lua_State *L = luaL_newstate(); - - lily_assert_int_equal(lua_gettop(L), 0); - create_glfw_window_hints_table(L); - lily_assert_int_equal(lua_gettop(L), 2); - lily_assert_true(lua_istable(L, 1)); - - /* window hints */ - lily_assert_int_equal( - get_int(L, 1, "resizable"), - GLFW_RESIZABLE - ); - - lily_assert_int_equal( - get_int(L, 1, "visible"), - GLFW_VISIBLE - ); - - lily_assert_int_equal( - get_int(L, 1, "decorated"), - GLFW_DECORATED - ); - - lily_assert_int_equal( - get_int(L, 1, "focused"), - GLFW_FOCUSED - ); - - lily_assert_int_equal( - get_int(L, 1, "autoIconify"), - GLFW_AUTO_ICONIFY - ); - - lily_assert_int_equal( - get_int(L, 1, "floating"), - GLFW_FLOATING - ); - - lily_assert_int_equal( - get_int(L, 1, "maximized"), - GLFW_MAXIMIZED - ); - - lily_assert_int_equal( - get_int(L, 1, "centerCursor"), - GLFW_CENTER_CURSOR - ); - - lily_assert_int_equal( - get_int(L, 1, "transparentFramebuffer"), - GLFW_TRANSPARENT_FRAMEBUFFER - ); - - lily_assert_int_equal( - get_int(L, 1, "focusOnShow"), - GLFW_FOCUS_ON_SHOW - ); - - lily_assert_int_equal( - get_int(L, 1, "scaleToMonitor"), - GLFW_SCALE_TO_MONITOR - ); - - - /* framebuffer hints */ - /* (don't expose accumulation or auxiliary buffer hints) */ - lily_assert_int_equal( - get_int(L, 1, "redBits"), - GLFW_RED_BITS - ); - - lily_assert_int_equal( - get_int(L, 1, "greenBits"), - GLFW_GREEN_BITS - ); - - lily_assert_int_equal( - get_int(L, 1, "blueBits"), - GLFW_BLUE_BITS - ); - - lily_assert_int_equal( - get_int(L, 1, "alphaBits"), - GLFW_ALPHA_BITS - ); - - lily_assert_int_equal( - get_int(L, 1, "depthBits"), - GLFW_DEPTH_BITS - ); - - lily_assert_int_equal( - get_int(L, 1, "stereoscopic"), - GLFW_STEREO - ); - - lily_assert_int_equal( - get_int(L, 1, "samples"), - GLFW_SAMPLES - ); - - lily_assert_int_equal( - get_int(L, 1, "srgbCapable"), - GLFW_SRGB_CAPABLE - ); - - lily_assert_int_equal( - get_int(L, 1, "doubleBuffer"), - GLFW_DOUBLEBUFFER - ); - - - /* monitor & context hints */ - lily_assert_int_equal( - get_int(L, 1, "refreshRate"), - GLFW_REFRESH_RATE - ); - - lily_assert_int_equal( - get_int(L, 1, "clientApi"), - GLFW_CLIENT_API - ); - - lily_assert_int_equal( - get_int(L, 1, "contextCreationApi"), - GLFW_CONTEXT_CREATION_API - ); - - lily_assert_int_equal( - get_int(L, 1, "contextVersionMajor"), - GLFW_CONTEXT_VERSION_MAJOR - ); - - lily_assert_int_equal( - get_int(L, 1, "contextVersionMinor"), - GLFW_CONTEXT_VERSION_MINOR - ); - - lily_assert_int_equal( - get_int(L, 1, "forwardCompatible"), - GLFW_OPENGL_FORWARD_COMPAT - ); - - lily_assert_int_equal( - get_int(L, 1, "debugContext"), - GLFW_OPENGL_DEBUG_CONTEXT - ); - - lily_assert_int_equal( - get_int(L, 1, "profile"), - GLFW_OPENGL_PROFILE - ); - - lily_assert_int_equal( - get_int(L, 1, "contextRobustness"), - GLFW_CONTEXT_ROBUSTNESS - ); - - lily_assert_int_equal( - get_int(L, 1, "contextReleaseBehavior"), - GLFW_CONTEXT_RELEASE_BEHAVIOR - ); - - lily_assert_int_equal( - get_int(L, 1, "noError"), - GLFW_CONTEXT_NO_ERROR - ); - - - /* special hint values */ - - lily_assert_int_equal( - get_int(L, 2, "dontCare"), - GLFW_DONT_CARE - ); - - /* client api */ - lily_assert_int_equal( - get_int(L, 2, "glApi"), - GLFW_OPENGL_API - ); - - lily_assert_int_equal( - get_int(L, 2, "glesApi"), - GLFW_OPENGL_ES_API - ); - - lily_assert_int_equal( - get_int(L, 2, "noApi"), - GLFW_NO_API - ); - - /* context api */ - lily_assert_int_equal( - get_int(L, 2, "nativeApi"), - GLFW_NATIVE_CONTEXT_API - ); - - lily_assert_int_equal( - get_int(L, 2, "eglApi"), - GLFW_EGL_CONTEXT_API - ); - - lily_assert_int_equal( - get_int(L, 2, "osMesaApi"), - GLFW_OSMESA_CONTEXT_API - ); - - /* robustness */ - lily_assert_int_equal( - get_int(L, 2, "noRobustness"), - GLFW_NO_ROBUSTNESS - ); - - lily_assert_int_equal( - get_int(L, 2, "noResetNotification"), - GLFW_NO_RESET_NOTIFICATION - ); - - lily_assert_int_equal( - get_int(L, 2, "loseContextOnReset"), - GLFW_LOSE_CONTEXT_ON_RESET - ); - - /* release */ - lily_assert_int_equal( - get_int(L, 2, "anyBehavior"), - GLFW_ANY_RELEASE_BEHAVIOR - ); - - lily_assert_int_equal( - get_int(L, 2, "flush"), - GLFW_RELEASE_BEHAVIOR_FLUSH - ); - - lily_assert_int_equal( - get_int(L, 2, "none"), - GLFW_RELEASE_BEHAVIOR_NONE - ); - - /* profile */ - lily_assert_int_equal( - get_int(L, 2, "anyProfile"), - GLFW_OPENGL_ANY_PROFILE - ); - - lily_assert_int_equal( - get_int(L, 2, "compatabilityProfile"), - GLFW_OPENGL_COMPAT_PROFILE - ); - - lily_assert_int_equal( - get_int(L, 2, "coreProfile"), - GLFW_OPENGL_CORE_PROFILE - ); - + lily_store_value(mock_glfwInit, int, 0); + lua_pushcfunction(L, gl_init); + int err = lua_pcall(L, 0, 0, 0); lua_close(L); -} - -void tointeger_parses_bool() -{ - USE_MOCK(mock_hs_throw_error); - lua_State *L = luaL_newstate(); - - lua_pushboolean(L, 0); - lily_assert_false(lua_toboolean(L, -1)); - lily_assert_int_equal(tointeger(L, -1), 0); - - lua_pushboolean(L, 1); - lily_assert_true(lua_toboolean(L, -1)); - lily_assert_int_equal(tointeger(L, -1), 1); - - lua_close(L); + lily_assert_int_equal(err, LUA_ERRRUN); + lily_assert_int_equal(mock_hs_throw_error->n_calls, 1); } -void tointeger_parses_int() +void gl_terminate_works() { - USE_MOCK(mock_hs_throw_error); - lua_State *L = luaL_newstate(); - - lua_pushinteger(L, 234); - lua_pushinteger(L, 55555); - - lily_assert_int_equal(tointeger(L, -2), 234); - lily_assert_int_equal(tointeger(L, -1), 55555); + lily_mock_use(&mock_glfwTerminate); + lua_State *L = luaL_newstate(); + lua_pushcfunction(L, gl_terminate); + int err = lua_pcall(L, 0, 0, 0); lua_close(L); + + lily_assert_int_equal(err, 0); + lily_assert_int_equal(mock_glfwTerminate->n_calls, 1); } -void tointeger_fails_other() +void suite_window() { - USE_MOCK(mock_hs_throw_error); - lua_State *L = luaL_newstate(); + lily_run_test(gl_init_succeeds); + lily_run_test(gl_init_fails); + lily_run_test(gl_terminate_works); - lua_pushstring(L, "hey there babe"); - tointeger(L, -1); - lily_assert_int_equal(mock_hs_throw_error_data->n_calls, 1); + lily_mock_destroy(mock_glfwInit); + lily_mock_destroy(mock_hs_throw_error); + lily_mock_destroy(mock_glfwTerminate); } -- cgit v1.2.1 From 275536b36657744d802866c060654e2b5cd5a5f8 Mon Sep 17 00:00:00 2001 From: sanine Date: Thu, 18 Aug 2022 22:46:40 -0500 Subject: implement working windows --- src/gl/window.test.c | 114 --------------------------------------------------- 1 file changed, 114 deletions(-) delete mode 100644 src/gl/window.test.c (limited to 'src/gl/window.test.c') diff --git a/src/gl/window.test.c b/src/gl/window.test.c deleted file mode 100644 index b4fb5d4..0000000 --- a/src/gl/window.test.c +++ /dev/null @@ -1,114 +0,0 @@ -#include -#include -#include -#include -#include "test/honey-test.h" - - -int mock_glfwInit_(void); -int mock_hs_throw_error_(lua_State *L, const char *str); -void mock_glfwTerminate_(); - -#define glfwInit mock_glfwInit_ -#define hs_throw_error mock_hs_throw_error_ -#define glfwTerminate mock_glfwTerminate_ -#include "gl/window.c" -#undef glfwTerminate -#undef hs_throw_error -#undef glfwInit - - -lily_mock_t *mock_glfwInit = NULL; -int mock_glfwInit_() -{ - struct lily_mock_arg_t args[] = {}; - lily_mock_store_call(mock_glfwInit, args); - - int result; - lily_get_value(mock_glfwInit, int, &result); - return result; -} - - -lily_mock_t *mock_hs_throw_error = NULL; -int mock_hs_throw_error_(lua_State *L, const char *str) -{ - struct lily_mock_arg_t args[] = { - { sizeof(const char *), &str } - }; - lily_mock_store_call(mock_hs_throw_error, args); - - lua_pushstring(L, "some error"); - lua_error(L); - - return 0; -} - - -lily_mock_t *mock_glfwTerminate = NULL; -void mock_glfwTerminate_() -{ - struct lily_mock_arg_t args[] = {}; - lily_mock_store_call(mock_glfwTerminate, args); -} - - -/* ~~~~~~~~ suite ~~~~~~~~ */ - -void gl_init_succeeds() -{ - lily_mock_use(&mock_glfwInit); - lily_mock_use(&mock_hs_throw_error); - - lua_State *L = luaL_newstate(); - lily_store_value(mock_glfwInit, int, 1); - lua_pushcfunction(L, gl_init); - int err = lua_pcall(L, 0, 0, 0); - lua_close(L); - - lily_assert_int_equal(err, 0); - lily_assert_int_equal(mock_glfwInit->n_calls, 1); - lily_assert_int_equal(mock_hs_throw_error->n_calls, 0); -} - - -void gl_init_fails() -{ - lily_mock_use(&mock_glfwInit); - lily_mock_use(&mock_hs_throw_error); - - lua_State *L = luaL_newstate(); - lily_store_value(mock_glfwInit, int, 0); - lua_pushcfunction(L, gl_init); - int err = lua_pcall(L, 0, 0, 0); - lua_close(L); - - lily_assert_int_equal(err, LUA_ERRRUN); - lily_assert_int_equal(mock_hs_throw_error->n_calls, 1); -} - - -void gl_terminate_works() -{ - lily_mock_use(&mock_glfwTerminate); - - lua_State *L = luaL_newstate(); - lua_pushcfunction(L, gl_terminate); - int err = lua_pcall(L, 0, 0, 0); - lua_close(L); - - lily_assert_int_equal(err, 0); - lily_assert_int_equal(mock_glfwTerminate->n_calls, 1); -} - - -void suite_window() -{ - lily_run_test(gl_init_succeeds); - lily_run_test(gl_init_fails); - lily_run_test(gl_terminate_works); - - lily_mock_destroy(mock_glfwInit); - lily_mock_destroy(mock_hs_throw_error); - lily_mock_destroy(mock_glfwTerminate); -} -- cgit v1.2.1 From 8b9b325b092b7157996cfbcdd7ce38f9e876fff8 Mon Sep 17 00:00:00 2001 From: sanine Date: Sat, 20 Aug 2022 15:37:04 -0500 Subject: fix bug in gl.bufferData and add error checking --- src/gl/window.test.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/gl/window.test.c (limited to 'src/gl/window.test.c') diff --git a/src/gl/window.test.c b/src/gl/window.test.c new file mode 100644 index 0000000..505c876 --- /dev/null +++ b/src/gl/window.test.c @@ -0,0 +1,24 @@ +#include +#include +#include +#include "test/honey-test.h" + +#include "window.c" + + +void create_window_data_works() +{ + lua_State *L = luaL_newstate(); + struct window_data *wdata = create_window_data(L); + + lily_assert_ptr_equal(L, wdata->L); + lily_assert_int_equal(wdata->framebuffer_size_callback, LUA_NOREF); + lua_close(L); + free(wdata); +} + + +void suite_window() +{ + lily_run_test(create_window_data_works); +} -- cgit v1.2.1