| 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_set& operator=(const flat_set& 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}, test_allocator<int>(6)); |
| 30 | using M = std::flat_set<int, C, decltype(ks)>; |
| 31 | auto mo = M(ks, C(5)); |
| 32 | auto m = M({{3, 4, 5}}, 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}, other_allocator<int>(6)); |
| 51 | using M = std::flat_set<int, C, Ks>; |
| 52 | auto mo = M(Ks(ks, other_allocator<int>(6)), C(5)); |
| 53 | auto m = M({3, 4, 5}, C(3), other_allocator<int>(2)); |
| 54 | m = mo; |
| 55 | |
| 56 | assert(m.key_comp() == C(5)); |
| 57 | assert(std::ranges::equal(m, ks)); |
| 58 | auto keys = std::move(m).extract(); |
| 59 | assert(keys.get_allocator() == other_allocator<int>(6)); |
| 60 | |
| 61 | // mo is unchanged |
| 62 | assert(mo.key_comp() == C(5)); |
| 63 | assert(std::ranges::equal(mo, ks)); |
| 64 | auto keys2 = std::move(mo).extract(); |
| 65 | assert(keys2.get_allocator() == other_allocator<int>(6)); |
| 66 | } |
| 67 | { |
| 68 | // comparator is copied and invariant is preserved |
| 69 | using M = std::flat_set<int, std::function<bool(int, int)>>; |
| 70 | M mo = M({1, 2}, std::less<int>()); |
| 71 | M m = M({1, 2}, std::greater<int>()); |
| 72 | assert(m.key_comp()(2, 1) == true); |
| 73 | assert(m != mo); |
| 74 | m = mo; |
| 75 | assert(m.key_comp()(2, 1) == false); |
| 76 | assert(m == mo); |
| 77 | } |
| 78 | { |
| 79 | // self-assignment |
| 80 | using M = std::flat_set<int>; |
| 81 | M m = {{1, 2}}; |
| 82 | m = static_cast<const M&>(m); |
| 83 | assert((m == M{{1, 2}})); |
| 84 | } |
| 85 | { |
| 86 | // Validate whether the container can be copy-assigned (move-assigned, swapped) |
| 87 | // with an ADL-hijacking operator& |
| 88 | std::flat_set<operator_hijacker> so; |
| 89 | std::flat_set<operator_hijacker> s; |
| 90 | s = so; |
| 91 | s = std::move(so); |
| 92 | swap(s, so); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | int main(int, char**) { |
| 97 | test(); |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |