1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // UNSUPPORTED: c++98, c++03
11
12 // <deque>
13
14 // template <class... Args> reference emplace_back(Args&&... args);
15 // return type is 'reference' in C++17; 'void' before
16
17 #include <deque>
18 #include <cstddef>
19 #include <cassert>
20
21 #include "test_macros.h"
22 #include "../../../Emplaceable.h"
23 #include "min_allocator.h"
24 #include "test_allocator.h"
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);
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(Emplaceable());
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)50 test(C& c1)
51 {
52 typedef typename C::iterator I;
53 std::size_t c1_osize = c1.size();
54 #if TEST_STD_VER > 14
55 typedef typename C::reference Ref;
56 Ref ref = c1.emplace_back(Emplaceable(1, 2.5));
57 #else
58 c1.emplace_back(Emplaceable(1, 2.5));
59 #endif
60 assert(c1.size() == c1_osize + 1);
61 assert(distance(c1.begin(), c1.end())
62 == static_cast<std::ptrdiff_t>(c1.size()));
63 I i = c1.end();
64 assert(*--i == Emplaceable(1, 2.5));
65 #if TEST_STD_VER > 14
66 assert(&(*i) == &ref);
67 #endif
68 }
69
70 template <class C>
71 void
testN(int start,int N)72 testN(int start, int N)
73 {
74 C c1 = make<C>(N, start);
75 test(c1);
76 }
77
main()78 int main()
79 {
80 {
81 int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
82 const int N = sizeof(rng)/sizeof(rng[0]);
83 for (int i = 0; i < N; ++i)
84 for (int j = 0; j < N; ++j)
85 testN<std::deque<Emplaceable> >(rng[i], rng[j]);
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 testN<std::deque<Emplaceable, min_allocator<Emplaceable>> >(rng[i], rng[j]);
93 }
94 {
95 std::deque<Tag_X, TaggingAllocator<Tag_X>> c;
96 c.emplace_back();
97 assert(c.size() == 1);
98 c.emplace_back(1, 2, 3);
99 assert(c.size() == 2);
100 c.emplace_front();
101 assert(c.size() == 3);
102 c.emplace_front(1, 2, 3);
103 assert(c.size() == 4);
104 }
105 }
106