aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Kagamino Lehoux <martin@lehoux.net>2025-08-23 22:12:28 +0200
committerMartin Kagamino Lehoux <martin@lehoux.net>2025-08-27 11:46:25 +0200
commit086cb73ef88a4962345b8284e47cea52da428135 (patch)
treefceb159be84afd98e68494c69208226ecf240f7c
parent37516080db6878bef6fb1e6c5542165f3f982792 (diff)
json keys typing & removed simple t_assert
-rw-r--r--Makefile1
-rw-r--r--README.md3
-rw-r--r--include/testing.h16
-rw-r--r--src/json.c8
-rw-r--r--tests/test_json.c3
5 files changed, 12 insertions, 19 deletions
diff --git a/Makefile b/Makefile
index 4c508f0..9d4845a 100644
--- a/Makefile
+++ b/Makefile
@@ -26,4 +26,3 @@ format:
check:
make test
clang-format -i --dry-run src/*.c include/*.h tests/*.c *.c --fail-on-incomplete-format
- ! rg 't_assert\(.*=='
diff --git a/README.md b/README.md
index 3e9b39b..18d97c5 100644
--- a/README.md
+++ b/README.md
@@ -2,14 +2,13 @@
## TODO
-- Vec_get not safe, I made a type mistake. And should be more efficient for loops
+- Vec_get not safe, I made a type mistake. And should be more efficient for loops. maybe i don't use that many vec and i can define the types manually
- `valgrind --leak-check=full ./your_program`
- web server, Json parsing, struct codegen
- assert not in release
- python based generate
- test helpers to load json files
- add test with backspaces weird
-- remove t_assert
## Benchmarks
diff --git a/include/testing.h b/include/testing.h
index dc070d3..754733b 100644
--- a/include/testing.h
+++ b/include/testing.h
@@ -2,13 +2,6 @@
#include <stdio.h>
#include <string.h>
-#define t_assert(fact) \
- if (!(fact)) { \
- fprintf(stderr, "Assertion failed: %s at %s:%d in %s()\n", #fact, \
- __FILE__, __LINE__, __func__); \
- return 1; \
- }
-
#define t_assert_equal(a, b) \
if (!(a == b)) { \
fprintf(stderr, \
@@ -25,10 +18,11 @@
return 1; \
}
-#define t_assert_strequal(a, b) \
- if ((a.len != b.len) || (a.len && memcmp(a.data, b.data, a.len))) { \
+#define t_assert_strequal(a, b) \
+ if ((a.len != b.len) || (a.len && memcmp(a.data, b.data, a.len))) { \
fprintf(stderr, \
- "Assertion failed: %.*s (%s) != %.*s (%s) at %s:%d in %s()\n", (i32)a.len, a.data, \
- #a, (i32)b.len, b.data, #b, __FILE__, __LINE__, __func__); \
+ "Assertion failed: %.*s (%s) != %.*s (%s) at %s:%d in %s()\n", \
+ (i32)a.len, a.data, #a, (i32)b.len, b.data, #b, __FILE__, \
+ __LINE__, __func__); \
return 1; \
}
diff --git a/src/json.c b/src/json.c
index f6dc4e6..2b78e90 100644
--- a/src/json.c
+++ b/src/json.c
@@ -14,7 +14,7 @@
JSONObject *JSONObject_new(Arena *a) {
JSONObject *object = new(a, JSONObject, 1);
object->len = 0;
- object->keys = Vec_new(a, Str, 2);
+ object->keys = Vec_new(a, StrView, 2);
object->values = Vec_new(a, JSON, 2);
return object;
}
@@ -96,9 +96,9 @@ i32 JSON_fprint(FILE *w, JSON json) {
case JSON_OBJECT:
fprintf(w, "{");
for (size i = 0; i < json.as.object.len; i++) {
- Str key = *Vec_get(json.as.object.keys, Str, i);
+ StrView key = *Vec_get(json.as.object.keys, StrView, i);
fprintf(w, "\"");
- Str_fprint(w, key);
+ StrView_fprint(w, key);
fprintf(w, "\": ");
JSON value = *Vec_get(json.as.object.values, JSON, i);
JSON_fprint(w, value);
@@ -217,7 +217,7 @@ parse_result parse_object(Arena *a, byte *src, uptr start_pos, uptr max_pos) {
parse_result result = {0};
result.pos = absorb_whitespaces(src, start_pos + 1, max_pos);
size len = 0;
- Vec keys = Vec_new(a, Str, 6);
+ Vec keys = Vec_new(a, StrView, 6);
Vec values = Vec_new(a, JSON, 6);
if (src[result.pos] == '}') {
result.pos++;
diff --git a/tests/test_json.c b/tests/test_json.c
index b5ad9af..82bb83e 100644
--- a/tests/test_json.c
+++ b/tests/test_json.c
@@ -90,7 +90,8 @@ i32 test_json_object() {
JSON doc = JSON_parse_raw(&perm, "{\"key\": \"value\"}");
t_assert_equal(doc.as.object.len, 1);
- t_assert(Str_equals(*Vec_get(doc.as.object.keys, Str, 0), S("key")));
+ StrView key = *Vec_get(doc.as.object.keys, StrView, 0);
+ t_assert_strequal(key, S("key"));
JSON value = *JSONObject_get(doc.as.object, S("key"));
t_assert_strequal(value.as.string, S("value"));
t_assert_equal(Arena_get_used(perm), 672);