• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // UNSUPPORTED: c++98, c++03
11 
12 // <experimental/filesystem>
13 
14 // class recursive_directory_iterator
15 
16 // bool recursion_pending() const;
17 
18 #include <experimental/filesystem>
19 #include <type_traits>
20 #include <set>
21 #include <cassert>
22 
23 #include "test_macros.h"
24 #include "rapid-cxx-test.hpp"
25 #include "filesystem_test_helper.hpp"
26 
27 using namespace std::experimental::filesystem;
28 
29 TEST_SUITE(recursive_directory_iterator_recursion_pending_tests)
30 
TEST_CASE(initial_value_test)31 TEST_CASE(initial_value_test)
32 {
33     recursive_directory_iterator it(StaticEnv::Dir);
34     TEST_REQUIRE(it.recursion_pending() == true);
35 }
36 
TEST_CASE(value_after_copy_construction_and_assignment_test)37 TEST_CASE(value_after_copy_construction_and_assignment_test)
38 {
39     recursive_directory_iterator rec_pending_it(StaticEnv::Dir);
40     recursive_directory_iterator no_rec_pending_it(StaticEnv::Dir);
41     no_rec_pending_it.disable_recursion_pending();
42 
43     { // copy construction
44         recursive_directory_iterator it(rec_pending_it);
45         TEST_CHECK(it.recursion_pending() == true);
46         it.disable_recursion_pending();
47         TEST_REQUIRE(rec_pending_it.recursion_pending() == true);
48 
49         recursive_directory_iterator it2(no_rec_pending_it);
50         TEST_CHECK(it2.recursion_pending() == false);
51     }
52     { // copy assignment
53         recursive_directory_iterator it(StaticEnv::Dir);
54         it.disable_recursion_pending();
55         it = rec_pending_it;
56         TEST_CHECK(it.recursion_pending() == true);
57         it.disable_recursion_pending();
58         TEST_REQUIRE(rec_pending_it.recursion_pending() == true);
59 
60         recursive_directory_iterator it2(StaticEnv::Dir);
61         it2 = no_rec_pending_it;
62         TEST_CHECK(it2.recursion_pending() == false);
63     }
64     TEST_CHECK(rec_pending_it.recursion_pending() == true);
65     TEST_CHECK(no_rec_pending_it.recursion_pending() == false);
66 }
67 
68 
TEST_CASE(value_after_move_construction_and_assignment_test)69 TEST_CASE(value_after_move_construction_and_assignment_test)
70 {
71     recursive_directory_iterator rec_pending_it(StaticEnv::Dir);
72     recursive_directory_iterator no_rec_pending_it(StaticEnv::Dir);
73     no_rec_pending_it.disable_recursion_pending();
74 
75     { // move construction
76         recursive_directory_iterator it_cp(rec_pending_it);
77         recursive_directory_iterator it(std::move(it_cp));
78         TEST_CHECK(it.recursion_pending() == true);
79 
80         recursive_directory_iterator it_cp2(no_rec_pending_it);
81         recursive_directory_iterator it2(std::move(it_cp2));
82         TEST_CHECK(it2.recursion_pending() == false);
83     }
84     { // copy assignment
85         recursive_directory_iterator it(StaticEnv::Dir);
86         it.disable_recursion_pending();
87         recursive_directory_iterator it_cp(rec_pending_it);
88         it = std::move(it_cp);
89         TEST_CHECK(it.recursion_pending() == true);
90 
91         recursive_directory_iterator it2(StaticEnv::Dir);
92         recursive_directory_iterator it_cp2(no_rec_pending_it);
93         it2 = std::move(it_cp2);
94         TEST_CHECK(it2.recursion_pending() == false);
95     }
96     TEST_CHECK(rec_pending_it.recursion_pending() == true);
97     TEST_CHECK(no_rec_pending_it.recursion_pending() == false);
98 }
99 
TEST_CASE(increment_resets_value)100 TEST_CASE(increment_resets_value)
101 {
102     const recursive_directory_iterator endIt;
103     {
104         recursive_directory_iterator it(StaticEnv::Dir);
105         it.disable_recursion_pending();
106         TEST_CHECK(it.recursion_pending() == false);
107         ++it;
108         TEST_CHECK(it.recursion_pending() == true);
109         TEST_CHECK(it.depth() == 0);
110     }
111     {
112         recursive_directory_iterator it(StaticEnv::Dir);
113         it.disable_recursion_pending();
114         TEST_CHECK(it.recursion_pending() == false);
115         it++;
116         TEST_CHECK(it.recursion_pending() == true);
117         TEST_CHECK(it.depth() == 0);
118     }
119     {
120         recursive_directory_iterator it(StaticEnv::Dir);
121         it.disable_recursion_pending();
122         TEST_CHECK(it.recursion_pending() == false);
123         std::error_code ec;
124         it.increment(ec);
125         TEST_CHECK(it.recursion_pending() == true);
126         TEST_CHECK(it.depth() == 0);
127     }
128 }
129 
TEST_CASE(pop_does_not_reset_value)130 TEST_CASE(pop_does_not_reset_value)
131 {
132     const recursive_directory_iterator endIt;
133 
134     auto& DE0 = StaticEnv::DirIterationList;
135     std::set<path> notSeenDepth0(std::begin(DE0), std::end(DE0));
136 
137     recursive_directory_iterator it(StaticEnv::Dir);
138     TEST_REQUIRE(it != endIt);
139 
140     while (it.depth() == 0) {
141         notSeenDepth0.erase(it->path());
142         ++it;
143         TEST_REQUIRE(it != endIt);
144     }
145     TEST_REQUIRE(it.depth() == 1);
146     it.disable_recursion_pending();
147     it.pop();
148     // Since the order of iteration is unspecified the pop() could result
149     // in the end iterator. When this is the case it is undefined behavior
150     // to call recursion_pending().
151     if (it == endIt) {
152         TEST_CHECK(notSeenDepth0.empty());
153 #if defined(_LIBCPP_VERSION)
154         TEST_CHECK(it.recursion_pending() == false);
155 #endif
156     } else {
157         TEST_CHECK(! notSeenDepth0.empty());
158         TEST_CHECK(it.recursion_pending() == false);
159     }
160 }
161 
162 TEST_SUITE_END()
163