| 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, c++11, c++14, c++17, c++20 |
| 10 | |
| 11 | // <flat_set> |
| 12 | |
| 13 | // flat_multiset& operator=(const flat_multiset& m); |
| 14 | |
| 15 | #include <algorithm> |
| 16 | #include <flat_set> |
| 17 | #include <functional> |
| 18 | #include <vector> |
| 19 | |
| 20 | #include "operator_hijacker.h" |
| 21 | #include "test_macros.h" |
| 22 | #include "../../../test_compare.h" |
| 23 | #include "test_allocator.h" |
| 24 | |
| 25 | void test() { |
| 26 | { |
| 27 | // test_allocator is not propagated |
| 28 | using C = test_less<int>; |
| 29 | std::vector<int, test_allocator<int>> ks({1, 3, 5, 5}, test_allocator<int>(6)); |
| 30 | using M = std::flat_multiset<int, C, decltype(ks)>; |
| 31 | auto mo = M(ks, C(5)); |
| 32 | auto m = M({{3, 4, 5, 4}}, C(3), test_allocator<int>(2)); |
| 33 | m = mo; |
| 34 | |
| 35 | assert(m.key_comp() == C(5)); |
| 36 | assert(std::ranges::equal(m, ks)); |
| 37 | auto keys = std::move(m).extract(); |
| 38 | assert(keys.get_allocator() == test_allocator<int>(2)); |
| 39 | |
| 40 | // mo is unchanged |
| 41 | assert(mo.key_comp() == C(5)); |
| 42 | assert(std::ranges::equal(mo, ks)); |
| 43 | auto keys2 = std::move(mo).extract(); |
| 44 | assert(keys2.get_allocator() == test_allocator<int>(6)); |
| 45 | } |
| 46 | { |
| 47 | // other_allocator is propagated |
| 48 | using C = test_less<int>; |
| 49 | using Ks = std::vector<int, other_allocator<int>>; |
| 50 | auto ks = Ks({1, 3, 5, 3}, other_allocator<int>(6)); |
| 51 | const int expected[] = {1, 3, 3, 5}; |
| 52 | using M = std::flat_multiset<int, C, Ks>; |
| 53 | auto mo = M(Ks(ks, other_allocator<int>(6)), C(5)); |
| 54 | auto m = M({3, 4, 5}, C(3), other_allocator<int>(2)); |
| 55 | m = mo; |
| 56 | |
| 57 | assert(m.key_comp() == C(5)); |
| 58 | assert(std::ranges::equal(m, expected)); |
| 59 | auto keys = std::move(m).extract(); |
| 60 | assert(keys.get_allocator() == other_allocator<int>(6)); |
| 61 | |
| 62 | // mo is unchanged |
| 63 | assert(mo.key_comp() == C(5)); |
| 64 | assert(std::ranges::equal(mo, expected)); |
| 65 | auto keys2 = std::move(mo).extract(); |
| 66 | assert(keys2.get_allocator() == other_allocator<int>(6)); |
| 67 | } |
| 68 | { |
| 69 | // comparator is copied and invariant is preserved |
| 70 | using M = std::flat_multiset<int, std::function<bool(int, int)>>; |
| 71 | M mo = M({1, 2}, std::less<int>()); |
| 72 | M m = M({1, 2}, std::greater<int>()); |
| 73 | assert(m.key_comp()(2, 1) == true); |
| 74 | assert(m != mo); |
| 75 | m = mo; |
| 76 | assert(m.key_comp()(2, 1) == false); |
| 77 | assert(m == mo); |
| 78 | } |
| 79 | { |
| 80 | // self-assignment |
| 81 | using M = std::flat_multiset<int>; |
| 82 | M m = {{1, 2}}; |
| 83 | m = std::as_const(m); |
| 84 | assert((m == M{{1, 2}})); |
| 85 | } |
| 86 | { |
| 87 | // was empty |
| 88 | using M = std::flat_multiset<int>; |
| 89 | M m; |
| 90 | assert(m.size() == 0); |
| 91 | m = {3, 1, 2, 2, 3, 4, 3, 5, 6, 5}; |
| 92 | int expected[] = {1, 2, 2, 3, 3, 3, 4, 5, 5, 6}; |
| 93 | assert(std::ranges::equal(m, expected)); |
| 94 | } |
| 95 | { |
| 96 | // Validate whether the container can be copy-assigned (move-assigned, swapped) |
| 97 | // with an ADL-hijacking operator& |
| 98 | std::flat_multiset<operator_hijacker> so; |
| 99 | std::flat_multiset<operator_hijacker> s; |
| 100 | s = so; |
| 101 | s = std::move(so); |
| 102 | swap(s, so); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | int main(int, char**) { |
| 107 | test(); |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |