Add tests for string allocation functions

This commit is contained in:
Abdelrahman Said 2025-02-09 18:47:10 +00:00
parent 023c74d8d9
commit ff4d3c9e99
3 changed files with 86 additions and 0 deletions

View File

@ -1,4 +1,7 @@
#include "test_str8.h"
#include "mem_allocator.h"
#include "mem_arena_allocator.h"
#include "misc_utils.h"
#include "str8.h"
#include "tester.h"
#include <stdbool.h>
@ -75,6 +78,62 @@ TestFuncResult test_str8_buf(void) {
return wapp_tester_result(result);
}
TestFuncResult test_str8_buf_alloc(void) {
bool result;
Allocator allocator = wapp_mem_arena_allocator_init(MB(10));
if (wapp_mem_allocator_invalid(&allocator)) {
return wapp_tester_result(false);
}
Str8 *s = wapp_str8_buf_alloc(&allocator, 4096);
result = s != NULL && s->capacity == 4096;
wapp_mem_arena_allocator_destroy(&allocator);
return wapp_tester_result(result);
}
TestFuncResult test_str8_alloc_cstr(void) {
bool result;
Allocator allocator = wapp_mem_arena_allocator_init(MB(10));
if (wapp_mem_allocator_invalid(&allocator)) {
return wapp_tester_result(false);
}
char *str = "Abdelrahman";
u64 length = strlen(str);
Str8 *s = wapp_str8_alloc_cstr(&allocator, str);
if (!s) {
return wapp_tester_result(false);
}
result = s->size == length && memcmp(s->buf, str, length) == 0;
wapp_mem_arena_allocator_destroy(&allocator);
return wapp_tester_result(result);
}
TestFuncResult test_str8_alloc_str8(void) {
bool result;
Allocator allocator = wapp_mem_arena_allocator_init(MB(10));
if (wapp_mem_allocator_invalid(&allocator)) {
return wapp_tester_result(false);
}
Str8 str = wapp_str8_lit("Abdelrahman");
Str8 *s = wapp_str8_alloc_str8(&allocator, &str);
if (!s) {
return wapp_tester_result(false);
}
result = s->size == str.size && memcmp(s->buf, str.buf, str.size) == 0;
wapp_mem_arena_allocator_destroy(&allocator);
return wapp_tester_result(result);
}
TestFuncResult test_str8_get_index_within_bounds(void) {
bool result;
@ -142,6 +201,25 @@ TestFuncResult test_str8_set(void) {
return wapp_tester_result(result);
}
TestFuncResult test_str8_substr(void) {
bool result;
Str8 s = wapp_str8_lit("Different strokes for different folks");
Str8RO sub1 = wapp_str8_substr(&s, 3, 9);
result = sub1.size == 6 && sub1.capacity == 6;
Str8RO sub2 = wapp_str8_substr(&s, 18, 21);
result = result && sub2.size == 3 && sub2.capacity == 3;
Str8RO sub3 = wapp_str8_substr(&s, 5, 1);
result = result && sub3.size == 0 && sub3.capacity == 0;
Str8RO sub4 = wapp_str8_substr(&s, 70, 80);
result = result && sub4.size == 0 && sub4.capacity == 0;
return wapp_tester_result(result);
}
TestFuncResult test_str8_find(void) {
bool result;
Str8RO s = wapp_str8_lit("Do as I say, not as I do");

View File

@ -10,9 +10,13 @@ BEGIN_C_LINKAGE
TestFuncResult test_str8_lit(void);
TestFuncResult test_str8_lit_ro(void);
TestFuncResult test_str8_buf(void);
TestFuncResult test_str8_buf_alloc(void);
TestFuncResult test_str8_alloc_cstr(void);
TestFuncResult test_str8_alloc_str8(void);
TestFuncResult test_str8_get_index_within_bounds(void);
TestFuncResult test_str8_get_index_out_of_bounds(void);
TestFuncResult test_str8_set(void);
TestFuncResult test_str8_substr(void);
TestFuncResult test_str8_find(void);
TestFuncResult test_str8_rfind(void);

View File

@ -15,9 +15,13 @@ int main(void) {
test_str8_lit,
test_str8_lit_ro,
test_str8_buf,
test_str8_buf_alloc,
test_str8_alloc_cstr,
test_str8_alloc_str8,
test_str8_get_index_within_bounds,
test_str8_get_index_out_of_bounds,
test_str8_set,
test_str8_substr,
test_str8_find,
test_str8_rfind,
test_cpath_join_path,