Debian Codegraph @

libcxx/test/std/containers/sequences/vector/vector.capacity/shrink_to_fit.pass.cpp

raw
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 shrink_to_fit();
12
13 #include <vector>
14 #include <cassert>
15 #include "test_macros.h"
16 #include "test_allocator.h"
17 #include "min_allocator.h"
18 #include "asan_testing.h"
19
20 TEST_CONSTEXPR_CXX20 bool tests() {
21 {
22 std::vector<int> v(100);
23 v.push_back(1);
24 assert(is_contiguous_container_asan_correct(v));
25 v.shrink_to_fit();
26 assert(v.capacity() == 101);
27 assert(v.size() == 101);
28 assert(is_contiguous_container_asan_correct(v));
29 }
30 {
31 std::vector<int, limited_allocator<int, 401> > v(100);
32 v.push_back(1);
33 assert(is_contiguous_container_asan_correct(v));
34 v.shrink_to_fit();
35 assert(v.capacity() == 101);
36 assert(v.size() == 101);
37 assert(is_contiguous_container_asan_correct(v));
38 }
39 #ifndef TEST_HAS_NO_EXCEPTIONS
40 if (!TEST_IS_CONSTANT_EVALUATED) {
41 std::vector<int, limited_allocator<int, 400> > v(100);
42 v.push_back(1);
43 assert(is_contiguous_container_asan_correct(v));
44 v.shrink_to_fit();
45 LIBCPP_ASSERT(v.capacity() == 200); // assumes libc++'s 2x growth factor
46 assert(v.size() == 101);
47 assert(is_contiguous_container_asan_correct(v));
48 }
49 #endif
50 #if TEST_STD_VER >= 11
51 {
52 std::vector<int, min_allocator<int>> v(100);
53 v.push_back(1);
54 assert(is_contiguous_container_asan_correct(v));
55 v.shrink_to_fit();
56 assert(v.capacity() == 101);
57 assert(v.size() == 101);
58 assert(is_contiguous_container_asan_correct(v));
59 }
60 {
61 std::vector<int, safe_allocator<int>> v(100);
62 v.push_back(1);
63 assert(is_contiguous_container_asan_correct(v));
64 v.shrink_to_fit();
65 assert(v.capacity() == 101);
66 assert(v.size() == 101);
67 assert(is_contiguous_container_asan_correct(v));
68 }
69 #endif
70
71 return true;
72 }
73
74 #if TEST_STD_VER >= 23
75 template <typename T>
76 struct increasing_allocator {
77 using value_type = T;
78 std::size_t min_elements = 1000;
79 increasing_allocator() = default;
80
81 template <typename U>
82 constexpr increasing_allocator(const increasing_allocator<U>& other) noexcept : min_elements(other.min_elements) {}
83
84 constexpr std::allocation_result<T*> allocate_at_least(std::size_t n) {
85 if (n < min_elements)
86 n = min_elements;
87 min_elements += 1000;
88 return std::allocator<T>{}.allocate_at_least(n);
89 }
90 constexpr T* allocate(std::size_t n) { return allocate_at_least(n).ptr; }
91 constexpr void deallocate(T* p, std::size_t n) noexcept { std::allocator<T>{}.deallocate(p, n); }
92 };
93
94 template <typename T, typename U>
95 bool operator==(increasing_allocator<T>, increasing_allocator<U>) {
96 return true;
97 }
98
99 // https://github.com/llvm/llvm-project/issues/95161
100 constexpr bool test_increasing_allocator() {
101 std::vector<int, increasing_allocator<int>> v;
102 v.push_back(1);
103 assert(is_contiguous_container_asan_correct(v));
104 std::size_t capacity = v.capacity();
105 v.shrink_to_fit();
106 assert(v.capacity() <= capacity);
107 assert(v.size() == 1);
108 assert(is_contiguous_container_asan_correct(v));
109
110 return true;
111 }
112 #endif // TEST_STD_VER >= 23
113
114 int main(int, char**)
115 {
116 tests();
117 #if TEST_STD_VER > 17
118 static_assert(tests());
119 #endif
120 #if TEST_STD_VER >= 23
121 test_increasing_allocator();
122 static_assert(test_increasing_allocator());
123 #endif
124
125 return 0;
126 }