|
| 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: libcpp-has-no-filesystem-library |
| 10 | + |
| 11 | +// Make sure the various containers' iterators are not broken by the use of `std::rel_ops`. |
| 12 | + |
| 13 | +#include <utility> // for std::rel_ops |
| 14 | + |
| 15 | +#include <array> |
| 16 | +#include <deque> |
| 17 | +#include <forward_list> |
| 18 | +#include <list> |
| 19 | +#include <map> |
| 20 | +#include <set> |
| 21 | +#include <string> |
| 22 | +#include <unordered_map> |
| 23 | +#include <unordered_set> |
| 24 | +#include <vector> |
| 25 | + |
| 26 | +#include "test_macros.h" |
| 27 | + |
| 28 | +#if TEST_STD_VER >= 11 |
| 29 | +#include "filesystem_include.h" |
| 30 | +#endif |
| 31 | + |
| 32 | +#if TEST_STD_VER >= 17 |
| 33 | +#include <string_view> |
| 34 | +#endif |
| 35 | + |
| 36 | +#if TEST_STD_VER >= 20 |
| 37 | +#include <span> |
| 38 | +#endif |
| 39 | + |
| 40 | +using namespace std::rel_ops; |
| 41 | + |
| 42 | +template<class It, class ConstIt> |
| 43 | +void test_eq(It it, ConstIt cit) { |
| 44 | + (void)(it == it); |
| 45 | + (void)(it != it); |
| 46 | + (void)(it == cit); |
| 47 | + (void)(it != cit); |
| 48 | + (void)(cit == it); |
| 49 | + (void)(cit != it); |
| 50 | + (void)(cit == cit); |
| 51 | + (void)(cit != cit); |
| 52 | +} |
| 53 | + |
| 54 | +template<class It, class ConstIt> |
| 55 | +void test_lt(It it, ConstIt cit) { |
| 56 | + (void)(it < it); |
| 57 | + (void)(it <= it); |
| 58 | + (void)(it > it); |
| 59 | + (void)(it >= it); |
| 60 | + (void)(it < cit); |
| 61 | + (void)(it <= cit); |
| 62 | + (void)(it > cit); |
| 63 | + (void)(it >= cit); |
| 64 | + (void)(cit < it); |
| 65 | + (void)(cit <= it); |
| 66 | + (void)(cit > it); |
| 67 | + (void)(cit >= it); |
| 68 | + (void)(cit < cit); |
| 69 | + (void)(cit <= cit); |
| 70 | + (void)(cit > cit); |
| 71 | + (void)(cit >= cit); |
| 72 | + |
| 73 | + // Test subtraction too, even though std::rel_ops shouldn't affect it. |
| 74 | + |
| 75 | + (void)(it - it); |
| 76 | + (void)(it - cit); |
| 77 | + (void)(cit - it); |
| 78 | + (void)(cit - cit); |
| 79 | +} |
| 80 | + |
| 81 | +template<class Container> |
| 82 | +void test_forward() { |
| 83 | + // There is no need to distinguish "forward" from "bidirectional." |
| 84 | + // libc++ already can't handle `c.rbegin() >= c.rbegin()` in the |
| 85 | + // presence of std::rel_ops, and neither can Microsoft nor libstdc++. |
| 86 | + |
| 87 | + Container c; |
| 88 | + typename Container::iterator it = c.begin(); |
| 89 | + typename Container::const_iterator cit = c.begin(); |
| 90 | + test_eq(it, cit); |
| 91 | +} |
| 92 | + |
| 93 | +template<class Container> |
| 94 | +void test_random_access() { |
| 95 | + Container c; |
| 96 | + typename Container::iterator it = c.begin(); |
| 97 | + typename Container::const_iterator cit = c.begin(); |
| 98 | + test_eq(it, cit); |
| 99 | + test_lt(it, cit); |
| 100 | +} |
| 101 | + |
| 102 | +template void test_random_access<std::array<int, 10> >(); |
| 103 | +template void test_random_access<std::deque<int> >(); |
| 104 | +template void test_forward<std::forward_list<int> >(); |
| 105 | +template void test_forward<std::list<int> >(); |
| 106 | +template void test_forward<std::map<int, int> >(); |
| 107 | +template void test_forward<std::multimap<int, int> >(); |
| 108 | +template void test_forward<std::multiset<int> >(); |
| 109 | +template void test_forward<std::set<int> >(); |
| 110 | +template void test_random_access<std::string>(); |
| 111 | +template void test_forward<std::unordered_map<int, int> >(); |
| 112 | +template void test_forward<std::unordered_multimap<int, int> >(); |
| 113 | +template void test_forward<std::unordered_multiset<int> >(); |
| 114 | +template void test_forward<std::unordered_set<int> >(); |
| 115 | +template void test_random_access<std::vector<int> >(); |
| 116 | + |
| 117 | +#if TEST_STD_VER >= 11 |
| 118 | +void test_directory_iterators() { |
| 119 | + fs::directory_iterator it; |
| 120 | + test_eq(it, it); |
| 121 | + |
| 122 | + fs::recursive_directory_iterator rdit; |
| 123 | + test_eq(rdit, rdit); |
| 124 | +} |
| 125 | + |
| 126 | +template void test_forward<fs::path>(); |
| 127 | +#endif |
| 128 | + |
| 129 | +#if TEST_STD_VER >= 17 |
| 130 | +template void test_random_access<std::string_view>(); |
| 131 | +#endif |
| 132 | + |
| 133 | +#if TEST_STD_VER >= 20 |
| 134 | +void test_span() { |
| 135 | + std::span<int> c; |
| 136 | + std::span<int>::iterator it = c.begin(); // span has no const_iterator |
| 137 | + test_eq(it, it); |
| 138 | + test_lt(it, it); |
| 139 | +} |
| 140 | +#endif |
0 commit comments