• Home
  • 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 // <deque>
10 
11 // iterator insert (const_iterator p, value_type&& v);
12 
13 // UNSUPPORTED: c++03
14 
15 #include <deque>
16 #include <cassert>
17 #include <cstddef>
18 
19 #include "test_macros.h"
20 #include "MoveOnly.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(MoveOnly(i));
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,int x)48 test(int P, C& c1, int x)
49 {
50     typedef typename C::const_iterator CI;
51     std::size_t c1_osize = c1.size();
52     CI i = c1.insert(c1.begin() + P, MoveOnly(x));
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     i = c1.begin();
57     for (int j = 0; j < P; ++j, ++i)
58         assert(*i == MoveOnly(j));
59     assert(*i == MoveOnly(x));
60     ++i;
61     for (int j = P; static_cast<std::size_t>(j) < c1_osize; ++j, ++i)
62         assert(*i == MoveOnly(j));
63 }
64 
65 template <class C>
66 void
testN(int start,int N)67 testN(int start, int N)
68 {
69     for (int i = 0; i <= 3; ++i)
70     {
71         if (0 <= i && i <= N)
72         {
73             C c1 = make<C>(N, start);
74             test(i, c1, -10);
75         }
76     }
77     for (int i = N/2-1; i <= N/2+1; ++i)
78     {
79         if (0 <= i && i <= N)
80         {
81             C c1 = make<C>(N, start);
82             test(i, c1, -10);
83         }
84     }
85     for (int i = N - 3; i <= N; ++i)
86     {
87         if (0 <= i && i <= N)
88         {
89             C c1 = make<C>(N, start);
90             test(i, c1, -10);
91         }
92     }
93 }
94 
main(int,char **)95 int main(int, char**)
96 {
97     {
98     int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
99     const int N = sizeof(rng)/sizeof(rng[0]);
100     for (int i = 0; i < N; ++i)
101         for (int j = 0; j < N; ++j)
102             testN<std::deque<MoveOnly> >(rng[i], rng[j]);
103     }
104     {
105     int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
106     const int N = sizeof(rng)/sizeof(rng[0]);
107     for (int i = 0; i < N; ++i)
108         for (int j = 0; j < N; ++j)
109             testN<std::deque<MoveOnly, min_allocator<MoveOnly>> >(rng[i], rng[j]);
110     }
111 
112   return 0;
113 }
114