xref: /llvm-project/libcxx/test/std/containers/sequences/vector.bool/reserve.pass.cpp (revision 98d3d5b5da66e3cf7807c23a0294280bb796466b)
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 // vector<bool>
11 
12 // void reserve(size_type n);
13 
14 #include <vector>
15 #include <cassert>
16 #include <stdexcept>
17 
18 #include "test_macros.h"
19 #include "min_allocator.h"
20 #include "test_allocator.h"
21 
tests()22 TEST_CONSTEXPR_CXX20 bool tests()
23 {
24     {
25         std::vector<bool> v;
26         v.reserve(10);
27         assert(v.capacity() >= 10);
28     }
29     {
30         std::vector<bool> v(100);
31         assert(v.capacity() >= 100);
32         v.reserve(50);
33         assert(v.size() == 100);
34         assert(v.capacity() >= 100);
35         v.reserve(150);
36         assert(v.size() == 100);
37         assert(v.capacity() >= 150);
38     }
39 #if TEST_STD_VER >= 11
40     {
41         std::vector<bool, min_allocator<bool>> v;
42         v.reserve(10);
43         assert(v.capacity() >= 10);
44     }
45     {
46         std::vector<bool, explicit_allocator<bool>> v;
47         v.reserve(10);
48         assert(v.capacity() >= 10);
49     }
50     {
51         std::vector<bool, min_allocator<bool>> v(100);
52         assert(v.capacity() >= 100);
53         v.reserve(50);
54         assert(v.size() == 100);
55         assert(v.capacity() >= 100);
56         v.reserve(150);
57         assert(v.size() == 100);
58         assert(v.capacity() >= 150);
59     }
60 #endif
61 #ifndef TEST_HAS_NO_EXCEPTIONS
62     if (!TEST_IS_CONSTANT_EVALUATED) {
63         std::vector<bool, limited_allocator<bool, 10> > v;
64         v.reserve(5);
65         try {
66             // A typical implementation would allocate chunks of bits.
67             // In libc++ the chunk has the same size as the machine word. It is
68             // reasonable to assume that in practice no implementation would use
69             // 64 kB or larger chunks.
70             v.reserve(10 * 65536);
71             assert(false);
72         } catch (const std::length_error&) {
73             // no-op
74         }
75         assert(v.capacity() >= 5);
76     }
77 #endif
78 
79     return true;
80 }
81 
main(int,char **)82 int main(int, char**)
83 {
84     tests();
85 #if TEST_STD_VER > 17
86     static_assert(tests());
87 #endif
88     return 0;
89 }
90