summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorClement Sibille <clements+git@lisible.xyz>2024-05-09 08:58:14 +0900
committerClement Sibille <clements+git@lisible.xyz>2024-05-09 08:58:14 +0900
commit5a26e1f481fe85f08093a414e27c1056b85c6715 (patch)
tree20a2384554cfcb437a4b604e02b4a9978507cafc /tests
Initial commit
Diffstat (limited to 'tests')
-rw-r--r--tests/string.c14
-rw-r--r--tests/test.h74
-rw-r--r--tests/test_runner.c17
3 files changed, 105 insertions, 0 deletions
diff --git a/tests/string.c b/tests/string.c
new file mode 100644
index 0000000..e3d68f4
--- /dev/null
+++ b/tests/string.c
@@ -0,0 +1,14 @@
+#include "lisiblestd/memory.h"
+#include "test.h"
+#include <lisiblestd/string.h>
+
+void t_String_from_str(void) {
+ String string;
+ String_from_str(&system_allocator, &string, "Some string");
+ T_ASSERT(strncmp(string.value, "Some string", 11));
+ T_ASSERT_EQ(string.length, 11);
+
+ String_destroy(&system_allocator, &string);
+}
+
+TEST_SUITE(TEST(t_String_from_str))
diff --git a/tests/test.h b/tests/test.h
new file mode 100644
index 0000000..223abf5
--- /dev/null
+++ b/tests/test.h
@@ -0,0 +1,74 @@
+#ifndef LSTD_TESTS_TEST_H
+#define LSTD_TESTS_TEST_H
+
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define TEST_DATA_PATH "../cuttereng/tests/tests_data/"
+
+typedef void (*TestFn)(void);
+
+typedef struct {
+ const char *name;
+ TestFn fn;
+} Test;
+
+extern Test tests[];
+extern size_t test_count;
+
+inline static void assert0(int boolean, const char *msg) {
+ if (!boolean) {
+ fprintf(stderr, "%s", msg);
+ exit(1);
+ }
+}
+
+inline static void assert(int boolean, const char *fmt, ...) {
+ if (!boolean) {
+ va_list args;
+ va_start(args, fmt);
+ fprintf(stderr, fmt, args);
+ va_end(args);
+ exit(1);
+ }
+}
+
+#define T_ASSERT(expr) T_ASSERT_MSG(expr, "%s", #expr)
+
+#define STRINGIFY(x) #x
+#define TOSTRING(x) STRINGIFY(x)
+
+#define T_ASSERT_MSG(expr, fmt, ...) \
+ do { \
+ assert((expr), \
+ "Assertion failed in " __FILE__ ":" TOSTRING(__LINE__) ": " fmt \
+ "\n", \
+ __VA_ARGS__); \
+ } while (0)
+#define T_ASSERT_MSG0(expr, msg) \
+ do { \
+ assert0((expr), "Assertion failed in " __FILE__ \
+ ":" TOSTRING(__LINE__) ": " msg "\n"); \
+ } while (0)
+
+#define T_ASSERT_EQ(a, b) T_ASSERT_MSG0((a == b), #a " != " #b)
+#define T_ASSERT_FLOAT_EQ(a, b, epsilon) \
+ T_ASSERT_MSG0((fabs(a - b) < epsilon), #a " != " #b)
+
+#define T_ASSERT_NULL(a) T_ASSERT_MSG0((a == NULL), #a " is not NULL")
+#define T_ASSERT_NOT_NULL(a) T_ASSERT_MSG0((a != NULL), #a " is NULL")
+
+#define T_ASSERT_STR_EQ(a, b, length) \
+ T_ASSERT_MSG0((strncmp(a, b, length) == 0), #a " != " #b)
+
+#define ARG_COUNT(...) (sizeof((Test[]){__VA_ARGS__}) / sizeof(Test))
+#define TEST_SUITE(...) \
+ Test tests[] = {__VA_ARGS__}; \
+ size_t test_count = ARG_COUNT(__VA_ARGS__);
+#define TEST(x) \
+ { .name = #x, .fn = x }
+
+#endif // LSTD_TESTS_TEST_H
diff --git a/tests/test_runner.c b/tests/test_runner.c
new file mode 100644
index 0000000..f05cd02
--- /dev/null
+++ b/tests/test_runner.c
@@ -0,0 +1,17 @@
+#include "test.h"
+
+#define ANSI_COLOR_GREEN "\x1b[32m"
+#define ANSI_COLOR_RESET "\x1b[0m"
+
+int main(int argc, char **argv) {
+ (void)argc;
+ (void)argv;
+
+ for (size_t i = 0; i < test_count; i++) {
+ fprintf(stderr, "Running test: %s...\n", tests[i].name);
+ tests[i].fn();
+ fprintf(stderr, ANSI_COLOR_GREEN " OK" ANSI_COLOR_RESET "\n");
+ }
+
+ return 0;
+}
Go back to lisible.xyz