wizapp-stdlib/tests/cpath/test_cpath.c

184 lines
5.4 KiB
C

#include "test_cpath.h"
#include "cpath.h"
#include "str8.h"
#include "tester.h"
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#define MAIN_BUF_SIZE 4096
#define TMP_BUF_SIZE 1024
TestFuncResult test_cpath_join_path(void) {
bool result;
Str8 expected = wapp_str8_buf(MAIN_BUF_SIZE);
Str8 out = wapp_str8_buf(MAIN_BUF_SIZE);
Str8 tmp = wapp_str8_buf(TMP_BUF_SIZE);
wapp_str8_format(&expected, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP);
wapp_str8_format(&tmp, "%c", PATH_SEP);
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"));
wapp_cpath_join_path(&out, &parts);
result = wapp_str8_equal(&out, &expected);
wapp_str8_list_pop_front(&parts);
wapp_str8_format(&expected, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP);
wapp_cpath_join_path(&out, &parts);
result = result && wapp_str8_equal(&out, &expected);
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));
wapp_str8_format(&expected, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP);
wapp_cpath_join_path(&out, &parts);
result = result && wapp_str8_equal(&out, &expected);
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"));
wapp_str8_format(&expected, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP);
wapp_cpath_join_path(&out, &parts);
result = result && wapp_str8_equal(&out, &expected);
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);
return wapp_tester_result(result);
}
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;
}
memset(dst, 0, strlen(dst));
wapp_cpath_dirname(dst, "home");
result = strcmp(dst, ".") == 0;
if (!result) {
goto TEST_DIRNAME_EXIT;
}
memset(dst, 0, strlen(dst));
wapp_cpath_dirname(dst, "");
result = strcmp(dst, ".") == 0;
if (!result) {
goto TEST_DIRNAME_EXIT;
}
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;
}
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;
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;
}
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;
}
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;
}
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;
}
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;
TEST_DIRUP_EXIT:
return wapp_tester_result(result);
}