| 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 | // <forward_list> |
| 10 | |
| 11 | // iterator begin(); // constexpr since C++26 |
| 12 | // iterator end(); // constexpr since C++26 |
| 13 | // const_iterator begin() const; // constexpr since C++26 |
| 14 | // const_iterator end() const; // constexpr since C++26 |
| 15 | // const_iterator cbegin() const; // constexpr since C++26 |
| 16 | // const_iterator cend() const; // constexpr since C++26 |
| 17 | |
| 18 | #include <forward_list> |
| 19 | #include <cassert> |
| 20 | #include <iterator> |
| 21 | |
| 22 | #include "test_macros.h" |
| 23 | #include "min_allocator.h" |
| 24 | |
| 25 | TEST_CONSTEXPR_CXX26 bool test() { |
| 26 | { |
| 27 | typedef int T; |
| 28 | typedef std::forward_list<T> C; |
| 29 | C c; |
| 30 | C::iterator i = c.begin(); |
| 31 | C::iterator j = c.end(); |
| 32 | assert(std::distance(i, j) == 0); |
| 33 | assert(i == j); |
| 34 | } |
| 35 | { |
| 36 | typedef int T; |
| 37 | typedef std::forward_list<T> C; |
| 38 | const C c; |
| 39 | C::const_iterator i = c.begin(); |
| 40 | C::const_iterator j = c.end(); |
| 41 | assert(std::distance(i, j) == 0); |
| 42 | assert(i == j); |
| 43 | } |
| 44 | { |
| 45 | typedef int T; |
| 46 | typedef std::forward_list<T> C; |
| 47 | C c; |
| 48 | C::const_iterator i = c.cbegin(); |
| 49 | C::const_iterator j = c.cend(); |
| 50 | assert(std::distance(i, j) == 0); |
| 51 | assert(i == j); |
| 52 | assert(i == c.end()); |
| 53 | } |
| 54 | { |
| 55 | typedef int T; |
| 56 | typedef std::forward_list<T> C; |
| 57 | const T t[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 58 | C c(std::begin(arr: t), std::end(arr: t)); |
| 59 | C::iterator i = c.begin(); |
| 60 | assert(*i == 0); |
| 61 | ++i; |
| 62 | assert(*i == 1); |
| 63 | *i = 10; |
| 64 | assert(*i == 10); |
| 65 | assert(std::distance(c.begin(), c.end()) == 10); |
| 66 | } |
| 67 | { |
| 68 | typedef int T; |
| 69 | typedef std::forward_list<T> C; |
| 70 | C::iterator i; |
| 71 | C::const_iterator j; |
| 72 | (void)i; |
| 73 | (void)j; |
| 74 | } |
| 75 | #if TEST_STD_VER >= 11 |
| 76 | { |
| 77 | typedef int T; |
| 78 | typedef std::forward_list<T, min_allocator<T>> C; |
| 79 | C c; |
| 80 | C::iterator i = c.begin(); |
| 81 | C::iterator j = c.end(); |
| 82 | assert(std::distance(i, j) == 0); |
| 83 | assert(i == j); |
| 84 | } |
| 85 | { |
| 86 | typedef int T; |
| 87 | typedef std::forward_list<T, min_allocator<T>> C; |
| 88 | const C c; |
| 89 | C::const_iterator i = c.begin(); |
| 90 | C::const_iterator j = c.end(); |
| 91 | assert(std::distance(i, j) == 0); |
| 92 | assert(i == j); |
| 93 | } |
| 94 | { |
| 95 | typedef int T; |
| 96 | typedef std::forward_list<T, min_allocator<T>> C; |
| 97 | C c; |
| 98 | C::const_iterator i = c.cbegin(); |
| 99 | C::const_iterator j = c.cend(); |
| 100 | assert(std::distance(i, j) == 0); |
| 101 | assert(i == j); |
| 102 | assert(i == c.end()); |
| 103 | } |
| 104 | { |
| 105 | typedef int T; |
| 106 | typedef std::forward_list<T, min_allocator<T>> C; |
| 107 | const T t[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 108 | C c(std::begin(t), std::end(t)); |
| 109 | C::iterator i = c.begin(); |
| 110 | assert(*i == 0); |
| 111 | ++i; |
| 112 | assert(*i == 1); |
| 113 | *i = 10; |
| 114 | assert(*i == 10); |
| 115 | assert(std::distance(c.begin(), c.end()) == 10); |
| 116 | } |
| 117 | { |
| 118 | typedef int T; |
| 119 | typedef std::forward_list<T, min_allocator<T>> C; |
| 120 | C::iterator i; |
| 121 | C::const_iterator j; |
| 122 | (void)i; |
| 123 | (void)j; |
| 124 | } |
| 125 | #endif |
| 126 | #if TEST_STD_VER > 11 |
| 127 | { // N3644 testing |
| 128 | std::forward_list<int>::iterator ii1{}, ii2{}; |
| 129 | std::forward_list<int>::iterator ii4 = ii1; |
| 130 | std::forward_list<int>::const_iterator cii{}; |
| 131 | assert(ii1 == ii2); |
| 132 | assert(ii1 == ii4); |
| 133 | |
| 134 | assert(!(ii1 != ii2)); |
| 135 | |
| 136 | assert((ii1 == cii)); |
| 137 | assert((cii == ii1)); |
| 138 | assert(!(ii1 != cii)); |
| 139 | assert(!(cii != ii1)); |
| 140 | |
| 141 | // std::forward_list<int> c; |
| 142 | // assert ( ii1 != c.cbegin()); |
| 143 | // assert ( cii != c.begin()); |
| 144 | // assert ( cii != c.cend()); |
| 145 | // assert ( ii1 != c.end()); |
| 146 | } |
| 147 | #endif |
| 148 | |
| 149 | return true; |
| 150 | } |
| 151 | |
| 152 | int main(int, char**) { |
| 153 | assert(test()); |
| 154 | #if TEST_STD_VER >= 26 |
| 155 | static_assert(test()); |
| 156 | #endif |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |