aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore2
-rw-r--r--CMakeLists.txt14
-rw-r--r--Makefile7
-rw-r--r--base_test.c15
-rw-r--r--generate.fish24
-rw-r--r--json.c3
-rw-r--r--main.c54
-rw-r--r--test_json.c61
-rw-r--r--test_string.c19
-rw-r--r--testing.h4
-rw-r--r--vec.c6
11 files changed, 148 insertions, 61 deletions
diff --git a/.gitignore b/.gitignore
index 58c83ee..c7a86e2 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,3 +2,5 @@ main
Debug/
Release/
+
+test.c
diff --git a/CMakeLists.txt b/CMakeLists.txt
index ddba94b..c022fc3 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,6 +1,7 @@
cmake_minimum_required(VERSION 3.30.5)
project(kagamic VERSION 0.1 LANGUAGES C)
set(CMAKE_C_STANDARD 17)
+
add_executable(main
main.c
string.h string.c
@@ -11,3 +12,16 @@ add_executable(main
)
target_link_libraries(main m)
target_compile_options(main PRIVATE -Wall -pedantic -Wextra)
+
+
+add_executable(test
+ test.c
+ testing.h
+ string.h string.c
+ memory.h memory.c
+ vec.h vec.c
+ json.h json.c
+ log.h log.c
+)
+target_link_libraries(test m)
+target_compile_options(test PRIVATE -Wall -pedantic -Wextra)
diff --git a/Makefile b/Makefile
index 58cdf1f..df455a7 100644
--- a/Makefile
+++ b/Makefile
@@ -9,3 +9,10 @@ release:
cd Release; \
cmake -DCMAKE_BUILD_TYPE=Release ..; \
make;
+
+test:
+ fish generate.fish;
+ make debug;
+ ./Debug/test;
+ make release;
+ ./Release/test;
diff --git a/base_test.c b/base_test.c
new file mode 100644
index 0000000..c19806f
--- /dev/null
+++ b/base_test.c
@@ -0,0 +1,15 @@
+#include <stdio.h>
+
+#define test(func) \
+ switch (func()) { \
+ case 0: \
+ printf("✅ %s\n", #func); \
+ success++; \
+ break; \
+ default: \
+ failures++; \
+ printf("🚫 %s\n", #func); \
+ }
+
+int success;
+int failures;
diff --git a/generate.fish b/generate.fish
new file mode 100644
index 0000000..1994cc9
--- /dev/null
+++ b/generate.fish
@@ -0,0 +1,24 @@
+echo "" > test.c;
+set fs (ls test_*.c);
+for f in $fs;
+ echo "#include \"$f\"" >> test.c;
+end;
+echo -e "" >> test.c
+cat base_test.c >> test.c;
+echo -e "" >> test.c;
+echo "int main() {" >> test.c;
+for f in $fs;
+ echo "Parsing $f";
+ set funcs (rg 'char test_' $f);
+ for func in $funcs;
+ set pfunc (echo $func | awk '{match($0, /test_[^(]+/); print substr($0, RSTART, RLENGTH)}');
+ echo "Found $pfunc in $f";
+ echo " test($pfunc);" >> test.c
+ end;
+end;
+echo "
+ printf(\"Success: %d, Failures: %d\n\", success, failures);
+
+ return failures;
+}
+" >> test.c;
diff --git a/json.c b/json.c
index 197dca3..2aa3013 100644
--- a/json.c
+++ b/json.c
@@ -31,7 +31,6 @@ parse_integer_result parse_integer(Arena *a, char *src, uintptr_t pos) {
for (int j = 0; j < i; j++) {
*result += ints[j] * pow(10, (i - j - 1));
}
- printf("json.parse_integer: parsed int=%d at pos=%lu\n", *result, pos);
return (parse_integer_result){result, pos + i};
}
@@ -49,7 +48,6 @@ typedef struct {
} parse_array_result;
parse_array_result parse_array(Arena *a, char *src, uintptr_t pos) {
- printf("json.parse_array: parsing array %s\n", src + pos);
assert(src[pos] == '[');
pos++;
Vec items = Vec_new(a, JSON, 2);
@@ -76,7 +74,6 @@ typedef struct {
} parse_string_result;
parse_string_result parse_string(Arena *a, char *src, uintptr_t pos) {
- printf("json.parse_string: %s\n", src + pos);
assert(src[pos] == '"');
int i = 1;
diff --git a/main.c b/main.c
index 88d7855..e5c5a25 100644
--- a/main.c
+++ b/main.c
@@ -5,9 +5,7 @@ Inspirations:
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
-#include <stdio.h>
-#include "json.h"
#include "memory.h"
#include "string.h"
#include "vec.h"
@@ -20,54 +18,4 @@ typedef struct {
int age;
} User;
-int main() {
- // Testing string
- Arena perm = Arena_new(128e3);
- assert(Str_equals(S("Bonjour"), S("Bonjour")));
- Str s = S("Hello");
- assert(Str_equals(s, Str_copy(&perm, s.data, s.len)));
- printf("Arena: mem=%d B\n", Arena_get_used(perm));
- assert(Arena_get_used(perm) == 5);
- // Testing JSON
- perm = Arena_new(128e3);
- JSON json_int = JSON_parse(&perm, "123");
- assert(*json_int.integer == 123);
- printf("Arena: mem=%d B\n", Arena_get_used(perm));
- assert(Arena_get_used(perm) == 28);
- // JSON Int Array
- perm = Arena_new(128e3);
- JSON json_int_array = JSON_parse(&perm, "[1,2,3]");
- assert(json_int_array.integer == NULL);
- assert(json_int_array.array->len == 3);
- int ints[3];
- int sum = 0;
- for (int i = 0; i < json_int_array.array->len; i++) {
- JSON *item = Vec_get(json_int_array.array, JSON, i);
- ints[i] = *item->integer;
- sum += *item->integer;
- }
- printf("sum=%d\n", sum);
- assert(ints[0] == 1);
- assert(ints[1] == 2);
- assert(ints[2] == 3);
- assert(sum == 6);
- printf("Arena: mem=%d B\n", Arena_get_used(perm));
- assert(Arena_get_used(perm) == 264);
- // JSON String
- perm = Arena_new(128e3);
- JSON js = JSON_parse(&perm, "\"abc\"");
- assert(Str_equals(*js.string, S("abc")) == 1);
- printf("Arena: mem=%d B\n", Arena_get_used(perm));
- assert(Arena_get_used(perm) == 27);
- // JSON Nested hybrid array
- perm = Arena_new(128e3);
- JSON jaa = JSON_parse(&perm, "[[1,\"abc\"]]");
- JSON sub_array = *Vec_get(jaa.array, JSON, 0);
- JSON *val_1 = Vec_get(sub_array.array, JSON, 0);
- assert(*val_1->integer == 1);
- JSON *val_2 = Vec_get(sub_array.array, JSON, 1);
- assert(Str_equals(*val_2->string, S("abc")));
- printf("Arena: mem=%d B\n", Arena_get_used(perm));
- assert(Arena_get_used(perm) == 203);
- return 0;
-}
+int main() { return 0; }
diff --git a/test_json.c b/test_json.c
new file mode 100644
index 0000000..b0f1028
--- /dev/null
+++ b/test_json.c
@@ -0,0 +1,61 @@
+#include "json.h"
+#include "testing.h"
+
+char test_json_integer() {
+ Arena perm = Arena_new(128e3);
+
+ JSON json_int = JSON_parse(&perm, "123");
+
+ t_assert(*json_int.integer == 123);
+ t_assert(Arena_get_used(perm) == 28);
+ return 0;
+}
+
+char test_json_int_array() {
+ Arena perm = Arena_new(128e3);
+
+ JSON json_int_array = JSON_parse(&perm, "[1,2,3]");
+
+ t_assert(json_int_array.integer == NULL);
+ t_assert(json_int_array.array->len == 3);
+ int ints[3];
+ int sum = 0;
+ for (int i = 0; i < json_int_array.array->len; i++) {
+ JSON *item = Vec_get(json_int_array.array, JSON, i);
+ ints[i] = *item->integer;
+ sum += *item->integer;
+ }
+ t_assert(ints[0] == 1);
+ t_assert(ints[1] == 2);
+ t_assert(ints[2] == 3);
+ t_assert(sum == 6);
+ t_assert(Arena_get_used(perm) == 264);
+
+ return 0;
+}
+
+char test_json_nested_array() {
+ Arena perm = Arena_new(128e3);
+
+ JSON jaa = JSON_parse(&perm, "[[1,\"abc\"]]");
+
+ JSON sub_array = *Vec_get(jaa.array, JSON, 0);
+ JSON *val_1 = Vec_get(sub_array.array, JSON, 0);
+ t_assert(*val_1->integer == 1);
+ JSON *val_2 = Vec_get(sub_array.array, JSON, 1);
+ t_assert(Str_equals(*val_2->string, S("abc")));
+ t_assert(Arena_get_used(perm) == 203);
+
+ return 0;
+}
+
+char test_json_string() {
+ Arena perm = Arena_new(128e3);
+
+ JSON js = JSON_parse(&perm, "\"abc\"");
+
+ t_assert(Str_equals(*js.string, S("abc")) == 1);
+ t_assert(Arena_get_used(perm) == 27);
+
+ return 0;
+}
diff --git a/test_string.c b/test_string.c
new file mode 100644
index 0000000..590a30f
--- /dev/null
+++ b/test_string.c
@@ -0,0 +1,19 @@
+#include "string.h"
+#include "testing.h"
+
+char test_string_literal_equality() {
+ t_assert(Str_equals(S("Bonjour"), S("Bonjour")));
+ return 0;
+}
+
+char test_string_copy() {
+ Arena perm = Arena_new(128e3);
+ Str src = S("Hello");
+
+ Str cpy = Str_copy(&perm, src.data, src.len);
+
+ t_assert(Str_equals(src, cpy));
+ t_assert(Arena_get_used(perm) == 5);
+
+ return 0;
+}
diff --git a/testing.h b/testing.h
new file mode 100644
index 0000000..f27daf2
--- /dev/null
+++ b/testing.h
@@ -0,0 +1,4 @@
+#define t_assert(fact) \
+ if (!(fact)) { \
+ return 1; \
+ }
diff --git a/vec.c b/vec.c
index 8923f19..a8fd57a 100644
--- a/vec.c
+++ b/vec.c
@@ -24,8 +24,4 @@ void Vec_push(Arena *a, Vec *v, void *data) {
v->len++;
}
-void *_Vec_get(Vec *v, int pos) {
- // printf("get: length=%lu, cap=%lu, pos=%d\n", v->len, v->cap, pos);
- // printf("get: start=%p, asked=%p\n", v->data, v->data + v->size * pos);
- return v->data + v->size * pos;
-}
+void *_Vec_get(Vec *v, int pos) { return v->data + v->size * pos; }