]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/27_io/filesystem/iterators/recursive_directory_iterator.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 27_io / filesystem / iterators / recursive_directory_iterator.cc
1 // Copyright (C) 2015-2021 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17
18 // { dg-options "-std=gnu++17" }
19 // { dg-do run { target c++17 } }
20 // { dg-require-filesystem-ts "" }
21
22 #include <filesystem>
23 #include <testsuite_hooks.h>
24 #include <testsuite_fs.h>
25
26 namespace fs = std::filesystem;
27
28 void
29 test01()
30 {
31 const std::error_code bad_ec = make_error_code(std::errc::invalid_argument);
32 std::error_code ec;
33
34 // Test non-existent path.
35 const auto p = __gnu_test::nonexistent_path();
36 fs::recursive_directory_iterator iter(p, ec);
37 VERIFY( ec );
38 VERIFY( iter == end(iter) );
39
40 // Test empty directory.
41 ec = bad_ec;
42 create_directory(p, fs::current_path(), ec);
43 VERIFY( !ec );
44 ec = bad_ec;
45 iter = fs::recursive_directory_iterator(p, ec);
46 VERIFY( !ec );
47 VERIFY( iter == end(iter) );
48
49 // Test non-empty directory.
50 ec = bad_ec;
51 create_directories(p / "d1/d2", ec);
52 VERIFY( !ec );
53 ec = bad_ec;
54 iter = fs::recursive_directory_iterator(p, ec);
55 VERIFY( !ec );
56 VERIFY( iter != end(iter) );
57 VERIFY( iter->path() == p/"d1" );
58 ++iter;
59 VERIFY( iter->path() == p/"d1/d2" );
60 ++iter;
61 VERIFY( iter == end(iter) );
62
63 #if ! (defined (__MINGW32__) || defined(__MINGW64__))
64 // Test inaccessible directory.
65 ec = bad_ec;
66 permissions(p, fs::perms::none, ec);
67 VERIFY( !ec );
68 iter = fs::recursive_directory_iterator(p, ec);
69 VERIFY( ec );
70 VERIFY( iter == end(iter) );
71
72 // Test inaccessible directory, skipping permission denied.
73 const auto opts = fs::directory_options::skip_permission_denied;
74 iter = fs::recursive_directory_iterator(p, opts, ec);
75 VERIFY( !ec );
76 VERIFY( iter == end(iter) );
77
78 // Test inaccessible sub-directory.
79 ec = bad_ec;
80 permissions(p, fs::perms::owner_all, ec);
81 VERIFY( !ec );
82 ec = bad_ec;
83 permissions(p/"d1/d2", fs::perms::none, ec);
84 VERIFY( !ec );
85 ec = bad_ec;
86 iter = fs::recursive_directory_iterator(p, ec);
87 VERIFY( !ec );
88 VERIFY( iter != end(iter) );
89 VERIFY( iter->path() == p/"d1" );
90 ++iter; // should recurse into d1
91 VERIFY( iter != end(iter) );
92 VERIFY( iter->path() == p/"d1/d2" );
93 iter.increment(ec); // should fail to recurse into p/d1/d2
94 VERIFY( ec );
95 VERIFY( iter == end(iter) );
96
97 // Test inaccessible sub-directory, skipping permission denied.
98 ec = bad_ec;
99 iter = fs::recursive_directory_iterator(p, opts, ec);
100 VERIFY( !ec );
101 VERIFY( iter != end(iter) );
102 VERIFY( iter->path() == p/"d1" );
103 ++iter; // should recurse into d1
104 VERIFY( iter != end(iter) );
105 VERIFY( iter->path() == p/"d1/d2" );
106 ec = bad_ec;
107 iter.increment(ec); // should fail to recurse into p/d1/d2, so skip it
108 VERIFY( !ec );
109 VERIFY( iter == end(iter) );
110 #endif
111
112 permissions(p/"d1/d2", fs::perms::owner_all, ec);
113 remove_all(p, ec);
114 }
115
116 void
117 test02()
118 {
119 const std::error_code bad_ec = make_error_code(std::errc::invalid_argument);
120 std::error_code ec;
121 const auto p = __gnu_test::nonexistent_path();
122 ec = bad_ec;
123 create_directories(p / "d1/d2", ec);
124 VERIFY( !ec );
125
126 // Test post-increment (libstdc++/71005)
127 ec = bad_ec;
128 auto iter = fs::recursive_directory_iterator(p, ec);
129 VERIFY( !ec );
130 VERIFY( iter != end(iter) );
131 const auto entry1 = *iter;
132 const auto entry2 = *iter++;
133 VERIFY( entry1 == entry2 );
134 VERIFY( entry1.path() == p/"d1" );
135 const auto entry3 = *iter;
136 const auto entry4 = *iter++;
137 VERIFY( entry3 == entry4 );
138 VERIFY( entry3.path() == p/"d1/d2" );
139 VERIFY( iter == end(iter) );
140
141 remove_all(p, ec);
142 }
143
144 void
145 test03()
146 {
147 const std::error_code bad_ec = make_error_code(std::errc::invalid_argument);
148 std::error_code ec;
149 const auto p = __gnu_test::nonexistent_path();
150 ec = bad_ec;
151 create_directories(p / "longer_than_small_string_buffer", ec);
152 VERIFY( !ec );
153
154 // Test for no reallocation on each dereference (this is a GNU extension)
155 auto iter = fs::recursive_directory_iterator(p, ec);
156 const auto* s1 = iter->path().c_str();
157 const auto* s2 = iter->path().c_str();
158 VERIFY( s1 == s2 );
159
160 remove_all(p, ec);
161 }
162
163 void
164 test04()
165 {
166 // libstdc++/71004
167 const fs::recursive_directory_iterator it;
168 VERIFY( it == end(it) );
169 }
170
171 void
172 test05()
173 {
174 auto p = __gnu_test::nonexistent_path();
175 create_directory(p);
176 create_directory(p / "x");
177 fs::recursive_directory_iterator it(p), endit;
178 VERIFY( begin(it) == it );
179 static_assert( noexcept(begin(it)), "begin is noexcept" );
180 VERIFY( end(it) == endit );
181 static_assert( noexcept(end(it)), "end is noexcept" );
182
183 std::error_code ec;
184 remove_all(p, ec);
185 }
186
187 int
188 main()
189 {
190 test01();
191 test02();
192 test03();
193 test04();
194 test05();
195 }