xref: /llvm-project/libcxx/test/std/containers/sequences/vector.bool/assign_iter_iter.pass.cpp (revision 4039a79de71bd969ef5bf944fd9f46430338ff7e)
1*4039a79dSPeng Liu //===----------------------------------------------------------------------===//
2*4039a79dSPeng Liu //
3*4039a79dSPeng Liu // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*4039a79dSPeng Liu // See https://llvm.org/LICENSE.txt for license information.
5*4039a79dSPeng Liu // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*4039a79dSPeng Liu //
7*4039a79dSPeng Liu //===----------------------------------------------------------------------===//
8*4039a79dSPeng Liu 
9*4039a79dSPeng Liu // <vector>
10*4039a79dSPeng Liu 
11*4039a79dSPeng Liu // template <class InputIt>
12*4039a79dSPeng Liu // constexpr void assign(InputIt first, InputIt last);
13*4039a79dSPeng Liu 
14*4039a79dSPeng Liu #include <vector>
15*4039a79dSPeng Liu #include <cassert>
16*4039a79dSPeng Liu #include "test_macros.h"
17*4039a79dSPeng Liu #include "test_iterators.h"
18*4039a79dSPeng Liu 
19*4039a79dSPeng Liu TEST_CONSTEXPR_CXX20 bool tests() {
20*4039a79dSPeng Liu   {   // Test with various cases where assign may or may not trigger reallocations for forward_iterator
21*4039a79dSPeng Liu     { // Reallocation happens
22*4039a79dSPeng Liu       std::vector<bool> in(128, true);
23*4039a79dSPeng Liu       std::vector<bool> v(5, false);
24*4039a79dSPeng Liu       assert(v.capacity() < in.size());
25*4039a79dSPeng Liu       using It = forward_iterator<std::vector<bool>::iterator>;
26*4039a79dSPeng Liu       v.assign(It(in.begin()), It(in.end()));
27*4039a79dSPeng Liu       assert(v == in);
28*4039a79dSPeng Liu     }
29*4039a79dSPeng Liu     { // No reallocation: fit within current size
30*4039a79dSPeng Liu       bool in[]     = {false, true, false, true, true};
31*4039a79dSPeng Liu       std::size_t N = sizeof(in) / sizeof(in[0]);
32*4039a79dSPeng Liu       std::vector<bool> v(2 * N, false);
33*4039a79dSPeng Liu       using It = forward_iterator<bool*>;
34*4039a79dSPeng Liu       v.assign(It(in), It(in + N));
35*4039a79dSPeng Liu       assert(v.size() == N);
36*4039a79dSPeng Liu       for (std::size_t i = 0; i < N; ++i)
37*4039a79dSPeng Liu         assert(v[i] == in[i]);
38*4039a79dSPeng Liu     }
39*4039a79dSPeng Liu     { // No reallocation: fit within spare space
40*4039a79dSPeng Liu       bool in[]     = {false, true, false, true, true};
41*4039a79dSPeng Liu       std::size_t N = sizeof(in) / sizeof(in[0]);
42*4039a79dSPeng Liu       std::vector<bool> v(N / 2, false);
43*4039a79dSPeng Liu       v.reserve(N * 2);
44*4039a79dSPeng Liu       using It = forward_iterator<bool*>;
45*4039a79dSPeng Liu       v.assign(It(in), It(in + N));
46*4039a79dSPeng Liu       assert(v.size() == N);
47*4039a79dSPeng Liu       for (std::size_t i = 0; i < N; ++i)
48*4039a79dSPeng Liu         assert(v[i] == in[i]);
49*4039a79dSPeng Liu     }
50*4039a79dSPeng Liu   }
51*4039a79dSPeng Liu 
52*4039a79dSPeng Liu   {   // Test with various cases where assign may or may not trigger reallocations for input_iterator
53*4039a79dSPeng Liu     { // Reallocation happens
54*4039a79dSPeng Liu       std::vector<bool> in(128, true);
55*4039a79dSPeng Liu       std::vector<bool> v(5, false);
56*4039a79dSPeng Liu       assert(v.capacity() < in.size());
57*4039a79dSPeng Liu       using It = cpp17_input_iterator<std::vector<bool>::iterator>;
58*4039a79dSPeng Liu       v.assign(It(in.begin()), It(in.end()));
59*4039a79dSPeng Liu       assert(v == in);
60*4039a79dSPeng Liu     }
61*4039a79dSPeng Liu     { // No reallocation: fit within current size
62*4039a79dSPeng Liu       bool in[]     = {false, true, false, true, true};
63*4039a79dSPeng Liu       std::size_t N = sizeof(in) / sizeof(in[0]);
64*4039a79dSPeng Liu       std::vector<bool> v(2 * N, false);
65*4039a79dSPeng Liu       using It = cpp17_input_iterator<bool*>;
66*4039a79dSPeng Liu       v.assign(It(in), It(in + N));
67*4039a79dSPeng Liu       assert(v.size() == N);
68*4039a79dSPeng Liu       for (std::size_t i = 0; i < N; ++i)
69*4039a79dSPeng Liu         assert(v[i] == in[i]);
70*4039a79dSPeng Liu     }
71*4039a79dSPeng Liu     { // No reallocation: fit within spare space
72*4039a79dSPeng Liu       bool in[]     = {false, true, false, true, true};
73*4039a79dSPeng Liu       std::size_t N = sizeof(in) / sizeof(in[0]);
74*4039a79dSPeng Liu       std::vector<bool> v(N / 2, false);
75*4039a79dSPeng Liu       v.reserve(N * 2);
76*4039a79dSPeng Liu       using It = cpp17_input_iterator<bool*>;
77*4039a79dSPeng Liu       v.assign(It(in), It(in + N));
78*4039a79dSPeng Liu       assert(v.size() == N);
79*4039a79dSPeng Liu       for (std::size_t i = 0; i < N; ++i)
80*4039a79dSPeng Liu         assert(v[i] == in[i]);
81*4039a79dSPeng Liu     }
82*4039a79dSPeng Liu   }
83*4039a79dSPeng Liu 
84*4039a79dSPeng Liu   return true;
85*4039a79dSPeng Liu }
86*4039a79dSPeng Liu 
87*4039a79dSPeng Liu int main(int, char**) {
88*4039a79dSPeng Liu   tests();
89*4039a79dSPeng Liu #if TEST_STD_VER > 17
90*4039a79dSPeng Liu   static_assert(tests());
91*4039a79dSPeng Liu #endif
92*4039a79dSPeng Liu   return 0;
93*4039a79dSPeng Liu }
94