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