262 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++11, c++1410 11// <filesystem>12 13// class path14 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>47#include <algorithm>48#include <cassert>49#include <cstddef>50#include <iterator>51#include <string>52#include <type_traits>53#include <vector>54 55#include "test_macros.h"56#include "test_iterators.h"57#include "count_new.h"58namespace fs = std::filesystem;59 60struct ComparePathExact {61 bool operator()(fs::path const& LHS, std::string const& RHS) const {62 return LHS.string() == RHS;63 }64};65 66struct PathDecomposeTestcase67{68 std::string raw;69 std::vector<std::string> elements;70 std::string root_path;71 std::string root_name;72 std::string root_directory;73 std::string relative_path;74 std::string parent_path;75 std::string filename;76};77 78const PathDecomposeTestcase PathTestCases[] =79 {80 {"", {}, "", "", "", "", "", ""}81 , {".", {"."}, "", "", "", ".", "", "."}82 , {"..", {".."}, "", "", "", "..", "", ".."}83 , {"foo", {"foo"}, "", "", "", "foo", "", "foo"}84 , {"/", {"/"}, "/", "", "/", "", "/", ""}85 , {"/foo", {"/", "foo"}, "/", "", "/", "foo", "/", "foo"}86 , {"foo/", {"foo", ""}, "", "", "", "foo/", "foo", ""}87 , {"/foo/", {"/", "foo", ""}, "/", "", "/", "foo/", "/foo", ""}88 , {"foo/bar", {"foo","bar"}, "", "", "", "foo/bar", "foo", "bar"}89 , {"/foo//bar", {"/","foo","bar"}, "/", "", "/", "foo/bar", "/foo", "bar"}90#ifdef _WIN3291 , {"//net", {"//net"}, "//net", "//net", "", "", "//net", ""}92 , {"//net/", {"//net", "/"}, "//net/", "//net", "/", "", "//net/", ""}93 , {"//net/foo", {"//net", "/", "foo"}, "//net/", "//net", "/", "foo", "//net/", "foo"}94#else95 , {"//net", {"/", "net"}, "/", "", "/", "net", "/", "net"}96 , {"//net/", {"/", "net", ""}, "/", "", "/", "net/", "//net", ""}97 , {"//net/foo", {"/", "net", "foo"}, "/", "", "/", "net/foo", "/net", "foo"}98#endif99 , {"///foo///", {"/", "foo", ""}, "/", "", "/", "foo///", "///foo", ""}100 , {"///foo///bar", {"/", "foo", "bar"}, "/", "", "/", "foo///bar", "///foo", "bar"}101 , {"/.", {"/", "."}, "/", "", "/", ".", "/", "."}102 , {"./", {".", ""}, "", "", "", "./", ".", ""}103 , {"/..", {"/", ".."}, "/", "", "/", "..", "/", ".."}104 , {"../", {"..", ""}, "", "", "", "../", "..", ""}105 , {"foo/.", {"foo", "."}, "", "", "", "foo/.", "foo", "."}106 , {"foo/..", {"foo", ".."}, "", "", "", "foo/..", "foo", ".."}107 , {"foo/./", {"foo", ".", ""}, "", "", "", "foo/./", "foo/.", ""}108 , {"foo/./bar", {"foo", ".", "bar"}, "", "", "", "foo/./bar", "foo/.", "bar"}109 , {"foo/../", {"foo", "..", ""}, "", "", "", "foo/../", "foo/..", ""}110 , {"foo/../bar", {"foo", "..", "bar"}, "", "", "", "foo/../bar", "foo/..", "bar"}111#ifdef _WIN32112 , {"c:", {"c:"}, "c:", "c:", "", "", "c:", ""}113 , {"c:/", {"c:", "/"}, "c:/", "c:", "/", "", "c:/", ""}114 , {"c:foo", {"c:", "foo"}, "c:", "c:", "", "foo", "c:", "foo"}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/bar", {"c:", "/", "foo", "bar"}, "c:/", "c:", "/", "foo/bar", "c:/foo", "bar"}119#else120 , {"c:", {"c:"}, "", "", "", "c:", "", "c:"}121 , {"c:/", {"c:", ""}, "", "", "", "c:/", "c:", ""}122 , {"c:foo", {"c:foo"}, "", "", "", "c:foo", "", "c:foo"}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/bar", {"c:", "foo", "bar"}, "", "", "", "c:/foo/bar", "c:/foo", "bar"}127#endif128 , {"prn:", {"prn:"}, "", "", "", "prn:", "", "prn:"}129#ifdef _WIN32130 , {"c:\\", {"c:", "\\"}, "c:\\", "c:", "\\", "", "c:\\", ""}131 , {"c:\\foo", {"c:", "\\", "foo"}, "c:\\", "c:", "\\", "foo", "c:\\", "foo"}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\\bar", {"c:", "/", "foo", "bar"}, "c:\\", "c:", "\\", "foo\\bar", "c:/foo", "bar"}136#else137 , {"c:\\", {"c:\\"}, "", "", "", "c:\\", "", "c:\\"}138 , {"c:\\foo", {"c:\\foo"}, "", "", "", "c:\\foo", "", "c:\\foo"}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\\bar", {"c:", "foo\\bar"}, "", "", "", "c:/foo\\bar", "c:", "foo\\bar"}143#endif144 , {"//", {"/"}, "/", "", "/", "", "/", ""}145 };146 147void decompPathTest()148{149 using namespace fs;150 for (auto const & TC : PathTestCases) {151 fs::path p(TC.raw);152 assert(p == TC.raw);153 154 assert(p.root_path() == TC.root_path);155 assert(p.has_root_path() != TC.root_path.empty());156 157#ifndef _WIN32158 assert(p.root_name().native().empty());159#endif160 assert(p.root_name() == TC.root_name);161 assert(p.has_root_name() != TC.root_name.empty());162 163 assert(p.root_directory() == TC.root_directory);164 assert(p.has_root_directory() != TC.root_directory.empty());165 166 assert(p.relative_path() == TC.relative_path);167 assert(p.has_relative_path() != TC.relative_path.empty());168 169 assert(p.parent_path() == TC.parent_path);170 assert(p.has_parent_path() != TC.parent_path.empty());171 172 assert(p.filename() == TC.filename);173 assert(p.has_filename() != TC.filename.empty());174 175#ifdef _WIN32176 if (!p.has_root_name()) {177 assert(p.is_absolute() == false);178 } else {179 std::string root_name = p.root_name().string();180 assert(root_name.length() >= 2);181 if (root_name[1] == ':') {182 // Drive letter, absolute if has a root directory183 assert(p.is_absolute() == p.has_root_directory());184 } else {185 // Possibly a server path186 // Convert to one separator style, for simplicity187 std::replace(root_name.begin(), root_name.end(), '\\', '/');188 if (root_name[0] == '/' && root_name[1] == '/')189 assert(p.is_absolute() == true);190 else191 assert(p.is_absolute() == false);192 }193 }194#else195 assert(p.is_absolute() == p.has_root_directory());196#endif197 assert(p.is_relative() != p.is_absolute());198 if (p.empty())199 assert(p.is_relative());200 201 assert(static_cast<std::size_t>(std::distance(p.begin(), p.end())) == TC.elements.size());202 assert(std::equal(p.begin(), p.end(), TC.elements.begin(), ComparePathExact()));203 204 // check backwards205 std::vector<fs::path> Parts;206 for (auto it = p.end(); it != p.begin(); )207 Parts.push_back(*--it);208 assert(static_cast<std::size_t>(std::distance(Parts.begin(), Parts.end())) == TC.elements.size());209 assert(std::equal(Parts.begin(), Parts.end(), TC.elements.rbegin(), ComparePathExact()));210 }211}212 213 214struct FilenameDecompTestcase215{216 std::string raw;217 std::string filename;218 std::string stem;219 std::string extension;220};221 222const FilenameDecompTestcase FilenameTestCases[] =223{224 {"", "", "", ""}225 , {".", ".", ".", ""}226 , {"..", "..", "..", ""}227 , {"/", "", "", ""}228 , {"foo", "foo", "foo", ""}229 , {"/foo/bar.txt", "bar.txt", "bar", ".txt"}230 , {"foo..txt", "foo..txt", "foo.", ".txt"}231 , {".profile", ".profile", ".profile", ""}232 , {".profile.txt", ".profile.txt", ".profile", ".txt"}233};234 235 236void decompFilenameTest()237{238 using namespace fs;239 for (auto const & TC : FilenameTestCases) {240 fs::path p(TC.raw);241 assert(p == TC.raw);242 ASSERT_NOEXCEPT(p.empty());243 244 assert(p.filename() == TC.filename);245 assert(p.has_filename() != TC.filename.empty());246 247 assert(p.stem() == TC.stem);248 assert(p.has_stem() != TC.stem.empty());249 250 assert(p.extension() == TC.extension);251 assert(p.has_extension() != TC.extension.empty());252 }253}254 255int main(int, char**)256{257 decompPathTest();258 decompFilenameTest();259 260 return 0;261}262