diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index eec0fb3a..d7dabaef 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -18,6 +18,7 @@ luna_test(libluna/TestBase64.cpp TestBase64) luna_test(libluna/TestUtf8.cpp TestUtf8) luna_test(libluna/TestFormat.cpp TestFormat) luna_test(libluna/TestHashTable.cpp TestHashTable) +luna_test(libluna/TestCPath.cpp TestCPath) luna_app(run-tests.cpp run-tests) endif() diff --git a/tests/libluna/TestCPath.cpp b/tests/libluna/TestCPath.cpp new file mode 100644 index 00000000..2ef5d61a --- /dev/null +++ b/tests/libluna/TestCPath.cpp @@ -0,0 +1,157 @@ +#include +#include +#include + +// Test cases taken from the basename(3) manpage on my Arch Linux system (which are taken from SUSv2). + +TestResult test_dirname_usr_lib() +{ + char path[] = "/usr/lib"; + + char* dir = dirname(path); + + validate(!strcmp(dir, "/usr")); + + test_success; +} + +TestResult test_basename_usr_lib() +{ + char path[] = "/usr/lib"; + + char* name = basename(path); + + validate(!strcmp(name, "lib")); + + test_success; +} + +TestResult test_dirname_absolute_usr() +{ + char path[] = "/usr/"; + + char* dir = dirname(path); + + validate(!strcmp(dir, "/")); + + test_success; +} + +TestResult test_basename_absolute_usr() +{ + char path[] = "/usr/"; + + char* name = basename(path); + + validate(!strcmp(name, "usr")); + + test_success; +} + +TestResult test_dirname_relative_usr() +{ + char path[] = "usr"; + + char* dir = dirname(path); + + validate(!strcmp(dir, ".")); + + test_success; +} + +TestResult test_basename_relative_usr() +{ + char path[] = "usr"; + + char* name = basename(path); + + validate(!strcmp(name, "usr")); + + test_success; +} + +TestResult test_dirname_root() +{ + char path[] = "/"; + + char* dir = dirname(path); + + validate(!strcmp(dir, "/")); + + test_success; +} + +TestResult test_basename_root() +{ + char path[] = "/"; + + char* name = basename(path); + + validate(!strcmp(name, "/")); + + test_success; +} + +TestResult test_dirname_self() +{ + char path[] = "."; + + char* dir = dirname(path); + + validate(!strcmp(dir, ".")); + + test_success; +} + +TestResult test_basename_self() +{ + char path[] = "."; + + char* name = basename(path); + + validate(!strcmp(name, ".")); + + test_success; +} + +TestResult test_dirname_parent() +{ + char path[] = ".."; + + char* dir = dirname(path); + + validate(!strcmp(dir, ".")); + + test_success; +} + +TestResult test_basename_parent() +{ + char path[] = ".."; + + char* name = basename(path); + + validate(!strcmp(name, "..")); + + test_success; +} + +Result test_main() +{ + test_prelude; + + run_test(test_basename_usr_lib); + run_test(test_basename_absolute_usr); + run_test(test_basename_relative_usr); + run_test(test_basename_root); + run_test(test_basename_self); + run_test(test_basename_parent); + run_test(test_dirname_usr_lib); + run_test(test_dirname_absolute_usr); + run_test(test_dirname_relative_usr); + run_test(test_dirname_root); + run_test(test_dirname_self); + run_test(test_dirname_parent); + + return {}; +}