path.decompose.pass.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // UNSUPPORTED: c++98, c++03
  9. // <filesystem>
  10. // class path
  11. // 8.4.9 path decomposition [path.decompose]
  12. //------------------------------------------
  13. // path root_name() const;
  14. // path root_directory() const;
  15. // path root_path() const;
  16. // path relative_path() const;
  17. // path parent_path() const;
  18. // path filename() const;
  19. // path stem() const;
  20. // path extension() const;
  21. //-------------------------------
  22. // 8.4.10 path query [path.query]
  23. //-------------------------------
  24. // bool empty() const noexcept;
  25. // bool has_root_path() const;
  26. // bool has_root_name() const;
  27. // bool has_root_directory() const;
  28. // bool has_relative_path() const;
  29. // bool has_parent_path() const;
  30. // bool has_filename() const;
  31. // bool has_stem() const;
  32. // bool has_extension() const;
  33. // bool is_absolute() const;
  34. // bool is_relative() const;
  35. //-------------------------------
  36. // 8.5 path iterators [path.itr]
  37. //-------------------------------
  38. // iterator begin() const;
  39. // iterator end() const;
  40. #include "filesystem_include.hpp"
  41. #include <type_traits>
  42. #include <vector>
  43. #include <cassert>
  44. #include "test_macros.h"
  45. #include "test_iterators.h"
  46. #include "count_new.hpp"
  47. #include "filesystem_test_helper.hpp"
  48. #include "assert_checkpoint.h"
  49. #include "verbose_assert.h"
  50. struct ComparePathExact {
  51. bool operator()(std::string const& LHS, std::string const& RHS) const {
  52. return LHS == RHS;
  53. }
  54. };
  55. struct PathDecomposeTestcase
  56. {
  57. std::string raw;
  58. std::vector<std::string> elements;
  59. std::string root_path;
  60. std::string root_name;
  61. std::string root_directory;
  62. std::string relative_path;
  63. std::string parent_path;
  64. std::string filename;
  65. };
  66. const PathDecomposeTestcase PathTestCases[] =
  67. {
  68. {"", {}, "", "", "", "", "", ""}
  69. , {".", {"."}, "", "", "", ".", "", "."}
  70. , {"..", {".."}, "", "", "", "..", "", ".."}
  71. , {"foo", {"foo"}, "", "", "", "foo", "", "foo"}
  72. , {"/", {"/"}, "/", "", "/", "", "/", ""}
  73. , {"/foo", {"/", "foo"}, "/", "", "/", "foo", "/", "foo"}
  74. , {"foo/", {"foo", ""}, "", "", "", "foo/", "foo", ""}
  75. , {"/foo/", {"/", "foo", ""}, "/", "", "/", "foo/", "/foo", ""}
  76. , {"foo/bar", {"foo","bar"}, "", "", "", "foo/bar", "foo", "bar"}
  77. , {"/foo//bar", {"/","foo","bar"}, "/", "", "/", "foo/bar", "/foo", "bar"}
  78. , {"//net", {"/", "net"}, "/", "", "/", "net", "/", "net"}
  79. , {"//net/foo", {"/", "net", "foo"}, "/", "", "/", "net/foo", "/net", "foo"}
  80. , {"///foo///", {"/", "foo", ""}, "/", "", "/", "foo///", "///foo", ""}
  81. , {"///foo///bar", {"/", "foo", "bar"}, "/", "", "/", "foo///bar", "///foo", "bar"}
  82. , {"/.", {"/", "."}, "/", "", "/", ".", "/", "."}
  83. , {"./", {".", ""}, "", "", "", "./", ".", ""}
  84. , {"/..", {"/", ".."}, "/", "", "/", "..", "/", ".."}
  85. , {"../", {"..", ""}, "", "", "", "../", "..", ""}
  86. , {"foo/.", {"foo", "."}, "", "", "", "foo/.", "foo", "."}
  87. , {"foo/..", {"foo", ".."}, "", "", "", "foo/..", "foo", ".."}
  88. , {"foo/./", {"foo", ".", ""}, "", "", "", "foo/./", "foo/.", ""}
  89. , {"foo/./bar", {"foo", ".", "bar"}, "", "", "", "foo/./bar", "foo/.", "bar"}
  90. , {"foo/../", {"foo", "..", ""}, "", "", "", "foo/../", "foo/..", ""}
  91. , {"foo/../bar", {"foo", "..", "bar"}, "", "", "", "foo/../bar", "foo/..", "bar"}
  92. , {"c:", {"c:"}, "", "", "", "c:", "", "c:"}
  93. , {"c:/", {"c:", ""}, "", "", "", "c:/", "c:", ""}
  94. , {"c:foo", {"c:foo"}, "", "", "", "c:foo", "", "c:foo"}
  95. , {"c:/foo", {"c:", "foo"}, "", "", "", "c:/foo", "c:", "foo"}
  96. , {"c:foo/", {"c:foo", ""}, "", "", "", "c:foo/", "c:foo", ""}
  97. , {"c:/foo/", {"c:", "foo", ""}, "", "", "", "c:/foo/", "c:/foo", ""}
  98. , {"c:/foo/bar", {"c:", "foo", "bar"}, "", "", "", "c:/foo/bar", "c:/foo", "bar"}
  99. , {"prn:", {"prn:"}, "", "", "", "prn:", "", "prn:"}
  100. , {"c:\\", {"c:\\"}, "", "", "", "c:\\", "", "c:\\"}
  101. , {"c:\\foo", {"c:\\foo"}, "", "", "", "c:\\foo", "", "c:\\foo"}
  102. , {"c:foo\\", {"c:foo\\"}, "", "", "", "c:foo\\", "", "c:foo\\"}
  103. , {"c:\\foo\\", {"c:\\foo\\"}, "", "", "", "c:\\foo\\", "", "c:\\foo\\"}
  104. , {"c:\\foo/", {"c:\\foo", ""}, "", "", "", "c:\\foo/", "c:\\foo", ""}
  105. , {"c:/foo\\bar", {"c:", "foo\\bar"}, "", "", "", "c:/foo\\bar", "c:", "foo\\bar"}
  106. , {"//", {"/"}, "/", "", "/", "", "/", ""}
  107. };
  108. void decompPathTest()
  109. {
  110. using namespace fs;
  111. for (auto const & TC : PathTestCases) {
  112. CHECKPOINT(TC.raw.c_str());
  113. fs::path p(TC.raw);
  114. ASSERT(p == TC.raw);
  115. ASSERT_EQ(p.root_path(), TC.root_path);
  116. ASSERT_NEQ(p.has_root_path(), TC.root_path.empty());
  117. ASSERT(p.root_name().native().empty())
  118. << DISPLAY(p.root_name());
  119. ASSERT_EQ(p.root_name(),TC.root_name);
  120. ASSERT_NEQ(p.has_root_name(), TC.root_name.empty());
  121. ASSERT_EQ(p.root_directory(), TC.root_directory);
  122. ASSERT_NEQ(p.has_root_directory(), TC.root_directory.empty());
  123. ASSERT_EQ(p.relative_path(), TC.relative_path);
  124. ASSERT_NEQ(p.has_relative_path(), TC.relative_path.empty());
  125. ASSERT_EQ(p.parent_path(), TC.parent_path);
  126. ASSERT_NEQ(p.has_parent_path(), TC.parent_path.empty());
  127. ASSERT_EQ(p.filename(), TC.filename);
  128. ASSERT_NEQ(p.has_filename(), TC.filename.empty());
  129. ASSERT_EQ(p.is_absolute(), p.has_root_directory());
  130. ASSERT_NEQ(p.is_relative(), p.is_absolute());
  131. if (p.empty())
  132. ASSERT(p.is_relative());
  133. ASSERT_COLLECTION_EQ_COMP(
  134. p.begin(), p.end(),
  135. TC.elements.begin(), TC.elements.end(),
  136. ComparePathExact()
  137. );
  138. // check backwards
  139. std::vector<fs::path> Parts;
  140. for (auto it = p.end(); it != p.begin(); )
  141. Parts.push_back(*--it);
  142. ASSERT_COLLECTION_EQ_COMP(Parts.begin(), Parts.end(),
  143. TC.elements.rbegin(), TC.elements.rend(),
  144. ComparePathExact());
  145. }
  146. }
  147. struct FilenameDecompTestcase
  148. {
  149. std::string raw;
  150. std::string filename;
  151. std::string stem;
  152. std::string extension;
  153. };
  154. const FilenameDecompTestcase FilenameTestCases[] =
  155. {
  156. {"", "", "", ""}
  157. , {".", ".", ".", ""}
  158. , {"..", "..", "..", ""}
  159. , {"/", "", "", ""}
  160. , {"foo", "foo", "foo", ""}
  161. , {"/foo/bar.txt", "bar.txt", "bar", ".txt"}
  162. , {"foo..txt", "foo..txt", "foo.", ".txt"}
  163. , {".profile", ".profile", ".profile", ""}
  164. , {".profile.txt", ".profile.txt", ".profile", ".txt"}
  165. };
  166. void decompFilenameTest()
  167. {
  168. using namespace fs;
  169. for (auto const & TC : FilenameTestCases) {
  170. CHECKPOINT(TC.raw.c_str());
  171. fs::path p(TC.raw);
  172. ASSERT_EQ(p, TC.raw);
  173. ASSERT_NOEXCEPT(p.empty());
  174. ASSERT_EQ(p.filename(), TC.filename);
  175. ASSERT_NEQ(p.has_filename(), TC.filename.empty());
  176. ASSERT_EQ(p.stem(), TC.stem);
  177. ASSERT_NEQ(p.has_stem(), TC.stem.empty());
  178. ASSERT_EQ(p.extension(), TC.extension);
  179. ASSERT_NEQ(p.has_extension(), TC.extension.empty());
  180. }
  181. }
  182. int main(int, char**)
  183. {
  184. decompPathTest();
  185. decompFilenameTest();
  186. return 0;
  187. }