feat(hectic C): json api

This commit is contained in:
2025-03-22 01:22:36 +00:00
parent aff923d470
commit bcaca5038d
11 changed files with 680 additions and 163 deletions

View File

@@ -35,5 +35,6 @@ int main(void) {
TEST_RAISE_GENERIC(raise_warn, LOG_LEVEL_WARN, "WARN");
TEST_RAISE_GENERIC(raise_exception, LOG_LEVEL_EXCEPTION, "EXCEPTION");
printf("%s all tests passed.\n", __FILE__);
return 0;
}

View File

@@ -56,6 +56,40 @@ void test_arena_strdup() {
arena_free(&arena);
}
void test_arena_repstr() {
Arena arena = arena_init(128);
const char *original = "Hello, World!";
// Replace substring starting at index 5, length 3 (", W") with " -"
// According to the macro logic, the suffix is taken from original[5+3+1] onward.
// That results in: "Hello" + " -" + "rld!" = "Hello -rld!"
char *result = arena_repstr(&arena, original, 5, 3, " -");
assert(strcmp(result, "Hello -rld!") == 0);
arena_free(&arena);
}
void test_arena_overwrite_detection() {
Arena arena = arena_init(128);
char *s1 = arena_alloc(&arena, 6);
strcpy(s1, "hello");
char *s2 = arena_alloc(&arena, 6);
strcpy(s2, "world");
assert(strcmp(s1, "hello") == 0);
assert(strcmp(s2, "world") == 0);
// Force allocation near capacity
void *large = arena_alloc_or_null(&arena, 100);
assert(large != NULL || arena.current == arena.begin + arena.capacity); // If NULL, out of memory
// Check strings again
assert(strcmp(s1, "hello") == 0);
assert(strcmp(s2, "world") == 0);
arena_free(&arena);
}
int main() {
set_output_color_mode(COLOR_MODE_DISABLE);
logger_level(LOG_LEVEL_DEBUG);
@@ -66,5 +100,7 @@ int main() {
test_arena_reset();
test_arena_null_init();
test_arena_strdup();
printf("All tests passed.\n");
test_arena_repstr();
test_arena_overwrite_detection();
printf("%s all tests passed.\n", __FILE__);
}

View File

@@ -0,0 +1,138 @@
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "chectic.h"
#define ARENA_SIZE 1024 * 1024
// Test 1: Parse JSON object with a string value.
static void test_parse_json_object(void) {
Arena arena = arena_init(ARENA_SIZE);
const char *json = "{\"key\":\"value\"}";
Json *root = json_parse(&arena, &json);
assert(root->type == JSON_OBJECT);
Json *child = root->child;
assert(child && strcmp(child->key, "key") == 0);
assert(child->type == JSON_STRING);
assert(strcmp(child->string, "value") == 0);
arena_free(&arena);
}
// Test 2: Parse JSON number.
static void test_parse_json_number(void) {
Arena arena = arena_init(ARENA_SIZE);
const char *json = "42";
Json *root = json_parse(&arena, &json);
assert(root->type == JSON_NUMBER);
assert(root->number == 42);
arena_free(&arena);
}
// Test 3: Parse JSON string.
static void test_parse_json_string(void) {
Arena arena = {0};
const char *json = "\"hello\"";
Json *root = json_parse(&arena, &json);
assert(root->type == JSON_STRING);
assert(strcmp(root->string, "hello") == 0);
arena_free(&arena);
}
// Test 4: Get object items by key.
static void test_get_object_items(void) {
Arena arena = arena_init(ARENA_SIZE);
const char *json = "{\"a\":\"1\", \"b\":2}";
Json *root = json_parse(&arena, &json);
Json *item_a = json_get_object_item(root, "a");
assert(item_a && item_a->type == JSON_STRING);
assert(strcmp(item_a->string, "1") == 0);
Json *item_b = json_get_object_item(root, "b");
assert(item_b && item_b->type == JSON_NUMBER);
assert(item_b->number == 2);
arena_free(&arena);
}
// Test 5: Print JSON object.
static void test_print_json_object(void) {
Arena arena = arena_init(ARENA_SIZE);
const char *json = "{\"key\":\"value\", \"num\":3.14}";
Json *root = json_parse(&arena, &json);
char *printed = json_print(&arena, root);
assert(strstr(printed, "\"key\":") != NULL);
assert(strstr(printed, "\"value\"") != NULL);
assert(strstr(printed, "\"num\":") != NULL);
assert(strstr(printed, "3.14") != NULL);
arena_free(&arena);
}
// Test 6: Print JSON number.
static void test_print_json_number(void) {
Arena arena = arena_init(ARENA_SIZE);
const char *json = "123.456";
Json *root = json_parse(&arena, &json);
char *printed = json_print(&arena, root);
double val = atof(printed);
assert(val == 123.456);
arena_free(&arena);
}
// Test 7: Print JSON string.
static void test_print_json_string(void) {
Arena arena = arena_init(ARENA_SIZE);
const char *json = "\"test string\"";
Json *root = json_parse(&arena, &json);
char *printed = json_print(&arena, root);
assert(strcmp(printed, "\"test string\"") == 0);
arena_free(&arena);
}
// Test 8: Nested JSON object.
static void test_nested_json_object(void) {
Arena arena = arena_init(1024 * 1024);
const char *json = "{\"outer\":{\"inner\":100}}";
Json *root = json_parse(&arena, &json);
assert(root != NULL);
assert(root->type == JSON_OBJECT);
Json *outer = json_get_object_item(root, "outer");
assert(outer != NULL);
assert(outer->type == JSON_OBJECT);
Json *inner = json_get_object_item(outer, "inner");
assert(inner != NULL);
assert(inner->type == JSON_NUMBER);
assert(inner->number == 100);
arena_free(&arena);
}
// Test 9: Arena reset and reuse.
static void test_arena_reset_reuse(void) {
Arena arena = arena_init(ARENA_SIZE);
const char *json1 = "{\"key\":\"value\"}";
Json *root1 = json_parse(&arena, &json1);
char *printed1 = json_print(&arena, root1);
arena_reset(&arena);
const char *json2 = "\"another test\"";
Json *root2 = json_parse(&arena, &json2);
char *printed2 = json_print(&arena, root2);
assert(strcmp(printed2, "\"another test\"") == 0);
arena_free(&arena);
}
int main(void) {
test_parse_json_object();
test_parse_json_number();
test_parse_json_string();
test_get_object_items();
test_print_json_object();
test_print_json_number();
test_print_json_string();
test_nested_json_object();
test_arena_reset_reuse();
printf("%s all tests passed.\n", __FILE__);
return 0;
}