From 1f75a21851b0a0bd4bc766c458e67721dd37c9fd Mon Sep 17 00:00:00 2001
From: sanine <sanine.not@pm.me>
Date: Mon, 22 Aug 2022 21:37:12 -0500
Subject: add append_table()

---
 CMakeLists.txt             | 23 +++++++++--------------
 src/gl/CMakeLists.txt      |  7 +++++++
 src/gl/gl.test.c           |  3 +--
 src/logging/CMakeLists.txt | 10 ++++++++++
 src/test/CMakeLists.txt    |  6 ++++++
 src/test/honey-test.h      |  2 ++
 src/util/CMakeLists.txt    |  9 +++++++++
 src/util/util.c            | 12 ++++++++++++
 src/util/util.h            |  8 ++++++++
 src/util/util.test.c       | 47 ++++++++++++++++++++++++++++++++++++++++++++++
 10 files changed, 111 insertions(+), 16 deletions(-)
 create mode 100644 src/logging/CMakeLists.txt
 create mode 100644 src/test/CMakeLists.txt
 create mode 100644 src/util/CMakeLists.txt
 create mode 100644 src/util/util.c
 create mode 100644 src/util/util.h
 create mode 100644 src/util/util.test.c

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 8b6c923..17bf14d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -32,10 +32,8 @@ add_subdirectory(${LIB_ROOT}/cglm)
 add_subdirectory(${LIB_ROOT}/glfw-3.3.8)
 
 
-set(HONEY_SOURCE ${SRC_ROOT}/main.c ${SRC_ROOT}/logging/logging.c)
+set(HONEY_SOURCE ${SRC_ROOT}/main.c)
 add_executable(honey ${HONEY_SOURCE})
-add_subdirectory(${SRC_ROOT}/gl)
-add_subdirectory(${SRC_ROOT}/image)
 
 set(LIBRARIES lua5.1 honeysuckle assimp glfw)
 if (WIN32)
@@ -51,18 +49,15 @@ target_link_libraries(honey ${LIBRARIES})
 string(LENGTH "${CMAKE_SOURCE_DIR}/" SOURCE_PATH_SIZE)
 add_definitions("-DSOURCE_PATH_SIZE=${SOURCE_PATH_SIZE}")
 
-set(TEST_SOURCES
-	${SRC_ROOT}/test/lily-test.c
-	${SRC_ROOT}/test/honey-test.c
-
-	${SRC_ROOT}/logging/logging.test.c
-	${SRC_ROOT}/gl/glad/glad.c
-	${SRC_ROOT}/gl/gl.test.c
-	${SRC_ROOT}/gl/window.test.c
-)
-
-add_executable(test EXCLUDE_FROM_ALL ${TEST_SOURCES})
+add_executable(test EXCLUDE_FROM_ALL)
 set_target_properties(test PROPERTIES
 	C_STANDARD 99
 	CMAKE_C_FLAGS "-Wall -Wextra -Werror -Wfatal-errors -Wpedantic")
 target_link_libraries(test lua5.1 honeysuckle glfw dl)
+
+
+add_subdirectory(${SRC_ROOT}/gl)
+add_subdirectory(${SRC_ROOT}/logging)
+add_subdirectory(${SRC_ROOT}/image)
+add_subdirectory(${SRC_ROOT}/util)
+add_subdirectory(${SRC_ROOT}/test)
diff --git a/src/gl/CMakeLists.txt b/src/gl/CMakeLists.txt
index d4bc770..c1c3db0 100644
--- a/src/gl/CMakeLists.txt
+++ b/src/gl/CMakeLists.txt
@@ -11,3 +11,10 @@ target_sources(honey PUBLIC
 	${GL}/gl.c
 	${GL}/glad/glad.c
 )
+
+
+target_sources(test PUBLIC
+	${GL}/glad/glad.c
+	${GL}/gl.test.c
+	${GL}/window.test.c
+)
diff --git a/src/gl/gl.test.c b/src/gl/gl.test.c
index 7a687b4..488126f 100644
--- a/src/gl/gl.test.c
+++ b/src/gl/gl.test.c
@@ -15,10 +15,9 @@ void mock_glBufferData_(int, size_t, const void *, int);
 #define glfwInit mock_glfwInit_
 #define hs_throw_error mock_hs_throw_error_
 #define glfwTerminate mock_glfwTerminate_
-#undef glBufferData
-#define glBufferData mock_glBufferData_
 #define setup_shader DUMMY_FUNCTION
 #define setup_drawing DUMMY_FUNCTION
+#define setup_texture DUMMY_FUNCTION
 #include "gl/gl.c"
 #include "gl/data.c"
 #undef glBufferData
diff --git a/src/logging/CMakeLists.txt b/src/logging/CMakeLists.txt
new file mode 100644
index 0000000..4d07f5e
--- /dev/null
+++ b/src/logging/CMakeLists.txt
@@ -0,0 +1,10 @@
+project(honey_engine)
+
+target_sources(honey PUBLIC
+	${CMAKE_CURRENT_LIST_DIR}/logging.c
+)
+
+
+target_sources(test PUBLIC
+	${CMAKE_CURRENT_LIST_DIR}/logging.test.c
+)
diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt
new file mode 100644
index 0000000..6b86edc
--- /dev/null
+++ b/src/test/CMakeLists.txt
@@ -0,0 +1,6 @@
+project(honey_engine)
+
+target_sources(test PUBLIC
+	${CMAKE_CURRENT_LIST_DIR}/honey-test.c
+	${CMAKE_CURRENT_LIST_DIR}/lily-test.c
+)
diff --git a/src/test/honey-test.h b/src/test/honey-test.h
index 30730d5..03218a4 100644
--- a/src/test/honey-test.h
+++ b/src/test/honey-test.h
@@ -31,10 +31,12 @@
 void suite_logging();
 void suite_gl();
 void suite_window();
+void suite_util();
 
 #define RUN_TESTS() \
 	lily_run_suite(suite_logging); \
 	lily_run_suite(suite_gl); \
 	lily_run_suite(suite_window); \
+	lily_run_suite(suite_util); \
 
 #endif
diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt
new file mode 100644
index 0000000..5a01ec6
--- /dev/null
+++ b/src/util/CMakeLists.txt
@@ -0,0 +1,9 @@
+project(honey_engine)
+
+target_sources(honey PUBLIC
+	${CMAKE_CURRENT_LIST_DIR}/util.c
+)
+
+target_sources(test PUBLIC
+	${CMAKE_CURRENT_LIST_DIR}/util.test.c
+)
diff --git a/src/util/util.c b/src/util/util.c
new file mode 100644
index 0000000..84edd27
--- /dev/null
+++ b/src/util/util.c
@@ -0,0 +1,12 @@
+#include <lua.h>
+
+void append_table(lua_State *L, int tbl_a, int tbl_b)
+{
+	lua_pushnil(L);
+	while(lua_next(L, tbl_b) != 0) {
+		lua_pushvalue(L, -2); // key
+		lua_pushvalue(L, -2); // value
+		lua_settable(L, tbl_a);
+		lua_pop(L, 1);
+	}
+}
diff --git a/src/util/util.h b/src/util/util.h
new file mode 100644
index 0000000..3ef4e0b
--- /dev/null
+++ b/src/util/util.h
@@ -0,0 +1,8 @@
+#ifndef HONEY_UTIL_H
+#define HONEY_UTIL_H
+
+#include <lua.h>
+
+void append_table(lua_State *L, int tbl_a, int tbl_b);
+
+#endif
diff --git a/src/util/util.test.c b/src/util/util.test.c
new file mode 100644
index 0000000..b4029d2
--- /dev/null
+++ b/src/util/util.test.c
@@ -0,0 +1,47 @@
+#include <lua.h>
+#include <lauxlib.h>
+#include <honeysuckle.h>
+#include "test/honey-test.h"
+
+
+#include "util.c"
+
+
+void test_append_table()
+{
+	lua_State *L = luaL_newstate();
+	int a = hs_create_table(L,
+		hs_str_int("one", 1),
+		hs_str_int("two", 2),
+	);
+	int b = hs_create_table(L,
+		hs_str_int("three", 3),
+		hs_str_int("four", 4),
+		hs_int_int(15, 2),
+	);
+	append_table(L, a, b);
+
+	lua_getfield(L, a, "one");
+	lily_assert_int_equal(lua_tointeger(L, -1), 1);
+	
+	lua_getfield(L, a, "two");
+	lily_assert_int_equal(lua_tointeger(L, -1), 2);
+	
+	lua_getfield(L, a, "three");
+	lily_assert_int_equal(lua_tointeger(L, -1), 3);
+
+	lua_getfield(L, a, "four");
+	lily_assert_int_equal(lua_tointeger(L, -1), 4);
+
+	lua_pushinteger(L, 15);
+	lua_gettable(L, a);
+	lily_assert_int_equal(lua_tointeger(L, -1), 2);
+
+	lua_close(L);
+}
+
+
+void suite_util()
+{
+	lily_run_test(test_append_table);
+}
-- 
cgit v1.2.1