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... Args> iterator emplace(const_iterator p, Args&&... args);
12
13 // UNSUPPORTED: c++03
14
15 #include <deque>
16 #include <cassert>
17 #include <cstddef>
18
19 #include "test_macros.h"
20 #include "../../../Emplaceable.h"
21 #include "min_allocator.h"
22
23
24 template <class C>
25 C
make(int size,int start=0)26 make(int size, int start = 0 )
27 {
28 const int b = 4096 / sizeof(int);
29 int init = 0;
30 if (start > 0)
31 {
32 init = (start+1) / b + ((start+1) % b != 0);
33 init *= b;
34 --init;
35 }
36 C c(init);
37 for (int i = 0; i < init-start; ++i)
38 c.pop_back();
39 for (int i = 0; i < size; ++i)
40 c.push_back(Emplaceable());
41 for (int i = 0; i < start; ++i)
42 c.pop_front();
43 return c;
44 }
45
46 template <class C>
47 void
test(int P,C & c1)48 test(int P, C& c1)
49 {
50 typedef typename C::const_iterator CI;
51 std::size_t c1_osize = c1.size();
52 CI i = c1.emplace(c1.begin() + P, Emplaceable(1, 2.5));
53 assert(i == c1.begin() + P);
54 assert(c1.size() == c1_osize + 1);
55 assert(static_cast<std::size_t>(distance(c1.begin(), c1.end())) == c1.size());
56 assert(*i == Emplaceable(1, 2.5));
57 }
58
59 template <class C>
60 void
testN(int start,int N)61 testN(int start, int N)
62 {
63 for (int i = 0; i <= 3; ++i)
64 {
65 if (0 <= i && i <= N)
66 {
67 C c1 = make<C>(N, start);
68 test(i, c1);
69 }
70 }
71 for (int i = N/2-1; i <= N/2+1; ++i)
72 {
73 if (0 <= i && i <= N)
74 {
75 C c1 = make<C>(N, start);
76 test(i, c1);
77 }
78 }
79 for (int i = N - 3; i <= N; ++i)
80 {
81 if (0 <= i && i <= N)
82 {
83 C c1 = make<C>(N, start);
84 test(i, c1);
85 }
86 }
87 }
88
89
main(int,char **)90 int main(int, char**)
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 testN<std::deque<Emplaceable> >(rng[i], rng[j]);
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<Emplaceable, min_allocator<Emplaceable>> >(rng[i], rng[j]);
105 }
106
107 return 0;
108 }
109