PR libstdc++/78870 support std::filesystem on Windows
[official-gcc.git] / libstdc++-v3 / testsuite / 27_io / filesystem / path / itr / traversal.cc
blob4852c03c78eb0c79be1e10f7a46c8cc8cda844c2
1 // { dg-options "-std=gnu++17 -lstdc++fs" }
2 // { dg-do run { target c++17 } }
3 // { dg-require-filesystem-ts "" }
5 // Copyright (C) 2014-2018 Free Software Foundation, Inc.
6 //
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
11 // any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License along
19 // with this library; see the file COPYING3. If not see
20 // <http://www.gnu.org/licenses/>.
22 // C++17 30.10.7.5 path iterators [fs.path.itr]
24 #include <filesystem>
25 #include <vector>
26 #include <algorithm>
27 #include <testsuite_hooks.h>
28 #include <testsuite_fs.h>
30 using std::filesystem::path;
32 void
33 test01()
35 path p;
36 VERIFY( p.begin() == p.end() );
38 std::vector<path> v, v2;
40 p = "/";
41 v.assign(p.begin(), p.end());
42 v2 = { "/" };
43 VERIFY( v == v2 );
45 p = "filename";
46 v.assign(p.begin(), p.end());
47 v2 = { "filename" };
48 VERIFY( v == v2 );
50 p = "dir/.";
51 v.assign(p.begin(), p.end());
52 v2 = { "dir", "." };
53 VERIFY( v == v2 );
55 p = "dir/";
56 v.assign(p.begin(), p.end());
57 v2 = { "dir", "" };
58 VERIFY( v == v2 );
60 p = "//rootname/dir/.";
61 v.assign(p.begin(), p.end());
62 #ifdef __CYGWIN__
63 v2 = { "//rootname", "/", "dir", "." };
64 #else
65 v2 = { "/", "rootname", "dir", "." };
66 #endif
67 VERIFY( v == v2 );
69 p = "//rootname/dir/";
70 v.assign(p.begin(), p.end());
71 #ifdef __CYGWIN__
72 v2 = { "//rootname", "/", "dir", "" };
73 #else
74 v2 = { "/", "rootname", "dir", "" };
75 #endif
76 VERIFY( v == v2 );
78 p = "//rootname/dir/filename";
79 v.assign(p.begin(), p.end());
80 #ifdef __CYGWIN__
81 v2 = { "//rootname", "/", "dir", "filename" };
82 #else
83 v2 = { "/", "rootname", "dir", "filename" };
84 #endif
85 VERIFY( v == v2 );
87 p = "c:relative/path";
88 v.assign(p.begin(), p.end());
89 #if defined(__MINGW32__) || defined(__MINGW64__)
90 v2 = { "c:", "relative", "path" };
91 #else
92 v2 = { "c:relative", "path" };
93 #endif
94 VERIFY( v == v2 );
96 p = "c:/absolute/path";
97 v.assign(p.begin(), p.end());
98 #if defined(__MINGW32__) || defined(__MINGW64__)
99 v2 = { "c:", "/", "absolute", "path" };
100 #else
101 v2 = { "c:", "absolute", "path" };
102 #endif
103 VERIFY( v == v2 );
106 void
107 test02()
109 using reverse_iterator = std::reverse_iterator<path::iterator>;
110 std::vector<path> fwd, rev;
112 for (const path& p : __gnu_test::test_paths)
114 const auto begin = p.begin(), end = p.end();
115 fwd.assign(begin, end);
116 rev.assign(reverse_iterator(end), reverse_iterator(begin));
117 VERIFY( fwd.size() == rev.size() );
118 VERIFY( std::equal(fwd.begin(), fwd.end(), rev.rbegin()) );
122 void
123 test03()
125 path paths[] = { "single", "multiple/elements", "trailing/slash/", "/." };
126 for (const path& p : paths)
127 for (auto iter = p.begin(); iter != p.end(); ++iter)
129 auto iter2 = iter;
130 ++iter;
131 iter2++;
132 VERIFY( iter2 == iter );
133 --iter;
134 iter2--;
135 VERIFY( iter2 == iter );
140 main()
142 test01();
143 test02();
144 test03();