Debian Codegraph @

libcxx/test/std/input.output/filesystems/class.rec.dir.itr/rec.dir.itr.members/pop.pass.cpp

raw
1 //===----------------------------------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8
9 // REQUIRES: can-create-symlinks
10 // UNSUPPORTED: c++03, c++11, c++14
11 // UNSUPPORTED: no-filesystem
12
13 // <filesystem>
14
15 // class recursive_directory_iterator
16
17 // void pop();
18 // void pop(error_code& ec);
19
20 #include <filesystem>
21 #include <set>
22 #include <cassert>
23
24 #include "test_macros.h"
25 #include "filesystem_test_helper.h"
26 namespace fs = std::filesystem;
27 using namespace fs;
28
29 static void signature_tests()
30 {
31 recursive_directory_iterator it{}; ((void)it);
32 std::error_code ec; ((void)ec);
33 ASSERT_NOT_NOEXCEPT(it.pop());
34 ASSERT_NOT_NOEXCEPT(it.pop(ec)); // may require allocation or other things
35 }
36
37 // NOTE: Since the order of iteration is unspecified we use a list of
38 // seen files at each depth to determine the new depth after a 'pop()' operation.
39 static void test_depth()
40 {
41 static_test_env static_env;
42 const recursive_directory_iterator endIt{};
43
44 auto& DE0 = static_env.DirIterationList;
45 std::set<path> notSeenDepth0(DE0.begin(), DE0.end());
46
47 auto& DE1 = static_env.DirIterationListDepth1;
48 std::set<path> notSeenDepth1(DE1.begin(), DE1.end());
49
50 std::error_code ec;
51 recursive_directory_iterator it(static_env.Dir, ec);
52 assert(it != endIt);
53 assert(it.depth() == 0);
54
55 while (it.depth() != 2) {
56 if (it.depth() == 0)
57 notSeenDepth0.erase(it->path());
58 else
59 notSeenDepth1.erase(it->path());
60 ++it;
61 assert(it != endIt);
62 }
63
64 while (true) {
65 auto set_ec = std::make_error_code(std::errc::address_in_use);
66 it.pop(set_ec);
67 assert(!set_ec);
68
69 if (it == endIt) {
70 // We must have seen every entry at depth 0 and 1.
71 assert(notSeenDepth0.empty() && notSeenDepth1.empty());
72 break;
73 }
74 else if (it.depth() == 1) {
75 // If we popped to depth 1 then there must be unseen entries
76 // at this level.
77 assert(!notSeenDepth1.empty());
78 assert(notSeenDepth1.count(it->path()));
79 notSeenDepth1.clear();
80 }
81 else if (it.depth() == 0) {
82 // If we popped to depth 0 there must be unseen entries at this
83 // level. There should also be no unseen entries at depth 1.
84 assert(!notSeenDepth0.empty());
85 assert(notSeenDepth1.empty());
86 assert(notSeenDepth0.count(it->path()));
87 notSeenDepth0.clear();
88 }
89 }
90 }
91
92 int main(int, char**) {
93 signature_tests();
94 test_depth();
95
96 return 0;
97 }