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 {
operator ()ComparePathExact60   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 #ifdef _WIN32
90     , {"//net", {"//net"}, "//net", "//net", "", "", "//net", ""}
91     , {"//net/", {"//net", "/"}, "//net/", "//net", "/", "", "//net/", ""}
92     , {"//net/foo", {"//net", "/", "foo"}, "//net/", "//net", "/", "foo", "//net/", "foo"}
93 #else
94     , {"//net", {"/", "net"}, "/", "", "/", "net", "/", "net"}
95     , {"//net/", {"/", "net", ""}, "/", "", "/", "net/", "//net", ""}
96     , {"//net/foo", {"/", "net", "foo"}, "/", "", "/", "net/foo", "/net", "foo"}
97 #endif
98     , {"///foo///", {"/", "foo", ""}, "/", "", "/", "foo///", "///foo", ""}
99     , {"///foo///bar", {"/", "foo", "bar"}, "/", "", "/", "foo///bar", "///foo", "bar"}
100     , {"/.", {"/", "."}, "/", "", "/", ".", "/", "."}
101     , {"./", {".", ""}, "", "", "", "./", ".", ""}
102     , {"/..", {"/", ".."}, "/", "", "/", "..", "/", ".."}
103     , {"../", {"..", ""}, "", "", "", "../", "..", ""}
104     , {"foo/.", {"foo", "."}, "", "", "", "foo/.", "foo", "."}
105     , {"foo/..", {"foo", ".."}, "", "", "", "foo/..", "foo", ".."}
106     , {"foo/./", {"foo", ".", ""}, "", "", "", "foo/./", "foo/.", ""}
107     , {"foo/./bar", {"foo", ".", "bar"}, "", "", "", "foo/./bar", "foo/.", "bar"}
108     , {"foo/../", {"foo", "..", ""}, "", "", "", "foo/../", "foo/..", ""}
109     , {"foo/../bar", {"foo", "..", "bar"}, "", "", "", "foo/../bar", "foo/..", "bar"}
110 #ifdef _WIN32
111     , {"c:", {"c:"}, "c:", "c:", "", "", "c:", ""}
112     , {"c:/", {"c:", "/"}, "c:/", "c:", "/", "", "c:/", ""}
113     , {"c:foo", {"c:", "foo"}, "c:", "c:", "", "foo", "c:", "foo"}
114     , {"c:/foo", {"c:", "/", "foo"}, "c:/", "c:", "/", "foo", "c:/", "foo"}
115     , {"c:foo/", {"c:", "foo", ""}, "c:", "c:", "", "foo/", "c:foo", ""}
116     , {"c:/foo/", {"c:", "/", "foo", ""}, "c:/", "c:", "/", "foo/",  "c:/foo", ""}
117     , {"c:/foo/bar", {"c:", "/", "foo", "bar"}, "c:/", "c:", "/", "foo/bar", "c:/foo", "bar"}
118 #else
119     , {"c:", {"c:"}, "", "", "", "c:", "", "c:"}
120     , {"c:/", {"c:", ""}, "", "", "", "c:/", "c:", ""}
121     , {"c:foo", {"c:foo"}, "", "", "", "c:foo", "", "c:foo"}
122     , {"c:/foo", {"c:", "foo"}, "", "", "", "c:/foo", "c:", "foo"}
123     , {"c:foo/", {"c:foo", ""}, "", "", "", "c:foo/", "c:foo", ""}
124     , {"c:/foo/", {"c:", "foo", ""}, "", "", "", "c:/foo/",  "c:/foo", ""}
125     , {"c:/foo/bar", {"c:", "foo", "bar"}, "", "", "", "c:/foo/bar", "c:/foo", "bar"}
126 #endif
127     , {"prn:", {"prn:"}, "", "", "", "prn:", "", "prn:"}
128 #ifdef _WIN32
129     , {"c:\\", {"c:", "\\"}, "c:\\", "c:", "\\", "", "c:\\", ""}
130     , {"c:\\foo", {"c:", "\\", "foo"}, "c:\\", "c:", "\\", "foo", "c:\\", "foo"}
131     , {"c:foo\\", {"c:", "foo", ""}, "c:", "c:", "", "foo\\", "c:foo", ""}
132     , {"c:\\foo\\", {"c:", "\\", "foo", ""}, "c:\\", "c:", "\\", "foo\\", "c:\\foo", ""}
133     , {"c:\\foo/",  {"c:", "\\", "foo", ""}, "c:\\", "c:", "\\", "foo/", "c:\\foo", ""}
134     , {"c:/foo\\bar", {"c:", "/", "foo", "bar"}, "c:\\", "c:", "\\", "foo\\bar", "c:/foo", "bar"}
135 #else
136     , {"c:\\", {"c:\\"}, "", "", "", "c:\\", "", "c:\\"}
137     , {"c:\\foo", {"c:\\foo"}, "", "", "", "c:\\foo", "", "c:\\foo"}
138     , {"c:foo\\", {"c:foo\\"}, "", "", "", "c:foo\\", "", "c:foo\\"}
139     , {"c:\\foo\\", {"c:\\foo\\"}, "", "", "", "c:\\foo\\", "", "c:\\foo\\"}
140     , {"c:\\foo/",  {"c:\\foo", ""}, "", "", "", "c:\\foo/", "c:\\foo", ""}
141     , {"c:/foo\\bar", {"c:", "foo\\bar"}, "", "", "", "c:/foo\\bar", "c:", "foo\\bar"}
142 #endif
143     , {"//", {"/"}, "/", "", "/", "", "/", ""}
144   };
145 
decompPathTest()146 void decompPathTest()
147 {
148   using namespace fs;
149   for (auto const & TC : PathTestCases) {
150     fs::path p(TC.raw);
151     assert(p == TC.raw);
152 
153     assert(p.root_path() == TC.root_path);
154     assert(p.has_root_path() != TC.root_path.empty());
155 
156 #ifndef _WIN32
157     assert(p.root_name().native().empty());
158 #endif
159     assert(p.root_name() == TC.root_name);
160     assert(p.has_root_name() != TC.root_name.empty());
161 
162     assert(p.root_directory() == TC.root_directory);
163     assert(p.has_root_directory() != TC.root_directory.empty());
164 
165     assert(p.relative_path() == TC.relative_path);
166     assert(p.has_relative_path() != TC.relative_path.empty());
167 
168     assert(p.parent_path() == TC.parent_path);
169     assert(p.has_parent_path() != TC.parent_path.empty());
170 
171     assert(p.filename() == TC.filename);
172     assert(p.has_filename() != TC.filename.empty());
173 
174 #ifdef _WIN32
175     if (!p.has_root_name()) {
176       assert(p.is_absolute() == false);
177     } else {
178       std::string root_name = p.root_name().string();
179       assert(root_name.length() >= 2);
180       if (root_name[1] == ':') {
181         // Drive letter, absolute if has a root directory
182         assert(p.is_absolute() == p.has_root_directory());
183       } else {
184         // Possibly a server path
185         // Convert to one separator style, for simplicity
186         std::replace(root_name.begin(), root_name.end(), '\\', '/');
187         if (root_name[0] == '/' && root_name[1] == '/')
188           assert(p.is_absolute() == true);
189         else
190           assert(p.is_absolute() == false);
191       }
192     }
193 #else
194     assert(p.is_absolute() == p.has_root_directory());
195 #endif
196     assert(p.is_relative() != p.is_absolute());
197     if (p.empty())
198       assert(p.is_relative());
199 
200     assert(static_cast<std::size_t>(std::distance(p.begin(), p.end())) == TC.elements.size());
201     assert(std::equal(p.begin(), p.end(), TC.elements.begin(), ComparePathExact()));
202 
203     // check backwards
204     std::vector<fs::path> Parts;
205     for (auto it = p.end(); it != p.begin(); )
206       Parts.push_back(*--it);
207     assert(static_cast<std::size_t>(std::distance(Parts.begin(), Parts.end())) == TC.elements.size());
208     assert(std::equal(Parts.begin(), Parts.end(), TC.elements.rbegin(), ComparePathExact()));
209   }
210 }
211 
212 
213 struct FilenameDecompTestcase
214 {
215   std::string raw;
216   std::string filename;
217   std::string stem;
218   std::string extension;
219 };
220 
221 const FilenameDecompTestcase FilenameTestCases[] =
222 {
223     {"", "", "", ""}
224   , {".", ".", ".", ""}
225   , {"..", "..", "..", ""}
226   , {"/", "", "", ""}
227   , {"foo", "foo", "foo", ""}
228   , {"/foo/bar.txt", "bar.txt", "bar", ".txt"}
229   , {"foo..txt", "foo..txt", "foo.", ".txt"}
230   , {".profile", ".profile", ".profile", ""}
231   , {".profile.txt", ".profile.txt", ".profile", ".txt"}
232 };
233 
234 
decompFilenameTest()235 void decompFilenameTest()
236 {
237   using namespace fs;
238   for (auto const & TC : FilenameTestCases) {
239     fs::path p(TC.raw);
240     assert(p == TC.raw);
241     ASSERT_NOEXCEPT(p.empty());
242 
243     assert(p.filename() == TC.filename);
244     assert(p.has_filename() != TC.filename.empty());
245 
246     assert(p.stem() == TC.stem);
247     assert(p.has_stem() != TC.stem.empty());
248 
249     assert(p.extension() == TC.extension);
250     assert(p.has_extension() != TC.extension.empty());
251   }
252 }
253 
main(int,char **)254 int main(int, char**)
255 {
256   decompPathTest();
257   decompFilenameTest();
258 
259   return 0;
260 }
261