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 <type_traits>
48 #include <vector>
49 #include <cassert>
50 
51 #include "test_macros.h"
52 #include "test_iterators.h"
53 #include "count_new.h"
54 #include "filesystem_test_helper.h"
55 #include "verbose_assert.h"
56 
57 struct ComparePathExact {
58   bool operator()(std::string const& LHS, std::string const& RHS) const {
59     return LHS == RHS;
60   }
61 };
62 
63 struct PathDecomposeTestcase
64 {
65     std::string raw;
66     std::vector<std::string> elements;
67     std::string root_path;
68     std::string root_name;
69     std::string root_directory;
70     std::string relative_path;
71     std::string parent_path;
72     std::string filename;
73 };
74 
75 const PathDecomposeTestcase PathTestCases[] =
76   {
77       {"", {}, "", "", "", "", "", ""}
78     , {".", {"."}, "", "", "", ".", "", "."}
79     , {"..", {".."}, "", "", "", "..", "", ".."}
80     , {"foo", {"foo"}, "", "", "", "foo", "", "foo"}
81     , {"/", {"/"}, "/", "", "/", "", "/", ""}
82     , {"/foo", {"/", "foo"}, "/", "", "/", "foo", "/", "foo"}
83     , {"foo/", {"foo", ""}, "", "", "", "foo/", "foo", ""}
84     , {"/foo/", {"/", "foo", ""}, "/", "", "/", "foo/", "/foo", ""}
85     , {"foo/bar", {"foo","bar"}, "",  "", "",  "foo/bar", "foo", "bar"}
86     , {"/foo//bar", {"/","foo","bar"}, "/", "", "/", "foo/bar", "/foo", "bar"}
87     , {"//net", {"/", "net"}, "/", "", "/", "net", "/", "net"}
88     , {"//net/foo", {"/", "net", "foo"}, "/", "", "/", "net/foo", "/net", "foo"}
89     , {"///foo///", {"/", "foo", ""}, "/", "", "/", "foo///", "///foo", ""}
90     , {"///foo///bar", {"/", "foo", "bar"}, "/", "", "/", "foo///bar", "///foo", "bar"}
91     , {"/.", {"/", "."}, "/", "", "/", ".", "/", "."}
92     , {"./", {".", ""}, "", "", "", "./", ".", ""}
93     , {"/..", {"/", ".."}, "/", "", "/", "..", "/", ".."}
94     , {"../", {"..", ""}, "", "", "", "../", "..", ""}
95     , {"foo/.", {"foo", "."}, "", "", "", "foo/.", "foo", "."}
96     , {"foo/..", {"foo", ".."}, "", "", "", "foo/..", "foo", ".."}
97     , {"foo/./", {"foo", ".", ""}, "", "", "", "foo/./", "foo/.", ""}
98     , {"foo/./bar", {"foo", ".", "bar"}, "", "", "", "foo/./bar", "foo/.", "bar"}
99     , {"foo/../", {"foo", "..", ""}, "", "", "", "foo/../", "foo/..", ""}
100     , {"foo/../bar", {"foo", "..", "bar"}, "", "", "", "foo/../bar", "foo/..", "bar"}
101     , {"c:", {"c:"}, "", "", "", "c:", "", "c:"}
102     , {"c:/", {"c:", ""}, "", "", "", "c:/", "c:", ""}
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/", {"c:", "foo", ""}, "", "", "", "c:/foo/",  "c:/foo", ""}
107     , {"c:/foo/bar", {"c:", "foo", "bar"}, "", "", "", "c:/foo/bar", "c:/foo", "bar"}
108     , {"prn:", {"prn:"}, "", "", "", "prn:", "", "prn:"}
109     , {"c:\\", {"c:\\"}, "", "", "", "c:\\", "", "c:\\"}
110     , {"c:\\foo", {"c:\\foo"}, "", "", "", "c:\\foo", "", "c:\\foo"}
111     , {"c:foo\\", {"c:foo\\"}, "", "", "", "c:foo\\", "", "c:foo\\"}
112     , {"c:\\foo\\", {"c:\\foo\\"}, "", "", "", "c:\\foo\\", "", "c:\\foo\\"}
113     , {"c:\\foo/",  {"c:\\foo", ""}, "", "", "", "c:\\foo/", "c:\\foo", ""}
114     , {"c:/foo\\bar", {"c:", "foo\\bar"}, "", "", "", "c:/foo\\bar", "c:", "foo\\bar"}
115     , {"//", {"/"}, "/", "", "/", "", "/", ""}
116   };
117 
118 void decompPathTest()
119 {
120   using namespace fs;
121   for (auto const & TC : PathTestCases) {
122     fs::path p(TC.raw);
123     ASSERT(p == TC.raw);
124 
125     ASSERT_EQ(p.root_path(), TC.root_path);
126     ASSERT_NEQ(p.has_root_path(), TC.root_path.empty());
127 
128     ASSERT(p.root_name().native().empty())
129         << DISPLAY(p.root_name());
130     ASSERT_EQ(p.root_name(),TC.root_name);
131     ASSERT_NEQ(p.has_root_name(), TC.root_name.empty());
132 
133     ASSERT_EQ(p.root_directory(), TC.root_directory);
134     ASSERT_NEQ(p.has_root_directory(), TC.root_directory.empty());
135 
136     ASSERT_EQ(p.relative_path(), TC.relative_path);
137     ASSERT_NEQ(p.has_relative_path(), TC.relative_path.empty());
138 
139     ASSERT_EQ(p.parent_path(), TC.parent_path);
140     ASSERT_NEQ(p.has_parent_path(), TC.parent_path.empty());
141 
142     ASSERT_EQ(p.filename(), TC.filename);
143     ASSERT_NEQ(p.has_filename(), TC.filename.empty());
144 
145     ASSERT_EQ(p.is_absolute(), p.has_root_directory());
146     ASSERT_NEQ(p.is_relative(), p.is_absolute());
147     if (p.empty())
148       ASSERT(p.is_relative());
149 
150     ASSERT_COLLECTION_EQ_COMP(
151         p.begin(), p.end(),
152         TC.elements.begin(), TC.elements.end(),
153         ComparePathExact()
154     );
155     // check backwards
156 
157     std::vector<fs::path> Parts;
158     for (auto it = p.end(); it != p.begin(); )
159       Parts.push_back(*--it);
160     ASSERT_COLLECTION_EQ_COMP(Parts.begin(), Parts.end(),
161                                  TC.elements.rbegin(), TC.elements.rend(),
162                               ComparePathExact());
163   }
164 }
165 
166 
167 struct FilenameDecompTestcase
168 {
169   std::string raw;
170   std::string filename;
171   std::string stem;
172   std::string extension;
173 };
174 
175 const FilenameDecompTestcase FilenameTestCases[] =
176 {
177     {"", "", "", ""}
178   , {".", ".", ".", ""}
179   , {"..", "..", "..", ""}
180   , {"/", "", "", ""}
181   , {"foo", "foo", "foo", ""}
182   , {"/foo/bar.txt", "bar.txt", "bar", ".txt"}
183   , {"foo..txt", "foo..txt", "foo.", ".txt"}
184   , {".profile", ".profile", ".profile", ""}
185   , {".profile.txt", ".profile.txt", ".profile", ".txt"}
186 };
187 
188 
189 void decompFilenameTest()
190 {
191   using namespace fs;
192   for (auto const & TC : FilenameTestCases) {
193     fs::path p(TC.raw);
194     ASSERT_EQ(p, TC.raw);
195     ASSERT_NOEXCEPT(p.empty());
196 
197     ASSERT_EQ(p.filename(), TC.filename);
198     ASSERT_NEQ(p.has_filename(), TC.filename.empty());
199 
200     ASSERT_EQ(p.stem(), TC.stem);
201     ASSERT_NEQ(p.has_stem(), TC.stem.empty());
202 
203     ASSERT_EQ(p.extension(), TC.extension);
204     ASSERT_NEQ(p.has_extension(), TC.extension.empty());
205   }
206 }
207 
208 int main(int, char**)
209 {
210   decompPathTest();
211   decompFilenameTest();
212 
213   return 0;
214 }
215