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 int main(int, char**) 22 { 23 { 24 std::vector<int> v; 25 v.reserve(10); 26 assert(v.capacity() >= 10); 27 assert(is_contiguous_container_asan_correct(v)); 28 } 29 { 30 std::vector<int> 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 assert(is_contiguous_container_asan_correct(v)); 39 } 40 { 41 // Add 1 for implementations that dynamically allocate a container proxy. 42 std::vector<int, limited_allocator<int, 250 + 1> > v(100); 43 assert(v.capacity() == 100); 44 v.reserve(50); 45 assert(v.size() == 100); 46 assert(v.capacity() == 100); 47 v.reserve(150); 48 assert(v.size() == 100); 49 assert(v.capacity() == 150); 50 assert(is_contiguous_container_asan_correct(v)); 51 } 52 #ifndef TEST_HAS_NO_EXCEPTIONS 53 { 54 std::vector<int> v; 55 size_t sz = v.max_size() + 1; 56 57 try { 58 v.reserve(sz); 59 assert(false); 60 } catch (const std::length_error&) { 61 assert(v.size() == 0); 62 assert(v.capacity() == 0); 63 } 64 } 65 { 66 std::vector<int> v(10, 42); 67 int* previous_data = v.data(); 68 size_t previous_capacity = v.capacity(); 69 size_t sz = v.max_size() + 1; 70 71 try { 72 v.reserve(sz); 73 assert(false); 74 } catch (std::length_error&) { 75 assert(v.size() == 10); 76 assert(v.capacity() == previous_capacity); 77 assert(v.data() == previous_data); 78 79 for (int i = 0; i < 10; ++i) { 80 assert(v[i] == 42); 81 } 82 } 83 } 84 #endif 85 #if TEST_STD_VER >= 11 86 { 87 std::vector<int, min_allocator<int>> v; 88 v.reserve(10); 89 assert(v.capacity() >= 10); 90 assert(is_contiguous_container_asan_correct(v)); 91 } 92 { 93 std::vector<int, min_allocator<int>> v(100); 94 assert(v.capacity() == 100); 95 v.reserve(50); 96 assert(v.size() == 100); 97 assert(v.capacity() == 100); 98 v.reserve(150); 99 assert(v.size() == 100); 100 assert(v.capacity() == 150); 101 assert(is_contiguous_container_asan_correct(v)); 102 } 103 #endif 104 #ifndef TEST_HAS_NO_EXCEPTIONS 105 { 106 std::vector<int, limited_allocator<int, 100> > v; 107 v.reserve(50); 108 assert(v.capacity() == 50); 109 assert(is_contiguous_container_asan_correct(v)); 110 try { 111 v.reserve(101); 112 assert(false); 113 } catch (const std::length_error&) { 114 // no-op 115 } 116 assert(v.capacity() == 50); 117 assert(is_contiguous_container_asan_correct(v)); 118 } 119 #endif 120 121 return 0; 122 } 123