202 lines
5.8 KiB
C++
202 lines
5.8 KiB
C++
#include "test_cpath.h"
|
|
#include "wapp.h"
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
|
|
#define MAIN_BUF_SIZE 4096
|
|
#define TMP_BUF_SIZE 1024
|
|
|
|
WpTestFuncResult test_cpath_join_path(void) {
|
|
b8 result;
|
|
|
|
WpStr8 expected = wpStr8Buf(MAIN_BUF_SIZE);
|
|
WpStr8 out = wpStr8Buf(MAIN_BUF_SIZE);
|
|
WpStr8 tmp = wpStr8Buf(TMP_BUF_SIZE);
|
|
|
|
wpStr8Format(&expected, "%chome%cabdelrahman%cDocuments", WAPP_PATH_SEP, WAPP_PATH_SEP, WAPP_PATH_SEP);
|
|
wpStr8Format(&tmp, "%c", WAPP_PATH_SEP);
|
|
|
|
WpStr8List parts = wapp_dbl_list(WpStr8);
|
|
|
|
wapp_dbl_list_push_back(WpStr8, &parts, &tmp);
|
|
|
|
WpStr8 home = wpStr8Lit("home");
|
|
wapp_dbl_list_push_back(WpStr8, &parts, &home);
|
|
|
|
WpStr8 user = wpStr8Lit("abdelrahman");
|
|
wapp_dbl_list_push_back(WpStr8, &parts, &user);
|
|
|
|
WpStr8 docs = wpStr8Lit("Documents");
|
|
wapp_dbl_list_push_back(WpStr8, &parts, &docs);
|
|
|
|
wapp_cpath_join_path(&out, &parts);
|
|
result = wpStr8Equal(&out, &expected);
|
|
|
|
wapp_dbl_list_pop_front(WpStr8, &parts);
|
|
|
|
wpStr8Format(&expected, "home%cabdelrahman%cDocuments", WAPP_PATH_SEP, WAPP_PATH_SEP);
|
|
|
|
wapp_cpath_join_path(&out, &parts);
|
|
result = result && wpStr8Equal(&out, &expected);
|
|
|
|
WpStr8RO str = wpStr8LitRo("home");
|
|
wpStr8ConcatCapped(&tmp, &str);
|
|
wapp_dbl_list_pop_front(WpStr8, &parts);
|
|
|
|
wapp_dbl_list_push_front(WpStr8, &parts, &tmp);
|
|
|
|
wpStr8Format(&expected, "%chome%cabdelrahman%cDocuments", WAPP_PATH_SEP, WAPP_PATH_SEP, WAPP_PATH_SEP);
|
|
|
|
wapp_cpath_join_path(&out, &parts);
|
|
result = result && wpStr8Equal(&out, &expected);
|
|
|
|
wpStr8Format(&tmp, "home%c", WAPP_PATH_SEP);
|
|
wapp_dbl_list_pop_front(WpStr8, &parts);
|
|
|
|
WpStr8 home_2 = wpStr8Lit("home");
|
|
wapp_dbl_list_push_front(WpStr8, &parts, &home_2);
|
|
|
|
wpStr8Format(&expected, "home%cabdelrahman%cDocuments", WAPP_PATH_SEP, WAPP_PATH_SEP);
|
|
|
|
wapp_cpath_join_path(&out, &parts);
|
|
result = result && wpStr8Equal(&out, &expected);
|
|
|
|
wapp_dbl_list_empty(WpStr8, &parts);
|
|
|
|
wpStr8Format(&tmp, "%chome", WAPP_PATH_SEP);
|
|
|
|
wapp_dbl_list_push_back(WpStr8, &parts, &tmp);
|
|
|
|
WpStr8 empty = wpStr8Lit("");
|
|
wapp_dbl_list_push_back(WpStr8, &parts, &empty);
|
|
|
|
wpStr8Format(&expected, "%chome", WAPP_PATH_SEP);
|
|
|
|
wapp_cpath_join_path(&out, &parts);
|
|
result = result && wpStr8Equal(&out, &expected);
|
|
|
|
wapp_dbl_list_pop_front(WpStr8, &parts);
|
|
|
|
WpStr8 empty_2 = wpStr8Lit("");
|
|
wapp_dbl_list_push_back(WpStr8, &parts, &empty_2);
|
|
|
|
wpStr8Format(&expected, "%s", "");
|
|
|
|
wapp_cpath_join_path(&out, &parts);
|
|
result = result && wpStr8Equal(&out, &expected);
|
|
|
|
wapp_dbl_list_pop_back(WpStr8, &parts);
|
|
|
|
WpStr8 home_3 = wpStr8Lit("home");
|
|
wapp_dbl_list_push_back(WpStr8, &parts, &home_3);
|
|
|
|
wpStr8CopyCstrCapped(&expected, "home");
|
|
|
|
wapp_cpath_join_path(&out, &parts);
|
|
result = result && wpStr8Equal(&out, &expected);
|
|
|
|
return wpTesterResult(result);
|
|
}
|
|
|
|
WpTestFuncResult test_cpath_dirname(void) {
|
|
WpAllocator arena = wapp_mem_arena_allocator_init(MiB(8));
|
|
if (wpMemAllocatorInvalid(&arena)) {
|
|
return wpTesterResult(false);
|
|
}
|
|
|
|
b8 result;
|
|
WpStr8 *output = nullptr;
|
|
|
|
WpStr8 expected = wpStr8Buf(MAIN_BUF_SIZE);
|
|
WpStr8 tmp = wpStr8Buf(TMP_BUF_SIZE);
|
|
|
|
// CASE 1
|
|
wpStr8Format(&tmp, "%c", WAPP_PATH_SEP);
|
|
wpStr8Format(&expected, "%c", WAPP_PATH_SEP);
|
|
|
|
output = wapp_cpath_dirname(&arena, &tmp);
|
|
result = output != nullptr && wpStr8Equal(output, &expected);
|
|
|
|
// CASE 2
|
|
wpStr8Format(&expected, "%s", ".");
|
|
|
|
WpStr8 path = wpStr8Lit("home");
|
|
output = wapp_cpath_dirname(&arena, &path);
|
|
result = result && output != nullptr && wpStr8Equal(output, &expected);
|
|
|
|
// CASE 3
|
|
path = wpStr8Lit("");
|
|
output = wapp_cpath_dirname(&arena, &path);
|
|
result = result && output != nullptr && wpStr8Equal(output, &expected);
|
|
|
|
// CASE 4
|
|
wpStr8Format(&tmp, "%chome%ctest", WAPP_PATH_SEP, WAPP_PATH_SEP);
|
|
wpStr8Format(&expected, "%chome", WAPP_PATH_SEP);
|
|
|
|
output = wapp_cpath_dirname(&arena, &tmp);
|
|
result = result && output != nullptr && wpStr8Equal(output, &expected);
|
|
|
|
// CASE 5
|
|
wpStr8Format(&tmp, "%chome%ctest%c", WAPP_PATH_SEP, WAPP_PATH_SEP, WAPP_PATH_SEP);
|
|
wpStr8Format(&expected, "%chome", WAPP_PATH_SEP);
|
|
|
|
output = wapp_cpath_dirname(&arena, &tmp);
|
|
result = result && output != nullptr && wpStr8Equal(output, &expected);
|
|
|
|
wapp_mem_arena_allocator_destroy(&arena);
|
|
|
|
return wpTesterResult(result);
|
|
}
|
|
|
|
WpTestFuncResult test_cpath_dirup(void) {
|
|
WpAllocator arena = wapp_mem_arena_allocator_init(MiB(8));
|
|
if (wpMemAllocatorInvalid(&arena)) {
|
|
return wpTesterResult(false);
|
|
}
|
|
|
|
b8 result;
|
|
WpStr8 *output = nullptr;
|
|
|
|
WpStr8 expected = wpStr8Buf(MAIN_BUF_SIZE);
|
|
WpStr8 tmp = wpStr8Buf(TMP_BUF_SIZE);
|
|
|
|
// CASE 1
|
|
wpStr8Format(&tmp, "%c", WAPP_PATH_SEP);
|
|
wpStr8Format(&expected, "%c", WAPP_PATH_SEP);
|
|
|
|
output = wapp_cpath_dirup(&arena, &tmp, 3);
|
|
result = output != nullptr && wpStr8Equal(output, &expected);
|
|
|
|
// CASE 2
|
|
wpStr8Format(&tmp, "%chome%cabdelrahman%cDocuments", WAPP_PATH_SEP, WAPP_PATH_SEP, WAPP_PATH_SEP);
|
|
wpStr8Format(&expected, "%c", WAPP_PATH_SEP);
|
|
|
|
output = wapp_cpath_dirup(&arena, &tmp, 3);
|
|
result = result && output != nullptr && wpStr8Equal(output, &expected);
|
|
|
|
// CASE 3
|
|
wpStr8Format(&tmp, "home%cabdelrahman%cDocuments", WAPP_PATH_SEP, WAPP_PATH_SEP);
|
|
wpStr8CopyCstrCapped(&expected, ".");
|
|
|
|
output = wapp_cpath_dirup(&arena, &tmp, 3);
|
|
result = result && output != nullptr && wpStr8Equal(output, &expected);
|
|
|
|
// CASE 4
|
|
wpStr8Format(&tmp, "%chome%cabdelrahman%cDocuments", WAPP_PATH_SEP, WAPP_PATH_SEP, WAPP_PATH_SEP);
|
|
wpStr8Format(&expected, "%chome", WAPP_PATH_SEP);
|
|
|
|
output = wapp_cpath_dirup(&arena, &tmp, 2);
|
|
result = result && output != nullptr && wpStr8Equal(output, &expected);
|
|
|
|
// CASE 5
|
|
wpStr8Format(&tmp, "home%cabdelrahman%cDocuments", WAPP_PATH_SEP, WAPP_PATH_SEP);
|
|
wpStr8CopyCstrCapped(&expected, "home");
|
|
|
|
output = wapp_cpath_dirup(&arena, &tmp, 2);
|
|
result = result && output != nullptr && wpStr8Equal(output, &expected);
|
|
|
|
wapp_mem_arena_allocator_destroy(&arena);
|
|
|
|
return wpTesterResult(result);
|
|
}
|