path.decompose.pass.cpp 7.0 KB

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