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 9 // UNSUPPORTED: c++03 10 11 // <filesystem> 12 13 // class path 14 15 // 8.4.9 path decomposition [path.decompose] 16 //------------------------------------------ 17 // path root_name() const; 18 // path root_directory() const; 19 // path root_path() const; 20 // path relative_path() const; 21 // path parent_path() const; 22 // path filename() const; 23 // path stem() const; 24 // path extension() const; 25 //------------------------------- 26 // 8.4.10 path query [path.query] 27 //------------------------------- 28 // bool empty() const noexcept; 29 // bool has_root_path() const; 30 // bool has_root_name() const; 31 // bool has_root_directory() const; 32 // bool has_relative_path() const; 33 // bool has_parent_path() const; 34 // bool has_filename() const; 35 // bool has_stem() const; 36 // bool has_extension() const; 37 // bool is_absolute() const; 38 // bool is_relative() const; 39 //------------------------------- 40 // 8.5 path iterators [path.itr] 41 //------------------------------- 42 // iterator begin() const; 43 // iterator end() const; 44 45 46 #include "filesystem_include.h" 47 #include <algorithm> 48 #include <cassert> 49 #include <cstddef> 50 #include <iterator> 51 #include <type_traits> 52 #include <vector> 53 54 #include "test_macros.h" 55 #include "test_iterators.h" 56 #include "count_new.h" 57 #include "filesystem_test_helper.h" 58 59 struct ComparePathExact { 60 bool operator()(fs::path const& LHS, std::string const& RHS) const { 61 return LHS.string() == RHS; 62 } 63 }; 64 65 struct PathDecomposeTestcase 66 { 67 std::string raw; 68 std::vector<std::string> elements; 69 std::string root_path; 70 std::string root_name; 71 std::string root_directory; 72 std::string relative_path; 73 std::string parent_path; 74 std::string filename; 75 }; 76 77 const PathDecomposeTestcase PathTestCases[] = 78 { 79 {"", {}, "", "", "", "", "", ""} 80 , {".", {"."}, "", "", "", ".", "", "."} 81 , {"..", {".."}, "", "", "", "..", "", ".."} 82 , {"foo", {"foo"}, "", "", "", "foo", "", "foo"} 83 , {"/", {"/"}, "/", "", "/", "", "/", ""} 84 , {"/foo", {"/", "foo"}, "/", "", "/", "foo", "/", "foo"} 85 , {"foo/", {"foo", ""}, "", "", "", "foo/", "foo", ""} 86 , {"/foo/", {"/", "foo", ""}, "/", "", "/", "foo/", "/foo", ""} 87 , {"foo/bar", {"foo","bar"}, "", "", "", "foo/bar", "foo", "bar"} 88 , {"/foo//bar", {"/","foo","bar"}, "/", "", "/", "foo/bar", "/foo", "bar"} 89 , {"//net", {"/", "net"}, "/", "", "/", "net", "/", "net"} 90 , {"//net/foo", {"/", "net", "foo"}, "/", "", "/", "net/foo", "/net", "foo"} 91 , {"///foo///", {"/", "foo", ""}, "/", "", "/", "foo///", "///foo", ""} 92 , {"///foo///bar", {"/", "foo", "bar"}, "/", "", "/", "foo///bar", "///foo", "bar"} 93 , {"/.", {"/", "."}, "/", "", "/", ".", "/", "."} 94 , {"./", {".", ""}, "", "", "", "./", ".", ""} 95 , {"/..", {"/", ".."}, "/", "", "/", "..", "/", ".."} 96 , {"../", {"..", ""}, "", "", "", "../", "..", ""} 97 , {"foo/.", {"foo", "."}, "", "", "", "foo/.", "foo", "."} 98 , {"foo/..", {"foo", ".."}, "", "", "", "foo/..", "foo", ".."} 99 , {"foo/./", {"foo", ".", ""}, "", "", "", "foo/./", "foo/.", ""} 100 , {"foo/./bar", {"foo", ".", "bar"}, "", "", "", "foo/./bar", "foo/.", "bar"} 101 , {"foo/../", {"foo", "..", ""}, "", "", "", "foo/../", "foo/..", ""} 102 , {"foo/../bar", {"foo", "..", "bar"}, "", "", "", "foo/../bar", "foo/..", "bar"} 103 , {"c:", {"c:"}, "", "", "", "c:", "", "c:"} 104 , {"c:/", {"c:", ""}, "", "", "", "c:/", "c:", ""} 105 , {"c:foo", {"c:foo"}, "", "", "", "c:foo", "", "c:foo"} 106 , {"c:/foo", {"c:", "foo"}, "", "", "", "c:/foo", "c:", "foo"} 107 , {"c:foo/", {"c:foo", ""}, "", "", "", "c:foo/", "c:foo", ""} 108 , {"c:/foo/", {"c:", "foo", ""}, "", "", "", "c:/foo/", "c:/foo", ""} 109 , {"c:/foo/bar", {"c:", "foo", "bar"}, "", "", "", "c:/foo/bar", "c:/foo", "bar"} 110 , {"prn:", {"prn:"}, "", "", "", "prn:", "", "prn:"} 111 , {"c:\\", {"c:\\"}, "", "", "", "c:\\", "", "c:\\"} 112 , {"c:\\foo", {"c:\\foo"}, "", "", "", "c:\\foo", "", "c:\\foo"} 113 , {"c:foo\\", {"c:foo\\"}, "", "", "", "c:foo\\", "", "c:foo\\"} 114 , {"c:\\foo\\", {"c:\\foo\\"}, "", "", "", "c:\\foo\\", "", "c:\\foo\\"} 115 , {"c:\\foo/", {"c:\\foo", ""}, "", "", "", "c:\\foo/", "c:\\foo", ""} 116 , {"c:/foo\\bar", {"c:", "foo\\bar"}, "", "", "", "c:/foo\\bar", "c:", "foo\\bar"} 117 , {"//", {"/"}, "/", "", "/", "", "/", ""} 118 }; 119 120 void decompPathTest() 121 { 122 using namespace fs; 123 for (auto const & TC : PathTestCases) { 124 fs::path p(TC.raw); 125 assert(p == TC.raw); 126 127 assert(p.root_path() == TC.root_path); 128 assert(p.has_root_path() != TC.root_path.empty()); 129 130 assert(p.root_name().native().empty()); 131 assert(p.root_name() == TC.root_name); 132 assert(p.has_root_name() != TC.root_name.empty()); 133 134 assert(p.root_directory() == TC.root_directory); 135 assert(p.has_root_directory() != TC.root_directory.empty()); 136 137 assert(p.relative_path() == TC.relative_path); 138 assert(p.has_relative_path() != TC.relative_path.empty()); 139 140 assert(p.parent_path() == TC.parent_path); 141 assert(p.has_parent_path() != TC.parent_path.empty()); 142 143 assert(p.filename() == TC.filename); 144 assert(p.has_filename() != TC.filename.empty()); 145 146 assert(p.is_absolute() == p.has_root_directory()); 147 assert(p.is_relative() != p.is_absolute()); 148 if (p.empty()) 149 assert(p.is_relative()); 150 151 assert(static_cast<std::size_t>(std::distance(p.begin(), p.end())) == TC.elements.size()); 152 assert(std::equal(p.begin(), p.end(), TC.elements.begin(), ComparePathExact())); 153 154 // check backwards 155 std::vector<fs::path> Parts; 156 for (auto it = p.end(); it != p.begin(); ) 157 Parts.push_back(*--it); 158 assert(static_cast<std::size_t>(std::distance(Parts.begin(), Parts.end())) == TC.elements.size()); 159 assert(std::equal(Parts.begin(), Parts.end(), TC.elements.rbegin(), ComparePathExact())); 160 } 161 } 162 163 164 struct FilenameDecompTestcase 165 { 166 std::string raw; 167 std::string filename; 168 std::string stem; 169 std::string extension; 170 }; 171 172 const FilenameDecompTestcase FilenameTestCases[] = 173 { 174 {"", "", "", ""} 175 , {".", ".", ".", ""} 176 , {"..", "..", "..", ""} 177 , {"/", "", "", ""} 178 , {"foo", "foo", "foo", ""} 179 , {"/foo/bar.txt", "bar.txt", "bar", ".txt"} 180 , {"foo..txt", "foo..txt", "foo.", ".txt"} 181 , {".profile", ".profile", ".profile", ""} 182 , {".profile.txt", ".profile.txt", ".profile", ".txt"} 183 }; 184 185 186 void decompFilenameTest() 187 { 188 using namespace fs; 189 for (auto const & TC : FilenameTestCases) { 190 fs::path p(TC.raw); 191 assert(p == TC.raw); 192 ASSERT_NOEXCEPT(p.empty()); 193 194 assert(p.filename() == TC.filename); 195 assert(p.has_filename() != TC.filename.empty()); 196 197 assert(p.stem() == TC.stem); 198 assert(p.has_stem() != TC.stem.empty()); 199 200 assert(p.extension() == TC.extension); 201 assert(p.has_extension() != TC.extension.empty()); 202 } 203 } 204 205 int main(int, char**) 206 { 207 decompPathTest(); 208 decompFilenameTest(); 209 210 return 0; 211 } 212