| 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 | // <deque> |
| 10 | |
| 11 | // template <class InputIterator> |
| 12 | // void assign(InputIterator f, InputIterator l); |
| 13 | |
| 14 | #include "asan_testing.h" |
| 15 | #include <deque> |
| 16 | #include <cassert> |
| 17 | #include <cstddef> |
| 18 | |
| 19 | #include "test_macros.h" |
| 20 | #include "test_iterators.h" |
| 21 | #include "min_allocator.h" |
| 22 | #if TEST_STD_VER >= 11 |
| 23 | # include "emplace_constructible.h" |
| 24 | #endif |
| 25 | |
| 26 | template <class C> |
| 27 | C make(int size, int start = 0) { |
| 28 | const int b = 4096 / sizeof(int); |
| 29 | int init = 0; |
| 30 | if (start > 0) { |
| 31 | init = (start + 1) / b + ((start + 1) % b != 0); |
| 32 | init *= b; |
| 33 | --init; |
| 34 | } |
| 35 | C c(init, 0); |
| 36 | for (int i = 0; i < init - start; ++i) |
| 37 | c.pop_back(); |
| 38 | for (int i = 0; i < size; ++i) |
| 39 | c.push_back(i); |
| 40 | for (int i = 0; i < start; ++i) |
| 41 | c.pop_front(); |
| 42 | return c; |
| 43 | } |
| 44 | |
| 45 | template <class C> |
| 46 | void test(C& c1, const C& c2) { |
| 47 | c1.assign(c2.begin(), c2.end()); |
| 48 | assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size()); |
| 49 | assert(c1 == c2); |
| 50 | LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c1)); |
| 51 | LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c2)); |
| 52 | } |
| 53 | |
| 54 | template <class C> |
| 55 | void testN(int start, int N, int M) { |
| 56 | C c1 = make<C>(N, start); |
| 57 | C c2 = make<C>(M); |
| 58 | test(c1, c2); |
| 59 | } |
| 60 | |
| 61 | template <class C> |
| 62 | void testI(C& c1, const C& c2) { |
| 63 | typedef typename C::const_iterator CI; |
| 64 | typedef cpp17_input_iterator<CI> ICI; |
| 65 | c1.assign(ICI(c2.begin()), ICI(c2.end())); |
| 66 | assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size()); |
| 67 | assert(c1 == c2); |
| 68 | LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c1)); |
| 69 | LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c2)); |
| 70 | } |
| 71 | |
| 72 | template <class C> |
| 73 | void testNI(int start, int N, int M) { |
| 74 | C c1 = make<C>(N, start); |
| 75 | C c2 = make<C>(M); |
| 76 | testI(c1, c2); |
| 77 | } |
| 78 | |
| 79 | void basic_test() { |
| 80 | { |
| 81 | int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049}; |
| 82 | const int N = sizeof(rng) / sizeof(rng[0]); |
| 83 | for (int i = 0; i < N; ++i) |
| 84 | for (int j = 0; j < N; ++j) |
| 85 | for (int k = 0; k < N; ++k) |
| 86 | testN<std::deque<int> >(start: rng[i], N: rng[j], M: rng[k]); |
| 87 | testNI<std::deque<int> >(start: 1500, N: 2000, M: 1000); |
| 88 | } |
| 89 | #if TEST_STD_VER >= 11 |
| 90 | { |
| 91 | int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049}; |
| 92 | const int N = sizeof(rng) / sizeof(rng[0]); |
| 93 | for (int i = 0; i < N; ++i) |
| 94 | for (int j = 0; j < N; ++j) |
| 95 | for (int k = 0; k < N; ++k) |
| 96 | testN<std::deque<int, min_allocator<int>> >(rng[i], rng[j], rng[k]); |
| 97 | testNI<std::deque<int, min_allocator<int>> >(1500, 2000, 1000); |
| 98 | } |
| 99 | { |
| 100 | int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049}; |
| 101 | const int N = sizeof(rng) / sizeof(rng[0]); |
| 102 | for (int i = 0; i < N; ++i) |
| 103 | for (int j = 0; j < N; ++j) |
| 104 | for (int k = 0; k < N; ++k) |
| 105 | testN<std::deque<int, safe_allocator<int>> >(rng[i], rng[j], rng[k]); |
| 106 | testNI<std::deque<int, safe_allocator<int>> >(1500, 2000, 1000); |
| 107 | } |
| 108 | #endif |
| 109 | } |
| 110 | |
| 111 | template <class It> |
| 112 | void test_emplacable_concept() { |
| 113 | #if TEST_STD_VER >= 11 |
| 114 | int arr1[] = {42}; |
| 115 | int arr2[] = {1, 101, 42}; |
| 116 | { |
| 117 | using T = EmplaceConstructibleMoveableAndAssignable<int>; |
| 118 | { |
| 119 | std::deque<T> v; |
| 120 | v.assign(It(arr1), It(std::end(arr1))); |
| 121 | assert(v[0].value == 42); |
| 122 | } |
| 123 | { |
| 124 | std::deque<T> v; |
| 125 | v.assign(It(arr2), It(std::end(arr2))); |
| 126 | assert(v[0].value == 1); |
| 127 | assert(v[1].value == 101); |
| 128 | assert(v[2].value == 42); |
| 129 | } |
| 130 | } |
| 131 | #endif |
| 132 | } |
| 133 | |
| 134 | void test_iterators() { |
| 135 | test_emplacable_concept<cpp17_input_iterator<int*> >(); |
| 136 | test_emplacable_concept<forward_iterator<int*> >(); |
| 137 | test_emplacable_concept<bidirectional_iterator<int*> >(); |
| 138 | test_emplacable_concept<random_access_iterator<int*> >(); |
| 139 | #if TEST_STD_VER > 17 |
| 140 | test_emplacable_concept<contiguous_iterator<int*> >(); |
| 141 | #endif |
| 142 | test_emplacable_concept<int*>(); |
| 143 | } |
| 144 | |
| 145 | int main(int, char**) { |
| 146 | basic_test(); |
| 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |