summaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2022-08-23 13:38:27 -0500
committersanine <sanine.not@pm.me>2022-08-23 13:38:27 -0500
commit3afbf2a13b2dada445fb667bf25600407fea480a (patch)
tree551329e6f74fc9f177616de0d6739e8b5331ae96 /src/util
parent261e3f991221fbad6bbf262f5e65b773e4b6c73e (diff)
parent25ed7eb9f84e9a822f698ad803901fbb2a5354cf (diff)
:wMerge branch 'gl-window' into main
Diffstat (limited to 'src/util')
-rw-r--r--src/util/CMakeLists.txt9
-rw-r--r--src/util/util.c12
-rw-r--r--src/util/util.h8
-rw-r--r--src/util/util.test.c47
4 files changed, 76 insertions, 0 deletions
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);
+}