xref: /llvm-project/libcxx/test/std/containers/sequences/deque/deque.capacity/resize_size_value.pass.cpp (revision 10ec9276d40024c23a481e6671dad1521151dd85)
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, const value_type& v);
12 
13 #include "asan_testing.h"
14 #include <deque>
15 #include <algorithm>
16 #include <iterator>
17 #include <cassert>
18 #include <cstddef>
19 
20 #include "test_macros.h"
21 #include "min_allocator.h"
22 
23 template <class C>
24 C
make(int size,int start=0)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, 0);
36     for (int i = 0; i < init-start; ++i)
37         c.pop_back();
38     LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c));
39     for (int i = 0; i < size; ++i)
40         c.push_back(i);
41     LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c));
42     for (int i = 0; i < start; ++i)
43         c.pop_front();
44     LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c));
45     return c;
46 }
47 
48 template <class C>
49 void
test(C & c1,int size,int x)50 test(C& c1, int size, int x)
51 {
52     typedef typename C::const_iterator CI;
53     typename C::size_type c1_osize = c1.size();
54     c1.resize(size, x);
55     assert(c1.size() == static_cast<std::size_t>(size));
56     assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());
57     LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c1));
58     CI i = c1.begin();
59     for (int j = 0; static_cast<std::size_t>(j) < std::min(c1_osize, c1.size()); ++j, ++i)
60         assert(*i == j);
61     for (std::size_t j = c1_osize; j < c1.size(); ++j, ++i)
62         assert(*i == x);
63 }
64 
65 template <class C>
66 void
testN(int start,int N,int M)67 testN(int start, int N, int M)
68 {
69     C c1 = make<C>(N, start);
70     test(c1, M, -10);
71 }
72 
main(int,char **)73 int main(int, char**)
74 {
75     {
76     int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
77     const int N = sizeof(rng)/sizeof(rng[0]);
78     for (int i = 0; i < N; ++i)
79         for (int j = 0; j < N; ++j)
80             for (int k = 0; k < N; ++k)
81                 testN<std::deque<int> >(rng[i], rng[j], rng[k]);
82     }
83 #if TEST_STD_VER >= 11
84     {
85     int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
86     const int N = sizeof(rng)/sizeof(rng[0]);
87     for (int i = 0; i < N; ++i)
88         for (int j = 0; j < N; ++j)
89             for (int k = 0; k < N; ++k)
90                 testN<std::deque<int, min_allocator<int>>>(rng[i], rng[j], rng[k]);
91     }
92     {
93     int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
94     const int N = sizeof(rng)/sizeof(rng[0]);
95     for (int i = 0; i < N; ++i)
96         for (int j = 0; j < N; ++j)
97             for (int k = 0; k < N; ++k)
98                 testN<std::deque<int, safe_allocator<int>>>(rng[i], rng[j], rng[k]);
99     }
100 #endif
101 
102   return 0;
103 }
104