Minor improvements to Filesystem tests
[official-gcc.git] / libstdc++-v3 / testsuite / 27_io / filesystem / iterators / recursive_directory_iterator.cc
blobd41a1506d3b9c0711e49612de52ea4e778ef389f
1 // Copyright (C) 2015-2017 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.
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.
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/>.
18 // { dg-options "-std=gnu++17 -lstdc++fs" }
19 // { dg-do run { target c++17 } }
20 // { dg-require-filesystem-ts "" }
22 #include <filesystem>
23 #include <testsuite_hooks.h>
24 #include <testsuite_fs.h>
26 namespace fs = std::filesystem;
28 void
29 test01()
31 const std::error_code bad_ec = make_error_code(std::errc::invalid_argument);
32 std::error_code ec;
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) );
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) );
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) );
63 // Test inaccessible directory.
64 ec = bad_ec;
65 permissions(p, fs::perms::none, ec);
66 VERIFY( !ec );
67 iter = fs::recursive_directory_iterator(p, ec);
68 VERIFY( ec );
69 VERIFY( iter == end(iter) );
71 // Test inaccessible directory, skipping permission denied.
72 const auto opts = fs::directory_options::skip_permission_denied;
73 iter = fs::recursive_directory_iterator(p, opts, ec);
74 VERIFY( !ec );
75 VERIFY( iter == end(iter) );
77 // Test inaccessible sub-directory.
78 ec = bad_ec;
79 permissions(p, fs::perms::owner_all, ec);
80 VERIFY( !ec );
81 ec = bad_ec;
82 permissions(p/"d1/d2", fs::perms::none, ec);
83 VERIFY( !ec );
84 ec = bad_ec;
85 iter = fs::recursive_directory_iterator(p, ec);
86 VERIFY( !ec );
87 VERIFY( iter != end(iter) );
88 VERIFY( iter->path() == p/"d1" );
89 ++iter; // should recurse into d1
90 VERIFY( iter != end(iter) );
91 VERIFY( iter->path() == p/"d1/d2" );
92 iter.increment(ec); // should fail to recurse into p/d1/d2
93 VERIFY( ec );
94 VERIFY( iter == end(iter) );
96 // Test inaccessible sub-directory, skipping permission denied.
97 ec = bad_ec;
98 iter = fs::recursive_directory_iterator(p, opts, ec);
99 VERIFY( !ec );
100 VERIFY( iter != end(iter) );
101 VERIFY( iter->path() == p/"d1" );
102 ++iter; // should recurse into d1
103 VERIFY( iter != end(iter) );
104 VERIFY( iter->path() == p/"d1/d2" );
105 ec = bad_ec;
106 iter.increment(ec); // should fail to recurse into p/d1/d2, so skip it
107 VERIFY( !ec );
108 VERIFY( iter == end(iter) );
110 permissions(p/"d1/d2", fs::perms::owner_all, ec);
111 remove_all(p, ec);
114 void
115 test02()
117 const std::error_code bad_ec = make_error_code(std::errc::invalid_argument);
118 std::error_code ec;
119 const auto p = __gnu_test::nonexistent_path();
120 ec = bad_ec;
121 create_directories(p / "d1/d2", ec);
122 VERIFY( !ec );
124 // Test post-increment (libstdc++/71005)
125 ec = bad_ec;
126 auto iter = fs::recursive_directory_iterator(p, ec);
127 VERIFY( !ec );
128 VERIFY( iter != end(iter) );
129 const auto entry1 = *iter;
130 const auto entry2 = *iter++;
131 VERIFY( entry1 == entry2 );
132 VERIFY( entry1.path() == p/"d1" );
133 const auto entry3 = *iter;
134 const auto entry4 = *iter++;
135 VERIFY( entry3 == entry4 );
136 VERIFY( entry3.path() == p/"d1/d2" );
137 VERIFY( iter == end(iter) );
139 remove_all(p, ec);
142 void
143 test03()
145 const std::error_code bad_ec = make_error_code(std::errc::invalid_argument);
146 std::error_code ec;
147 const auto p = __gnu_test::nonexistent_path();
148 ec = bad_ec;
149 create_directories(p / "longer_than_small_string_buffer", ec);
150 VERIFY( !ec );
152 // Test for no reallocation on each dereference (this is a GNU extension)
153 auto iter = fs::recursive_directory_iterator(p, ec);
154 const auto* s1 = iter->path().c_str();
155 const auto* s2 = iter->path().c_str();
156 VERIFY( s1 == s2 );
158 remove_all(p, ec);
161 void
162 test04()
164 // libstdc++/71004
165 const fs::recursive_directory_iterator it;
166 VERIFY( it == end(it) );
169 void
170 test05()
172 auto p = __gnu_test::nonexistent_path();
173 create_directory(p);
174 create_directory_symlink(p, p / "l");
175 fs::recursive_directory_iterator it(p), endit;
176 VERIFY( begin(it) == it );
177 static_assert( noexcept(begin(it)), "begin is noexcept" );
178 VERIFY( end(it) == endit );
179 static_assert( noexcept(end(it)), "end is noexcept" );
183 main()
185 test01();
186 test02();
187 test03();
188 test04();
189 test05();