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 // UNSUPPORTED: c++03
10 
11 // <filesystem>
12 
13 // class recursive_directory_iterator
14 
15 // int depth() const
16 
17 #include "filesystem_include.h"
18 #include <type_traits>
19 #include <set>
20 #include <cassert>
21 
22 #include "test_macros.h"
23 #include "rapid-cxx-test.h"
24 #include "filesystem_test_helper.h"
25 
26 using namespace fs;
27 
28 TEST_SUITE(recursive_directory_iterator_depth_tests)
29 
TEST_CASE(test_depth)30 TEST_CASE(test_depth)
31 {
32     static_test_env static_env;
33     const path testDir = static_env.Dir;
34     const path DirDepth1 = static_env.Dir2;
35     const path DirDepth2 = static_env.Dir3;
36     const recursive_directory_iterator endIt{};
37 
38     std::error_code ec;
39     recursive_directory_iterator it(testDir, ec);
40     TEST_REQUIRE(!ec);
41     TEST_CHECK(it.depth() == 0);
42 
43     bool seen_d1, seen_d2;
44     seen_d1 = seen_d2 = false;
45 
46     while (it != endIt) {
47         const path entry = *it;
48         const path parent = entry.parent_path();
49         if (parent == testDir) {
50             TEST_CHECK(it.depth() == 0);
51         } else if (parent == DirDepth1) {
52             TEST_CHECK(it.depth() == 1);
53             seen_d1 = true;
54         } else if (parent == DirDepth2) {
55             TEST_CHECK(it.depth() == 2);
56             seen_d2 = true;
57         } else {
58             TEST_CHECK(!"Unexpected depth while iterating over static env");
59         }
60         ++it;
61     }
62     TEST_REQUIRE(seen_d1 && seen_d2);
63     TEST_CHECK(it == endIt);
64 }
65 
66 TEST_SUITE_END()
67