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 push_front(const value_type& v);
12
13#include "asan_testing.h"
14#include <deque>
15#include <cassert>
16#include <cstddef>
17
18#include "test_macros.h"
19#include "min_allocator.h"
20
21template <class C>
22C make(int size, int start = 0) {
23 const int b = 4096 / sizeof(int);
24 int init = 0;
25 if (start > 0) {
26 init = (start + 1) / b + ((start + 1) % b != 0);
27 init *= b;
28 --init;
29 }
30 C c(init, 0);
31 for (int i = 0; i < init - start; ++i)
32 c.pop_back();
33 for (int i = 0; i < size; ++i)
34 c.push_back(i);
35 for (int i = 0; i < start; ++i)
36 c.pop_front();
37 return c;
38}
39
40template <class C>
41void test(C& c1, int x) {
42 typedef typename C::iterator I;
43 std::size_t c1_osize = c1.size();
44 c1.push_front(x);
45 assert(c1.size() == c1_osize + 1);
46 assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());
47 I i = c1.begin();
48 assert(*i == x);
49 LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c1));
50 ++i;
51 for (int j = 0; static_cast<std::size_t>(j) < c1_osize; ++j, ++i)
52 assert(*i == j);
53}
54
55template <class C>
56void testN(int start, int N) {
57 C c1 = make<C>(N, start);
58 test(c1, -10);
59}
60
61int main(int, char**) {
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.modifiers/push_front.pass.cpp