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// <deque>
10
11// void shrink_to_fit();
12
13#include "asan_testing.h"
14#include <deque>
15#include <cassert>
16
17#include "test_macros.h"
18#include "min_allocator.h"
19
20template <class C>
21C make(int size, int start = 0) {
22 const int b = 4096 / sizeof(int);
23 int init = 0;
24 if (start > 0) {
25 init = (start + 1) / b + ((start + 1) % b != 0);
26 init *= b;
27 --init;
28 }
29 C c(init, 0);
30 for (int i = 0; i < init - start; ++i)
31 c.pop_back();
32 for (int i = 0; i < size; ++i)
33 c.push_back(i);
34 for (int i = 0; i < start; ++i)
35 c.pop_front();
36 return c;
37}
38
39template <class C>
40void test(C& c1) {
41 C s = c1;
42 c1.shrink_to_fit();
43 assert(c1 == s);
44 LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c1));
45}
46
47template <class C>
48void testN(int start, int N) {
49 C c1 = make<C>(N, start);
50 test(c1);
51}
52
53int main(int, char**) {
54 {
55 int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
56 const int N = sizeof(rng) / sizeof(rng[0]);
57 for (int i = 0; i < N; ++i)
58 for (int j = 0; j < N; ++j)
59 testN<std::deque<int> >(start: rng[i], N: rng[j]);
60 }
61#if TEST_STD_VER >= 11
62 {
63 int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
64 const int N = sizeof(rng) / sizeof(rng[0]);
65 for (int i = 0; i < N; ++i)
66 for (int j = 0; j < N; ++j)
67 testN<std::deque<int, min_allocator<int>> >(rng[i], rng[j]);
68 }
69 {
70 int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
71 const int N = sizeof(rng) / sizeof(rng[0]);
72 for (int i = 0; i < N; ++i)
73 for (int j = 0; j < N; ++j)
74 testN<std::deque<int, safe_allocator<int>> >(rng[i], rng[j]);
75 }
76#endif
77
78 return 0;
79}
80

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