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 // void resize(size_type n);
12 
13 #include <deque>
14 #include <algorithm>
15 #include <iterator>
16 #include <cassert>
17 #include <cstddef>
18 
19 #include "test_macros.h"
20 #include "min_allocator.h"
21 
22 template <class C>
23 C
make(int size,int start=0)24 make(int size, int start = 0 )
25 {
26     const int b = 4096 / sizeof(int);
27     int init = 0;
28     if (start > 0)
29     {
30         init = (start+1) / b + ((start+1) % b != 0);
31         init *= b;
32         --init;
33     }
34     C c(init, 0);
35     for (int i = 0; i < init-start; ++i)
36         c.pop_back();
37     for (int i = 0; i < size; ++i)
38         c.push_back(i);
39     for (int i = 0; i < start; ++i)
40         c.pop_front();
41     return c;
42 }
43 
44 template <class C>
45 void
test(C & c1,int size)46 test(C& c1, int size)
47 {
48     typedef typename C::const_iterator CI;
49     typename C::size_type c1_osize = c1.size();
50     c1.resize(size);
51     assert(c1.size() == static_cast<std::size_t>(size));
52     assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());
53     CI i = c1.begin();
54     for (int j = 0; static_cast<std::size_t>(j) < std::min(c1_osize, c1.size()); ++j, ++i)
55         assert(*i == j);
56     for (std::size_t j = c1_osize; j < c1.size(); ++j, ++i)
57         assert(*i == 0);
58 }
59 
60 template <class C>
61 void
testN(int start,int N,int M)62 testN(int start, int N, int M)
63 {
64     C c1 = make<C>(N, start);
65     test(c1, M);
66 }
67 
main(int,char **)68 int main(int, char**)
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             for (int k = 0; k < N; ++k)
76                 testN<std::deque<int> >(rng[i], rng[j], rng[k]);
77     }
78 #if TEST_STD_VER >= 11
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             for (int k = 0; k < N; ++k)
85                 testN<std::deque<int, min_allocator<int>>>(rng[i], rng[j], rng[k]);
86     }
87 #endif
88 
89   return 0;
90 }
91