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