awtk/tests/path_test.cc

117 lines
3.4 KiB
C++
Raw Normal View History

2018-08-26 12:13:30 +08:00
#include "base/path.h"
#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"));
}
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] == '/') {
s[i] = PATH_SEP;
}
}
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"));
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"));
}
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);
}