1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 // UNSUPPORTED: no-filesystem
11 // UNSUPPORTED: availability-filesystem-missing
15 // class recursive_directory_iterator
17 // recursive_directory_iterator& operator=(recursive_directory_iterator const&);
19 #include "filesystem_include.h"
20 #include <type_traits>
24 #include "test_macros.h"
25 #include "filesystem_test_helper.h"
27 // The filesystem specification explicitly allows for self-move on
28 // the directory iterators. Turn off this warning so we can test it.
29 TEST_CLANG_DIAGNOSTIC_IGNORED("-Wself-move")
33 recursive_directory_iterator
createInterestingIterator(const static_test_env
&static_env
)
34 // Create an "interesting" iterator where all fields are
35 // in a non-default state. The returned 'it' is in a
37 // it.options() == directory_options::skip_permission_denied
39 // it.recursion_pending() == true
41 const path testDir
= static_env
.Dir
;
42 const recursive_directory_iterator endIt
;
43 recursive_directory_iterator
it(testDir
,
44 directory_options::skip_permission_denied
);
46 while (it
.depth() != 1) {
50 assert(it
.depth() == 1);
51 it
.disable_recursion_pending();
55 recursive_directory_iterator
createDifferentInterestingIterator(const static_test_env
&static_env
)
56 // Create an "interesting" iterator where all fields are
57 // in a non-default state. The returned 'it' is in a
59 // it.options() == directory_options::follow_directory_symlink
61 // it.recursion_pending() == false
63 const path testDir
= static_env
.Dir
;
64 const recursive_directory_iterator endIt
;
65 recursive_directory_iterator
it(testDir
,
66 directory_options::follow_directory_symlink
);
68 while (it
.depth() != 2) {
72 assert(it
.depth() == 2);
77 static void test_assignment_signature()
79 using D
= recursive_directory_iterator
;
80 static_assert(std::is_nothrow_move_assignable
<D
>::value
, "");
84 static void test_move_to_end_iterator()
86 static_test_env static_env
;
87 const recursive_directory_iterator endIt
;
89 recursive_directory_iterator from
= createInterestingIterator(static_env
);
90 const recursive_directory_iterator
from_copy(from
);
91 const path entry
= *from
;
93 recursive_directory_iterator to
;
97 assert(to
.options() == from_copy
.options());
98 assert(to
.depth() == from_copy
.depth());
99 assert(to
.recursion_pending() == from_copy
.recursion_pending());
100 assert(from
== endIt
|| from
== to
);
104 static void test_move_from_end_iterator()
106 static_test_env static_env
;
107 recursive_directory_iterator from
;
108 recursive_directory_iterator to
= createInterestingIterator(static_env
);
110 to
= std::move(from
);
112 assert(to
== recursive_directory_iterator
{});
115 static void test_move_valid_iterator()
117 static_test_env static_env
;
118 const recursive_directory_iterator endIt
;
120 recursive_directory_iterator it
= createInterestingIterator(static_env
);
121 const recursive_directory_iterator
it_copy(it
);
122 const path entry
= *it
;
124 recursive_directory_iterator it2
= createDifferentInterestingIterator(static_env
);
125 const recursive_directory_iterator
it2_copy(it2
);
127 assert(it2
.options() != it
.options());
128 assert(it2
.depth() != it
.depth());
129 assert(it2
.recursion_pending() != it
.recursion_pending());
130 assert(*it2
!= entry
);
133 assert(it2
!= it2_copy
&& it2
!= endIt
);
134 assert(it2
.options() == it_copy
.options());
135 assert(it2
.depth() == it_copy
.depth());
136 assert(it2
.recursion_pending() == it_copy
.recursion_pending());
137 assert(*it2
== entry
);
138 assert(it
== endIt
|| it
== it2
);
141 static void test_returns_reference_to_self()
143 recursive_directory_iterator it
;
144 recursive_directory_iterator it2
;
145 recursive_directory_iterator
& ref
= (it2
= std::move(it
));
146 assert(&ref
== &it2
);
149 static void test_self_move()
151 static_test_env static_env
;
152 // Create two non-equal iterators that have exactly the same state.
153 recursive_directory_iterator it
= createInterestingIterator(static_env
);
154 recursive_directory_iterator it2
= createInterestingIterator(static_env
);
156 assert(it2
.options() == it
.options());
157 assert(it2
.depth() == it
.depth());
158 assert(it2
.recursion_pending() == it
.recursion_pending());
162 assert(it2
.options() == it
.options());
163 assert(it2
.depth() == it
.depth());
164 assert(it2
.recursion_pending() == it
.recursion_pending());
168 int main(int, char**) {
169 test_assignment_signature();
170 test_move_to_end_iterator();
171 test_move_from_end_iterator();
172 test_move_valid_iterator();
173 test_returns_reference_to_self();