path.decompose.pass.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 <experimental/filesystem>
  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. namespace fs = std::experimental::filesystem;
  50. struct PathDecomposeTestcase
  51. {
  52. std::string raw;
  53. std::vector<std::string> elements;
  54. std::string root_path;
  55. std::string root_name;
  56. std::string root_directory;
  57. std::string relative_path;
  58. std::string parent_path;
  59. std::string filename;
  60. };
  61. const PathDecomposeTestcase PathTestCases[] =
  62. {
  63. {"", {}, "", "", "", "", "", ""}
  64. , {".", {"."}, "", "", "", ".", "", "."}
  65. , {"..", {".."}, "", "", "", "..", "", ".."}
  66. , {"foo", {"foo"}, "", "", "", "foo", "", "foo"}
  67. , {"/", {"/"}, "/", "", "/", "", "", "/"}
  68. , {"/foo", {"/", "foo"}, "/", "", "/", "foo", "/", "foo"}
  69. , {"foo/", {"foo", "."}, "", "", "", "foo/", "foo", "."}
  70. , {"/foo/", {"/", "foo", "."}, "/", "", "/", "foo/", "/foo", "."}
  71. , {"foo/bar", {"foo","bar"}, "", "", "", "foo/bar", "foo", "bar"}
  72. , {"/foo//bar", {"/","foo","bar"}, "/", "", "/", "foo/bar", "/foo", "bar"}
  73. , {"//net", {"//net"}, "//net", "//net", "", "", "", "//net"}
  74. , {"//net/foo", {"//net", "/", "foo"}, "//net/", "//net", "/", "foo", "//net/", "foo"}
  75. , {"///foo///", {"/", "foo", "."}, "/", "", "/", "foo///", "///foo", "."}
  76. , {"///foo///bar", {"/", "foo", "bar"}, "/", "", "/", "foo///bar", "///foo", "bar"}
  77. , {"/.", {"/", "."}, "/", "", "/", ".", "/", "."}
  78. , {"./", {".", "."}, "", "", "", "./", ".", "."}
  79. , {"/..", {"/", ".."}, "/", "", "/", "..", "/", ".."}
  80. , {"../", {"..", "."}, "", "", "", "../", "..", "."}
  81. , {"foo/.", {"foo", "."}, "", "", "", "foo/.", "foo", "."}
  82. , {"foo/..", {"foo", ".."}, "", "", "", "foo/..", "foo", ".."}
  83. , {"foo/./", {"foo", ".", "."}, "", "", "", "foo/./", "foo/.", "."}
  84. , {"foo/./bar", {"foo", ".", "bar"}, "", "", "", "foo/./bar", "foo/.", "bar"}
  85. , {"foo/../", {"foo", "..", "."}, "", "", "", "foo/../", "foo/..", "."}
  86. , {"foo/../bar", {"foo", "..", "bar"}, "", "", "", "foo/../bar", "foo/..", "bar"}
  87. , {"c:", {"c:"}, "", "", "", "c:", "", "c:"}
  88. , {"c:/", {"c:", "."}, "", "", "", "c:/", "c:", "."}
  89. , {"c:foo", {"c:foo"}, "", "", "", "c:foo", "", "c:foo"}
  90. , {"c:/foo", {"c:", "foo"}, "", "", "", "c:/foo", "c:", "foo"}
  91. , {"c:foo/", {"c:foo", "."}, "", "", "", "c:foo/", "c:foo", "."}
  92. , {"c:/foo/", {"c:", "foo", "."}, "", "", "", "c:/foo/", "c:/foo", "."}
  93. , {"c:/foo/bar", {"c:", "foo", "bar"}, "", "", "", "c:/foo/bar", "c:/foo", "bar"}
  94. , {"prn:", {"prn:"}, "", "", "", "prn:", "", "prn:"}
  95. , {"c:\\", {"c:\\"}, "", "", "", "c:\\", "", "c:\\"}
  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/", {"c:\\foo", "."}, "", "", "", "c:\\foo/", "c:\\foo", "."}
  100. , {"c:/foo\\bar", {"c:", "foo\\bar"}, "", "", "", "c:/foo\\bar", "c:", "foo\\bar"}
  101. , {"//", {"//"}, "//", "//", "", "", "", "//"}
  102. };
  103. void decompPathTest()
  104. {
  105. using namespace fs;
  106. for (auto const & TC : PathTestCases) {
  107. path p(TC.raw);
  108. assert(p == TC.raw);
  109. assert(p.root_path() == TC.root_path);
  110. assert(p.has_root_path() != TC.root_path.empty());
  111. assert(p.root_name() == TC.root_name);
  112. assert(p.has_root_name() != TC.root_name.empty());
  113. assert(p.root_directory() == TC.root_directory);
  114. assert(p.has_root_directory() != TC.root_directory.empty());
  115. assert(p.relative_path() == TC.relative_path);
  116. assert(p.has_relative_path() != TC.relative_path.empty());
  117. assert(p.parent_path() == TC.parent_path);
  118. assert(p.has_parent_path() != TC.parent_path.empty());
  119. assert(p.filename() == TC.filename);
  120. assert(p.has_filename() != TC.filename.empty());
  121. assert(p.is_absolute() == p.has_root_directory());
  122. assert(p.is_relative() != p.is_absolute());
  123. assert(checkCollectionsEqual(p.begin(), p.end(),
  124. TC.elements.begin(), TC.elements.end()));
  125. // check backwards
  126. std::vector<fs::path> Parts;
  127. for (auto it = p.end(); it != p.begin(); )
  128. Parts.push_back(*--it);
  129. assert(checkCollectionsEqual(Parts.begin(), Parts.end(),
  130. TC.elements.rbegin(), TC.elements.rend()));
  131. }
  132. }
  133. struct FilenameDecompTestcase
  134. {
  135. std::string raw;
  136. std::string filename;
  137. std::string stem;
  138. std::string extension;
  139. };
  140. const FilenameDecompTestcase FilenameTestCases[] =
  141. {
  142. {"", "", "", ""}
  143. , {".", ".", ".", ""}
  144. , {"..", "..", "..", ""}
  145. , {"/", "/", "/", ""}
  146. , {"foo", "foo", "foo", ""}
  147. , {"/foo/bar.txt", "bar.txt", "bar", ".txt"}
  148. , {"foo..txt", "foo..txt", "foo.", ".txt"}
  149. };
  150. void decompFilenameTest()
  151. {
  152. using namespace fs;
  153. for (auto const & TC : FilenameTestCases) {
  154. path p(TC.raw);
  155. assert(p == TC.raw);
  156. ASSERT_NOEXCEPT(p.empty());
  157. assert(p.filename() == TC.filename);
  158. assert(p.has_filename() != TC.filename.empty());
  159. assert(p.stem() == TC.stem);
  160. assert(p.has_stem() != TC.stem.empty());
  161. assert(p.extension() == TC.extension);
  162. assert(p.has_extension() != TC.extension.empty());
  163. }
  164. }
  165. int main()
  166. {
  167. decompPathTest();
  168. decompFilenameTest();
  169. }