filepath_test.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright 2010-2024 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx/blob/master/LICENSE
  4. */
  5. #include "test.h"
  6. #include <bx/file.h>
  7. struct FilePathTest
  8. {
  9. const char* filePath;
  10. const char* expected;
  11. };
  12. FilePathTest s_filePathTest[] =
  13. {
  14. // Already clean
  15. {"", "."},
  16. {"abc", "abc"},
  17. {"abc/def", "abc/def"},
  18. {"a/b/c", "a/b/c"},
  19. {".", "."},
  20. {"..", ".."},
  21. {"../..", "../.."},
  22. {"../../abc", "../../abc"},
  23. {"/abc", "/abc"},
  24. {"/", "/"},
  25. // Do not remove trailing slash
  26. {"abc/", "abc/"},
  27. {"abc/def/", "abc/def/"},
  28. {"a/b/c/", "a/b/c/"},
  29. {"./", "./"},
  30. {"../", "../"},
  31. {"../../", "../../"},
  32. {"/abc/", "/abc/"},
  33. // Remove doubled slash
  34. {"abc//def//ghi", "abc/def/ghi"},
  35. {"//abc", "/abc"},
  36. {"///abc", "/abc"},
  37. {"//abc//", "/abc/"},
  38. {"abc//", "abc/"},
  39. // Remove . elements
  40. {"abc/./def", "abc/def"},
  41. {"/./abc/def", "/abc/def"},
  42. {"abc/.", "abc"},
  43. // Remove .. elements
  44. {"abc/def/ghi/../jkl", "abc/def/jkl"},
  45. {"abc/def/../ghi/../jkl", "abc/jkl"},
  46. {"abc/def/..", "abc"},
  47. {"abc/def/../..", "."},
  48. {"/abc/def/../..", "/"},
  49. {"abc/def/../../..", ".."},
  50. {"/abc/def/../../..", "/"},
  51. {"abc/def/../../../ghi/jkl/../../../mno", "../../mno"},
  52. // Combinations
  53. {"abc/./../def", "def"},
  54. {"abc//./../def", "def"},
  55. {"abc/../../././../def", "../../def"},
  56. {"abc\\/../..\\/././../def", "../../def"},
  57. {"\\abc/def\\../..\\..", "/"},
  58. };
  59. struct FilePathSplit
  60. {
  61. const char* filePath;
  62. bool absolute;
  63. const char* path;
  64. const char* fileName;
  65. const char* baseName;
  66. const char* extension;
  67. };
  68. static const FilePathSplit s_filePathSplit[] =
  69. {
  70. { "\\abc/def\\../..\\../test.txt", true, "/", "test.txt", "test", ".txt" },
  71. { "/abv/gd/555/333/pod.mac", true, "/abv/gd/555/333/", "pod.mac", "pod", ".mac" },
  72. { "archive.tar.gz", false, "", "archive.tar.gz", "archive", ".tar.gz" },
  73. { "tmp/archive.tar.gz", false, "tmp/", "archive.tar.gz", "archive", ".tar.gz" },
  74. { "/tmp/archive.tar.gz", true, "/tmp/", "archive.tar.gz", "archive", ".tar.gz" },
  75. { "d:/tmp/archive.tar.gz", true, "D:/tmp/", "archive.tar.gz", "archive", ".tar.gz" },
  76. { "/tmp/abv/gd", true, "/tmp/abv/", "gd", "gd", "" },
  77. { "/tmp/abv/gd/", true, "/tmp/abv/gd/", "", "", "" },
  78. };
  79. TEST_CASE("FilePath", "[filepath][string]")
  80. {
  81. bx::FilePath fp;
  82. for (uint32_t ii = 0; ii < BX_COUNTOF(s_filePathTest); ++ii)
  83. {
  84. const FilePathTest& test = s_filePathTest[ii];
  85. fp.set(test.filePath);
  86. REQUIRE(0 == bx::strCmp(test.expected, fp) );
  87. }
  88. for (uint32_t ii = 0; ii < BX_COUNTOF(s_filePathSplit); ++ii)
  89. {
  90. const FilePathSplit& test = s_filePathSplit[ii];
  91. fp.set(test.filePath);
  92. const bx::StringView path = fp.getPath();
  93. const bx::StringView fileName = fp.getFileName();
  94. const bx::StringView baseName = fp.getBaseName();
  95. const bx::StringView ext = fp.getExt();
  96. REQUIRE(0 == bx::strCmp(test.path, path) );
  97. REQUIRE(0 == bx::strCmp(test.fileName, fileName) );
  98. REQUIRE(0 == bx::strCmp(test.baseName, baseName) );
  99. REQUIRE(0 == bx::strCmp(test.extension, ext) );
  100. REQUIRE(test.absolute == fp.isAbsolute() );
  101. };
  102. }
  103. TEST_CASE("FilePath temp", "[filepath]")
  104. {
  105. bx::FilePath tmp(bx::Dir::Temp);
  106. REQUIRE(0 != bx::strCmp(".", tmp.getPath().getPtr() ) );
  107. tmp.set(bx::Dir::Temp);
  108. tmp.join("bx.test");
  109. bx::removeAll(tmp, bx::ErrorIgnore{});
  110. tmp.join("bx.test/abvgd/555333/test");
  111. REQUIRE(bx::makeAll(tmp, bx::ErrorAssert{}) );
  112. tmp.set(bx::Dir::Temp);
  113. tmp.join("bx.test");
  114. REQUIRE(bx::removeAll(tmp, bx::ErrorAssert{}) );
  115. }
  116. TEST_CASE("FilePath special", "[filepath]")
  117. {
  118. {
  119. bx::FilePath tmp(bx::Dir::Current);
  120. bx::StringView sv(tmp);
  121. DBG("%S", &sv);
  122. }
  123. {
  124. bx::FilePath tmp(bx::Dir::Executable);
  125. bx::StringView sv(tmp);
  126. DBG("%S", &sv);
  127. }
  128. {
  129. bx::FilePath tmp(bx::Dir::Home);
  130. bx::StringView sv(tmp);
  131. DBG("%S", &sv);
  132. }
  133. {
  134. bx::FilePath tmp(bx::Dir::Temp);
  135. bx::StringView sv(tmp);
  136. DBG("%S", &sv);
  137. }
  138. }