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

77
main.c
View File

@@ -25,51 +25,64 @@ sv_t sv_copy(sv_t old)
return SV(newstr, old.size); return SV(newstr, old.size);
} }
int main(void) const char *words[] = {
{ "aliquam", "erat", "volutpat", "nunc", "eleifend",
const char *words[] = { "leo", "vitae", "magna", "in", "id",
"aliquam", "erat", "volutpat", "nunc", "eleifend", "erat", "non", "orci", "commodo", "lobortis",
"leo", "vitae", "magna", "in", "id", "proin", "neque", "massa", "cursus", "ut",
"erat", "non", "orci", "commodo", "lobortis", "gravida", "ut", "lobortis", "eget", "lacus",
"proin", "neque", "massa", "cursus", "ut", "sed", "diam", "praesent", "fermentum", "tempor",
"gravida", "ut", "lobortis", "eget", "lacus", "tellus", "nullam", "tempus", "mauris", "ac",
"sed", "diam", "praesent", "fermentum", "tempor", "felis", "vel", "velit", "tristique", "imperdiet",
"tellus", "nullam", "tempus", "mauris", "ac", "donec", "at", "pede", "etiam", "vel",
"felis", "vel", "velit", "tristique", "imperdiet", "neque", "nec", "dui", "dignissim", "bibendum",
"donec", "at", "pede", "etiam", "vel", "vivamus", "id", "enim", "phasellus", "neque",
"neque", "nec", "dui", "dignissim", "bibendum", "orci", "porta", "a", "aliquet", "quis",
"vivamus", "id", "enim", "phasellus", "neque", "semper", "a", "massa", "phasellus", "purus",
"orci", "porta", "a", "aliquet", "quis", "pellentesque", "tristique", "imperdiet", "tortor", "nam",
"semper", "a", "massa", "phasellus", "purus", "euismod", "tellus", "id", "erat",
"pellentesque", "tristique", "imperdiet", "tortor", "nam", };
"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;