test(hmpl): render interpolation tags
This commit is contained in:
40
package/c/hectic/test/00-logger.c
Normal file
40
package/c/hectic/test/00-logger.c
Normal file
@@ -0,0 +1,40 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include "hectic.h"
|
||||
|
||||
#define TEST_RAISE_GENERIC(LOG_MACRO, LEVEL, LEVEL_STR) do { \
|
||||
FILE *orig_stderr = stderr; \
|
||||
FILE *temp = tmpfile(); \
|
||||
char result_buffer[256]; \
|
||||
if (!temp) { perror("tmpfile"); exit(EXIT_FAILURE); } \
|
||||
stderr = temp; \
|
||||
logger_level(LEVEL); \
|
||||
const char* time_str = LOG_MACRO("message"); \
|
||||
logger_level_reset(); \
|
||||
fflush(stderr); \
|
||||
fseek(temp, 0, SEEK_SET); \
|
||||
size_t nread = fread(result_buffer, 1, sizeof(result_buffer)-1, temp); \
|
||||
result_buffer[nread] = '\0'; \
|
||||
stderr = orig_stderr; \
|
||||
fclose(temp); \
|
||||
char expected_buffer[256]; \
|
||||
sprintf(expected_buffer, "%s " LEVEL_STR " " __FILE__ ":%d message\n", time_str, __LINE__); \
|
||||
printf("DEBUG: [%s] [%s]\n", result_buffer, expected_buffer); \
|
||||
assert(strcmp(result_buffer, expected_buffer) == 0); \
|
||||
} while(0)
|
||||
|
||||
int main(void) {
|
||||
set_output_color_mode(COLOR_MODE_DISABLE);
|
||||
|
||||
TEST_RAISE_GENERIC(raise_debug, LOG_LEVEL_DEBUG, "DEBUG");
|
||||
TEST_RAISE_GENERIC(raise_log, LOG_LEVEL_LOG, "LOG");
|
||||
TEST_RAISE_GENERIC(raise_info, LOG_LEVEL_INFO, "INFO");
|
||||
TEST_RAISE_GENERIC(raise_notice, LOG_LEVEL_NOTICE, "NOTICE");
|
||||
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;
|
||||
}
|
||||
106
package/c/hectic/test/01-arena.c
Normal file
106
package/c/hectic/test/01-arena.c
Normal file
@@ -0,0 +1,106 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include "hectic.h"
|
||||
|
||||
void test_arena_init() {
|
||||
Arena arena = arena_init(128);
|
||||
assert(arena.begin != NULL);
|
||||
assert(arena.current == arena.begin);
|
||||
assert(arena.capacity == 128);
|
||||
arena_free(&arena);
|
||||
}
|
||||
|
||||
void test_arena_alloc() {
|
||||
Arena arena = arena_init(64);
|
||||
void *ptr1 = arena_alloc(&arena, 16);
|
||||
assert(ptr1 != NULL);
|
||||
void *ptr2 = arena_alloc(&arena, 16);
|
||||
assert(ptr2 != NULL);
|
||||
raise_debug("%d - %d = %d", (size_t)ptr2, (size_t)ptr1, (char *)ptr2 - (char *)ptr1);
|
||||
assert((char *)ptr2 - (char *)ptr1 == 16);
|
||||
arena_free(&arena);
|
||||
}
|
||||
|
||||
void test_arena_alloc_or_null_out_of_memory() {
|
||||
Arena arena = arena_init(32);
|
||||
void *ptr = arena_alloc_or_null(&arena, 64);
|
||||
raise_debug("%d %d %d %d", arena.begin, arena.current, arena.capacity, (size_t)ptr);
|
||||
assert(ptr == NULL);
|
||||
arena_free(&arena);
|
||||
}
|
||||
|
||||
void test_arena_reset() {
|
||||
Arena arena = arena_init(64);
|
||||
void *ptr1 = arena_alloc(&arena, 16);
|
||||
arena_reset(&arena);
|
||||
void *ptr2 = arena_alloc(&arena, 16);
|
||||
assert(ptr1 == ptr2); // same address after reset
|
||||
arena_free(&arena);
|
||||
}
|
||||
|
||||
void test_arena_null_init() {
|
||||
Arena arena = {0};
|
||||
void *ptr = arena_alloc_or_null(&arena, 32);
|
||||
assert(ptr != NULL);
|
||||
arena_free(&arena);
|
||||
}
|
||||
|
||||
void test_arena_strdup() {
|
||||
Arena arena = arena_init(64);
|
||||
const char *orig = "Hello, Arena!";
|
||||
char *copy = arena_strdup(&arena, orig);
|
||||
assert(copy != NULL);
|
||||
assert(strcmp(copy, orig) == 0);
|
||||
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);
|
||||
|
||||
test_arena_init();
|
||||
test_arena_alloc();
|
||||
test_arena_alloc_or_null_out_of_memory();
|
||||
test_arena_reset();
|
||||
test_arena_null_init();
|
||||
test_arena_strdup();
|
||||
test_arena_repstr();
|
||||
test_arena_overwrite_detection();
|
||||
printf("%s all tests passed.\n", __FILE__);
|
||||
}
|
||||
138
package/c/hectic/test/02-json.c
Normal file
138
package/c/hectic/test/02-json.c
Normal file
@@ -0,0 +1,138 @@
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include "hectic.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_to_string(&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_to_string(&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_to_string(&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_to_string(&arena, root1);
|
||||
arena_reset(&arena);
|
||||
const char *json2 = "\"another test\"";
|
||||
Json *root2 = json_parse(&arena, &json2);
|
||||
char *printed2 = json_to_string(&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;
|
||||
}
|
||||
Reference in New Issue
Block a user