|
| 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 | +// <vector> |
| 10 | + |
| 11 | +// void assign(_InputIterator __first, _InputIterator __last); |
| 12 | +// void assign(_ForwardIterator __first, _ForwardIterator __last); |
| 13 | + |
| 14 | +#include <vector> |
| 15 | +#include <cassert> |
| 16 | +#include "test_macros.h" |
| 17 | +#include "test_iterators.h" |
| 18 | + |
| 19 | +TEST_CONSTEXPR_CXX20 bool tests() { |
| 20 | + { // Test with various cases where assign may or may not trigger reallocations for forward_iterator |
| 21 | + { // Reallocation happens |
| 22 | + std::vector<bool> in(128, true); |
| 23 | + std::vector<bool> v(5, false); |
| 24 | + assert(v.capacity() < in.size()); |
| 25 | + using It = forward_iterator<std::vector<bool>::iterator>; |
| 26 | + v.assign(It(in.begin()), It(in.end())); |
| 27 | + assert(v == in); |
| 28 | + } |
| 29 | + { // No reallocation: fit wintin current size |
| 30 | + bool in[] = {false, true, false, true, true}; |
| 31 | + TEST_CONSTEXPR std::size_t N = sizeof(in) / sizeof(in[0]); |
| 32 | + std::vector<bool> v(2 * N, false); |
| 33 | + using It = forward_iterator<bool*>; |
| 34 | + v.assign(It(in), It(in + N)); |
| 35 | + assert(v.size() == N); |
| 36 | + for (std::size_t i = 0; i < N; ++i) |
| 37 | + assert(v[i] == in[i]); |
| 38 | + } |
| 39 | + { // No reallocation: fit wintin spare space |
| 40 | + bool in[] = {false, true, false, true, true}; |
| 41 | + TEST_CONSTEXPR std::size_t N = sizeof(in) / sizeof(in[0]); |
| 42 | + std::vector<bool> v(N / 2, false); |
| 43 | + v.reserve(N * 2); |
| 44 | + using It = forward_iterator<bool*>; |
| 45 | + v.assign(It(in), It(in + N)); |
| 46 | + assert(v.size() == N); |
| 47 | + for (std::size_t i = 0; i < N; ++i) |
| 48 | + assert(v[i] == in[i]); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + { // Test with various cases where assign may or may not trigger reallocations for input_iterator |
| 53 | + { // Reallocation happens |
| 54 | + std::vector<bool> in(128, true); |
| 55 | + std::vector<bool> v(5, false); |
| 56 | + assert(v.capacity() < in.size()); |
| 57 | + using It = cpp17_input_iterator<std::vector<bool>::iterator>; |
| 58 | + v.assign(It(in.begin()), It(in.end())); |
| 59 | + assert(v == in); |
| 60 | + } |
| 61 | + { // No reallocation: fit wintin current size |
| 62 | + bool in[] = {false, true, false, true, true}; |
| 63 | + TEST_CONSTEXPR std::size_t N = sizeof(in) / sizeof(in[0]); |
| 64 | + std::vector<bool> v(2 * N, false); |
| 65 | + using It = cpp17_input_iterator<bool*>; |
| 66 | + v.assign(It(in), It(in + N)); |
| 67 | + assert(v.size() == N); |
| 68 | + for (std::size_t i = 0; i < N; ++i) |
| 69 | + assert(v[i] == in[i]); |
| 70 | + } |
| 71 | + { // No reallocation: fit wintin spare space |
| 72 | + bool in[] = {false, true, false, true, true}; |
| 73 | + TEST_CONSTEXPR std::size_t N = sizeof(in) / sizeof(in[0]); |
| 74 | + std::vector<bool> v(N / 2, false); |
| 75 | + v.reserve(N * 2); |
| 76 | + using It = cpp17_input_iterator<bool*>; |
| 77 | + v.assign(It(in), It(in + N)); |
| 78 | + assert(v.size() == N); |
| 79 | + for (std::size_t i = 0; i < N; ++i) |
| 80 | + assert(v[i] == in[i]); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return true; |
| 85 | +} |
| 86 | + |
| 87 | +int main(int, char**) { |
| 88 | + tests(); |
| 89 | +#if TEST_STD_VER > 17 |
| 90 | + static_assert(tests()); |
| 91 | +#endif |
| 92 | + return 0; |
| 93 | +} |
0 commit comments