Minor improvements to Filesystem tests
[official-gcc.git] / libstdc++-v3 / testsuite / 27_io / filesystem / iterators / directory_iterator.cc
blob9cdbd7aafa045cb6497ee977d262ab9e72228e8a
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::directory_iterator iter(p, ec);
37 VERIFY( ec );
38 VERIFY( iter == end(iter) );
40 // Test empty directory.
41 create_directory(p, fs::current_path(), ec);
42 VERIFY( !ec );
43 ec = bad_ec;
44 iter = fs::directory_iterator(p, ec);
45 VERIFY( !ec );
46 VERIFY( iter == end(iter) );
48 // Test non-empty directory.
49 ec = bad_ec;
50 create_directory_symlink(p, p / "l", ec);
51 VERIFY( !ec );
52 ec = bad_ec;
53 iter = fs::directory_iterator(p, ec);
54 VERIFY( !ec );
55 VERIFY( iter != fs::directory_iterator() );
56 VERIFY( iter->path() == p/"l" );
57 ++iter;
58 VERIFY( iter == end(iter) );
60 // Test inaccessible directory.
61 ec = bad_ec;
62 permissions(p, fs::perms::none, ec);
63 VERIFY( !ec );
64 iter = fs::directory_iterator(p, ec);
65 VERIFY( ec );
66 VERIFY( iter == end(iter) );
68 // Test inaccessible directory, skipping permission denied.
69 const auto opts = fs::directory_options::skip_permission_denied;
70 ec = bad_ec;
71 iter = fs::directory_iterator(p, opts, ec);
72 VERIFY( !ec );
73 VERIFY( iter == end(iter) );
75 permissions(p, fs::perms::owner_all, ec);
76 remove_all(p, ec);
79 void
80 test02()
82 const std::error_code bad_ec = make_error_code(std::errc::invalid_argument);
83 std::error_code ec;
84 const auto p = __gnu_test::nonexistent_path();
85 ec = bad_ec;
86 create_directory(p, fs::current_path(), ec);
87 create_directory_symlink(p, p / "l", ec);
88 VERIFY( !ec );
90 // Test post-increment (libstdc++/71005)
91 ec = bad_ec;
92 auto iter = fs::directory_iterator(p, ec);
93 VERIFY( !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) );
101 remove_all(p, ec);
104 void
105 test03()
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);
110 VERIFY( !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();
116 VERIFY( s1 == s2 );
118 remove_all(p, ec);
121 void
122 test04()
124 const fs::directory_iterator it;
125 VERIFY( it == fs::directory_iterator() );
128 void
129 test05()
131 auto p = __gnu_test::nonexistent_path();
132 create_directory(p);
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" );
142 main()
144 test01();
145 test02();
146 test03();
147 test04();
148 test05();