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
10 // UNSUPPORTED: c++98, c++03
11
12 // <filesystem>
13
14 // class path
15
16 // 8.4.9 path decomposition [path.decompose]
17 //------------------------------------------
18 // path root_name() const;
19 // path root_directory() const;
20 // path root_path() const;
21 // path relative_path() const;
22 // path parent_path() const;
23 // path filename() const;
24 // path stem() const;
25 // path extension() const;
26 //-------------------------------
27 // 8.4.10 path query [path.query]
28 //-------------------------------
29 // bool empty() const noexcept;
30 // bool has_root_path() const;
31 // bool has_root_name() const;
32 // bool has_root_directory() const;
33 // bool has_relative_path() const;
34 // bool has_parent_path() const;
35 // bool has_filename() const;
36 // bool has_stem() const;
37 // bool has_extension() const;
38 // bool is_absolute() const;
39 // bool is_relative() const;
40 //-------------------------------
41 // 8.5 path iterators [path.itr]
42 //-------------------------------
43 // iterator begin() const;
44 // iterator end() const;
45
46
47 #include "filesystem_include.hpp"
48 #include <type_traits>
49 #include <vector>
50 #include <cassert>
51
52 #include "test_macros.h"
53 #include "test_iterators.h"
54 #include "count_new.hpp"
55 #include "filesystem_test_helper.hpp"
56 #include "assert_checkpoint.h"
57 #include "verbose_assert.h"
58
59 struct ComparePathExact {
operator ()ComparePathExact60 bool operator()(std::string const& LHS, std::string const& RHS) const {
61 return LHS == 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
decompPathTest()120 void decompPathTest()
121 {
122 using namespace fs;
123 for (auto const & TC : PathTestCases) {
124 CHECKPOINT(TC.raw.c_str());
125 fs::path p(TC.raw);
126 ASSERT(p == TC.raw);
127
128 ASSERT_EQ(p.root_path(), TC.root_path);
129 ASSERT_NEQ(p.has_root_path(), TC.root_path.empty());
130
131 ASSERT(p.root_name().native().empty())
132 << DISPLAY(p.root_name());
133 ASSERT_EQ(p.root_name(),TC.root_name);
134 ASSERT_NEQ(p.has_root_name(), TC.root_name.empty());
135
136 ASSERT_EQ(p.root_directory(), TC.root_directory);
137 ASSERT_NEQ(p.has_root_directory(), TC.root_directory.empty());
138
139 ASSERT_EQ(p.relative_path(), TC.relative_path);
140 ASSERT_NEQ(p.has_relative_path(), TC.relative_path.empty());
141
142 ASSERT_EQ(p.parent_path(), TC.parent_path);
143 ASSERT_NEQ(p.has_parent_path(), TC.parent_path.empty());
144
145 ASSERT_EQ(p.filename(), TC.filename);
146 ASSERT_NEQ(p.has_filename(), TC.filename.empty());
147
148 ASSERT_EQ(p.is_absolute(), p.has_root_directory());
149 ASSERT_NEQ(p.is_relative(), p.is_absolute());
150 if (p.empty())
151 ASSERT(p.is_relative());
152
153 ASSERT_COLLECTION_EQ_COMP(
154 p.begin(), p.end(),
155 TC.elements.begin(), TC.elements.end(),
156 ComparePathExact()
157 );
158 // check backwards
159
160 std::vector<fs::path> Parts;
161 for (auto it = p.end(); it != p.begin(); )
162 Parts.push_back(*--it);
163 ASSERT_COLLECTION_EQ_COMP(Parts.begin(), Parts.end(),
164 TC.elements.rbegin(), TC.elements.rend(),
165 ComparePathExact());
166 }
167 }
168
169
170 struct FilenameDecompTestcase
171 {
172 std::string raw;
173 std::string filename;
174 std::string stem;
175 std::string extension;
176 };
177
178 const FilenameDecompTestcase FilenameTestCases[] =
179 {
180 {"", "", "", ""}
181 , {".", ".", ".", ""}
182 , {"..", "..", "..", ""}
183 , {"/", "", "", ""}
184 , {"foo", "foo", "foo", ""}
185 , {"/foo/bar.txt", "bar.txt", "bar", ".txt"}
186 , {"foo..txt", "foo..txt", "foo.", ".txt"}
187 , {".profile", ".profile", ".profile", ""}
188 , {".profile.txt", ".profile.txt", ".profile", ".txt"}
189 };
190
191
decompFilenameTest()192 void decompFilenameTest()
193 {
194 using namespace fs;
195 for (auto const & TC : FilenameTestCases) {
196 CHECKPOINT(TC.raw.c_str());
197 fs::path p(TC.raw);
198 ASSERT_EQ(p, TC.raw);
199 ASSERT_NOEXCEPT(p.empty());
200
201 ASSERT_EQ(p.filename(), TC.filename);
202 ASSERT_NEQ(p.has_filename(), TC.filename.empty());
203
204 ASSERT_EQ(p.stem(), TC.stem);
205 ASSERT_NEQ(p.has_stem(), TC.stem.empty());
206
207 ASSERT_EQ(p.extension(), TC.extension);
208 ASSERT_NEQ(p.has_extension(), TC.extension.empty());
209 }
210 }
211
main()212 int main()
213 {
214 decompPathTest();
215 decompFilenameTest();
216 }
217