xref: /llvm-project/libcxx/test/std/containers/sequences/deque/deque.modifiers/insert_rvalue.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 // iterator insert (const_iterator p, value_type&& v);
12 
13 // UNSUPPORTED: c++03
14 
15 #include "asan_testing.h"
16 #include <deque>
17 #include <cassert>
18 #include <cstddef>
19 
20 #include "test_macros.h"
21 #include "MoveOnly.h"
22 #include "min_allocator.h"
23 
24 
25 template <class C>
26 C
make(int size,int start=0)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);
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(MoveOnly(i));
42     for (int i = 0; i < start; ++i)
43         c.pop_front();
44     return c;
45 }
46 
47 template <class C>
48 void
test(int P,C & c1,int x)49 test(int P, C& c1, int x)
50 {
51     typedef typename C::const_iterator CI;
52     std::size_t c1_osize = c1.size();
53     CI i = c1.insert(c1.begin() + P, MoveOnly(x));
54     assert(i == c1.begin() + P);
55     assert(c1.size() == c1_osize + 1);
56     assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());
57     LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c1));
58     i = c1.begin();
59     for (int j = 0; j < P; ++j, (void) ++i)
60         assert(*i == MoveOnly(j));
61     assert(*i == MoveOnly(x));
62     ++i;
63     for (int j = P; static_cast<std::size_t>(j) < c1_osize; ++j, (void) ++i)
64         assert(*i == MoveOnly(j));
65 }
66 
67 template <class C>
68 void
testN(int start,int N)69 testN(int start, int N)
70 {
71     for (int i = 0; i <= 3; ++i)
72     {
73         if (0 <= i && i <= N)
74         {
75             C c1 = make<C>(N, start);
76             test(i, c1, -10);
77         }
78     }
79     for (int i = N/2-1; i <= N/2+1; ++i)
80     {
81         if (0 <= i && i <= N)
82         {
83             C c1 = make<C>(N, start);
84             test(i, c1, -10);
85         }
86     }
87     for (int i = N - 3; i <= N; ++i)
88     {
89         if (0 <= i && i <= N)
90         {
91             C c1 = make<C>(N, start);
92             test(i, c1, -10);
93         }
94     }
95 }
96 
main(int,char **)97 int main(int, char**)
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             testN<std::deque<MoveOnly> >(rng[i], rng[j]);
105     }
106     {
107     int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
108     const int N = sizeof(rng)/sizeof(rng[0]);
109     for (int i = 0; i < N; ++i)
110         for (int j = 0; j < N; ++j)
111             testN<std::deque<MoveOnly, min_allocator<MoveOnly>> >(rng[i], rng[j]);
112     }
113     {
114     int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
115     const int N = sizeof(rng)/sizeof(rng[0]);
116     for (int i = 0; i < N; ++i)
117         for (int j = 0; j < N; ++j)
118             testN<std::deque<MoveOnly, safe_allocator<MoveOnly>> >(rng[i], rng[j]);
119     }
120 
121   return 0;
122 }
123