]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/27_io/filesystem/iterators/directory_iterator.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 27_io / filesystem / iterators / directory_iterator.cc
1 // Copyright (C) 2015-2023 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-do run { target c++17 } }
19 // { dg-require-filesystem-ts "" }
20
21 #include <filesystem>
22 #include <testsuite_hooks.h>
23 #include <testsuite_fs.h>
24
25 namespace fs = std::filesystem;
26
27 void
28 test01()
29 {
30 const std::error_code bad_ec = make_error_code(std::errc::invalid_argument);
31 std::error_code ec;
32
33 // Test non-existent path.
34 const auto p = __gnu_test::nonexistent_path();
35 fs::directory_iterator iter(p, ec);
36 VERIFY( ec );
37 VERIFY( iter == end(iter) );
38
39 // Test empty directory.
40 create_directory(p, fs::current_path(), ec);
41 VERIFY( !ec );
42 ec = bad_ec;
43 iter = fs::directory_iterator(p, ec);
44 VERIFY( !ec );
45 VERIFY( iter == end(iter) );
46
47 // Test non-empty directory.
48 ec = bad_ec;
49 create_directory(p / "x", ec);
50 VERIFY( !ec );
51 ec = bad_ec;
52 iter = fs::directory_iterator(p, ec);
53 VERIFY( !ec );
54 VERIFY( iter != fs::directory_iterator() );
55 VERIFY( iter->path() == p/"x" );
56 ++iter;
57 VERIFY( iter == end(iter) );
58
59 if (__gnu_test::permissions_are_testable())
60 {
61 // Test inaccessible directory.
62 ec = bad_ec;
63 permissions(p, fs::perms::none, ec);
64 VERIFY( !ec );
65 iter = fs::directory_iterator(p, ec);
66 VERIFY( ec );
67 VERIFY( iter == end(iter) );
68
69 // Test inaccessible directory, skipping permission denied.
70 const auto opts = fs::directory_options::skip_permission_denied;
71 ec = bad_ec;
72 iter = fs::directory_iterator(p, opts, ec);
73 VERIFY( !ec );
74 VERIFY( iter == end(iter) );
75
76 permissions(p, fs::perms::owner_all, ec);
77 }
78
79 remove_all(p, ec);
80 }
81
82 void
83 test02()
84 {
85 const std::error_code bad_ec = make_error_code(std::errc::invalid_argument);
86 std::error_code ec;
87 const auto p = __gnu_test::nonexistent_path();
88 ec = bad_ec;
89 create_directory(p, fs::current_path(), ec);
90 create_directory(p / "x", ec);
91 VERIFY( !ec );
92
93 // Test post-increment (libstdc++/71005)
94 ec = bad_ec;
95 auto iter = fs::directory_iterator(p, ec);
96 VERIFY( !ec );
97 VERIFY( iter != end(iter) );
98 const auto entry1 = *iter;
99 const auto entry2 = *iter++;
100 VERIFY( entry1 == entry2 );
101 VERIFY( entry1.path() == p/"x" );
102 VERIFY( iter == end(iter) );
103
104 // Test post-increment (libstdc++/89986)
105 ec = bad_ec;
106 iter = fs::directory_iterator(p, ec);
107 VERIFY( !ec );
108 VERIFY( iter != end(iter) );
109 iter.increment(ec);
110 VERIFY( !ec );
111 VERIFY( iter == end(iter) );
112
113 remove_all(p, ec);
114 }
115
116 void
117 test03()
118 {
119 std::error_code ec = make_error_code(std::errc::invalid_argument);
120 const auto p = __gnu_test::nonexistent_path();
121 create_directories(p / "longer_than_small_string_buffer", ec);
122 VERIFY( !ec );
123
124 // Test for no reallocation on each dereference (this is a GNU extension)
125 auto iter = fs::directory_iterator(p, ec);
126 const auto* s1 = iter->path().c_str();
127 const auto* s2 = iter->path().c_str();
128 VERIFY( s1 == s2 );
129
130 remove_all(p, ec);
131 }
132
133 void
134 test04()
135 {
136 const fs::directory_iterator it;
137 VERIFY( it == fs::directory_iterator() );
138 }
139
140 void
141 test05()
142 {
143 auto p = __gnu_test::nonexistent_path();
144 create_directory(p);
145 create_directory(p / "x");
146 fs::directory_iterator it(p), endit;
147 VERIFY( begin(it) == it );
148 static_assert( noexcept(begin(it)), "begin is noexcept" );
149 VERIFY( end(it) == endit );
150 static_assert( noexcept(end(it)), "end is noexcept" );
151
152 std::error_code ec;
153 remove_all(p, ec);
154 }
155
156 int
157 main()
158 {
159 test01();
160 test02();
161 test03();
162 test04();
163 test05();
164 }