xref: /llvm-project/libcxx/test/std/containers/sequences/vector/vector.capacity/reserve.pass.cpp (revision 550d32f205202b73f21903b29df04fe2e89ae648)
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 // <vector>
10 
11 // void reserve(size_type n);
12 
13 #include <vector>
14 #include <cassert>
15 #include <stdexcept>
16 #include "test_macros.h"
17 #include "test_allocator.h"
18 #include "min_allocator.h"
19 #include "asan_testing.h"
20 
21 TEST_CONSTEXPR_CXX20 bool tests() {
22   {
23     std::vector<int> v;
24     v.reserve(10);
25     assert(v.capacity() >= 10);
26     assert(is_contiguous_container_asan_correct(v));
27   }
28   {
29     std::vector<int> v(100);
30     assert(v.capacity() == 100);
31     v.reserve(50);
32     assert(v.size() == 100);
33     assert(v.capacity() == 100);
34     v.reserve(150);
35     assert(v.size() == 100);
36     assert(v.capacity() == 150);
37     assert(is_contiguous_container_asan_correct(v));
38   }
39   {
40     // Add 1 for implementations that dynamically allocate a container proxy.
41     std::vector<int, limited_allocator<int, 250 + 1> > v(100);
42     assert(v.capacity() == 100);
43     v.reserve(50);
44     assert(v.size() == 100);
45     assert(v.capacity() == 100);
46     v.reserve(150);
47     assert(v.size() == 100);
48     assert(v.capacity() == 150);
49     assert(is_contiguous_container_asan_correct(v));
50   }
51 #if TEST_STD_VER >= 11
52   {
53     std::vector<int, min_allocator<int>> v;
54     v.reserve(10);
55     assert(v.capacity() >= 10);
56     assert(is_contiguous_container_asan_correct(v));
57   }
58   {
59     std::vector<int, min_allocator<int>> v(100);
60     assert(v.capacity() == 100);
61     v.reserve(50);
62     assert(v.size() == 100);
63     assert(v.capacity() == 100);
64     v.reserve(150);
65     assert(v.size() == 100);
66     assert(v.capacity() == 150);
67     assert(is_contiguous_container_asan_correct(v));
68   }
69   {
70     std::vector<int, safe_allocator<int>> v;
71     v.reserve(10);
72     assert(v.capacity() >= 10);
73     assert(is_contiguous_container_asan_correct(v));
74   }
75   {
76     std::vector<int, safe_allocator<int>> v(100);
77     assert(v.capacity() == 100);
78     v.reserve(50);
79     assert(v.size() == 100);
80     assert(v.capacity() == 100);
81     v.reserve(150);
82     assert(v.size() == 100);
83     assert(v.capacity() == 150);
84     assert(is_contiguous_container_asan_correct(v));
85   }
86 #endif
87 
88   return true;
89 }
90 
91 int main(int, char**) {
92   tests();
93 
94 #if TEST_STD_VER > 17
95   static_assert(tests());
96 #endif
97 
98   return 0;
99 }
100