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

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