| 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 | // template <class InputIterator> |
| 12 | // void assign(InputIterator first, InputIterator last); // constexpr since C++26 |
| 13 | |
| 14 | #include <forward_list> |
| 15 | #include <cassert> |
| 16 | #include <iterator> |
| 17 | |
| 18 | #include "test_macros.h" |
| 19 | #include "test_iterators.h" |
| 20 | #include "min_allocator.h" |
| 21 | |
| 22 | TEST_CONSTEXPR_CXX26 bool test() { |
| 23 | { |
| 24 | typedef int T; |
| 25 | typedef std::forward_list<T> C; |
| 26 | const T t0[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 27 | const T t1[] = {10, 11, 12, 13}; |
| 28 | C c(std::begin(arr: t1), std::end(arr: t1)); |
| 29 | typedef cpp17_input_iterator<const T*> I; |
| 30 | c.assign(n: I(std::begin(arr: t0)), val: I(std::end(arr: t0))); |
| 31 | int n = 0; |
| 32 | for (C::const_iterator i = c.cbegin(); i != c.cend(); ++i, ++n) |
| 33 | assert(*i == n); |
| 34 | assert(n == 10); |
| 35 | } |
| 36 | { |
| 37 | typedef int T; |
| 38 | typedef std::forward_list<T> C; |
| 39 | const T t0[] = {10, 11, 12, 13}; |
| 40 | const T t1[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 41 | C c(std::begin(arr: t1), std::end(arr: t1)); |
| 42 | typedef cpp17_input_iterator<const T*> I; |
| 43 | c.assign(n: I(std::begin(arr: t0)), val: I(std::end(arr: t0))); |
| 44 | int n = 0; |
| 45 | for (C::const_iterator i = c.cbegin(); i != c.cend(); ++i, ++n) |
| 46 | assert(*i == 10 + n); |
| 47 | assert(n == 4); |
| 48 | } |
| 49 | #if TEST_STD_VER >= 11 |
| 50 | { |
| 51 | typedef int T; |
| 52 | typedef std::forward_list<T, min_allocator<T>> C; |
| 53 | const T t0[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 54 | const T t1[] = {10, 11, 12, 13}; |
| 55 | C c(std::begin(t1), std::end(t1)); |
| 56 | typedef cpp17_input_iterator<const T*> I; |
| 57 | c.assign(I(std::begin(t0)), I(std::end(t0))); |
| 58 | int n = 0; |
| 59 | for (C::const_iterator i = c.cbegin(); i != c.cend(); ++i, ++n) |
| 60 | assert(*i == n); |
| 61 | assert(n == 10); |
| 62 | } |
| 63 | { |
| 64 | typedef int T; |
| 65 | typedef std::forward_list<T, min_allocator<T>> C; |
| 66 | const T t0[] = {10, 11, 12, 13}; |
| 67 | const T t1[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 68 | C c(std::begin(t1), std::end(t1)); |
| 69 | typedef cpp17_input_iterator<const T*> I; |
| 70 | c.assign(I(std::begin(t0)), I(std::end(t0))); |
| 71 | int n = 0; |
| 72 | for (C::const_iterator i = c.cbegin(); i != c.cend(); ++i, (void)++n) |
| 73 | assert(*i == 10 + n); |
| 74 | assert(n == 4); |
| 75 | } |
| 76 | #endif |
| 77 | |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | int main(int, char**) { |
| 82 | assert(test()); |
| 83 | #if TEST_STD_VER >= 26 |
| 84 | static_assert(test()); |
| 85 | #endif |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |