diff options
| -rw-r--r-- | Makefile | 1 | ||||
| -rw-r--r-- | README.md | 3 | ||||
| -rw-r--r-- | include/testing.h | 16 | ||||
| -rw-r--r-- | src/json.c | 8 | ||||
| -rw-r--r-- | tests/test_json.c | 3 |
5 files changed, 12 insertions, 19 deletions
@@ -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\(.*==' @@ -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; \ } @@ -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); |
