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