feat: hectic C: debug string constructor init
This commit is contained in:
@@ -26,7 +26,7 @@
|
||||
} while(0)
|
||||
|
||||
int main(void) {
|
||||
logger_level(LOG_LEVEL_DEBUG);
|
||||
printf("Running %s\n", __FILE__);
|
||||
|
||||
TEST_RAISE_GENERIC(raise_debug, LOG_LEVEL_DEBUG, "DEBUG");
|
||||
TEST_RAISE_GENERIC(raise_log, LOG_LEVEL_LOG, "LOG");
|
||||
|
||||
98
package/c/hectic/test/01-debug.c
Executable file
98
package/c/hectic/test/01-debug.c
Executable file
@@ -0,0 +1,98 @@
|
||||
#include "hectic.h"
|
||||
|
||||
typedef struct TestStruct TestStruct;
|
||||
|
||||
struct TestStruct {
|
||||
int a;
|
||||
int b;
|
||||
char *c;
|
||||
TestStruct *next;
|
||||
};
|
||||
|
||||
typedef struct TestStruct2 TestStruct2;
|
||||
|
||||
struct TestStruct2 {
|
||||
int a;
|
||||
char *c;
|
||||
float f;
|
||||
TestStruct2 *left;
|
||||
TestStruct *other;
|
||||
};
|
||||
|
||||
|
||||
typedef struct PtrSet {
|
||||
void **data;
|
||||
size_t size;
|
||||
size_t capacity;
|
||||
} PtrSet;
|
||||
|
||||
static bool debug_ptrset_contains(PtrSet *set, void *ptr) {
|
||||
for (size_t i = 0; i < set->size; i++) {
|
||||
if (set->data[i] == ptr)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static void debug_ptrset_add(PtrSet *set, void *ptr) {
|
||||
if (set->size == set->capacity) {
|
||||
set->capacity = set->capacity ? set->capacity * 2 : 4;
|
||||
set->data = realloc(set->data, set->capacity * sizeof(void*));
|
||||
}
|
||||
set->data[set->size++] = ptr;
|
||||
}
|
||||
|
||||
|
||||
#define STRING_TO_DEBUG_STR(arena, name, string) \
|
||||
arena_strdup_fmt__(__FILE__, __func__, __LINE__, arena, "%s = %p \"%s\"", name, string, string)
|
||||
|
||||
#define NUMBER_TO_DEBUG_STR(arena, name, number) \
|
||||
arena_strdup_fmt__(__FILE__, __func__, __LINE__, arena, "%s = %d", name, number)
|
||||
|
||||
#define STRUCT_TO_DEBUG_STR(arena, type, name, ptr, ...) __extension__ ({ \
|
||||
char *result; \
|
||||
if ((ptr) == NULL) { \
|
||||
result = arena_strdup_fmt__(__FILE__, __func__, __LINE__, arena, "%s %s = NULL", #type, name); \
|
||||
} else { \
|
||||
char* fields = arena_strdup_fmt__(__FILE__, __func__, __LINE__, arena, "%s, %s, %s", __VA_ARGS__); \
|
||||
result = arena_strdup_fmt__(__FILE__, __func__, __LINE__, arena, "%s %s = {%s} %p", #type, name, fields, ptr); \
|
||||
} \
|
||||
result; \
|
||||
})
|
||||
|
||||
#define test_struct_to_debug_str(arena, name, self) test_struct_to_debug_str__(arena, name, self)
|
||||
|
||||
char *test_struct_to_debug_str__(Arena *arena, char *name, TestStruct *self) {
|
||||
if (name == NULL) {
|
||||
name = "$1";
|
||||
}
|
||||
|
||||
char *result = STRUCT_TO_DEBUG_STR(arena, TestStruct, name, self,
|
||||
NUMBER_TO_DEBUG_STR(arena, "a", self->a),
|
||||
NUMBER_TO_DEBUG_STR(arena, "b", self->b),
|
||||
test_struct_to_debug_str__(arena, "next", self->next)
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
//char *test_struct_to_debug_str__(Arena *arena, char *name, char *type, TestStruct *self) {
|
||||
// char *result = arena_strdup_fmt(arena, "%s %s{", name, type);
|
||||
//
|
||||
// result = arena_strdup_fmt(arena, "%s%s", result, NUMBER_TO_DEBUG_STR(arena, "a", self->a));
|
||||
// result = arena_strdup_fmt(arena, "%s%s", result, NUMBER_TO_DEBUG_STR(arena, "b", self->b));
|
||||
// result = arena_strdup_fmt(arena, "%s%s", result, test_struct_to_debug_str__(arena, "next", TestStruct, self->next));
|
||||
//
|
||||
// result = arena_strdup_fmt(arena, "%s} (%p)", result, self);
|
||||
// return result;
|
||||
//}
|
||||
|
||||
int main(void) {
|
||||
printf("%sRunning %s%s%s\n", OPTIONAL_COLOR(COLOR_GREEN), OPTIONAL_COLOR(COLOR_CYAN), __FILE__, OPTIONAL_COLOR(COLOR_RESET));
|
||||
init_logger();
|
||||
|
||||
TestStruct test_struct = {.a = 1, .b = 2, .next = NULL};
|
||||
raise_notice("%s", test_struct_to_debug_str(DISPOSABLE_ARENA, "test_struct", &test_struct));
|
||||
|
||||
printf("%sAll tests passed %s%s%s\n", OPTIONAL_COLOR(COLOR_GREEN), OPTIONAL_COLOR(COLOR_CYAN), __FILE__, OPTIONAL_COLOR(COLOR_RESET));
|
||||
return 0;
|
||||
}
|
||||
@@ -91,7 +91,7 @@ void test_arena_overwrite_detection() {
|
||||
}
|
||||
|
||||
int main() {
|
||||
logger_level(LOG_LEVEL_DEBUG);
|
||||
init_logger();
|
||||
|
||||
test_arena_init();
|
||||
test_arena_alloc();
|
||||
@@ -124,7 +124,7 @@ static void test_arena_reset_reuse(void) {
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
logger_level(LOG_LEVEL_DEBUG);
|
||||
init_logger();
|
||||
|
||||
test_parse_json_object();
|
||||
test_parse_json_number();
|
||||
@@ -1,92 +0,0 @@
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "hectic.h"
|
||||
|
||||
#define ARENA_SIZE 1024 * 1024
|
||||
|
||||
static char *remove_all_spaces(char *s) {
|
||||
char *new_s = NULL;
|
||||
while (*s) {
|
||||
if (*s != ' ' && *s != '\t' && *s != '\n') {
|
||||
new_s = s;
|
||||
}
|
||||
s++;
|
||||
}
|
||||
return new_s;
|
||||
}
|
||||
|
||||
static void test_template_node_to_debug_str(Arena *arena) {
|
||||
TemplateNode *root = arena_alloc(arena, sizeof(TemplateNode));
|
||||
root->type = TEMPLATE_NODE_TEXT;
|
||||
root->value.text.content = arena_strncpy(arena, "Hello", 5);
|
||||
|
||||
root->next = arena_alloc(arena, sizeof(TemplateNode));
|
||||
root->next->type = TEMPLATE_NODE_INTERPOLATE;
|
||||
root->next->value.interpolate.key = arena_strncpy(arena, "name", 4);
|
||||
|
||||
root->next->next = arena_alloc(arena, sizeof(TemplateNode));
|
||||
root->next->next->type = TEMPLATE_NODE_TEXT;
|
||||
root->next->next->value.text.content = arena_strncpy(arena, "!", 1);
|
||||
|
||||
char *debug_str = template_node_to_debug_str(arena, root);
|
||||
|
||||
raise_notice("debug_str: %s", debug_str);
|
||||
assert(strcmp(
|
||||
remove_all_spaces(debug_str),
|
||||
remove_all_spaces("" \
|
||||
"[" \
|
||||
" {" \
|
||||
" \"type\":\"TEXT\"," \
|
||||
" \"content\":{" \
|
||||
" \"content\":\"Hello\"" \
|
||||
" }" \
|
||||
" }," \
|
||||
" {" \
|
||||
" \"type\":\"INTERPOLATE\"," \
|
||||
" \"content\":{" \
|
||||
" \"key\":\"name\"" \
|
||||
" }" \
|
||||
" }," \
|
||||
" {" \
|
||||
" \"type\":\"TEXT\"," \
|
||||
" \"content\":{" \
|
||||
" \"content\":\"!\"" \
|
||||
" }" \
|
||||
" }" \
|
||||
"]")) == 0);
|
||||
}
|
||||
|
||||
static void test_template_parse(Arena *arena, TemplateConfig *config) {
|
||||
const char *template = "Hello {% name %}!";
|
||||
TemplateResult *result = template_parse(arena, &template, config);
|
||||
|
||||
Arena *debug_arena = DISPOSABLE_ARENA;
|
||||
const char *debug_str = template_node_to_debug_str(debug_arena, &result->Result.node);
|
||||
raise_notice("debug_str: %s", debug_str);
|
||||
raise_notice("result: %s", json_to_pretty_str(debug_arena, json_parse(debug_arena, &debug_str)));
|
||||
assert(result->type == TEMPLATE_RESULT_NODE);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
init_logger();
|
||||
|
||||
Arena arena = arena_init(ARENA_SIZE);
|
||||
|
||||
TemplateConfig config = template_default_config();
|
||||
|
||||
printf("%sRunning template parser tests...%s\n", OPTIONAL_COLOR(COLOR_GREEN), OPTIONAL_COLOR(COLOR_RESET));
|
||||
|
||||
test_template_node_to_debug_str(&arena);
|
||||
printf("%sTest 0: template_node_to_debug_str passed%s\n", OPTIONAL_COLOR(COLOR_GREEN), OPTIONAL_COLOR(COLOR_RESET));
|
||||
arena_reset(&arena);
|
||||
|
||||
test_template_parse(&arena, &config);
|
||||
printf("%sTest 1: template_parse passed%s\n", OPTIONAL_COLOR(COLOR_GREEN), OPTIONAL_COLOR(COLOR_RESET));
|
||||
arena_reset(&arena);
|
||||
|
||||
arena_free(&arena);
|
||||
printf("%s%s all tests passed.%s\n", OPTIONAL_COLOR(COLOR_GREEN), __FILE__, OPTIONAL_COLOR(COLOR_RESET));
|
||||
return 0;
|
||||
}
|
||||
@@ -83,7 +83,7 @@ void test_slice_string() {
|
||||
}
|
||||
|
||||
int main() {
|
||||
logger_level(LOG_LEVEL_DEBUG);
|
||||
init_logger();
|
||||
|
||||
test_slice_create();
|
||||
test_slice_subslice();
|
||||
92
package/c/hectic/test/06-templater.c
Executable file
92
package/c/hectic/test/06-templater.c
Executable file
@@ -0,0 +1,92 @@
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "hectic.h"
|
||||
|
||||
#define ARENA_SIZE 1024 * 1024
|
||||
|
||||
//static char *remove_all_spaces(char *s) {
|
||||
// char *new_s = NULL;
|
||||
// while (*s) {
|
||||
// if (*s != ' ' && *s != '\t' && *s != '\n') {
|
||||
// new_s = s;
|
||||
// }
|
||||
// s++;
|
||||
// }
|
||||
// return new_s;
|
||||
//}
|
||||
|
||||
static void test_template_node_to_debug_str(Arena *arena) {
|
||||
TemplateNode *root = arena_alloc(arena, sizeof(TemplateNode));
|
||||
root->type = TEMPLATE_NODE_TEXT;
|
||||
root->value.text.content = arena_strncpy(arena, "Hello", 5);
|
||||
|
||||
root->next = arena_alloc(arena, sizeof(TemplateNode));
|
||||
root->next->type = TEMPLATE_NODE_INTERPOLATE;
|
||||
root->next->value.interpolate.key = arena_strncpy(arena, "name", 4);
|
||||
|
||||
root->next->next = arena_alloc(arena, sizeof(TemplateNode));
|
||||
root->next->next->type = TEMPLATE_NODE_TEXT;
|
||||
root->next->next->value.text.content = arena_strncpy(arena, "!", 1);
|
||||
|
||||
char *debug_str = template_node_to_debug_str(arena, root);
|
||||
|
||||
raise_notice("debug_str: %s", debug_str);
|
||||
//assert(strcmp(
|
||||
// remove_all_spaces(debug_str),
|
||||
// remove_all_spaces(""
|
||||
// "["
|
||||
// " {"
|
||||
// " \"type\":\"TEXT\","
|
||||
// " \"content\":{"
|
||||
// " \"content\":\"Hello\""
|
||||
// " }"
|
||||
// " },"
|
||||
// " {"
|
||||
// " \"type\":\"INTERPOLATE\","
|
||||
// " \"content\":{"
|
||||
// " \"key\":\"name\""
|
||||
// " }"
|
||||
// " },"
|
||||
// " {"
|
||||
// " \"type\":\"TEXT\","
|
||||
// " \"content\":{"
|
||||
// " \"content\":\"!\""
|
||||
// " }"
|
||||
// " }"
|
||||
// "]")) == 0);
|
||||
}
|
||||
|
||||
//static void test_template_parse(Arena *arena, TemplateConfig *config) {
|
||||
// const char *template = "Hello {% name %}!";
|
||||
// TemplateResult *result = template_parse(arena, &template, config);
|
||||
//
|
||||
// Arena *debug_arena = DISPOSABLE_ARENA;
|
||||
// const char *debug_str = template_node_to_debug_str(debug_arena, &result->Result.node);
|
||||
// raise_notice("debug_str: %s", debug_str);
|
||||
// raise_notice("result: %s", json_to_pretty_str(debug_arena, json_parse(debug_arena, &debug_str)));
|
||||
// assert(result->type == TEMPLATE_RESULT_NODE);
|
||||
//}
|
||||
|
||||
int main(void) {
|
||||
init_logger();
|
||||
|
||||
Arena arena = arena_init(ARENA_SIZE);
|
||||
|
||||
//TemplateConfig config = template_default_config();
|
||||
|
||||
printf("%sRunning template parser tests...%s\n", OPTIONAL_COLOR(COLOR_GREEN), OPTIONAL_COLOR(COLOR_RESET));
|
||||
|
||||
test_template_node_to_debug_str(&arena);
|
||||
printf("%sTest 0: template_node_to_debug_str passed%s\n", OPTIONAL_COLOR(COLOR_GREEN), OPTIONAL_COLOR(COLOR_RESET));
|
||||
arena_reset(&arena);
|
||||
|
||||
//test_template_parse(&arena, &config);
|
||||
//printf("%sTest 1: template_parse passed%s\n", OPTIONAL_COLOR(COLOR_GREEN), OPTIONAL_COLOR(COLOR_RESET));
|
||||
//arena_reset(&arena);
|
||||
|
||||
arena_free(&arena);
|
||||
printf("%s%s all tests passed.%s\n", OPTIONAL_COLOR(COLOR_GREEN), __FILE__, OPTIONAL_COLOR(COLOR_RESET));
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user