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
10
11// <vector>
12
13// template <class... Args> iterator emplace(const_iterator pos, Args&&... args);
14
15#include <vector>
16#include <cassert>
17
18#include "test_macros.h"
19#include "min_allocator.h"
20#include "asan_testing.h"
21
22TEST_CONSTEXPR_CXX20 bool tests() {
23 {
24 std::vector<int> v;
25 v.reserve(n: 3);
26 assert(is_contiguous_container_asan_correct(v));
27 v = { 1, 2, 3 };
28 v.emplace(position: v.begin(), args&: v.back());
29 assert(v[0] == 3);
30 assert(is_contiguous_container_asan_correct(v));
31 }
32 {
33 std::vector<int> v;
34 v.reserve(n: 4);
35 assert(is_contiguous_container_asan_correct(v));
36 v = { 1, 2, 3 };
37 v.emplace(position: v.begin(), args&: v.back());
38 assert(v[0] == 3);
39 assert(is_contiguous_container_asan_correct(v));
40 }
41 {
42 std::vector<int, min_allocator<int>> v;
43 v.reserve(3);
44 assert(is_contiguous_container_asan_correct(v));
45 v = { 1, 2, 3 };
46 v.emplace(v.begin(), v.back());
47 assert(v[0] == 3);
48 assert(is_contiguous_container_asan_correct(v));
49 }
50 {
51 std::vector<int, min_allocator<int>> v;
52 v.reserve(4);
53 assert(is_contiguous_container_asan_correct(v));
54 v = { 1, 2, 3 };
55 v.emplace(v.begin(), v.back());
56 assert(v[0] == 3);
57 assert(is_contiguous_container_asan_correct(v));
58 }
59 {
60 std::vector<int, safe_allocator<int>> v;
61 v.reserve(3);
62 assert(is_contiguous_container_asan_correct(v));
63 v = {1, 2, 3};
64 v.emplace(v.begin(), v.back());
65 assert(v[0] == 3);
66 assert(is_contiguous_container_asan_correct(v));
67 }
68 {
69 std::vector<int, safe_allocator<int>> v;
70 v.reserve(4);
71 assert(is_contiguous_container_asan_correct(v));
72 v = {1, 2, 3};
73 v.emplace(v.begin(), v.back());
74 assert(v[0] == 3);
75 assert(is_contiguous_container_asan_correct(v));
76 }
77 {
78 std::vector<int> v;
79 v.reserve(n: 8);
80 std::size_t old_capacity = v.capacity();
81 assert(old_capacity >= 8);
82
83 v.resize(new_size: 4); // keep the existing capacity
84 assert(v.capacity() == old_capacity);
85
86 v.emplace(position: v.cend(), args: 42);
87 assert(v.size() == 5);
88 assert(v.capacity() == old_capacity);
89 assert(v[4] == 42);
90 }
91
92 return true;
93}
94
95int main(int, char**) {
96 tests();
97#if TEST_STD_VER > 17
98 static_assert(tests());
99#endif
100 return 0;
101}
102

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