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 
17 #include "test_macros.h"
18 #include "min_allocator.h"
19 #include "test_allocator.h"
20 
21 int main(int, char**)
22 {
23     {
24         std::vector<bool> v;
25         v.reserve(10);
26         assert(v.capacity() >= 10);
27     }
28     {
29         std::vector<bool> 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     }
38 #if TEST_STD_VER >= 11
39     {
40         std::vector<bool, min_allocator<bool>> v;
41         v.reserve(10);
42         assert(v.capacity() >= 10);
43     }
44     {
45         std::vector<bool, explicit_allocator<bool>> v;
46         v.reserve(10);
47         assert(v.capacity() >= 10);
48     }
49     {
50         std::vector<bool, min_allocator<bool>> v(100);
51         assert(v.capacity() >= 100);
52         v.reserve(50);
53         assert(v.size() == 100);
54         assert(v.capacity() >= 100);
55         v.reserve(150);
56         assert(v.size() == 100);
57         assert(v.capacity() >= 150);
58     }
59 #endif
60 #ifndef TEST_HAS_NO_EXCEPTIONS
61     {
62         std::vector<bool, limited_allocator<bool, 10> > v;
63         v.reserve(5);
64         try {
65             // A typical implementation would allocate chunks of bits.
66             // In libc++ the chunk has the same size as the machine word. It is
67             // reasonable to assume that in practice no implementation would use
68             // 64 kB or larger chunks.
69             v.reserve(10 * 65536);
70             assert(false);
71         } catch (const std::length_error&) {
72             // no-op
73         }
74         assert(v.capacity() >= 5);
75     }
76 #endif
77 
78   return 0;
79 }
80