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
20TEST_CONSTEXPR_CXX20 bool tests() {
21 {
22 std::vector<int> v(100);
23 v.push_back(x: 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
74int main(int, char**)
75{
76 tests();
77#if TEST_STD_VER > 17
78 static_assert(tests());
79#endif
80 return 0;
81}
82

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