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 push_back(const value_type& x);
12
13#include <vector>
14#include <cassert>
15#include <cstddef>
16#include "test_macros.h"
17#include "test_allocator.h"
18#include "min_allocator.h"
19#include "asan_testing.h"
20
21TEST_CONSTEXPR_CXX20 bool tests() {
22 {
23 std::vector<int> c;
24 c.push_back(x: 0);
25 assert(c.size() == 1);
26 assert(is_contiguous_container_asan_correct(c));
27 for (int j = 0; static_cast<std::size_t>(j) < c.size(); ++j)
28 assert(c[j] == j);
29 c.push_back(x: 1);
30 assert(c.size() == 2);
31 assert(is_contiguous_container_asan_correct(c));
32 for (int j = 0; static_cast<std::size_t>(j) < c.size(); ++j)
33 assert(c[j] == j);
34 c.push_back(x: 2);
35 assert(c.size() == 3);
36 assert(is_contiguous_container_asan_correct(c));
37 for (int j = 0; static_cast<std::size_t>(j) < c.size(); ++j)
38 assert(c[j] == j);
39 c.push_back(x: 3);
40 assert(c.size() == 4);
41 assert(is_contiguous_container_asan_correct(c));
42 for (int j = 0; static_cast<std::size_t>(j) < c.size(); ++j)
43 assert(c[j] == j);
44 c.push_back(x: 4);
45 assert(c.size() == 5);
46 assert(is_contiguous_container_asan_correct(c));
47 for (int j = 0; static_cast<std::size_t>(j) < c.size(); ++j)
48 assert(c[j] == j);
49 }
50 {
51 // libc++ needs 15 because it grows by 2x (1 + 2 + 4 + 8).
52 // Use 17 for implementations that dynamically allocate a container proxy
53 // and grow by 1.5x (1 for proxy + 1 + 2 + 3 + 4 + 6).
54 std::vector<int, limited_allocator<int, 17> > c;
55 c.push_back(0);
56 assert(c.size() == 1);
57 assert(is_contiguous_container_asan_correct(c));
58 for (int j = 0; static_cast<std::size_t>(j) < c.size(); ++j)
59 assert(c[j] == j);
60 c.push_back(1);
61 assert(c.size() == 2);
62 assert(is_contiguous_container_asan_correct(c));
63 for (int j = 0; static_cast<std::size_t>(j) < c.size(); ++j)
64 assert(c[j] == j);
65 c.push_back(2);
66 assert(c.size() == 3);
67 assert(is_contiguous_container_asan_correct(c));
68 for (int j = 0; static_cast<std::size_t>(j) < c.size(); ++j)
69 assert(c[j] == j);
70 c.push_back(3);
71 assert(c.size() == 4);
72 assert(is_contiguous_container_asan_correct(c));
73 for (int j = 0; static_cast<std::size_t>(j) < c.size(); ++j)
74 assert(c[j] == j);
75 c.push_back(4);
76 assert(c.size() == 5);
77 assert(is_contiguous_container_asan_correct(c));
78 for (int j = 0; static_cast<std::size_t>(j) < c.size(); ++j)
79 assert(c[j] == j);
80 }
81#if TEST_STD_VER >= 11
82 {
83 std::vector<int, min_allocator<int>> c;
84 c.push_back(0);
85 assert(c.size() == 1);
86 assert(is_contiguous_container_asan_correct(c));
87 for (int j = 0; static_cast<std::size_t>(j) < c.size(); ++j)
88 assert(c[j] == j);
89 c.push_back(1);
90 assert(c.size() == 2);
91 assert(is_contiguous_container_asan_correct(c));
92 for (int j = 0; static_cast<std::size_t>(j) < c.size(); ++j)
93 assert(c[j] == j);
94 c.push_back(2);
95 assert(c.size() == 3);
96 assert(is_contiguous_container_asan_correct(c));
97 for (int j = 0; static_cast<std::size_t>(j) < c.size(); ++j)
98 assert(c[j] == j);
99 c.push_back(3);
100 assert(c.size() == 4);
101 assert(is_contiguous_container_asan_correct(c));
102 for (int j = 0; static_cast<std::size_t>(j) < c.size(); ++j)
103 assert(c[j] == j);
104 c.push_back(4);
105 assert(c.size() == 5);
106 assert(is_contiguous_container_asan_correct(c));
107 for (int j = 0; static_cast<std::size_t>(j) < c.size(); ++j)
108 assert(c[j] == j);
109 }
110#endif
111
112 return true;
113}
114
115int main(int, char**) {
116 tests();
117#if TEST_STD_VER > 17
118 static_assert(tests());
119#endif
120 return 0;
121}
122

source code of libcxx/test/std/containers/sequences/vector/vector.modifiers/push_back.pass.cpp