| 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, c++11, c++14 |
| 10 | // UNSUPPORTED: availability-filesystem-missing |
| 11 | |
| 12 | // <filesystem> |
| 13 | |
| 14 | // class path |
| 15 | |
| 16 | // template <class Source> |
| 17 | // path(const Source& source); |
| 18 | // template <class InputIterator> |
| 19 | // path(InputIterator first, InputIterator last); |
| 20 | |
| 21 | |
| 22 | #include <filesystem> |
| 23 | #include <cassert> |
| 24 | #include <iterator> |
| 25 | #include <type_traits> |
| 26 | |
| 27 | #include "test_macros.h" |
| 28 | namespace fs = std::filesystem; |
| 29 | |
| 30 | template <class Iter1, class Iter2> |
| 31 | bool checkCollectionsEqual( |
| 32 | Iter1 start1, Iter1 const end1 |
| 33 | , Iter2 start2, Iter2 const end2 |
| 34 | ) |
| 35 | { |
| 36 | while (start1 != end1 && start2 != end2) { |
| 37 | if (*start1 != *start2) { |
| 38 | return false; |
| 39 | } |
| 40 | ++start1; ++start2; |
| 41 | } |
| 42 | return (start1 == end1 && start2 == end2); |
| 43 | } |
| 44 | |
| 45 | template <class Iter1, class Iter2> |
| 46 | bool checkCollectionsEqualBackwards( |
| 47 | Iter1 const start1, Iter1 end1 |
| 48 | , Iter2 const start2, Iter2 end2 |
| 49 | ) |
| 50 | { |
| 51 | while (start1 != end1 && start2 != end2) { |
| 52 | --end1; --end2; |
| 53 | if (*end1 != *end2) { |
| 54 | return false; |
| 55 | } |
| 56 | } |
| 57 | return (start1 == end1 && start2 == end2); |
| 58 | } |
| 59 | |
| 60 | void checkIteratorConcepts() { |
| 61 | using namespace fs; |
| 62 | using It = path::iterator; |
| 63 | using Traits = std::iterator_traits<It>; |
| 64 | ASSERT_SAME_TYPE(path::const_iterator, It); |
| 65 | #if TEST_STD_VER > 17 |
| 66 | static_assert(std::bidirectional_iterator<It>); |
| 67 | #endif |
| 68 | ASSERT_SAME_TYPE(Traits::value_type, path); |
| 69 | LIBCPP_STATIC_ASSERT(std::is_same<Traits::iterator_category, std::input_iterator_tag>::value, "" ); |
| 70 | LIBCPP_STATIC_ASSERT(std::is_same<Traits::pointer, path const*>::value, "" ); |
| 71 | LIBCPP_STATIC_ASSERT(std::is_same<Traits::reference, path>::value, "" ); |
| 72 | { |
| 73 | It it; |
| 74 | ASSERT_SAME_TYPE(It&, decltype(++it)); |
| 75 | ASSERT_SAME_TYPE(It, decltype(it++)); |
| 76 | ASSERT_SAME_TYPE(It&, decltype(--it)); |
| 77 | ASSERT_SAME_TYPE(It, decltype(it--)); |
| 78 | ASSERT_SAME_TYPE(Traits::reference, decltype(*it)); |
| 79 | ASSERT_SAME_TYPE(Traits::pointer, decltype(it.operator->())); |
| 80 | #ifdef _WIN32 |
| 81 | ASSERT_SAME_TYPE(std::wstring const&, decltype(it->native())); |
| 82 | #else |
| 83 | ASSERT_SAME_TYPE(std::string const&, decltype(it->native())); |
| 84 | #endif |
| 85 | ASSERT_SAME_TYPE(bool, decltype(it == it)); |
| 86 | ASSERT_SAME_TYPE(bool, decltype(it != it)); |
| 87 | } |
| 88 | { |
| 89 | path const p; |
| 90 | ASSERT_SAME_TYPE(It, decltype(p.begin())); |
| 91 | ASSERT_SAME_TYPE(It, decltype(p.end())); |
| 92 | assert(p.begin() == p.end()); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | void checkBeginEndBasic() { |
| 97 | using namespace fs; |
| 98 | using It = path::iterator; |
| 99 | { |
| 100 | path const p; |
| 101 | ASSERT_SAME_TYPE(It, decltype(p.begin())); |
| 102 | ASSERT_SAME_TYPE(It, decltype(p.end())); |
| 103 | assert(p.begin() == p.end()); |
| 104 | } |
| 105 | { |
| 106 | path const p("foo" ); |
| 107 | It default_constructed; |
| 108 | default_constructed = p.begin(); |
| 109 | assert(default_constructed == p.begin()); |
| 110 | assert(default_constructed != p.end()); |
| 111 | default_constructed = p.end(); |
| 112 | assert(default_constructed == p.end()); |
| 113 | assert(default_constructed != p.begin()); |
| 114 | } |
| 115 | { |
| 116 | path p("//root_name//first_dir////second_dir" ); |
| 117 | #ifdef _WIN32 |
| 118 | const path expect[] = {"//root_name" , "/" , "first_dir" , "second_dir" }; |
| 119 | #else |
| 120 | const path expect[] = {"/" , "root_name" , "first_dir" , "second_dir" }; |
| 121 | #endif |
| 122 | assert(checkCollectionsEqual(p.begin(), p.end(), std::begin(expect), std::end(expect))); |
| 123 | assert(checkCollectionsEqualBackwards(p.begin(), p.end(), std::begin(expect), std::end(expect))); |
| 124 | } |
| 125 | { |
| 126 | path p("////foo/bar/baz///" ); |
| 127 | const path expect[] = {"/" , "foo" , "bar" , "baz" , "" }; |
| 128 | assert(checkCollectionsEqual(p.begin(), p.end(), std::begin(expect), std::end(expect))); |
| 129 | assert(checkCollectionsEqualBackwards(p.begin(), p.end(), std::begin(expect), std::end(expect))); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | int main(int, char**) { |
| 134 | using namespace fs; |
| 135 | checkIteratorConcepts(); |
| 136 | checkBeginEndBasic(); // See path.decompose.pass.cpp for more tests. |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |