• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // UNSUPPORTED: c++03
10 
11 // <deque>
12 
13 // template <class... Args> reference emplace_back(Args&&... args);
14 // return type is 'reference' in C++17; 'void' before
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
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(Emplaceable());
42     for (int i = 0; i < start; ++i)
43         c.pop_front();
44     return c;
45 }
46 
47 template <class C>
48 void
test(C & c1)49 test(C& c1)
50 {
51     typedef typename C::iterator I;
52     std::size_t c1_osize = c1.size();
53 #if TEST_STD_VER > 14
54     typedef typename C::reference Ref;
55     Ref ref = c1.emplace_back(Emplaceable(1, 2.5));
56 #else
57               c1.emplace_back(Emplaceable(1, 2.5));
58 #endif
59     assert(c1.size() == c1_osize + 1);
60     assert(distance(c1.begin(), c1.end())
61                == static_cast<std::ptrdiff_t>(c1.size()));
62     I i = c1.end();
63     assert(*--i == Emplaceable(1, 2.5));
64 #if TEST_STD_VER > 14
65     assert(&(*i) == &ref);
66 #endif
67 }
68 
69 template <class C>
70 void
testN(int start,int N)71 testN(int start, int N)
72 {
73     C c1 = make<C>(N, start);
74     test(c1);
75 }
76 
main(int,char **)77 int main(int, char**)
78 {
79     {
80     int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
81     const int N = sizeof(rng)/sizeof(rng[0]);
82     for (int i = 0; i < N; ++i)
83         for (int j = 0; j < N; ++j)
84             testN<std::deque<Emplaceable> >(rng[i], rng[j]);
85     }
86     {
87     int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
88     const int N = sizeof(rng)/sizeof(rng[0]);
89     for (int i = 0; i < N; ++i)
90         for (int j = 0; j < N; ++j)
91             testN<std::deque<Emplaceable, min_allocator<Emplaceable>> >(rng[i], rng[j]);
92     }
93     {
94         std::deque<Tag_X, TaggingAllocator<Tag_X>> c;
95         c.emplace_back();
96         assert(c.size() == 1);
97         c.emplace_back(1, 2, 3);
98         assert(c.size() == 2);
99         c.emplace_front();
100         assert(c.size() == 3);
101         c.emplace_front(1, 2, 3);
102         assert(c.size() == 4);
103     }
104 
105   return 0;
106 }
107