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 
16 #include <deque>
17 #include <cstddef>
18 #include <cassert>
19 
20 #include "test_macros.h"
21 #include "../../../Emplaceable.h"
22 #include "min_allocator.h"
23 #include "test_allocator.h"
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);
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(Emplaceable());
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)
50 {
51     typedef typename C::iterator I;
52     typedef typename C::reference Ref;
53     std::size_t c1_osize = c1.size();
54     Ref ref = c1.emplace_back(Emplaceable(1, 2.5));
55     assert(c1.size() == c1_osize + 1);
56     assert(distance(c1.begin(), c1.end())
57                == static_cast<std::ptrdiff_t>(c1.size()));
58     I i = c1.end();
59     assert(*--i == Emplaceable(1, 2.5));
60     assert(&(*i) == &ref);
61 }
62 
63 template <class C>
64 void
65 testN(int start, int N)
66 {
67     C c1 = make<C>(N, start);
68     test(c1);
69 }
70 
71 int main()
72 {
73     {
74     int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
75     const int N = sizeof(rng)/sizeof(rng[0]);
76     for (int i = 0; i < N; ++i)
77         for (int j = 0; j < N; ++j)
78             testN<std::deque<Emplaceable> >(rng[i], rng[j]);
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, min_allocator<Emplaceable>> >(rng[i], rng[j]);
86     }
87     {
88         std::deque<Tag_X, TaggingAllocator<Tag_X>> c;
89         c.emplace_back();
90         assert(c.size() == 1);
91         c.emplace_back(1, 2, 3);
92         assert(c.size() == 2);
93         c.emplace_front();
94         assert(c.size() == 3);
95         c.emplace_front(1, 2, 3);
96         assert(c.size() == 4);
97     }
98 }
99