5c80fb7ab4d1ba98187c72391a5067f68eb6312d
[gcc.git] / libstdc++-v3 / testsuite / experimental / filesystem / iterators / directory_iterator.cc
1 // Copyright (C) 2015-2016 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 "-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 != fs::directory_iterator() );
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 == fs::directory_iterator() );
45
46 // Test non-empty directory.
47 create_directory_symlink(p, p / "l", 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/"l" );
53 ++iter;
54 VERIFY( iter == fs::directory_iterator() );
55
56 // Test inaccessible directory.
57 permissions(p, fs::perms::none, ec);
58 VERIFY( !ec );
59 iter = fs::directory_iterator(p, ec);
60 VERIFY( ec );
61 VERIFY( iter != fs::directory_iterator() );
62
63 // Test inaccessible directory, skipping permission denied.
64 const auto opts = fs::directory_options::skip_permission_denied;
65 iter = fs::directory_iterator(p, opts, ec);
66 VERIFY( !ec );
67 VERIFY( iter == fs::directory_iterator() );
68
69 permissions(p, fs::perms::owner_all, ec);
70 remove_all(p, ec);
71 }
72
73 void
74 test02()
75 {
76 std::error_code ec;
77 const auto p = __gnu_test::nonexistent_path();
78 create_directory(p, fs::current_path(), ec);
79 create_directory_symlink(p, p / "l", ec);
80 VERIFY( !ec );
81
82 // Test post-increment (libstdc++/71005)
83 auto iter = fs::directory_iterator(p, ec);
84 VERIFY( !ec );
85 VERIFY( iter != fs::directory_iterator() );
86 const auto entry1 = *iter;
87 const auto entry2 = *iter++;
88 VERIFY( entry1 == entry2 );
89 VERIFY( entry1.path() == p/"l" );
90 VERIFY( iter == fs::directory_iterator() );
91
92 remove_all(p, ec);
93 }
94
95 void
96 test03()
97 {
98 std::error_code ec;
99 const auto p = __gnu_test::nonexistent_path();
100 create_directories(p / "longer_than_small_string_buffer", ec);
101 VERIFY( !ec );
102
103 // Test for no reallocation on each dereference (this is a GNU extension)
104 auto iter = fs::directory_iterator(p, ec);
105 const auto* s1 = iter->path().c_str();
106 const auto* s2 = iter->path().c_str();
107 VERIFY( s1 == s2 );
108
109 remove_all(p, ec);
110 }
111
112 void
113 test04()
114 {
115 const fs::directory_iterator it;
116 VERIFY( it == fs::directory_iterator() );
117 }
118
119 void
120 test05()
121 {
122 auto p = __gnu_test::nonexistent_path();
123 create_directory(p);
124 create_directory_symlink(p, p / "l");
125 fs::directory_iterator it(p), endit;
126 VERIFY( begin(it) == it );
127 static_assert( noexcept(begin(it)), "begin is noexcept" );
128 VERIFY( end(it) == endit );
129 static_assert( noexcept(end(it)), "end is noexcept" );
130 }
131
132 int
133 main()
134 {
135 test01();
136 test02();
137 test03();
138 test04();
139 test05();
140 }