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