| 1 |
// { dg-do run { target c++17 } } |
| 2 |
|
| 3 |
// Copyright (C) 2014-2025 Free Software Foundation, Inc. |
| 4 |
// |
| 5 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 6 |
// software; you can redistribute it and/or modify it under the |
| 7 |
// terms of the GNU General Public License as published by the |
| 8 |
// Free Software Foundation; either version 3, or (at your option) |
| 9 |
// any later version. |
| 10 |
|
| 11 |
// This library is distributed in the hope that it will be useful, |
| 12 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
// GNU General Public License for more details. |
| 15 |
|
| 16 |
// You should have received a copy of the GNU General Public License along |
| 17 |
// with this library; see the file COPYING3. If not see |
| 18 |
// <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
// C++17 30.10.7.5 path iterators [fs.path.itr] |
| 21 |
|
| 22 |
#include <filesystem> |
| 23 |
#include <vector> |
| 24 |
#include <algorithm> |
| 25 |
#include <testsuite_hooks.h> |
| 26 |
#include <testsuite_fs.h> |
| 27 |
|
| 28 |
using std::filesystem::path; |
| 29 |
|
| 30 |
void |
| 31 |
test01() |
| 32 |
{ |
| 33 |
path p; |
| 34 |
VERIFY( p.begin() == p.end() ); |
| 35 |
|
| 36 |
std::vector<path> v, v2; |
| 37 |
|
| 38 |
p = "/"; |
| 39 |
v.assign(p.begin(), p.end()); |
| 40 |
v2 = { "/" }; |
| 41 |
VERIFY( v == v2 ); |
| 42 |
|
| 43 |
p = "filename"; |
| 44 |
v.assign(p.begin(), p.end()); |
| 45 |
v2 = { "filename" }; |
| 46 |
VERIFY( v == v2 ); |
| 47 |
|
| 48 |
p = "dir/."; |
| 49 |
v.assign(p.begin(), p.end()); |
| 50 |
v2 = { "dir", "." }; |
| 51 |
VERIFY( v == v2 ); |
| 52 |
|
| 53 |
p = "dir/"; |
| 54 |
v.assign(p.begin(), p.end()); |
| 55 |
v2 = { "dir", "" }; |
| 56 |
VERIFY( v == v2 ); |
| 57 |
|
| 58 |
p = "//rootname/dir/."; |
| 59 |
v.assign(p.begin(), p.end()); |
| 60 |
#ifdef __CYGWIN__ |
| 61 |
v2 = { "//rootname", "/", "dir", "." }; |
| 62 |
#else |
| 63 |
v2 = { "/", "rootname", "dir", "." }; |
| 64 |
#endif |
| 65 |
VERIFY( v == v2 ); |
| 66 |
|
| 67 |
p = "//rootname/dir/"; |
| 68 |
v.assign(p.begin(), p.end()); |
| 69 |
#ifdef __CYGWIN__ |
| 70 |
v2 = { "//rootname", "/", "dir", "" }; |
| 71 |
#else |
| 72 |
v2 = { "/", "rootname", "dir", "" }; |
| 73 |
#endif |
| 74 |
VERIFY( v == v2 ); |
| 75 |
|
| 76 |
p = "//rootname/dir/filename"; |
| 77 |
v.assign(p.begin(), p.end()); |
| 78 |
#ifdef __CYGWIN__ |
| 79 |
v2 = { "//rootname", "/", "dir", "filename" }; |
| 80 |
#else |
| 81 |
v2 = { "/", "rootname", "dir", "filename" }; |
| 82 |
#endif |
| 83 |
VERIFY( v == v2 ); |
| 84 |
|
| 85 |
p = "c:relative/path"; |
| 86 |
v.assign(p.begin(), p.end()); |
| 87 |
#if defined(__MINGW32__) || defined(__MINGW64__) |
| 88 |
v2 = { "c:", "relative", "path" }; |
| 89 |
#else |
| 90 |
v2 = { "c:relative", "path" }; |
| 91 |
#endif |
| 92 |
VERIFY( v == v2 ); |
| 93 |
|
| 94 |
p = "c:/absolute/path"; |
| 95 |
v.assign(p.begin(), p.end()); |
| 96 |
#if defined(__MINGW32__) || defined(__MINGW64__) |
| 97 |
v2 = { "c:", "/", "absolute", "path" }; |
| 98 |
#else |
| 99 |
v2 = { "c:", "absolute", "path" }; |
| 100 |
#endif |
| 101 |
VERIFY( v == v2 ); |
| 102 |
} |
| 103 |
|
| 104 |
void |
| 105 |
test02() |
| 106 |
{ |
| 107 |
using reverse_iterator = std::reverse_iterator<path::iterator>; |
| 108 |
std::vector<path> fwd, rev; |
| 109 |
|
| 110 |
for (const path p : __gnu_test::test_paths) |
| 111 |
{ |
| 112 |
const auto begin = p.begin(), end = p.end(); |
| 113 |
fwd.assign(begin, end); |
| 114 |
rev.assign(reverse_iterator(end), reverse_iterator(begin)); |
| 115 |
VERIFY( fwd.size() == rev.size() ); |
| 116 |
VERIFY( std::equal(fwd.begin(), fwd.end(), rev.rbegin()) ); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
void |
| 121 |
test03() |
| 122 |
{ |
| 123 |
path paths[] = { "single", "multiple/elements", "trailing/slash/", "/." }; |
| 124 |
for (const path& p : paths) |
| 125 |
for (auto iter = p.begin(); iter != p.end(); ++iter) |
| 126 |
{ |
| 127 |
auto iter2 = iter; |
| 128 |
++iter; |
| 129 |
iter2++; |
| 130 |
VERIFY( iter2 == iter ); |
| 131 |
--iter; |
| 132 |
iter2--; |
| 133 |
VERIFY( iter2 == iter ); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
void |
| 138 |
test04() |
| 139 |
{ |
| 140 |
std::filesystem::path p = "/a/b/c/d/e/f/g"; |
| 141 |
VERIFY( std::distance(p.begin(), p.end()) == 8); |
| 142 |
auto it = p.begin(); |
| 143 |
std::advance(it, 1); |
| 144 |
VERIFY( std::distance(p.begin(), it) == 1 ); |
| 145 |
VERIFY( it->string() == "a" ); |
| 146 |
std::advance(it, 3); |
| 147 |
VERIFY( std::distance(p.begin(), it) == 4 ); |
| 148 |
VERIFY( it->string() == "d" ); |
| 149 |
std::advance(it, -2); |
| 150 |
VERIFY( std::distance(p.begin(), it) == 2 ); |
| 151 |
VERIFY( it->string() == "b" ); |
| 152 |
} |
| 153 |
|
| 154 |
int |
| 155 |
main() |
| 156 |
{ |
| 157 |
test01(); |
| 158 |
test02(); |
| 159 |
test03(); |
| 160 |
test04(); |
| 161 |
} |