| 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" |
| 59 |
namespace fs = std::filesystem; |
| 60 |
|
| 61 |
struct ComparePathExact { |
| 62 |
bool operator()(fs::path const& LHS, std::string const& RHS) const { |
| 63 |
return LHS.string() == RHS; |
| 64 |
} |
| 65 |
}; |
| 66 |
|
| 67 |
struct 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 |
|
| 79 |
const PathDecomposeTestcase PathTestCases[] = |
| 80 |
{ |
| 81 |
{"", {}, "", "", "", "", "", ""} |
| 82 |
, {".", {"."}, "", "", "", ".", "", "."} |
| 83 |
, {"..", {".."}, "", "", "", "..", "", ".."} |
| 84 |
, {"foo", {"foo"}, "", "", "", "foo", "", "foo"} |
| 85 |
, {"/", {"/"}, "/", "", "/", "", "/", ""} |
| 86 |
, {"/foo", {"/", "foo"}, "/", "", "/", "foo", "/", "foo"} |
| 87 |
, {"foo/", {"foo", ""}, "", "", "", "foo/", "foo", ""} |
| 88 |
, {"/foo/", {"/", "foo", ""}, "/", "", "/", "foo/", "/foo", ""} |
| 89 |
, {"foo/bar", {"foo","bar"}, "", "", "", "foo/bar", "foo", "bar"} |
| 90 |
, {"/foo//bar", {"/","foo","bar"}, "/", "", "/", "foo/bar", "/foo", "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 |
, {"//net", {"/", "net"}, "/", "", "/", "net", "/", "net"} |
| 97 |
, {"//net/", {"/", "net", ""}, "/", "", "/", "net/", "//net", ""} |
| 98 |
, {"//net/foo", {"/", "net", "foo"}, "/", "", "/", "net/foo", "/net", "foo"} |
| 99 |
#endif |
| 100 |
, {"///foo///", {"/", "foo", ""}, "/", "", "/", "foo///", "///foo", ""} |
| 101 |
, {"///foo///bar", {"/", "foo", "bar"}, "/", "", "/", "foo///bar", "///foo", "bar"} |
| 102 |
, {"/.", {"/", "."}, "/", "", "/", ".", "/", "."} |
| 103 |
, {"./", {".", ""}, "", "", "", "./", ".", ""} |
| 104 |
, {"/..", {"/", ".."}, "/", "", "/", "..", "/", ".."} |
| 105 |
, {"../", {"..", ""}, "", "", "", "../", "..", ""} |
| 106 |
, {"foo/.", {"foo", "."}, "", "", "", "foo/.", "foo", "."} |
| 107 |
, {"foo/..", {"foo", ".."}, "", "", "", "foo/..", "foo", ".."} |
| 108 |
, {"foo/./", {"foo", ".", ""}, "", "", "", "foo/./", "foo/.", ""} |
| 109 |
, {"foo/./bar", {"foo", ".", "bar"}, "", "", "", "foo/./bar", "foo/.", "bar"} |
| 110 |
, {"foo/../", {"foo", "..", ""}, "", "", "", "foo/../", "foo/..", ""} |
| 111 |
, {"foo/../bar", {"foo", "..", "bar"}, "", "", "", "foo/../bar", "foo/..", "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 |
, {"c:", {"c:"}, "", "", "", "c:", "", "c:"} |
| 122 |
, {"c:/", {"c:", ""}, "", "", "", "c:/", "c:", ""} |
| 123 |
, {"c:foo", {"c:foo"}, "", "", "", "c:foo", "", "c:foo"} |
| 124 |
, {"c:/foo", {"c:", "foo"}, "", "", "", "c:/foo", "c:", "foo"} |
| 125 |
, {"c:foo/", {"c:foo", ""}, "", "", "", "c:foo/", "c:foo", ""} |
| 126 |
, {"c:/foo/", {"c:", "foo", ""}, "", "", "", "c:/foo/", "c:/foo", ""} |
| 127 |
, {"c:/foo/bar", {"c:", "foo", "bar"}, "", "", "", "c:/foo/bar", "c:/foo", "bar"} |
| 128 |
#endif |
| 129 |
, {"prn:", {"prn:"}, "", "", "", "prn:", "", "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 |
, {"c:\\", {"c:\\"}, "", "", "", "c:\\", "", "c:\\"} |
| 139 |
, {"c:\\foo", {"c:\\foo"}, "", "", "", "c:\\foo", "", "c:\\foo"} |
| 140 |
, {"c:foo\\", {"c:foo\\"}, "", "", "", "c:foo\\", "", "c:foo\\"} |
| 141 |
, {"c:\\foo\\", {"c:\\foo\\"}, "", "", "", "c:\\foo\\", "", "c:\\foo\\"} |
| 142 |
, {"c:\\foo/", {"c:\\foo", ""}, "", "", "", "c:\\foo/", "c:\\foo", ""} |
| 143 |
, {"c:/foo\\bar", {"c:", "foo\\bar"}, "", "", "", "c:/foo\\bar", "c:", "foo\\bar"} |
| 144 |
#endif |
| 145 |
, {"//", {"/"}, "/", "", "/", "", "/", ""} |
| 146 |
}; |
| 147 |
|
| 148 |
void 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(*--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 |
|
| 215 |
struct FilenameDecompTestcase |
| 216 |
{ |
| 217 |
std::string raw; |
| 218 |
std::string filename; |
| 219 |
std::string stem; |
| 220 |
std::string extension; |
| 221 |
}; |
| 222 |
|
| 223 |
const FilenameDecompTestcase FilenameTestCases[] = |
| 224 |
{ |
| 225 |
{"", "", "", ""} |
| 226 |
, {".", ".", ".", ""} |
| 227 |
, {"..", "..", "..", ""} |
| 228 |
, {"/", "", "", ""} |
| 229 |
, {"foo", "foo", "foo", ""} |
| 230 |
, {"/foo/bar.txt", "bar.txt", "bar", ".txt"} |
| 231 |
, {"foo..txt", "foo..txt", "foo.", ".txt"} |
| 232 |
, {".profile", ".profile", ".profile", ""} |
| 233 |
, {".profile.txt", ".profile.txt", ".profile", ".txt"} |
| 234 |
}; |
| 235 |
|
| 236 |
|
| 237 |
void 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 |
|
| 256 |
int main(int, char**) |
| 257 |
{ |
| 258 |
decompPathTest(); |
| 259 |
decompFilenameTest(); |
| 260 |
|
| 261 |
return 0; |
| 262 |
} |