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