Made some tests to go through

This commit is contained in:
2025-08-21 00:06:57 +01:00
parent d2bb858144
commit 59946c8831
3 changed files with 47 additions and 34 deletions

View File

@@ -73,7 +73,7 @@ void vec_init(vec_t *, u64);
void vec_free(vec_t *); void vec_free(vec_t *);
void *vec_data(vec_t *); void *vec_data(vec_t *);
void vec_ensure_free(vec_t *, u64); void vec_ensure_free(vec_t *, u64);
void vec_append(vec_t *, void *, u64); void vec_append(vec_t *, const void *const, u64);
void vec_clone(vec_t *, vec_t *); void vec_clone(vec_t *, vec_t *);
/// Symbol table /// Symbol table

43
main.c
View File

@@ -25,8 +25,6 @@ sv_t sv_copy(sv_t old)
return SV(newstr, old.size); return SV(newstr, old.size);
} }
int main(void)
{
const char *words[] = { const char *words[] = {
"aliquam", "erat", "volutpat", "nunc", "eleifend", "aliquam", "erat", "volutpat", "nunc", "eleifend",
"leo", "vitae", "magna", "in", "id", "leo", "vitae", "magna", "in", "id",
@@ -45,31 +43,46 @@ int main(void)
"euismod", "tellus", "id", "erat", "euismod", "tellus", "id", "erat",
}; };
void vec_test(void)
{
vec_t vec = {0}; vec_t vec = {0};
vec_init(&vec, 0); vec_init(&vec, 0);
for (u64 i = 0; i < ARRSIZE(words); ++i) for (u64 i = 0; i < ARRSIZE(words); ++i)
{ {
vec_append(&vec, words[i], strlen(words[i])); vec_append(&vec, words[i], strlen(words[i]));
vec_append(&vec, "\n", 1); vec_append(&vec, " ", 1);
printf("%lu/%lu, inlined?: %s\n", vec.size, vec.capacity, printf("[vec_test]: %lu/%lu, inlined?: %s\n", vec.size, vec.capacity,
vec.is_inlined ? "yes" : "no"); vec.is_inlined ? "yes" : "no");
} }
printf("%lu/%lu: %.*s\n", vec.size, vec.capacity, (int)vec.size, printf("[vec_test]: Final: %lu/%lu: %.*s\n", vec.size, vec.capacity,
vec_data(&vec)); (int)vec.size, (char *)vec_data(&vec));
vec_free(&vec); vec_free(&vec);
}
// sym_table_t table = {0}; void symtable_test(void)
// sym_table_init(&table); {
// // Let's hash the words of lorem ipsum sym_table_t table = {0};
// for (u64 i = 0; i < ARRSIZE(words); ++i) sym_table_init(&table);
// { for (u64 i = 0; i < ARRSIZE(words); ++i)
// char *ptr = sym_table_find(&table, SV(words[i], strlen(words[i]))); {
// printf("%s => %p\n", words[i], ptr); char *ptr = sym_table_find(&table, SV(words[i], strlen(words[i])));
// } printf("[symtable_test]: %s => %p\n", words[i], ptr);
}
// sym_table_cleanup(&table); printf(
"[symtable_test]: |words|=%lu, |table|= %lu => Unique word ratio: %lf\n",
ARRSIZE(words), table.count, table.count / (double)ARRSIZE(words));
sym_table_cleanup(&table);
}
int main(void)
{
vec_test();
printf("\n");
symtable_test();
return 0; return 0;
} }

2
vec.c
View File

@@ -76,7 +76,7 @@ void vec_ensure_free(vec_t *vec, u64 size)
} }
} }
void vec_append(vec_t *vec, void *ptr, u64 size) void vec_append(vec_t *vec, const void *const ptr, u64 size)
{ {
if (!vec) if (!vec)
return; return;