test: hectic C: debug constructor
This commit is contained in:
@@ -1,68 +1,97 @@
|
|||||||
#include "hectic.h"
|
#include "hectic.h"
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
typedef struct TestStruct TestStruct;
|
typedef struct Struct Struct;
|
||||||
|
|
||||||
struct TestStruct {
|
struct Struct {
|
||||||
int a;
|
int a;
|
||||||
int b;
|
int b;
|
||||||
char *c;
|
char *c;
|
||||||
TestStruct *next;
|
Struct *next;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct TestStruct2 TestStruct2;
|
typedef struct Struct2 Struct2;
|
||||||
|
|
||||||
struct TestStruct2 {
|
struct Struct2 {
|
||||||
int a;
|
int a;
|
||||||
char *c;
|
char *c;
|
||||||
float f;
|
float f;
|
||||||
TestStruct2 *left;
|
Struct2 *left;
|
||||||
TestStruct *other;
|
Struct *other;
|
||||||
};
|
};
|
||||||
|
|
||||||
char *test_struct_to_debug_str(Arena *arena, char *name, TestStruct *self, PtrSet *visited) {
|
char *struct_to_debug_str(Arena *arena, char *name, Struct *self, PtrSet *visited) {
|
||||||
raise_trace("test_struct_to_debug_str: name: %s, self: %p, visited: %p", name, self, visited);
|
raise_trace("struct_to_debug_str: name: %s, self: %p, visited: %p", name, self, visited);
|
||||||
|
|
||||||
char *result = arena_alloc(arena, MEM_KiB);
|
char *result = arena_alloc(arena, MEM_KiB);
|
||||||
STRUCT_TO_DEBUG_STR(arena, result, TestStruct, name, self, visited, 3,
|
STRUCT_TO_DEBUG_STR(arena, result, Struct, name, self, visited, 3,
|
||||||
NUMBER_TO_DEBUG_STR(arena, "a", self->a),
|
NUMBER_TO_DEBUG_STR(arena, "a", self->a),
|
||||||
NUMBER_TO_DEBUG_STR(arena, "b", self->b),
|
NUMBER_TO_DEBUG_STR(arena, "b", self->b),
|
||||||
test_struct_to_debug_str(arena, "next", self->next, visited)
|
struct_to_debug_str(arena, "next", self->next, visited)
|
||||||
);
|
);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *test_struct2_to_debug_str(Arena *arena, char *name, TestStruct2 *self, PtrSet *visited) {
|
char *struct2_to_debug_str(Arena *arena, char *name, Struct2 *self, PtrSet *visited) {
|
||||||
raise_trace("test_struct2_to_debug_str: name: %s, self: %p, visited: %p", name, self, visited);
|
raise_trace("struct2_to_debug_str: name: %s, self: %p, visited: %p", name, self, visited);
|
||||||
|
|
||||||
char *result = arena_alloc(arena, MEM_KiB);
|
char *result = arena_alloc(arena, MEM_KiB);
|
||||||
STRUCT_TO_DEBUG_STR(arena, result, TestStruct, name, self, visited, 5,
|
STRUCT_TO_DEBUG_STR(arena, result, Struct2, name, self, visited, 5,
|
||||||
NUMBER_TO_DEBUG_STR(arena, "a", self->a),
|
NUMBER_TO_DEBUG_STR(arena, "a", self->a),
|
||||||
NUMBER_TO_DEBUG_STR(arena, "f", self->f),
|
NUMBER_TO_DEBUG_STR(arena, "f", self->f),
|
||||||
STRING_TO_DEBUG_STR(arena, "c", self->c),
|
STRING_TO_DEBUG_STR(arena, "c", self->c),
|
||||||
test_struct_to_debug_str(arena, "other", self->other, visited),
|
struct_to_debug_str(arena, "other", self->other, visited),
|
||||||
test_struct2_to_debug_str(arena, "left", self->left, visited)
|
struct2_to_debug_str(arena, "left", self->left, visited)
|
||||||
);
|
);
|
||||||
|
|
||||||
raise_trace("returning result");
|
raise_trace("returning result");
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void test_struct_to_debug_str(Arena *arena) {
|
||||||
|
// Mock a struct with a cycle
|
||||||
|
Struct test_struct = {.a = 1, .b = 2, .next = NULL};
|
||||||
|
test_struct.next = &test_struct;
|
||||||
|
|
||||||
|
PtrSet *visited = ptrset_init(arena);
|
||||||
|
char *result = struct_to_debug_str(arena, "struct", &test_struct, visited);
|
||||||
|
raise_notice("result: %s", result);
|
||||||
|
|
||||||
|
char *check = arena_alloc(arena, MEM_KiB);
|
||||||
|
sprintf(check, "Struct struct = {a = 1, b = 2, Struct next = {cycle detected} %p} %p", (void*)&test_struct, (void*)&test_struct);
|
||||||
|
raise_notice("check: %s", check);
|
||||||
|
assert(strcmp(result, check) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_struct2_to_debug_str(Arena *arena) {
|
||||||
|
// Mock a struct with some structs inside, null and cycle
|
||||||
|
Struct test_struct = {.a = 1, .b = 2, .next = NULL};
|
||||||
|
test_struct.next = &test_struct;
|
||||||
|
|
||||||
|
Struct2 test_struct2 = {.a = 1, .c = "hello", .f = 3.14, .left = NULL, .other = &test_struct};
|
||||||
|
|
||||||
|
PtrSet *visited = ptrset_init(arena);
|
||||||
|
char *result = struct2_to_debug_str(arena, "struct2", &test_struct2, visited);
|
||||||
|
raise_notice("result: %s", result);
|
||||||
|
char *check = arena_alloc(arena, MEM_KiB);
|
||||||
|
sprintf(check, "Struct2 struct2 = {a = 1, f = 3, c = %p \"hello\", Struct other = {a = 1, b = 2, Struct next = {cycle detected} %p} %p, Struct2 left = NULL} %p", (void*)test_struct2.c,(void*)&test_struct, (void*)&test_struct, (void*)&test_struct2);
|
||||||
|
raise_notice("check: %s", check);
|
||||||
|
assert(strcmp(result, check) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
printf("%sRunning %s%s%s\n", OPTIONAL_COLOR(COLOR_GREEN), OPTIONAL_COLOR(COLOR_CYAN), __FILE__, OPTIONAL_COLOR(COLOR_RESET));
|
printf("%sRunning %s%s%s\n", OPTIONAL_COLOR(COLOR_GREEN), OPTIONAL_COLOR(COLOR_CYAN), __FILE__, OPTIONAL_COLOR(COLOR_RESET));
|
||||||
init_logger();
|
init_logger();
|
||||||
|
|
||||||
TestStruct test_struct = {.a = 1, .b = 2, .next = NULL};
|
Arena arena = arena_init(MEM_MiB);
|
||||||
test_struct.next = &test_struct;
|
|
||||||
Arena lifetime = arena_init(MEM_MiB);
|
|
||||||
PtrSet *visited = ptrset_init(&lifetime);
|
|
||||||
raise_notice("%s", test_struct_to_debug_str(&lifetime, "test_struct", &test_struct, visited));
|
|
||||||
|
|
||||||
TestStruct2 test_struct2 = {.a = 1, .c = "hello", .f = 3.14, .left = NULL, .other = &test_struct};
|
printf("%sTesting struct_to_debug_str%s\n", OPTIONAL_COLOR(COLOR_GREEN), OPTIONAL_COLOR(COLOR_RESET));
|
||||||
visited = ptrset_init(&lifetime);
|
test_struct_to_debug_str(&arena);
|
||||||
raise_notice("%s", test_struct2_to_debug_str(&lifetime, "test_struct2", &test_struct2, visited));
|
|
||||||
|
|
||||||
|
printf("%sTesting struct_to_debug_str2%s\n", OPTIONAL_COLOR(COLOR_GREEN), OPTIONAL_COLOR(COLOR_RESET));
|
||||||
|
test_struct2_to_debug_str(&arena);
|
||||||
|
|
||||||
arena_free(&lifetime);
|
arena_free(&arena);
|
||||||
printf("%sAll tests passed %s%s%s\n", OPTIONAL_COLOR(COLOR_GREEN), OPTIONAL_COLOR(COLOR_CYAN), __FILE__, OPTIONAL_COLOR(COLOR_RESET));
|
printf("%sAll tests passed %s%s%s\n", OPTIONAL_COLOR(COLOR_GREEN), OPTIONAL_COLOR(COLOR_CYAN), __FILE__, OPTIONAL_COLOR(COLOR_RESET));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user