awtk/tests/path_test.cc

201 lines
6.0 KiB
C++
Raw Normal View History

#include "tkc/path.h"
2018-08-26 12:13:30 +08:00
#include "gtest/gtest.h"
#include <string>
using std::string;
TEST(Path, basename) {
char result[MAX_PATH + 1];
ASSERT_EQ(path_basename("/a/test.bin", result, sizeof(result)), RET_OK);
ASSERT_EQ(string(result), string("test.bin"));
ASSERT_EQ(path_basename("test.bin", result, sizeof(result)), RET_OK);
ASSERT_EQ(string(result), string("test.bin"));
}
2021-07-11 11:37:58 +08:00
TEST(Path, extname_is) {
ASSERT_EQ(path_extname_is("test.bin", ".bin"), TRUE);
ASSERT_EQ(path_extname_is("test.Bin", ".bin"), TRUE);
ASSERT_EQ(path_extname_is("test.jpg", ".JPG"), TRUE);
ASSERT_EQ(path_extname_is("test.jpg", ".JPG"), TRUE);
ASSERT_EQ(path_extname_is("test.jpg", NULL), FALSE);
}
2018-08-26 12:13:30 +08:00
TEST(Path, extname) {
char result[MAX_PATH + 1];
ASSERT_EQ(path_extname("/a/test.bin", result, sizeof(result)), RET_OK);
ASSERT_EQ(string(result), string(".bin"));
ASSERT_EQ(path_extname("test.bin", result, sizeof(result)), RET_OK);
ASSERT_EQ(string(result), string(".bin"));
ASSERT_EQ(path_extname(".bin", result, sizeof(result)), RET_OK);
ASSERT_EQ(string(result), string(".bin"));
ASSERT_EQ(path_extname("bin", result, sizeof(result)), RET_FAIL);
}
TEST(Path, dirname) {
char result[MAX_PATH + 1];
ASSERT_EQ(path_dirname("/a/test.bin", result, sizeof(result)), RET_OK);
ASSERT_EQ(string(result), string("/a/"));
ASSERT_EQ(path_dirname("/a/b/test.bin", result, sizeof(result)), RET_OK);
ASSERT_EQ(string(result), string("/a/b/"));
ASSERT_EQ(path_dirname("test.bin", result, sizeof(result)), RET_FAIL);
ASSERT_EQ(string(result), string(""));
}
TEST(Path, exe) {
char result[MAX_PATH + 1];
ASSERT_EQ(path_exe(result), RET_OK);
ASSERT_EQ(strstr(result, "runTest") != NULL, true);
}
string normalPath(const char* str) {
string s = str;
int32_t i = 0;
int32_t n = s.size();
for (i = 0; i < n; i++) {
if (s[i] == '/') {
2019-07-05 06:52:29 +08:00
s[i] = TK_PATH_SEP;
2018-08-26 12:13:30 +08:00
}
}
return s;
}
TEST(Path, normalize) {
char result[MAX_PATH + 1];
ASSERT_EQ(path_normalize("/a/b", result, sizeof(result)), RET_OK);
ASSERT_EQ(string(result), normalPath("/a/b"));
2020-06-05 17:16:19 +08:00
2020-06-03 18:23:56 +08:00
ASSERT_EQ(path_normalize("/a//b", result, sizeof(result)), RET_OK);
ASSERT_EQ(string(result), normalPath("/a/b"));
2020-06-05 17:16:19 +08:00
2020-06-03 18:23:56 +08:00
ASSERT_EQ(path_normalize("/a//./////././/b", result, sizeof(result)), RET_OK);
ASSERT_EQ(string(result), normalPath("/a/b"));
2018-08-29 11:27:38 +08:00
2018-08-28 18:20:07 +08:00
ASSERT_EQ(path_normalize("/a/b.exe", result, sizeof(result)), RET_OK);
ASSERT_EQ(string(result), normalPath("/a/b.exe"));
2018-08-26 12:13:30 +08:00
ASSERT_EQ(path_normalize("/a/./b", result, sizeof(result)), RET_OK);
ASSERT_EQ(string(result), normalPath("/a/b"));
ASSERT_EQ(path_normalize("./a/./b", result, sizeof(result)), RET_OK);
ASSERT_EQ(string(result), normalPath("a/b"));
ASSERT_EQ(path_normalize(".\\a\\.\\b", result, sizeof(result)), RET_OK);
ASSERT_EQ(string(result), normalPath("a/b"));
ASSERT_EQ(path_normalize("./a/../b", result, sizeof(result)), RET_OK);
ASSERT_EQ(string(result), normalPath("b"));
ASSERT_EQ(path_normalize("./a/b/../c", result, sizeof(result)), RET_OK);
ASSERT_EQ(string(result), normalPath("a/c"));
ASSERT_EQ(path_normalize("./a/b/../../c", result, sizeof(result)), RET_OK);
ASSERT_EQ(string(result), normalPath("c"));
ASSERT_EQ(path_normalize("/a/b/../c", result, sizeof(result)), RET_OK);
ASSERT_EQ(string(result), normalPath("/a/c"));
ASSERT_EQ(path_normalize("/a/b/../../c", result, sizeof(result)), RET_OK);
ASSERT_EQ(string(result), normalPath("/c"));
2020-06-05 17:16:19 +08:00
2020-06-03 18:23:56 +08:00
ASSERT_EQ(path_normalize("/a/b/../", result, sizeof(result)), RET_OK);
ASSERT_EQ(string(result), normalPath("/a/"));
ASSERT_EQ(path_normalize("/a/b/..", result, sizeof(result)), RET_OK);
ASSERT_EQ(string(result), normalPath("/a/"));
2020-06-05 17:16:19 +08:00
2020-06-03 18:23:56 +08:00
ASSERT_EQ(path_normalize("/a/b/..\\", result, sizeof(result)), RET_OK);
ASSERT_EQ(string(result), normalPath("/a/"));
ASSERT_EQ(path_normalize("/a/b\\..", result, sizeof(result)), RET_OK);
ASSERT_EQ(string(result), normalPath("/a/"));
2020-06-05 17:16:19 +08:00
2020-06-03 18:23:56 +08:00
ASSERT_EQ(path_normalize("\\a\\b/../../c", result, sizeof(result)), RET_OK);
ASSERT_EQ(string(result), normalPath("/c"));
2018-08-26 12:13:30 +08:00
}
TEST(Path, build) {
char result[MAX_PATH + 1];
ASSERT_EQ(path_build(result, sizeof(result), "a", "b", NULL), RET_OK);
ASSERT_EQ(string(result), normalPath("a/b"));
}
TEST(Path, abs) {
char result[MAX_PATH + 1];
ASSERT_EQ(path_abs("abc", result, sizeof(result)), RET_OK);
ASSERT_EQ(strstr(result, "abc") != NULL, TRUE);
}
TEST(Path, approot) {
char result[MAX_PATH + 1];
char app_root[MAX_PATH + 1];
2018-09-21 10:32:39 +08:00
memset(app_root, 0x00, sizeof(app_root));
2018-08-26 12:13:30 +08:00
ASSERT_EQ(path_app_root(app_root), RET_OK);
ASSERT_EQ(path_build(result, sizeof(result), app_root, "bin", NULL), RET_OK);
ASSERT_EQ(path_exist(result), TRUE);
}
2020-03-05 06:22:23 +08:00
2020-05-04 18:00:55 +08:00
static const char* normalize_path_str(char* str) {
char* p = str;
2020-05-07 12:03:13 +08:00
while (*p) {
2020-05-04 18:00:55 +08:00
if (*p == '\\') {
*p = '/';
}
p++;
}
return str;
}
2020-03-05 06:22:23 +08:00
TEST(Path, replace_basename) {
const char* filename = "a/b/test.txt";
char result[MAX_PATH + 1];
ASSERT_EQ(path_replace_basename(result, sizeof(result), filename, "abc.txt"), RET_OK);
2020-05-04 18:00:55 +08:00
ASSERT_STREQ(normalize_path_str(result), "a/b/abc.txt");
2020-03-05 06:22:23 +08:00
}
TEST(Path, replace_extname) {
const char* filename = "a/b/test.txt";
char result[MAX_PATH + 1];
ASSERT_EQ(path_replace_extname(result, sizeof(result), filename, "abc"), RET_OK);
2020-05-04 18:00:55 +08:00
ASSERT_STREQ(normalize_path_str(result), "a/b/test.abc");
2020-03-05 06:22:23 +08:00
}
2021-04-15 15:22:21 +08:00
TEST(Path, path_remove_last_slash) {
char result[MAX_PATH + 1];
strcpy(result, "./a/");
ASSERT_EQ(path_remove_last_slash(result), RET_OK);
ASSERT_STREQ(result, "./a");
2021-04-20 18:25:27 +08:00
2021-04-15 15:22:21 +08:00
strcpy(result, "./a\\");
ASSERT_EQ(path_remove_last_slash(result), RET_OK);
ASSERT_STREQ(result, "./a");
2021-04-20 18:25:27 +08:00
2021-04-15 15:22:21 +08:00
strcpy(result, "./");
ASSERT_EQ(path_remove_last_slash(result), RET_OK);
ASSERT_STREQ(result, ".");
strcpy(result, "/");
ASSERT_EQ(path_remove_last_slash(result), RET_OK);
ASSERT_STREQ(result, "/");
2021-04-20 18:25:27 +08:00
2021-04-15 15:22:21 +08:00
strcpy(result, "////");
ASSERT_EQ(path_remove_last_slash(result), RET_OK);
ASSERT_STREQ(result, "/");
2021-04-20 18:25:27 +08:00
2021-04-15 15:22:21 +08:00
strcpy(result, "/a/b//");
ASSERT_EQ(path_remove_last_slash(result), RET_OK);
ASSERT_STREQ(result, "/a/b");
2021-04-20 18:25:27 +08:00
2021-04-15 15:22:21 +08:00
ASSERT_NE(path_remove_last_slash(NULL), RET_OK);
}