diff options
| author | sanine <sanine.not@pm.me> | 2022-08-18 22:03:13 -0500 | 
|---|---|---|
| committer | sanine <sanine.not@pm.me> | 2022-08-18 22:03:13 -0500 | 
| commit | 253f1d1ca8b4b81f206e4aeb20afe440a6dae8be (patch) | |
| tree | d4f7d2d470b956b73f61111f78e199d24e170654 | |
| parent | a1ac349c22f43ece1dcad7b21ed82dbb434cbf06 (diff) | |
add glfwInit and glfwTerminate bindings
| -rw-r--r-- | src/gl/window.c | 93 | ||||
| -rw-r--r-- | src/gl/window.test.c | 446 | ||||
| -rw-r--r-- | src/test/lily-test.c | 300 | ||||
| -rw-r--r-- | src/test/lily-test.h | 87 | 
4 files changed, 268 insertions, 658 deletions
| diff --git a/src/gl/window.c b/src/gl/window.c index e16b330..bc654f9 100644 --- a/src/gl/window.c +++ b/src/gl/window.c @@ -3,102 +3,17 @@  #include <lua.h>  #include <honeysuckle.h> -/* build a table of all possible window hints */ -void create_glfw_window_hints_table(lua_State *L) -{ -	/* hint keys */ -	hs_create_table(L, -		/* window hints */ -		hs_str_int("resizable", GLFW_RESIZABLE), -		hs_str_int("visible", GLFW_VISIBLE), -		hs_str_int("decorated", GLFW_DECORATED), -		hs_str_int("focused", GLFW_FOCUSED), -		hs_str_int("autoIconify", GLFW_AUTO_ICONIFY), -		hs_str_int("floating", GLFW_FLOATING), -		hs_str_int("maximized", GLFW_MAXIMIZED), -		hs_str_int("centerCursor", GLFW_CENTER_CURSOR), -		hs_str_int("transparentFramebuffer", GLFW_TRANSPARENT_FRAMEBUFFER), -		hs_str_int("focusOnShow", GLFW_FOCUS_ON_SHOW), -		hs_str_int("scaleToMonitor", GLFW_SCALE_TO_MONITOR), - -		/* framebuffer hints */ -		hs_str_int("redBits", GLFW_RED_BITS), -		hs_str_int("greenBits", GLFW_GREEN_BITS), -		hs_str_int("blueBits", GLFW_BLUE_BITS), -		hs_str_int("alphaBits", GLFW_ALPHA_BITS), -		hs_str_int("depthBits", GLFW_DEPTH_BITS), -		hs_str_int("stereoscopic", GLFW_STEREO), -		hs_str_int("samples", GLFW_SAMPLES), -		hs_str_int("srgbCapable", GLFW_SRGB_CAPABLE), -		hs_str_int("doubleBuffer", GLFW_DOUBLEBUFFER), - -		/* monitor & context hints */ -		hs_str_int("refreshRate", GLFW_REFRESH_RATE), -		hs_str_int("clientApi", GLFW_CLIENT_API), -		hs_str_int("contextCreationApi", GLFW_CONTEXT_CREATION_API), -		hs_str_int("contextVersionMajor", GLFW_CONTEXT_VERSION_MAJOR), -		hs_str_int("contextVersionMinor", GLFW_CONTEXT_VERSION_MINOR), -		hs_str_int("forwardCompatible", GLFW_OPENGL_FORWARD_COMPAT), -		hs_str_int("debugContext", GLFW_OPENGL_DEBUG_CONTEXT), -		hs_str_int("profile", GLFW_OPENGL_PROFILE), -		hs_str_int("contextRobustness", GLFW_CONTEXT_ROBUSTNESS), -		hs_str_int("contextReleaseBehavior", GLFW_CONTEXT_RELEASE_BEHAVIOR), -		hs_str_int("noError", GLFW_CONTEXT_NO_ERROR) -	); - -	/* special hint values */ -	hs_create_table(L, -		hs_str_int("dontCare", GLFW_DONT_CARE), - -		/* client api */ -		hs_str_int("glApi", GLFW_OPENGL_API), -		hs_str_int("glesApi", GLFW_OPENGL_ES_API), -		hs_str_int("noApi", GLFW_NO_API), - -		/* context api */ -		hs_str_int("nativeApi", GLFW_NATIVE_CONTEXT_API), -		hs_str_int("eglApi", GLFW_EGL_CONTEXT_API), -		hs_str_int("osMesaApi", GLFW_OSMESA_CONTEXT_API), - -		/* robustness */ -		hs_str_int("noRobustness", GLFW_NO_ROBUSTNESS), -		hs_str_int("noResetNotification", GLFW_NO_RESET_NOTIFICATION), -		hs_str_int("loseContextOnReset", GLFW_LOSE_CONTEXT_ON_RESET), - -		/* context release */ -		hs_str_int("anyBehavior", GLFW_ANY_RELEASE_BEHAVIOR), -		hs_str_int("flush", GLFW_RELEASE_BEHAVIOR_FLUSH), -		hs_str_int("none", GLFW_RELEASE_BEHAVIOR_NONE), - -		/* profile */ -		hs_str_int("anyProfile", GLFW_OPENGL_ANY_PROFILE), -		hs_str_int("compatabilityProfile", GLFW_OPENGL_COMPAT_PROFILE), -		hs_str_int("coreProfile", GLFW_OPENGL_CORE_PROFILE) -	); -} - -  int gl_init(lua_State *L)  { -	if (glfwInit() != GLFW_TRUE) { +	if (!glfwInit()) {  		hs_throw_error(L, "failed to initialize GLFW");  	}  	return 0;  } -lua_Integer tointeger(lua_State *L, int index) +int gl_terminate(lua_State *L)  { -	if (lua_isboolean(L, index)) { -		return lua_toboolean(L, index); -	} -	else if (lua_isnumber(L, index)) { -		return lua_tointeger(L, index); -	} -	else { -		hs_throw_error(L, -			"expected boolean or number; got %s instead", -			lua_typename(L, lua_type(L, index)) -		); -	} +	glfwTerminate(); +	return 0;  } 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 <honeysuckle.h>  #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; i<n_args; i++) { -		char *str = va_arg(vl, char*); -		mock_enqueue(mock_hs_throw_error, char*, str); -	} -} +	lua_pushstring(L, "some error"); +	lua_error(L); +	return 0; +} -/* ~~~~~~~~ TESTS ~~~~~~~~ */ -void gl_init_succeeds(); -void gl_init_fail_glfwInit(); -void glfw_window_hints_table(); -void tointeger_parses_bool(); -void tointeger_parses_int(); -void tointeger_fails_other(); -void suite_window() +lily_mock_t *mock_glfwTerminate = NULL; +void mock_glfwTerminate_()  { -	lily_run_test(gl_init_succeeds); -	lily_run_test(gl_init_fail_glfwInit); -	lily_run_test(glfw_window_hints_table); -	lily_run_test(tointeger_parses_bool); -	lily_run_test(tointeger_parses_int); -	lily_run_test(tointeger_fails_other); - -	CLEAN_MOCK(mock_glfwInit); -	CLEAN_MOCK(mock_hs_throw_error); +	struct lily_mock_arg_t args[] = {}; +	lily_mock_store_call(mock_glfwTerminate, args);  } -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - +/* ~~~~~~~~ suite ~~~~~~~~ */  void gl_init_succeeds()  { -	USE_MOCK(mock_glfwInit); -	USE_MOCK(mock_hs_throw_error); -	 -	/* queue success */ -	mock_enqueue(mock_glfwInit, int, GLFW_TRUE); -	gl_init(NULL); +	lily_mock_use(&mock_glfwInit); +	lily_mock_use(&mock_hs_throw_error); -	lily_assert_int_equal(mock_glfwInit_data->n_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);  } diff --git a/src/test/lily-test.c b/src/test/lily-test.c index 20728fe..2f43a28 100644 --- a/src/test/lily-test.c +++ b/src/test/lily-test.c @@ -54,207 +54,207 @@ struct lily_globals_t _lily_globals = { {0}, 0, NULL, "" };  /* run an individual test */  void _lily_run_test(const char *name, lily_test_t test)  { -   printf("  %s: ", name); +	printf("  %s: ", name); -   _lily_globals.error_msg = NULL; -   _lily_globals.error_location = ""; -   int val = setjmp(_lily_globals.env); +	_lily_globals.error_msg = NULL; +	_lily_globals.error_location = ""; +	int val = setjmp(_lily_globals.env); -   if (val) { +	if (val) {  	/* test failed */ -      printf("FAIL - %s (%s)\n", -	     _lily_globals.error_msg, -	     _lily_globals.error_location); -      free(_lily_globals.error_msg); /* error message was allocated! */ +		printf("FAIL - %s (%s)\n", +		  _lily_globals.error_msg, +		  _lily_globals.error_location); +		free(_lily_globals.error_msg); /* error message was allocated! */  	  return; -   } +	} -   test(); +	test(); -   /* test succeeded */ -   printf("OK\n"); +	/* test succeeded */ +	printf("OK\n");  }  /* run a suite */  void _lily_run_suite(const char *name, lily_test_t suite)  { -   printf("=== %s ===\n", name); -   suite(); -   printf("\n"); +	printf("=== %s ===\n", name); +	suite(); +	printf("\n");  }  /* ======== ASSERTIONS ======== */  static void _lily_assert_msg(bool statement, const char *location, -			     const char *format_string, va_list args) +				  const char *format_string, va_list args)  { -   if (statement) { -      va_end(args); -      return; // no error, return -   } +	if (statement) { +		va_end(args); +		return; // no error, return +	} -   _lily_globals.error_location = location; +	_lily_globals.error_location = location; -   va_list args_len; -   va_copy(args_len, args); -   size_t length = vsnprintf(NULL, 0, format_string, args_len); -   va_end(args_len); +	va_list args_len; +	va_copy(args_len, args); +	size_t length = vsnprintf(NULL, 0, format_string, args_len); +	va_end(args_len); -   if (_lily_globals.error_msg_len < length+1 || -       _lily_globals.error_msg == NULL) { -      if (_lily_globals.error_msg != NULL) +	if (_lily_globals.error_msg_len < length+1 || +		 _lily_globals.error_msg == NULL) { +		if (_lily_globals.error_msg != NULL)  	 free(_lily_globals.error_msg); -      char *msg = malloc((length+2) * sizeof(char)); +		char *msg = malloc((length+2) * sizeof(char)); -      if (msg == NULL) { +		if (msg == NULL) {  	 fprintf(stderr, "WARNING: failed to allocate memory for failed test message!\n");  	 _lily_globals.error_msg = NULL;  	 va_end(args);  	 longjmp(_lily_globals.env, 1);  	 return; -      } -      else { +		} +		else {  	 _lily_globals.error_msg = msg;  	 _lily_globals.error_msg_len = length+1; -      } -   } +		} +	} -   vsnprintf(_lily_globals.error_msg, length+1, format_string, args); +	vsnprintf(_lily_globals.error_msg, length+1, format_string, args); -   va_end(args); -   longjmp(_lily_globals.env, 1); +	va_end(args); +	longjmp(_lily_globals.env, 1);  }  void lily_assert_msg(bool statement, const char *location, -		     const char *format_string, ...) +			  const char *format_string, ...)  { -   va_list args; -   va_start(args, format_string); -   // _lily_assert_msg may long jump, so it takes calls va_end(args) for you -   _lily_assert_msg(statement, location, format_string, args); +	va_list args; +	va_start(args, format_string); +	// _lily_assert_msg may long jump, so it takes calls va_end(args) for you +	_lily_assert_msg(statement, location, format_string, args);  }  void _lily_assert_true(const char *statement, bool value, const char *location)  { -   lily_assert_msg(value, location, -		   "%s is not true", -		   statement); +	lily_assert_msg(value, location, +			"%s is not true", +			statement);  }  void _lily_assert_false(const char *statement, bool value, const char *location)  { -   lily_assert_msg(!value, location, -		   "%s is not false", -		   statement); +	lily_assert_msg(!value, location, +			"%s is not false", +			statement);  }  void _lily_assert_not_null(const char *name, void *ptr, const char *location)  { -   lily_assert_msg(ptr != NULL, location, -		   "%s is NULL", -		   name); +	lily_assert_msg(ptr != NULL, location, +			"%s is NULL", +			name);  }  void _lily_assert_null(const char *name, void *ptr, const char *location)  { -   lily_assert_msg(ptr == NULL, location, -		   "%s (%p) is not NULL", -		   name, ptr); +	lily_assert_msg(ptr == NULL, location, +			"%s (%p) is not NULL", +			name, ptr);  }  void _lily_assert_ptr_equal(const char *name_a, const char *name_b, -			    void *a, void *b, const char *location) +				 void *a, void *b, const char *location)  { -   lily_assert_msg(a == b, location, -		   "%s (%p) is not equal to %s (%p)", -		   name_a, a, name_b, b); +	lily_assert_msg(a == b, location, +			"%s (%p) is not equal to %s (%p)", +			name_a, a, name_b, b);  }  void _lily_assert_ptr_not_equal(const char *name_a, const char *name_b, -			    void *a, void *b, const char *location) +				 void *a, void *b, const char *location)  { -   lily_assert_msg(a != b, location, -		   "%s (%p) is equal to %s", -		   name_a, a, name_b); +	lily_assert_msg(a != b, location, +			"%s (%p) is equal to %s", +			name_a, a, name_b);  }  void _lily_assert_int_equal(const char *name_a, const char *name_b, -			    intmax_t a, intmax_t b, const char *location) +				 intmax_t a, intmax_t b, const char *location)  { -   lily_assert_msg(a == b, location, -		   "%s (%d) is not equal to %s (%d)", -		   name_a, a, name_b, b); +	lily_assert_msg(a == b, location, +			"%s (%d) is not equal to %s (%d)", +			name_a, a, name_b, b);  }  void _lily_assert_int_not_equal(const char *name_a, const char *name_b, -			    intmax_t a, intmax_t b, const char *location) +				 intmax_t a, intmax_t b, const char *location)  { -   lily_assert_msg(a != b, location, -		   "%s (%d) is equal to %s", -		   name_a, a, name_b); +	lily_assert_msg(a != b, location, +			"%s (%d) is equal to %s", +			name_a, a, name_b);  }  void _lily_assert_float_equal(const char *name_a, const char *name_b, -			      double a, double b, double epsilon, const char *location) +					double a, double b, double epsilon, const char *location)  { -   lily_assert_msg(abs(a - b) <= epsilon, -		   "%s (%f) is not equal to %s (%f) (epsilon: %f)", -		   name_a, a, name_b, b, epsilon); +	lily_assert_msg(abs(a - b) <= epsilon, +			"%s (%f) is not equal to %s (%f) (epsilon: %f)", +			name_a, a, name_b, b, epsilon);  }  void _lily_assert_float_not_equal(const char *name_a, const char *name_b,  				  double a, double b, double epsilon, const char *location)  { -   lily_assert_msg(abs(a - b) > epsilon, -		   "%s (%f) is equal to %s (%f) (epsilon: %f)", -		   name_a, a, name_b, b, epsilon); +	lily_assert_msg(abs(a - b) > epsilon, +			"%s (%f) is equal to %s (%f) (epsilon: %f)", +			name_a, a, name_b, b, epsilon);  }  void _lily_assert_string_equal(const char *name_a, const char *name_b, -			       char *a, char *b, const char *location) +					 char *a, char *b, const char *location)  { -   lily_assert_msg(strcmp(a, b) == 0, -		   "%s ('%s') is not equal to %s ('%s')", -		   name_a, a, name_b, b); +	lily_assert_msg(strcmp(a, b) == 0, +			"%s ('%s') is not equal to %s ('%s')", +			name_a, a, name_b, b);  }  void _lily_assert_string_not_equal(const char *name_a, const char *name_b, -				   char *a, char *b, const char *location) +					char *a, char *b, const char *location)  { -   lily_assert_msg(strcmp(a, b) != 0, -		   "%s ('%s') is equal to %s", -		   name_a, a, name_b); +	lily_assert_msg(strcmp(a, b) != 0, +			"%s ('%s') is equal to %s", +			name_a, a, name_b);  }  void _lily_assert_memory_equal(const char *name_a, const char *name_b, -			       void *a, void *b, size_t size, const char *location) +					 void *a, void *b, size_t size, const char *location)  { -   lily_assert_msg(memcmp(a, b, size) == 0, -		   "%s and %s contain different data", name_a, name_b); +	lily_assert_msg(memcmp(a, b, size) == 0, +			"%s and %s contain different data", name_a, name_b);  }  void _lily_assert_memory_not_equal(const char *name_a, const char *name_b, -				   void *a, void *b, size_t size, const char *location) +					void *a, void *b, size_t size, const char *location)  { -   lily_assert_msg(memcmp(a, b, size) == 0, -		   "%s contains the same data s %s", name_a, name_b); +	lily_assert_msg(memcmp(a, b, size) == 0, +			"%s contains the same data s %s", name_a, name_b);  } @@ -262,98 +262,104 @@ void _lily_assert_memory_not_equal(const char *name_a, const char *name_b,  lily_queue_t* lily_queue_create()  { -   const size_t size = 256 * sizeof(uint8_t); +	const size_t size = 256 * sizeof(uint8_t); -   lily_queue_t *q = malloc(sizeof(lily_queue_t)); -   q->buf_size = size; -   q->buf = malloc(size); -   q->front = q->buf; -   q->back = q->front; +	lily_queue_t *q = malloc(sizeof(lily_queue_t)); +	q->buf_size = size; +	q->buf = malloc(size); +	q->front = q->buf; +	q->back = q->front; -   return q; +	return q;  }  void lily_queue_destroy(lily_queue_t *q)  { -   free(q->buf); -   free(q); +	free(q->buf); +	free(q);  }  void _lily_enqueue(lily_queue_t *q, size_t size, uint8_t *data)  { -   size_t used = q->back - q->buf; -   size_t remaining = q->buf_size - used; - -   if (remaining < size) { -      /* re-allocate bigger buffer */ -      size_t size_new = 2 * q->buf_size; -      uint8_t *buf_new = realloc(q->buf, size_new); -      if (buf_new == NULL) { +	size_t used = q->back - q->buf; +	size_t remaining = q->buf_size - used; + +	if (remaining < size) { +		/* re-allocate bigger buffer */ +		size_t size_new = 2 * q->buf_size; +		uint8_t *buf_new = realloc(q->buf, size_new); +		if (buf_new == NULL) {  	 fprintf(stderr, "failed to allocated %lu bytes for queue %p!\n",  		 size_new, q);  	 return; -      } -      q->buf = buf_new; -   } +		} +		q->buf = buf_new; +	} -   memcpy(q->back, data, size); -   q->back += size; +	memcpy(q->back, data, size); +	q->back += size;  }  void _lily_dequeue(lily_queue_t *q, size_t size, uint8_t *ptr)  { -   size_t dist = q->back - q->front; -   if (dist < size) { -      fprintf(stderr, "attempted to read %lu bytes out of queue %p, " -	      "which has %lu bytes presently queued\n", size, q, dist); -      return; -   } - -   memcpy(ptr, q->front, size); -   q->front += size; +	size_t dist = q->back - q->front; +	if (dist < size) { +		fprintf(stderr, "attempted to read %lu bytes out of queue %p, " +			"which has %lu bytes presently queued\n", size, q, dist); +		return; +	} + +	memcpy(ptr, q->front, size); +	q->front += size;  }  lily_mock_t * lily_mock_create()  { -   lily_mock_t *m = malloc(sizeof(lily_mock_t)); -   m->n_calls = 0; -   m->arguments = lily_queue_create(); -   m->values = lily_queue_create(); -   return m; +	lily_mock_t *m = malloc(sizeof(lily_mock_t)); +	m->n_calls = 0; +	m->arguments = lily_queue_create(); +	m->values = lily_queue_create(); +	return m;  }  void lily_mock_destroy(lily_mock_t *m)  { -   lily_queue_destroy(m->arguments); -   lily_queue_destroy(m->values); -   free(m); +	lily_queue_destroy(m->arguments); +	lily_queue_destroy(m->values); +	free(m); +} + +void lily_mock_use(lily_mock_t **m) +{ +	if (*m != NULL) lily_mock_destroy(*m); +	*m = lily_mock_create();  }  void _lily_mock_call(lily_mock_t *m, struct lily_mock_arg_t *args, size_t n_args)  { -   m->n_calls += 1; +	m->n_calls += 1; -   for (int i=0; i<n_args; i++) { -      _lily_enqueue(m->arguments, args[i].size, args[i].var); -   } +	for (int i=0; i<n_args; i++) { +		_lily_enqueue(m->arguments, args[i].size, args[i].var); +	}  }  void _lily_get_call(lily_mock_t *m, -		    struct lily_mock_arg_t *args, -		    size_t n_args, -		    unsigned int call_num) +			 struct lily_mock_arg_t *args, +			 size_t n_args, +			 unsigned int call_num)  { -   size_t stride = 0; -   for (int i=0; i<n_args; i++) { -      stride += args[i].size; -   } - -   m->arguments->front = m->arguments->buf + (call_num * stride); -   for (int i=0; i<n_args; i++) { -      _lily_dequeue(m->arguments, args[i].size, args[i].var); -   } +	size_t stride = 0; +	for (int i=0; i<n_args; i++) { +		stride += args[i].size; +	} + +	m->arguments->front = m->arguments->buf + (call_num * stride); +	for (int i=0; i<n_args; i++) { +		_lily_dequeue(m->arguments, args[i].size, args[i].var); +	}  } diff --git a/src/test/lily-test.h b/src/test/lily-test.h index 90fc2c8..b5f380c 100644 --- a/src/test/lily-test.h +++ b/src/test/lily-test.h @@ -45,6 +45,10 @@  #ifndef LILY_TEST_H  #define LILY_TEST_H +#define LILY_VERSION_MAJOR 1 +#define LILY_VERSION_MINOR 0 +#define LILY_VERSION_PATCH 0 +  #include <stdbool.h>  #include <stddef.h>  #include <stdint.h> @@ -53,7 +57,7 @@  #define STR_IMP(x) #x  #define STR(x) STR_IMP(x)  /* define SOURCE_PATH_SIZE to strip away the -   leading parts of the full compilation path */ +	leading parts of the full compilation path */  #ifndef SOURCE_PATH_SIZE  #define LILY_LOCATION (__FILE__ ":" STR(__LINE__))  #else @@ -62,10 +66,10 @@  /** a few nasty globals that make everything clean for the end user */  struct lily_globals_t { -   jmp_buf env; -   size_t error_msg_len; -   char *error_msg; -   const char *error_location; +	jmp_buf env; +	size_t error_msg_len; +	char *error_msg; +	const char *error_location;  };  extern struct lily_globals_t _lily_globals; @@ -89,7 +93,7 @@ void _lily_run_suite(const char *name, lily_test_t suite);  /** basic assertion function, mostly used by the other assertions */  void lily_assert_msg(bool statement, const char *location, -		     const char *format_string, ...); +			  const char *format_string, ...);  #define lily_assert_true(statement) _lily_assert_true(#statement, statement, LILY_LOCATION)  void _lily_assert_true(const char *statement, bool value, const char *location); @@ -109,7 +113,7 @@ void _lily_assert_null(const char *name, void *ptr, const char *location);  #define lily_assert_ptr_equal(a, b) _lily_assert_ptr_equal(#a, #b, a, b, LILY_LOCATION)  void _lily_assert_ptr_equal(const char *name_a, const char *name_b, -			    void *a, void *b, const char *location); +				 void *a, void *b, const char *location);  #define lily_assert_ptr_not_equal(a, b) _lily_assert_ptr_not_equal(#a, #b, a, b, LILY_LOCATION) @@ -119,7 +123,7 @@ void _lily_assert_ptr_not_equal(const char *name_a, const char *name_b,  #define lily_assert_int_equal(a, b) _lily_assert_int_equal(#a, #b, a, b, LILY_LOCATION)  void _lily_assert_int_equal(const char *name_a, const char *name_b, -			    intmax_t a, intmax_t b, const char *location); +				 intmax_t a, intmax_t b, const char *location);  #define lily_assert_int_not_equal(a, b) _lily_assert_int_not_equal(#a, #b, a, b, LILY_LOCATION) @@ -128,37 +132,37 @@ void _lily_assert_int_not_equal(const char *name_a, const char *name_b,  #define lily_assert_float_equal(a, b, epsilon)				\ -   _lily_assert_float_equal(#a, #b, a, b, epsilon, LILY_LOCATION) +	_lily_assert_float_equal(#a, #b, a, b, epsilon, LILY_LOCATION)  void _lily_assert_float_equal(const char *name_a, const char *name_b, -			      double a, double b, double epsilon, const char *location); +					double a, double b, double epsilon, const char *location);  #define lily_assert_float_not_equal(a, b, epsilon)			\ -   _lily_assert_float_not_equal(#a, #b, a, b, epsilon, LILY_LOCATION) +	_lily_assert_float_not_equal(#a, #b, a, b, epsilon, LILY_LOCATION)  void _lily_assert_float_not_equal(const char *name_a, const char *name_b,  				  double a, double b, double epsilon, const char *location);  #define lily_assert_string_equal(a, b) _lily_assert_string_equal(#a, #b, a, b, LILY_LOCATION)  void _lily_assert_string_equal(const char *name_a, const char *name_b, -			       char *a, char *b, const char *location); +					 char *a, char *b, const char *location);  #define lily_assert_string_not_equal(a, b) _lily_assert_string_not_equal(#a, #b, a, b, LILY_LOCATION)  void _lily_assert_string_not_equal(const char *name_a, const char *name_b, -				   char *a, char *b, const char *location); +					char *a, char *b, const char *location);  #define lily_assert_memory_equal(a, b, size)			\ -   _lily_assert_memory_equal(#a, #b, a, b, size, LILY_LOCATION) +	_lily_assert_memory_equal(#a, #b, a, b, size, LILY_LOCATION)  void _lily_assert_memory_equal(const char *name_a, const char *name_b, -			       void *a, void *b, size_t size, const char *location); +					 void *a, void *b, size_t size, const char *location);  #define lily_assert_memory_not_equal(a, b, size)			\ -   _lily_assert_memory_not_equal(#a, #b, a, b, size, LILY_LOCATION) +	_lily_assert_memory_not_equal(#a, #b, a, b, size, LILY_LOCATION)  void _lily_assert_memory_not_equal(const char *name_a, const char *name_b, -				   void *a, void *b, size_t size, const char *location); +					void *a, void *b, size_t size, const char *location);  /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -171,9 +175,9 @@ void _lily_assert_memory_not_equal(const char *name_a, const char *name_b,  /** queue structure capable of containing arbitrary data */  typedef struct lily_queue_t { -   size_t buf_size; -   uint8_t *buf; -   uint8_t *front, *back; +	size_t buf_size; +	uint8_t *buf; +	uint8_t *front, *back;  } lily_queue_t; @@ -191,10 +195,10 @@ void lily_queue_destroy(lily_queue_t *q);   * value - the value to append   */  #define lily_enqueue(q, type, value)				\ -   do {								\ -      type _var = value;					\ -      _lily_enqueue(q, sizeof(type), (uint8_t*)(&_var));	\ -   } while(0) +	do {								\ +		type _var = value;					\ +		_lily_enqueue(q, sizeof(type), (uint8_t*)(&_var));	\ +	} while(0)  void _lily_enqueue(lily_queue_t *q, size_t size, uint8_t *data); @@ -205,48 +209,55 @@ void _lily_enqueue(lily_queue_t *q, size_t size, uint8_t *data);   * ptr - the location to store the popped value   */  #define lily_dequeue(q, type, ptr)			\ -   _lily_dequeue(q, sizeof(type), (uint8_t*) ptr) +	_lily_dequeue(q, sizeof(type), (uint8_t*) ptr)  void _lily_dequeue(lily_queue_t *q, size_t size, uint8_t *ptr);  struct lily_mock_arg_t { -   size_t size; -   void *var; +	size_t size; +	void *var;  };  #define LILY_NARGS(args) (sizeof(args)/sizeof(struct lily_mock_arg_t))  /** structure to store data for mock functions */  typedef struct lily_mock_t { -   unsigned int n_calls; -   lily_queue_t *arguments; -   lily_queue_t *values; +	unsigned int n_calls; +	lily_queue_t *arguments; +	lily_queue_t *values;  } lily_mock_t;  /** setup mock function storage */  lily_mock_t * lily_mock_create();  /** tear down mock function storage */  void lily_mock_destroy(lily_mock_t *m); - +/** automatically re-create mock function storage */ +void lily_mock_use(lily_mock_t **m);  /** store a call to a mock function */ +#define lily_mock_store_call(m, args)	\ +	_lily_mock_call(m, args, LILY_NARGS(args)) +/* lily_mock_call is deprecated!! do not use it in new programs */  #define lily_mock_call(m, args)			\ -   _lily_mock_call(m, args, LILY_NARGS(args)) +	_lily_mock_call(m, args, LILY_NARGS(args))  void _lily_mock_call(lily_mock_t *m, struct lily_mock_arg_t *args, size_t n_args);  /** retrieve a call to a mock function */ +#define lily_mock_get_call(m, args, call_num)	\ +	_lily_get_call(m, args, LILY_NARGS(args), call_num) +/* lily_get_call is deprecated!! do not use it in new programs */  #define lily_get_call(m, args, call_num)		\ -   _lily_get_call(m, args, LILY_NARGS(args), call_num) +	_lily_get_call(m, args, LILY_NARGS(args), call_num)  void _lily_get_call(lily_mock_t *m, -		    struct lily_mock_arg_t *args, -		    size_t n_args, -		    unsigned int call_num); +			 struct lily_mock_arg_t *args, +			 size_t n_args, +			 unsigned int call_num);  /** store a value in a mock structure */  #define lily_store_value(m, type, value)	\ -   lily_enqueue(m->values, type, value) +	lily_enqueue(m->values, type, value)  /** retrieve a value from a mock structure */  #define lily_get_value(m, type, ptr)		\ -   lily_dequeue(m->values, type, ptr) +	lily_dequeue(m->values, type, ptr)  #endif | 
