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 | // <deque> |
12 | |
13 | // template <class... Args> reference emplace_back(Args&&... args); |
14 | // return type is 'reference' in C++17; 'void' before |
15 | |
16 | #include "asan_testing.h" |
17 | #include <deque> |
18 | #include <cstddef> |
19 | #include <cassert> |
20 | |
21 | #include "test_macros.h" |
22 | #include "../../../Emplaceable.h" |
23 | #include "min_allocator.h" |
24 | #include "test_allocator.h" |
25 | |
26 | template <class C> |
27 | C make(int size, int start = 0) { |
28 | const int b = 4096 / sizeof(int); |
29 | int init = 0; |
30 | if (start > 0) { |
31 | init = (start + 1) / b + ((start + 1) % b != 0); |
32 | init *= b; |
33 | --init; |
34 | } |
35 | C c(init); |
36 | for (int i = 0; i < init - start; ++i) |
37 | c.pop_back(); |
38 | for (int i = 0; i < size; ++i) |
39 | c.push_back(Emplaceable()); |
40 | for (int i = 0; i < start; ++i) |
41 | c.pop_front(); |
42 | return c; |
43 | } |
44 | |
45 | template <class C> |
46 | void test(C& c1) { |
47 | typedef typename C::iterator I; |
48 | std::size_t c1_osize = c1.size(); |
49 | #if TEST_STD_VER > 14 |
50 | typedef typename C::reference Ref; |
51 | Ref ref = c1.emplace_back(Emplaceable(1, 2.5)); |
52 | #else |
53 | c1.emplace_back(Emplaceable(1, 2.5)); |
54 | #endif |
55 | assert(c1.size() == c1_osize + 1); |
56 | assert(std::distance(c1.begin(), c1.end()) == static_cast<std::ptrdiff_t>(c1.size())); |
57 | I i = c1.end(); |
58 | assert(*--i == Emplaceable(1, 2.5)); |
59 | LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c1)); |
60 | #if TEST_STD_VER > 14 |
61 | assert(&(*i) == &ref); |
62 | #endif |
63 | } |
64 | |
65 | template <class C> |
66 | void testN(int start, int N) { |
67 | C c1 = make<C>(N, start); |
68 | test(c1); |
69 | } |
70 | |
71 | int main(int, char**) { |
72 | { |
73 | int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049}; |
74 | const int N = sizeof(rng) / sizeof(rng[0]); |
75 | for (int i = 0; i < N; ++i) |
76 | for (int j = 0; j < N; ++j) |
77 | testN<std::deque<Emplaceable> >(rng[i], rng[j]); |
78 | } |
79 | { |
80 | int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049}; |
81 | const int N = sizeof(rng) / sizeof(rng[0]); |
82 | for (int i = 0; i < N; ++i) |
83 | for (int j = 0; j < N; ++j) |
84 | testN<std::deque<Emplaceable, min_allocator<Emplaceable>> >(rng[i], rng[j]); |
85 | } |
86 | { |
87 | std::deque<Tag_X, TaggingAllocator<Tag_X>> c; |
88 | c.emplace_back(); |
89 | assert(c.size() == 1); |
90 | LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c)); |
91 | c.emplace_back(1, 2, 3); |
92 | assert(c.size() == 2); |
93 | LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c)); |
94 | c.emplace_front(); |
95 | assert(c.size() == 3); |
96 | LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c)); |
97 | c.emplace_front(1, 2, 3); |
98 | assert(c.size() == 4); |
99 | LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c)); |
100 | } |
101 | |
102 | return 0; |
103 | } |
104 | |