1 // Copyright (C) 2015-2017 Free Software Foundation, Inc.
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)
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 "" }
23 #include <testsuite_hooks.h>
24 #include <testsuite_fs.h>
26 namespace fs
= std::filesystem
;
31 const std::error_code bad_ec
= make_error_code(std::errc::invalid_argument
);
34 // Test non-existent path.
35 const auto p
= __gnu_test::nonexistent_path();
36 fs::directory_iterator
iter(p
, ec
);
38 VERIFY( iter
== end(iter
) );
40 // Test empty directory.
41 create_directory(p
, fs::current_path(), ec
);
44 iter
= fs::directory_iterator(p
, ec
);
46 VERIFY( iter
== end(iter
) );
48 // Test non-empty directory.
50 create_directory_symlink(p
, p
/ "l", ec
);
53 iter
= fs::directory_iterator(p
, ec
);
55 VERIFY( iter
!= fs::directory_iterator() );
56 VERIFY( iter
->path() == p
/"l" );
58 VERIFY( iter
== end(iter
) );
60 // Test inaccessible directory.
62 permissions(p
, fs::perms::none
, ec
);
64 iter
= fs::directory_iterator(p
, ec
);
66 VERIFY( iter
== end(iter
) );
68 // Test inaccessible directory, skipping permission denied.
69 const auto opts
= fs::directory_options::skip_permission_denied
;
71 iter
= fs::directory_iterator(p
, opts
, ec
);
73 VERIFY( iter
== end(iter
) );
75 permissions(p
, fs::perms::owner_all
, ec
);
82 const std::error_code bad_ec
= make_error_code(std::errc::invalid_argument
);
84 const auto p
= __gnu_test::nonexistent_path();
86 create_directory(p
, fs::current_path(), ec
);
87 create_directory_symlink(p
, p
/ "l", ec
);
90 // Test post-increment (libstdc++/71005)
92 auto iter
= fs::directory_iterator(p
, ec
);
94 VERIFY( iter
!= end(iter
) );
95 const auto entry1
= *iter
;
96 const auto entry2
= *iter
++;
97 VERIFY( entry1
== entry2
);
98 VERIFY( entry1
.path() == p
/"l" );
99 VERIFY( iter
== end(iter
) );
107 std::error_code ec
= make_error_code(std::errc::invalid_argument
);
108 const auto p
= __gnu_test::nonexistent_path();
109 create_directories(p
/ "longer_than_small_string_buffer", ec
);
112 // Test for no reallocation on each dereference (this is a GNU extension)
113 auto iter
= fs::directory_iterator(p
, ec
);
114 const auto* s1
= iter
->path().c_str();
115 const auto* s2
= iter
->path().c_str();
124 const fs::directory_iterator it
;
125 VERIFY( it
== fs::directory_iterator() );
131 auto p
= __gnu_test::nonexistent_path();
133 create_directory_symlink(p
, p
/ "l");
134 fs::directory_iterator
it(p
), endit
;
135 VERIFY( begin(it
) == it
);
136 static_assert( noexcept(begin(it
)), "begin is noexcept" );
137 VERIFY( end(it
) == endit
);
138 static_assert( noexcept(end(it
)), "end is noexcept" );