summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/fixtures/empty.ini0
-rw-r--r--tests/fixtures/normal.ini10
-rw-r--r--tests/test_buffer.c71
-rw-r--r--tests/test_config.c45
4 files changed, 126 insertions, 0 deletions
diff --git a/tests/fixtures/empty.ini b/tests/fixtures/empty.ini
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/fixtures/empty.ini
diff --git a/tests/fixtures/normal.ini b/tests/fixtures/normal.ini
new file mode 100644
index 0000000..04e6ef9
--- /dev/null
+++ b/tests/fixtures/normal.ini
@@ -0,0 +1,10 @@
+[github]
+endpoint = https://api.github.com/graphql
+token = token
+user_agent = user-agent
+owner = my-org
+
+[git]
+base = /srv/git
+owner = 1000
+group = 1001
diff --git a/tests/test_buffer.c b/tests/test_buffer.c
new file mode 100644
index 0000000..a8241c4
--- /dev/null
+++ b/tests/test_buffer.c
@@ -0,0 +1,71 @@
+//
+// Created by Anshul Gupta on 4/9/25.
+//
+
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <stdint.h>
+#include <cmocka.h>
+#include <string.h>
+
+#include "../src/buffer.h"
+
+static void buffer_new_empty_test(void **state)
+{
+ (void) state;
+ const buffer_t buf = buffer_new(0);
+ assert_null(buf.data);
+ assert_int_equal(buf.len, 0);
+ assert_int_equal(buf.cap, 0);
+}
+
+static void buffer_new_test(void **state)
+{
+ (void) state;
+ const buffer_t buf = buffer_new(10);
+ assert_non_null(buf.data);
+ assert_int_equal(buf.len, 0);
+ assert_int_equal(buf.cap, 16); // 10 aligned to 8 bytes
+ buffer_free(buf);
+}
+
+static void buffer_reserve_test(void **state)
+{
+ (void) state;
+ buffer_t buf = buffer_new(10);
+ assert_int_equal(buf.cap, 16); // 10 aligned to 8 bytes
+ buffer_reserve(&buf, 20);
+ assert_int_equal(buf.cap, 24); // 20 aligned to 8 bytes
+ buffer_free(buf);
+}
+
+static void buffer_append_test(void **state)
+{
+ (void) state;
+ buffer_t buf = buffer_new(0);
+ assert_int_equal(buf.cap, 0);
+ const char *data = "Hello, World!";
+ const size_t len = strlen(data);
+ buffer_append(&buf, data, len);
+ assert_int_equal(buf.len, len);
+ assert_int_equal(buf.cap, 16); // 16 bytes allocated
+ assert_memory_equal(buf.data, data, len);
+ buffer_append(&buf, data, len);
+ assert_int_equal(buf.len, len * 2);
+ assert_int_equal(buf.cap, 32); // 32 bytes allocated
+ assert_memory_equal(buf.data + len, data, len);
+ buffer_free(buf);
+}
+
+int main(void)
+{
+ const struct CMUnitTest tests[] = {
+ cmocka_unit_test(buffer_new_empty_test),
+ cmocka_unit_test(buffer_new_test),
+ cmocka_unit_test(buffer_reserve_test),
+ cmocka_unit_test(buffer_append_test),
+ };
+
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
diff --git a/tests/test_config.c b/tests/test_config.c
new file mode 100644
index 0000000..41cacb1
--- /dev/null
+++ b/tests/test_config.c
@@ -0,0 +1,45 @@
+//
+// Created by Anshul Gupta on 4/9/25.
+//
+
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <stdint.h>
+#include <cmocka.h>
+#include <string.h>
+
+#include "../src/config.h"
+
+static void config_read_empty(void **state)
+{
+ (void) state;
+ const char *path = "../tests/fixtures/empty.ini";
+ struct config *cfg = config_read(path);
+ assert_null(cfg);
+}
+
+static void config_read_normal(void **state)
+{
+ (void) state;
+ const char *path = "../tests/fixtures/normal.ini";
+ struct config *cfg = config_read(path);
+ assert_non_null(cfg);
+ assert_string_equal(cfg->endpoint, "https://api.github.com/graphql");
+ assert_string_equal(cfg->token, "token");
+ assert_string_equal(cfg->user_agent, "user-agent");
+ assert_string_equal(cfg->owner, "my-org");
+ assert_string_equal(cfg->git_base, "/srv/git");
+ assert_int_equal(cfg->git_owner, 1000);
+ assert_int_equal(cfg->git_group, 1001);
+}
+
+int main(void)
+{
+ const struct CMUnitTest tests[] = {
+ cmocka_unit_test(config_read_empty),
+ cmocka_unit_test(config_read_normal)
+ };
+
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}