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

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