]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/experimental/filesystem/iterators/directory_iterator.cc
Use effective-target instead of -std options
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / experimental / filesystem / iterators / directory_iterator.cc
CommitLineData
818ab71a 1// Copyright (C) 2015-2016 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
52066eae
JW
18// { dg-options "-lstdc++fs" }
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{
31 bool test __attribute__((unused)) = false;
32 std::error_code ec;
33
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 != fs::directory_iterator() );
39
40 // Test empty directory.
41 create_directory(p, fs::current_path(), ec);
42 VERIFY( !ec );
43 iter = fs::directory_iterator(p, ec);
44 VERIFY( !ec );
45 VERIFY( iter == fs::directory_iterator() );
46
47 // Test non-empty directory.
48 create_directory_symlink(p, p / "l", ec);
49 VERIFY( !ec );
50 iter = fs::directory_iterator(p, ec);
51 VERIFY( !ec );
52 VERIFY( iter != fs::directory_iterator() );
53 VERIFY( iter->path() == p/"l" );
54 ++iter;
55 VERIFY( iter == fs::directory_iterator() );
56
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 != fs::directory_iterator() );
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 == fs::directory_iterator() );
69
70 permissions(p, fs::perms::owner_all, ec);
71 remove_all(p, ec);
72}
73
d7187f9e
JW
74void
75test02()
76{
77 bool test __attribute__((unused)) = false;
78
79 std::error_code ec;
80 const auto p = __gnu_test::nonexistent_path();
81 create_directory(p, fs::current_path(), ec);
82 create_directory_symlink(p, p / "l", ec);
83 VERIFY( !ec );
84
85 // Test post-increment (libstdc++/71005)
86 auto iter = fs::directory_iterator(p, ec);
87 VERIFY( !ec );
88 VERIFY( iter != fs::directory_iterator() );
89 const auto entry1 = *iter;
90 const auto entry2 = *iter++;
91 VERIFY( entry1 == entry2 );
92 VERIFY( entry1.path() == p/"l" );
93 VERIFY( iter == fs::directory_iterator() );
94
95 remove_all(p, ec);
96}
97
98void
99test03()
100{
101 bool test __attribute__((unused)) = false;
102
103 std::error_code ec;
104 const auto p = __gnu_test::nonexistent_path();
105 create_directories(p / "longer_than_small_string_buffer", ec);
106 VERIFY( !ec );
107
108 // Test for no reallocation on each dereference (this is a GNU extension)
109 auto iter = fs::directory_iterator(p, ec);
110 const auto* s1 = iter->path().c_str();
111 const auto* s2 = iter->path().c_str();
112 VERIFY( s1 == s2 );
113
114 remove_all(p, ec);
115}
116
7f99d40a
JW
117void
118test04()
119{
120 bool test __attribute__((unused)) = false;
121
122 const fs::directory_iterator it;
123 VERIFY( it == fs::directory_iterator() );
124}
125
126void
127test05()
128{
129 bool test __attribute__((unused)) = false;
130
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" );
139}
140
429ee11a
JW
141int
142main()
143{
144 test01();
d7187f9e
JW
145 test02();
146 test03();
7f99d40a
JW
147 test04();
148 test05();
429ee11a 149}