test/paths: add tests for mp_basename and mp_dirname

Note that those functions have different behaviour than POSIX ones.
This commit is contained in:
Kacper Michajłow 2024-06-26 18:02:37 +02:00
parent 4a0c0ce92d
commit 529450a965
1 changed files with 49 additions and 0 deletions

View File

@ -20,6 +20,13 @@ static void test_normalize(char *file, int line, char *expected, char *path)
talloc_free(ctx); talloc_free(ctx);
} }
static void test_dirname(char *file, int line, char *path, char *expected)
{
char *res = bstrto0(NULL, mp_dirname(path));
assert_string_equal_impl(file, line, res, expected);
talloc_free(res);
}
#define TEST_JOIN(a, b, c) \ #define TEST_JOIN(a, b, c) \
test_join(__FILE__, __LINE__, a, b, c); test_join(__FILE__, __LINE__, a, b, c);
@ -29,6 +36,12 @@ static void test_normalize(char *file, int line, char *expected, char *path)
#define TEST_NORMALIZE(expected, path) \ #define TEST_NORMALIZE(expected, path) \
test_normalize(__FILE__, __LINE__, expected, path) test_normalize(__FILE__, __LINE__, expected, path)
#define TEST_BASENAME(path, expected) \
assert_string_equal_impl(__FILE__, __LINE__, mp_basename(path), expected)
#define TEST_DIRNAME(path, expected) \
test_dirname(__FILE__, __LINE__, path, expected)
int main(void) int main(void)
{ {
TEST_ABS(true, "/ab"); TEST_ABS(true, "/ab");
@ -103,5 +116,41 @@ int main(void)
TEST_NORMALIZE("/usr", "/usr/bin/.."); TEST_NORMALIZE("/usr", "/usr/bin/..");
#endif #endif
TEST_BASENAME("/usr/local/bin", "bin");
TEST_BASENAME("/usr/local/", "");
TEST_BASENAME("/usr/", "");
TEST_BASENAME("/", "");
TEST_BASENAME("usr/local/bin", "bin");
TEST_BASENAME("usr/local/", "");
TEST_BASENAME("usr/", "");
TEST_BASENAME("usr", "usr");
TEST_BASENAME("", "");
TEST_BASENAME(".", ".");
TEST_BASENAME("..", "..");
#if HAVE_DOS_PATHS
TEST_BASENAME("C:\\Windows\\System32", "System32");
TEST_BASENAME("C:\\Windows\\", "");
TEST_BASENAME("C:\\", "");
TEST_BASENAME("C:", "");
#endif
TEST_DIRNAME("/usr/local/bin", "/usr/local/");
TEST_DIRNAME("/usr/local/", "/usr/local/");
TEST_DIRNAME("/usr/", "/usr/");
TEST_DIRNAME("/", "/");
TEST_DIRNAME("usr/local/bin", "usr/local/");
TEST_DIRNAME("usr/local/", "usr/local/");
TEST_DIRNAME("usr/", "usr/");
TEST_DIRNAME("usr", ".");
TEST_DIRNAME("", ".");
TEST_DIRNAME(".", ".");
TEST_DIRNAME("..", ".");
#if HAVE_DOS_PATHS
TEST_DIRNAME("C:\\Windows\\System32", "C:\\Windows\\");
TEST_DIRNAME("C:\\Windows\\", "C:\\Windows\\");
TEST_DIRNAME("C:\\", "C:\\");
TEST_DIRNAME("C:", "C:");
#endif
return 0; return 0;
} }