Modify Str8 substr functionality

This commit is contained in:
2025-02-22 13:39:03 +00:00
parent 99f7dcd794
commit 28ac9ed8c8
5 changed files with 107 additions and 45 deletions

View File

@@ -82,14 +82,27 @@ TestFuncResult test_str8_buf(void) {
TestFuncResult test_str8_alloc_buf(void) {
bool result;
Allocator allocator = wapp_mem_arena_allocator_init(MB(10));
Allocator allocator = wapp_mem_arena_allocator_init(KB(100));
if (wapp_mem_allocator_invalid(&allocator)) {
return wapp_tester_result(false);
}
Str8 *s = wapp_str8_alloc_buf(&allocator, 4096);
result = s != NULL && s->capacity == 4096;
u64 capacity = 4096;
Str8 *s = wapp_str8_alloc_buf(&allocator, capacity);
if (!s) {
result = false;
goto TEST_ALLOC_BUF_CLEANUP;
}
result = s->capacity == capacity;
const char *cstr = "My name is Abdelrahman";
wapp_str8_copy_cstr_capped(s, cstr);
result = result && s->capacity == capacity && s->size == strlen(cstr) && memcmp(s->buf, cstr, s->size) == 0;
TEST_ALLOC_BUF_CLEANUP:
wapp_mem_arena_allocator_destroy(&allocator);
return wapp_tester_result(result);
@@ -97,7 +110,7 @@ TestFuncResult test_str8_alloc_buf(void) {
TestFuncResult test_str8_alloc_cstr(void) {
bool result;
Allocator allocator = wapp_mem_arena_allocator_init(MB(10));
Allocator allocator = wapp_mem_arena_allocator_init(KB(100));
if (wapp_mem_allocator_invalid(&allocator)) {
return wapp_tester_result(false);
}
@@ -118,7 +131,7 @@ TestFuncResult test_str8_alloc_cstr(void) {
TestFuncResult test_str8_alloc_str8(void) {
bool result;
Allocator allocator = wapp_mem_arena_allocator_init(MB(10));
Allocator allocator = wapp_mem_arena_allocator_init(KB(100));
if (wapp_mem_allocator_invalid(&allocator)) {
return wapp_tester_result(false);
}
@@ -129,7 +142,27 @@ TestFuncResult test_str8_alloc_str8(void) {
return wapp_tester_result(false);
}
result = s->size == str.size && memcmp(s->buf, str.buf, str.size) == 0;
result = wapp_str8_equal(s, &str);
wapp_mem_arena_allocator_destroy(&allocator);
return wapp_tester_result(result);
}
TestFuncResult test_str8_alloc_substr(void) {
bool result;
Allocator allocator = wapp_mem_arena_allocator_init(KB(100));
if (wapp_mem_allocator_invalid(&allocator)) {
return wapp_tester_result(false);
}
Str8 str = wapp_str8_lit("Abdelrahman");
Str8 *s = wapp_str8_alloc_substr(&allocator, &str, 3, 8);
if (!s) {
return wapp_tester_result(false);
}
result = s->size == 5 && memcmp(s->buf, "elrah", s->size) == 0;
wapp_mem_arena_allocator_destroy(&allocator);
@@ -218,20 +251,20 @@ TestFuncResult test_str8_equal(void) {
return wapp_tester_result(result);
}
TestFuncResult test_str8_substr(void) {
TestFuncResult test_str8_slice(void) {
bool result;
Str8 s = wapp_str8_lit("Different strokes for different folks");
Str8RO sub1 = wapp_str8_substr(&s, 3, 9);
Str8RO sub1 = wapp_str8_slice(&s, 3, 9);
result = sub1.size == 6 && sub1.capacity == 6;
Str8RO sub2 = wapp_str8_substr(&s, 18, 21);
Str8RO sub2 = wapp_str8_slice(&s, 18, 21);
result = result && sub2.size == 3 && sub2.capacity == 3;
Str8RO sub3 = wapp_str8_substr(&s, 5, 1);
Str8RO sub3 = wapp_str8_slice(&s, 5, 1);
result = result && sub3.size == 0 && sub3.capacity == 0;
Str8RO sub4 = wapp_str8_substr(&s, 70, 80);
Str8RO sub4 = wapp_str8_slice(&s, 70, 80);
result = result && sub4.size == 0 && sub4.capacity == 0;
return wapp_tester_result(result);
@@ -239,7 +272,7 @@ TestFuncResult test_str8_substr(void) {
TestFuncResult test_str8_concat(void) {
bool result;
Allocator arena = wapp_mem_arena_allocator_init(MB(10));
Allocator arena = wapp_mem_arena_allocator_init(KB(100));
Str8 str = wapp_str8_lit("Hello world");
Str8 suffix1 = wapp_str8_lit(" from me.");
@@ -347,7 +380,7 @@ TestFuncResult test_str8_rfind(void) {
TestFuncResult test_str8_split(void) {
bool result;
Allocator arena = wapp_mem_arena_allocator_init(MB(10));
Allocator arena = wapp_mem_arena_allocator_init(KB(100));
Str8 str = wapp_str8_lit("hello world from me");
Str8 delim1 = wapp_str8_lit(" ");
@@ -356,14 +389,14 @@ TestFuncResult test_str8_split(void) {
Str8List *list2 = wapp_str8_split(&arena, &str, &delim2);
Str8RO splits1[] = {
wapp_str8_substr(&str, 0, 5),
wapp_str8_substr(&str, 6, 11),
wapp_str8_substr(&str, 12, 16),
wapp_str8_substr(&str, 17, 19),
wapp_str8_slice(&str, 0, 5),
wapp_str8_slice(&str, 6, 11),
wapp_str8_slice(&str, 12, 16),
wapp_str8_slice(&str, 17, 19),
};
Str8RO splits2[] = {
wapp_str8_substr(&str, 0, 12),
wapp_str8_substr(&str, 16, 19),
wapp_str8_slice(&str, 0, 12),
wapp_str8_slice(&str, 16, 19),
};
u64 index1 = 0;
@@ -404,16 +437,16 @@ TestFuncResult test_str8_split(void) {
TestFuncResult test_str8_split_with_max(void) {
bool result;
Allocator arena = wapp_mem_arena_allocator_init(MB(10));
Allocator arena = wapp_mem_arena_allocator_init(KB(100));
Str8 str = wapp_str8_lit("hello world from me");
Str8 delim = wapp_str8_lit(" ");
Str8List *list = wapp_str8_split_with_max(&arena, &str, &delim, 2);
Str8RO splits[] = {
wapp_str8_substr(&str, 0, 5),
wapp_str8_substr(&str, 6, 11),
wapp_str8_substr(&str, 12, 19),
wapp_str8_slice(&str, 0, 5),
wapp_str8_slice(&str, 6, 11),
wapp_str8_slice(&str, 12, 19),
};
u64 index = 0;
@@ -439,7 +472,7 @@ TestFuncResult test_str8_split_with_max(void) {
TestFuncResult test_str8_rsplit(void) {
bool result;
Allocator arena = wapp_mem_arena_allocator_init(MB(10));
Allocator arena = wapp_mem_arena_allocator_init(KB(100));
Str8 str = wapp_str8_lit("hello world from me");
Str8 delim1 = wapp_str8_lit(" ");
@@ -448,14 +481,14 @@ TestFuncResult test_str8_rsplit(void) {
Str8List *list2 = wapp_str8_rsplit(&arena, &str, &delim2);
Str8RO splits1[] = {
wapp_str8_substr(&str, 0, 5),
wapp_str8_substr(&str, 6, 11),
wapp_str8_substr(&str, 12, 16),
wapp_str8_substr(&str, 17, 19),
wapp_str8_slice(&str, 0, 5),
wapp_str8_slice(&str, 6, 11),
wapp_str8_slice(&str, 12, 16),
wapp_str8_slice(&str, 17, 19),
};
Str8RO splits2[] = {
wapp_str8_substr(&str, 0, 12),
wapp_str8_substr(&str, 16, 19),
wapp_str8_slice(&str, 0, 12),
wapp_str8_slice(&str, 16, 19),
};
u64 index1 = 0;
@@ -496,16 +529,16 @@ TestFuncResult test_str8_rsplit(void) {
TestFuncResult test_str8_rsplit_with_max(void) {
bool result;
Allocator arena = wapp_mem_arena_allocator_init(MB(10));
Allocator arena = wapp_mem_arena_allocator_init(KB(100));
Str8 str = wapp_str8_lit("hello world from me");
Str8 delim = wapp_str8_lit(" ");
Str8List *list = wapp_str8_rsplit_with_max(&arena, &str, &delim, 2);
Str8RO splits[] = {
wapp_str8_substr(&str, 0, 11),
wapp_str8_substr(&str, 12, 16),
wapp_str8_substr(&str, 17, 19),
wapp_str8_slice(&str, 0, 11),
wapp_str8_slice(&str, 12, 16),
wapp_str8_slice(&str, 17, 19),
};
u64 index = 0;
@@ -531,7 +564,7 @@ TestFuncResult test_str8_rsplit_with_max(void) {
TestFuncResult test_str8_join(void) {
bool result;
Allocator arena = wapp_mem_arena_allocator_init(MB(10));
Allocator arena = wapp_mem_arena_allocator_init(KB(100));
Str8 str = wapp_str8_lit("hello world from me");
Str8 delim1 = wapp_str8_lit(" ");