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 <cassert>
18 
19 #include "../../../Emplaceable.h"
20 #include "min_allocator.h"
21 #include "test_allocator.h"
22 
23 template <class C>
24 C
25 make(int size, int start = 0 )
26 {
27     const int b = 4096 / sizeof(int);
28     int init = 0;
29     if (start > 0)
30     {
31         init = (start+1) / b + ((start+1) % b != 0);
32         init *= b;
33         --init;
34     }
35     C c(init);
36     for (int i = 0; i < init-start; ++i)
37         c.pop_back();
38     for (int i = 0; i < size; ++i)
39         c.push_back(Emplaceable());
40     for (int i = 0; i < start; ++i)
41         c.pop_front();
42     return c;
43 }
44 
45 template <class C>
46 void
47 test(C& c1)
48 {
49     typedef typename C::iterator I;
50     typedef typename C::reference Ref;
51     std::size_t c1_osize = c1.size();
52     Ref ref = c1.emplace_back(Emplaceable(1, 2.5));
53     assert(c1.size() == c1_osize + 1);
54     assert(distance(c1.begin(), c1.end()) == c1.size());
55     I i = c1.end();
56     assert(*--i == Emplaceable(1, 2.5));
57     assert(&(*i) == &ref);
58 }
59 
60 template <class C>
61 void
62 testN(int start, int N)
63 {
64     C c1 = make<C>(N, start);
65     test(c1);
66 }
67 
68 int main()
69 {
70     {
71     int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
72     const int N = sizeof(rng)/sizeof(rng[0]);
73     for (int i = 0; i < N; ++i)
74         for (int j = 0; j < N; ++j)
75             testN<std::deque<Emplaceable> >(rng[i], rng[j]);
76     }
77     {
78     int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
79     const int N = sizeof(rng)/sizeof(rng[0]);
80     for (int i = 0; i < N; ++i)
81         for (int j = 0; j < N; ++j)
82             testN<std::deque<Emplaceable, min_allocator<Emplaceable>> >(rng[i], rng[j]);
83     }
84     {
85         std::deque<Tag_X, TaggingAllocator<Tag_X>> c;
86         c.emplace_back();
87         assert(c.size() == 1);
88         c.emplace_back(1, 2, 3);
89         assert(c.size() == 2);
90         c.emplace_front();
91         assert(c.size() == 3);
92         c.emplace_front(1, 2, 3);
93         assert(c.size() == 4);
94     }
95 }
96