Switch wapp_cpath_join_path to use Str8

This commit is contained in:
2025-02-22 17:48:25 +00:00
parent ba5e902a1d
commit ed4ec54c7a
4 changed files with 107 additions and 68 deletions

View File

@@ -1,5 +1,6 @@
#include "test_cpath.h"
#include "cpath.h"
#include "str8.h"
#include "tester.h"
#include <string.h>
#include <stdio.h>
@@ -9,77 +10,76 @@
#define TMP_BUF_SIZE 1024
TestFuncResult test_cpath_join_path(void) {
char expected[MAIN_BUF_SIZE] = {0};
char out[MAIN_BUF_SIZE] = {0};
char tmp[TMP_BUF_SIZE] = {0};
bool result;
snprintf(expected, MAIN_BUF_SIZE, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP);
snprintf(tmp, 2, "%c", PATH_SEP);
wapp_cpath_join_path(out, tmp, "home", "abdelrahman", "Documents");
Str8 expected = wapp_str8_buf(MAIN_BUF_SIZE);
Str8 out = wapp_str8_buf(MAIN_BUF_SIZE);
Str8 tmp = wapp_str8_buf(TMP_BUF_SIZE);
bool result = strcmp(out, expected) == 0;
if (!result) {
goto TEST_JOIN_PATH_EXIT;
}
wapp_str8_format(&expected, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP);
wapp_str8_format(&tmp, "%c", PATH_SEP);
memset(out, 0, strlen(out));
snprintf(expected, MAIN_BUF_SIZE, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP);
wapp_cpath_join_path(out, "home", "abdelrahman", "Documents");
Str8List parts = {0};
wapp_str8_list_push_back(&parts, &wapp_str8_node_from_str8(tmp));
wapp_str8_list_push_back(&parts, &wapp_str8_node_from_cstr("home"));
wapp_str8_list_push_back(&parts, &wapp_str8_node_from_cstr("abdelrahman"));
wapp_str8_list_push_back(&parts, &wapp_str8_node_from_cstr("Documents"));
result = result && strcmp(out, expected) == 0;
if (!result) {
goto TEST_JOIN_PATH_EXIT;
}
wapp_cpath_join_path(&out, &parts);
result = wapp_str8_equal(&out, &expected);
memset(out, 0, strlen(out));
memset(tmp, 0, strlen(tmp));
snprintf(expected, MAIN_BUF_SIZE, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP);
snprintf(tmp, TMP_BUF_SIZE, "%chome", PATH_SEP);
wapp_cpath_join_path(out, tmp, "abdelrahman", "Documents");
wapp_str8_list_pop_front(&parts);
result = result && strcmp(out, expected) == 0;
if (!result) {
goto TEST_JOIN_PATH_EXIT;
}
wapp_str8_format(&expected, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP);
memset(out, 0, strlen(out));
memset(tmp, 0, strlen(tmp));
snprintf(expected, MAIN_BUF_SIZE, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP);
snprintf(tmp, TMP_BUF_SIZE, "home%c", PATH_SEP);
wapp_cpath_join_path(out, tmp, "abdelrahman", "Documents");
wapp_cpath_join_path(&out, &parts);
result = result && wapp_str8_equal(&out, &expected);
result = result && strcmp(out, expected) == 0;
if (!result) {
goto TEST_JOIN_PATH_EXIT;
}
wapp_str8_concat_capped(&tmp, &wapp_str8_lit_ro("home"));
wapp_str8_list_pop_front(&parts);
wapp_str8_list_push_front(&parts, &wapp_str8_node_from_str8(tmp));
memset(out, 0, strlen(out));
memset(tmp, 0, strlen(tmp));
snprintf(expected, MAIN_BUF_SIZE, "%chome", PATH_SEP);
snprintf(tmp, TMP_BUF_SIZE, "%chome", PATH_SEP);
wapp_cpath_join_path(out, tmp, "");
wapp_str8_format(&expected, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP);
result = result && strcmp(out, expected) == 0;
if (!result) {
goto TEST_JOIN_PATH_EXIT;
}
wapp_cpath_join_path(&out, &parts);
result = result && wapp_str8_equal(&out, &expected);
memset(out, 0, strlen(out));
snprintf(expected, 1, "%s", "");
wapp_cpath_join_path(out, "", "");
wapp_str8_format(&tmp, "home%c", PATH_SEP);
wapp_str8_list_pop_front(&parts);
wapp_str8_list_push_front(&parts, &wapp_str8_node_from_cstr("home"));
result = result && strcmp(out, expected) == 0;
if (!result) {
goto TEST_JOIN_PATH_EXIT;
}
wapp_str8_format(&expected, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP);
memset(out, 0, strlen(out));
snprintf(expected, MAIN_BUF_SIZE, "home");
wapp_cpath_join_path(out, "", "home");
wapp_cpath_join_path(&out, &parts);
result = result && wapp_str8_equal(&out, &expected);
result = result && strcmp(out, expected) == 0;
wapp_str8_list_empty(&parts);
wapp_str8_format(&tmp, "%chome", PATH_SEP);
wapp_str8_list_push_back(&parts, &wapp_str8_node_from_str8(tmp));
wapp_str8_list_push_back(&parts, &wapp_str8_node_from_cstr(""));
wapp_str8_format(&expected, "%chome", PATH_SEP);
wapp_cpath_join_path(&out, &parts);
result = result && wapp_str8_equal(&out, &expected);
wapp_str8_list_pop_front(&parts);
wapp_str8_list_push_back(&parts, &wapp_str8_node_from_cstr(""));
wapp_str8_format(&expected, "%s", "");
wapp_cpath_join_path(&out, &parts);
result = result && wapp_str8_equal(&out, &expected);
wapp_str8_list_pop_back(&parts);
wapp_str8_list_push_back(&parts, &wapp_str8_node_from_cstr("home"));
wapp_str8_copy_cstr_capped(&expected, "home");
wapp_cpath_join_path(&out, &parts);
result = result && wapp_str8_equal(&out, &expected);
TEST_JOIN_PATH_EXIT:
return wapp_tester_result(result);
}

View File

@@ -44,6 +44,7 @@ int main(void) {
test_str8_list_pop_front,
test_str8_list_pop_back,
test_str8_list_remove,
test_str8_list_empty,
test_cpath_join_path,
test_cpath_dirname,
test_cpath_dirup,