| 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 | // <map> |
| 12 | |
| 13 | // class map |
| 14 | |
| 15 | // map(map&& m, const allocator_type& a); |
| 16 | |
| 17 | #include <map> |
| 18 | #include <cassert> |
| 19 | #include <iterator> |
| 20 | |
| 21 | #include "test_macros.h" |
| 22 | #include "MoveOnly.h" |
| 23 | #include "../../../test_compare.h" |
| 24 | #include "test_allocator.h" |
| 25 | #include "min_allocator.h" |
| 26 | #include "Counter.h" |
| 27 | |
| 28 | int main(int, char**) { |
| 29 | { |
| 30 | typedef std::pair<MoveOnly, MoveOnly> V; |
| 31 | typedef std::pair<const MoveOnly, MoveOnly> VC; |
| 32 | typedef test_less<MoveOnly> C; |
| 33 | typedef test_allocator<VC> A; |
| 34 | typedef std::map<MoveOnly, MoveOnly, C, A> M; |
| 35 | typedef std::move_iterator<V*> I; |
| 36 | V a1[] = {V(1, 1), V(1, 2), V(1, 3), V(2, 1), V(2, 2), V(2, 3), V(3, 1), V(3, 2), V(3, 3)}; |
| 37 | M m1(I(a1), I(a1 + sizeof(a1) / sizeof(a1[0])), C(5), A(7)); |
| 38 | V a2[] = {V(1, 1), V(1, 2), V(1, 3), V(2, 1), V(2, 2), V(2, 3), V(3, 1), V(3, 2), V(3, 3)}; |
| 39 | M m2(I(a2), I(a2 + sizeof(a2) / sizeof(a2[0])), C(5), A(7)); |
| 40 | M m3(std::move(m1), A(7)); |
| 41 | assert(m3 == m2); |
| 42 | assert(m3.get_allocator() == A(7)); |
| 43 | assert(m3.key_comp() == C(5)); |
| 44 | LIBCPP_ASSERT(m1.empty()); |
| 45 | } |
| 46 | { |
| 47 | typedef std::pair<MoveOnly, MoveOnly> V; |
| 48 | typedef std::pair<const MoveOnly, MoveOnly> VC; |
| 49 | typedef test_less<MoveOnly> C; |
| 50 | typedef test_allocator<VC> A; |
| 51 | typedef std::map<MoveOnly, MoveOnly, C, A> M; |
| 52 | typedef std::move_iterator<V*> I; |
| 53 | V a1[] = {V(1, 1), V(1, 2), V(1, 3), V(2, 1), V(2, 2), V(2, 3), V(3, 1), V(3, 2), V(3, 3)}; |
| 54 | M m1(I(a1), I(a1 + sizeof(a1) / sizeof(a1[0])), C(5), A(7)); |
| 55 | V a2[] = {V(1, 1), V(1, 2), V(1, 3), V(2, 1), V(2, 2), V(2, 3), V(3, 1), V(3, 2), V(3, 3)}; |
| 56 | M m2(I(a2), I(a2 + sizeof(a2) / sizeof(a2[0])), C(5), A(7)); |
| 57 | M m3(std::move(m1), A(5)); |
| 58 | assert(m3 == m2); |
| 59 | assert(m3.get_allocator() == A(5)); |
| 60 | assert(m3.key_comp() == C(5)); |
| 61 | LIBCPP_ASSERT(m1.empty()); |
| 62 | } |
| 63 | { |
| 64 | typedef std::pair<MoveOnly, MoveOnly> V; |
| 65 | typedef std::pair<const MoveOnly, MoveOnly> VC; |
| 66 | typedef test_less<MoveOnly> C; |
| 67 | typedef other_allocator<VC> A; |
| 68 | typedef std::map<MoveOnly, MoveOnly, C, A> M; |
| 69 | typedef std::move_iterator<V*> I; |
| 70 | V a1[] = {V(1, 1), V(1, 2), V(1, 3), V(2, 1), V(2, 2), V(2, 3), V(3, 1), V(3, 2), V(3, 3)}; |
| 71 | M m1(I(a1), I(a1 + sizeof(a1) / sizeof(a1[0])), C(5), A(7)); |
| 72 | V a2[] = {V(1, 1), V(1, 2), V(1, 3), V(2, 1), V(2, 2), V(2, 3), V(3, 1), V(3, 2), V(3, 3)}; |
| 73 | M m2(I(a2), I(a2 + sizeof(a2) / sizeof(a2[0])), C(5), A(7)); |
| 74 | M m3(std::move(m1), A(5)); |
| 75 | assert(m3 == m2); |
| 76 | assert(m3.get_allocator() == A(5)); |
| 77 | assert(m3.key_comp() == C(5)); |
| 78 | LIBCPP_ASSERT(m1.empty()); |
| 79 | } |
| 80 | { |
| 81 | typedef Counter<int> T; |
| 82 | typedef std::pair<int, T> V; |
| 83 | typedef std::pair<const int, T> VC; |
| 84 | typedef test_allocator<VC> A; |
| 85 | typedef std::less<int> C; |
| 86 | typedef std::map<const int, T, C, A> M; |
| 87 | typedef V* I; |
| 88 | Counter_base::gConstructed = 0; |
| 89 | { |
| 90 | V a1[] = {V(1, 1), V(1, 2), V(1, 3), V(2, 1), V(2, 2), V(2, 3), V(3, 1), V(3, 2), V(3, 3)}; |
| 91 | const std::size_t num = sizeof(a1) / sizeof(a1[0]); |
| 92 | assert(Counter_base::gConstructed == num); |
| 93 | |
| 94 | M m1(I(a1), I(a1 + num), C(), A()); |
| 95 | assert(Counter_base::gConstructed == num + 3); |
| 96 | |
| 97 | M m2(m1); |
| 98 | assert(m2 == m1); |
| 99 | assert(Counter_base::gConstructed == num + 6); |
| 100 | |
| 101 | M m3(std::move(m1), A()); |
| 102 | assert(m3 == m2); |
| 103 | LIBCPP_ASSERT(m1.empty()); |
| 104 | assert(Counter_base::gConstructed >= (int)(num + 6)); |
| 105 | assert(Counter_base::gConstructed <= (int)(num + 6 + m1.size())); |
| 106 | |
| 107 | { |
| 108 | M m4(std::move(m2), A(5)); |
| 109 | assert(Counter_base::gConstructed >= (int)(num + 6)); |
| 110 | assert(Counter_base::gConstructed <= (int)(num + 6 + m1.size() + m2.size())); |
| 111 | assert(m4 == m3); |
| 112 | LIBCPP_ASSERT(m2.empty()); |
| 113 | } |
| 114 | assert(Counter_base::gConstructed >= (int)(num + 3)); |
| 115 | assert(Counter_base::gConstructed <= (int)(num + 3 + m1.size() + m2.size())); |
| 116 | } |
| 117 | assert(Counter_base::gConstructed == 0); |
| 118 | } |
| 119 | { |
| 120 | typedef std::pair<MoveOnly, MoveOnly> V; |
| 121 | typedef std::pair<const MoveOnly, MoveOnly> VC; |
| 122 | typedef test_less<MoveOnly> C; |
| 123 | typedef min_allocator<VC> A; |
| 124 | typedef std::map<MoveOnly, MoveOnly, C, A> M; |
| 125 | typedef std::move_iterator<V*> I; |
| 126 | V a1[] = {V(1, 1), V(1, 2), V(1, 3), V(2, 1), V(2, 2), V(2, 3), V(3, 1), V(3, 2), V(3, 3)}; |
| 127 | M m1(I(a1), I(a1 + sizeof(a1) / sizeof(a1[0])), C(5), A()); |
| 128 | V a2[] = {V(1, 1), V(1, 2), V(1, 3), V(2, 1), V(2, 2), V(2, 3), V(3, 1), V(3, 2), V(3, 3)}; |
| 129 | M m2(I(a2), I(a2 + sizeof(a2) / sizeof(a2[0])), C(5), A()); |
| 130 | M m3(std::move(m1), A()); |
| 131 | assert(m3 == m2); |
| 132 | assert(m3.get_allocator() == A()); |
| 133 | assert(m3.key_comp() == C(5)); |
| 134 | LIBCPP_ASSERT(m1.empty()); |
| 135 | } |
| 136 | { |
| 137 | typedef std::pair<MoveOnly, MoveOnly> V; |
| 138 | typedef std::pair<const MoveOnly, MoveOnly> VC; |
| 139 | typedef test_less<MoveOnly> C; |
| 140 | typedef explicit_allocator<VC> A; |
| 141 | typedef std::map<MoveOnly, MoveOnly, C, A> M; |
| 142 | typedef std::move_iterator<V*> I; |
| 143 | V a1[] = {V(1, 1), V(1, 2), V(1, 3), V(2, 1), V(2, 2), V(2, 3), V(3, 1), V(3, 2), V(3, 3)}; |
| 144 | M m1(I(a1), I(a1 + sizeof(a1) / sizeof(a1[0])), C(5), A{}); |
| 145 | V a2[] = {V(1, 1), V(1, 2), V(1, 3), V(2, 1), V(2, 2), V(2, 3), V(3, 1), V(3, 2), V(3, 3)}; |
| 146 | M m2(I(a2), I(a2 + sizeof(a2) / sizeof(a2[0])), C(5), A{}); |
| 147 | M m3(std::move(m1), A{}); |
| 148 | assert(m3 == m2); |
| 149 | assert(m3.get_allocator() == A{}); |
| 150 | assert(m3.key_comp() == C(5)); |
| 151 | LIBCPP_ASSERT(m1.empty()); |
| 152 | } |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |