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