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 path
14 
15 // template <class Source>
16 //      path(const Source& source);
17 // template <class InputIterator>
18 //      path(InputIterator first, InputIterator last);
19 
20 
21 #include "filesystem_include.h"
22 #include <iterator>
23 #include <type_traits>
24 #include <cassert>
25 
26 #include "test_macros.h"
27 #include "filesystem_test_helper.h"
28 
29 
30 
31 template <class It>
32 std::reverse_iterator<It> mkRev(It it) {
33   return std::reverse_iterator<It>(it);
34 }
35 
36 void checkIteratorConcepts() {
37   using namespace fs;
38   using It = path::iterator;
39   using Traits = std::iterator_traits<It>;
40   ASSERT_SAME_TYPE(Traits::iterator_category, std::bidirectional_iterator_tag);
41   ASSERT_SAME_TYPE(Traits::value_type, path);
42   ASSERT_SAME_TYPE(Traits::pointer,   path const*);
43   ASSERT_SAME_TYPE(Traits::reference, path const&);
44   {
45     It it;
46     ASSERT_SAME_TYPE(It&, decltype(++it));
47     ASSERT_SAME_TYPE(It, decltype(it++));
48     ASSERT_SAME_TYPE(It&, decltype(--it));
49     ASSERT_SAME_TYPE(It, decltype(it--));
50     ASSERT_SAME_TYPE(Traits::reference, decltype(*it));
51     ASSERT_SAME_TYPE(Traits::pointer, decltype(it.operator->()));
52 #ifdef _WIN32
53     ASSERT_SAME_TYPE(std::wstring const&, decltype(it->native()));
54 #else
55     ASSERT_SAME_TYPE(std::string const&, decltype(it->native()));
56 #endif
57     ASSERT_SAME_TYPE(bool, decltype(it == it));
58     ASSERT_SAME_TYPE(bool, decltype(it != it));
59   }
60   {
61     path const p;
62     ASSERT_SAME_TYPE(It, decltype(p.begin()));
63     ASSERT_SAME_TYPE(It, decltype(p.end()));
64     assert(p.begin() == p.end());
65   }
66 }
67 
68 void checkBeginEndBasic() {
69   using namespace fs;
70   using It = path::iterator;
71   {
72     path const p;
73     ASSERT_SAME_TYPE(It, decltype(p.begin()));
74     ASSERT_SAME_TYPE(It, decltype(p.end()));
75     assert(p.begin() == p.end());
76   }
77   {
78     path const p("foo");
79     It default_constructed;
80     default_constructed = p.begin();
81     assert(default_constructed == p.begin());
82     assert(default_constructed != p.end());
83     default_constructed = p.end();
84     assert(default_constructed == p.end());
85     assert(default_constructed != p.begin());
86   }
87   {
88     path p("//root_name//first_dir////second_dir");
89 #ifdef _WIN32
90     const path expect[] = {"//root_name", "/", "first_dir", "second_dir"};
91 #else
92     const path expect[] = {"/", "root_name", "first_dir", "second_dir"};
93 #endif
94     assert(checkCollectionsEqual(p.begin(), p.end(), std::begin(expect), std::end(expect)));
95     assert(checkCollectionsEqualBackwards(p.begin(), p.end(), std::begin(expect), std::end(expect)));
96 
97   }
98   {
99     path p("////foo/bar/baz///");
100     const path expect[] = {"/", "foo", "bar", "baz", ""};
101     assert(checkCollectionsEqual(p.begin(), p.end(), std::begin(expect), std::end(expect)));
102     assert(checkCollectionsEqualBackwards(p.begin(), p.end(), std::begin(expect), std::end(expect)));
103 
104   }
105 
106 }
107 
108 int main(int, char**) {
109   using namespace fs;
110   checkIteratorConcepts();
111   checkBeginEndBasic(); // See path.decompose.pass.cpp for more tests.
112 
113   return 0;
114 }
115