Replace char * with Str8 in cpath dirup

This commit is contained in:
2025-02-22 22:09:04 +00:00
parent d9314fb41e
commit dbedcb3100
3 changed files with 149 additions and 121 deletions

View File

@@ -1,5 +1,8 @@
#include "test_cpath.h"
#include "cpath.h"
#include "mem_allocator.h"
#include "mem_arena_allocator.h"
#include "misc_utils.h"
#include "str8.h"
#include "tester.h"
#include <string.h>
@@ -84,100 +87,101 @@ TestFuncResult test_cpath_join_path(void) {
}
TestFuncResult test_cpath_dirname(void) {
char dst[MAIN_BUF_SIZE] = {0};
char expected[MAIN_BUF_SIZE] = {0};
char tmp[TMP_BUF_SIZE] = {0};
snprintf(tmp, 2, "%c", PATH_SEP);
wapp_cpath_dirname(dst, tmp);
bool result = strcmp(dst, tmp) == 0;
if (!result) {
goto TEST_DIRNAME_EXIT;
Allocator arena = wapp_mem_arena_allocator_init(MB(8));
if (wapp_mem_allocator_invalid(&arena)) {
return wapp_tester_result(false);
}
memset(dst, 0, strlen(dst));
wapp_cpath_dirname(dst, "home");
result = strcmp(dst, ".") == 0;
if (!result) {
goto TEST_DIRNAME_EXIT;
}
bool result;
Str8 *output = NULL;
memset(dst, 0, strlen(dst));
wapp_cpath_dirname(dst, "");
result = strcmp(dst, ".") == 0;
if (!result) {
goto TEST_DIRNAME_EXIT;
}
Str8 expected = wapp_str8_buf(MAIN_BUF_SIZE);
Str8 tmp = wapp_str8_buf(TMP_BUF_SIZE);
memset(dst, 0, strlen(dst));
snprintf(tmp, TMP_BUF_SIZE, "%chome%ctest", PATH_SEP, PATH_SEP);
snprintf(expected, MAIN_BUF_SIZE, "%chome", PATH_SEP);
wapp_cpath_dirname(dst, tmp);
result = strcmp(dst, expected) == 0;
if (!result) {
goto TEST_DIRNAME_EXIT;
}
// CASE 1
wapp_str8_format(&tmp, "%c", PATH_SEP);
wapp_str8_format(&expected, "%c", PATH_SEP);
memset(dst, 0, strlen(dst));
memset(tmp, 0, strlen(tmp));
memset(expected, 0, strlen(expected));
snprintf(tmp, TMP_BUF_SIZE, "%chome%ctest%c", PATH_SEP, PATH_SEP, PATH_SEP);
snprintf(expected, MAIN_BUF_SIZE, "%chome", PATH_SEP);
wapp_cpath_dirname(dst, tmp);
result = strcmp(dst, expected) == 0;
output = wapp_cpath_dirname(&arena, &tmp);
result = output != NULL && wapp_str8_equal(output, &expected);
// CASE 2
wapp_str8_format(&expected, "%s", ".");
output = wapp_cpath_dirname(&arena, &wapp_str8_lit("home"));
result = result && output != NULL && wapp_str8_equal(output, &expected);
// CASE 3
output = wapp_cpath_dirname(&arena, &wapp_str8_lit(""));
result = result && output != NULL && wapp_str8_equal(output, &expected);
// CASE 4
wapp_str8_format(&tmp, "%chome%ctest", PATH_SEP, PATH_SEP);
wapp_str8_format(&expected, "%chome", PATH_SEP);
output = wapp_cpath_dirname(&arena, &tmp);
result = result && output != NULL && wapp_str8_equal(output, &expected);
// CASE 5
wapp_str8_format(&tmp, "%chome%ctest%c", PATH_SEP, PATH_SEP, PATH_SEP);
wapp_str8_format(&expected, "%chome", PATH_SEP);
output = wapp_cpath_dirname(&arena, &tmp);
result = result && output != NULL && wapp_str8_equal(output, &expected);
wapp_mem_arena_allocator_destroy(&arena);
TEST_DIRNAME_EXIT:
return wapp_tester_result(result);
}
TestFuncResult test_cpath_dirup(void) {
char dst[MAIN_BUF_SIZE] = {0};
char expected[MAIN_BUF_SIZE] = {0};
char tmp[TMP_BUF_SIZE] = {0};
snprintf(tmp, 2, "%c", PATH_SEP);
wapp_cpath_dirup(dst, 3, tmp);
bool result = strcmp(dst, tmp) == 0;
if (!result) {
goto TEST_DIRUP_EXIT;
Allocator arena = wapp_mem_arena_allocator_init(MB(8));
if (wapp_mem_allocator_invalid(&arena)) {
return wapp_tester_result(false);
}
memset(dst, 0, strlen(dst));
memset(tmp, 0, strlen(tmp));
snprintf(tmp, TMP_BUF_SIZE, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP);
snprintf(expected, MAIN_BUF_SIZE, "%c", PATH_SEP);
wapp_cpath_dirup(dst, 3, tmp);
result = strcmp(dst, expected) == 0;
if (!result) {
goto TEST_DIRUP_EXIT;
}
bool result;
Str8 *output = NULL;
memset(dst, 0, strlen(dst));
memset(tmp, 0, strlen(tmp));
snprintf(tmp, TMP_BUF_SIZE, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP);
wapp_cpath_dirup(dst, 3, tmp);
result = strcmp(dst, ".") == 0;
if (!result) {
goto TEST_DIRUP_EXIT;
}
Str8 expected = wapp_str8_buf(MAIN_BUF_SIZE);
Str8 tmp = wapp_str8_buf(TMP_BUF_SIZE);
memset(dst, 0, strlen(dst));
memset(tmp, 0, strlen(tmp));
memset(expected, 0, strlen(expected));
snprintf(tmp, TMP_BUF_SIZE, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP);
snprintf(expected, MAIN_BUF_SIZE, "%chome", PATH_SEP);
wapp_cpath_dirup(dst, 2, tmp);
result = strcmp(dst, expected) == 0;
if (!result) {
goto TEST_DIRUP_EXIT;
}
// CASE 1
wapp_str8_format(&tmp, "%c", PATH_SEP);
wapp_str8_format(&expected, "%c", PATH_SEP);
memset(dst, 0, strlen(dst));
memset(tmp, 0, strlen(tmp));
snprintf(tmp, TMP_BUF_SIZE, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP);
wapp_cpath_dirup(dst, 2, tmp);
result = strcmp(dst, "home") == 0;
output = wapp_cpath_dirup(&arena, &tmp, 3);
result = output != NULL && wapp_str8_equal(output, &expected);
// CASE 2
wapp_str8_format(&tmp, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP);
wapp_str8_format(&expected, "%c", PATH_SEP);
output = wapp_cpath_dirup(&arena, &tmp, 3);
result = result && output != NULL && wapp_str8_equal(output, &expected);
// CASE 3
wapp_str8_format(&tmp, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP);
wapp_str8_copy_cstr_capped(&expected, ".");
output = wapp_cpath_dirup(&arena, &tmp, 3);
result = result && output != NULL && wapp_str8_equal(output, &expected);
// CASE 4
wapp_str8_format(&tmp, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP);
wapp_str8_format(&expected, "%chome", PATH_SEP);
output = wapp_cpath_dirup(&arena, &tmp, 2);
result = result && output != NULL && wapp_str8_equal(output, &expected);
// CASE 5
wapp_str8_format(&tmp, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP);
wapp_str8_copy_cstr_capped(&expected, "home");
output = wapp_cpath_dirup(&arena, &tmp, 2);
result = result && output != NULL && wapp_str8_equal(output, &expected);
wapp_mem_arena_allocator_destroy(&arena);
TEST_DIRUP_EXIT:
return wapp_tester_result(result);
}