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