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

source code of libcxx/test/std/input.output/filesystems/class.path/path.member/path.decompose/path.decompose.pass.cpp