| 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_map> |
| 12 | |
| 13 | // flat_multimap(flat_multimap&&); |
| 14 | |
| 15 | #include <algorithm> |
| 16 | #include <deque> |
| 17 | #include <flat_map> |
| 18 | #include <functional> |
| 19 | #include <utility> |
| 20 | #include <vector> |
| 21 | |
| 22 | #include "../helpers.h" |
| 23 | #include "test_macros.h" |
| 24 | #include "../../../test_compare.h" |
| 25 | #include "test_allocator.h" |
| 26 | #include "min_allocator.h" |
| 27 | |
| 28 | int main(int, char**) { |
| 29 | { |
| 30 | using C = test_less<int>; |
| 31 | using A = test_allocator<int>; |
| 32 | using M = std::flat_multimap<int, int, C, std::vector<int, A>, std::deque<int, A>>; |
| 33 | M mo = M({{1, 1}, {1, 2}, {3, 1}}, C(5), A(7)); |
| 34 | M m = std::move(mo); |
| 35 | assert((m == M{{1, 1}, {1, 2}, {3, 1}})); |
| 36 | assert(m.key_comp() == C(5)); |
| 37 | assert(m.keys().get_allocator() == A(7)); |
| 38 | assert(m.values().get_allocator() == A(7)); |
| 39 | |
| 40 | assert(mo.empty()); |
| 41 | assert(mo.key_comp() == C(5)); |
| 42 | assert(mo.keys().get_allocator().get_id() == test_alloc_base::moved_value); |
| 43 | assert(mo.values().get_allocator().get_id() == test_alloc_base::moved_value); |
| 44 | } |
| 45 | { |
| 46 | using C = test_less<int>; |
| 47 | using A = min_allocator<int>; |
| 48 | using M = std::flat_multimap<int, int, C, std::vector<int, A>, std::deque<int, A>>; |
| 49 | M mo = M({{1, 1}, {1, 2}, {3, 1}}, C(5), A()); |
| 50 | M m = std::move(mo); |
| 51 | assert((m == M{{1, 1}, {1, 2}, {3, 1}})); |
| 52 | assert(m.key_comp() == C(5)); |
| 53 | assert(m.keys().get_allocator() == A()); |
| 54 | assert(m.values().get_allocator() == A()); |
| 55 | |
| 56 | assert(mo.empty()); |
| 57 | assert(mo.key_comp() == C(5)); |
| 58 | assert(m.keys().get_allocator() == A()); |
| 59 | assert(m.values().get_allocator() == A()); |
| 60 | } |
| 61 | { |
| 62 | // A moved-from flat_multimap maintains its class invariant in the presence of moved-from comparators. |
| 63 | using M = std::flat_multimap<int, int, std::function<bool(int, int)>>; |
| 64 | M mo = M({{1, 1}, {1, 2}, {3, 1}}, std::less<int>()); |
| 65 | M m = std::move(mo); |
| 66 | assert(m.size() == 3); |
| 67 | assert(std::is_sorted(m.begin(), m.end(), m.value_comp())); |
| 68 | assert(m.key_comp()(1, 2) == true); |
| 69 | |
| 70 | assert(std::is_sorted(mo.begin(), mo.end(), mo.value_comp())); |
| 71 | LIBCPP_ASSERT(m.key_comp()(1, 2) == true); |
| 72 | LIBCPP_ASSERT(mo.empty()); |
| 73 | mo.insert({{1, 1}, {1, 2}, {3, 1}}); // insert has no preconditions |
| 74 | assert(m == mo); |
| 75 | } |
| 76 | { |
| 77 | // moved-from object maintains invariant if one of underlying container does not clear after move |
| 78 | using M = std::flat_multimap<int, int, std::less<>, std::vector<int>, CopyOnlyVector<int>>; |
| 79 | M m1 = M({1, 1, 3}, {1, 2, 3}); |
| 80 | M m2 = std::move(m1); |
| 81 | assert(m2.size() == 3); |
| 82 | check_invariant(m1); |
| 83 | LIBCPP_ASSERT(m1.empty()); |
| 84 | LIBCPP_ASSERT(m1.keys().size() == 0); |
| 85 | LIBCPP_ASSERT(m1.values().size() == 0); |
| 86 | } |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |