| 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&&, const allocator_type&); |
| 14 | |
| 15 | #include <algorithm> |
| 16 | #include <deque> |
| 17 | #include <flat_map> |
| 18 | #include <functional> |
| 19 | #include <ranges> |
| 20 | #include <vector> |
| 21 | |
| 22 | #include "../helpers.h" |
| 23 | #include "test_macros.h" |
| 24 | #include "../../../test_compare.h" |
| 25 | #include "test_allocator.h" |
| 26 | |
| 27 | int main(int, char**) { |
| 28 | { |
| 29 | // The constructors in this subclause shall not participate in overload |
| 30 | // resolution unless uses_allocator_v<key_container_type, Alloc> is true |
| 31 | // and uses_allocator_v<mapped_container_type, Alloc> is true. |
| 32 | |
| 33 | using C = test_less<int>; |
| 34 | using A1 = test_allocator<int>; |
| 35 | using A2 = other_allocator<int>; |
| 36 | using V1 = std::vector<int, A1>; |
| 37 | using V2 = std::vector<int, A2>; |
| 38 | using M1 = std::flat_multimap<int, int, C, V1, V1>; |
| 39 | using M2 = std::flat_multimap<int, int, C, V1, V2>; |
| 40 | using M3 = std::flat_multimap<int, int, C, V2, V1>; |
| 41 | static_assert(std::is_constructible_v<M1, M1&&, const A1&>); |
| 42 | static_assert(!std::is_constructible_v<M1, M1&&, const A2&>); |
| 43 | static_assert(!std::is_constructible_v<M2, M2&&, const A2&>); |
| 44 | static_assert(!std::is_constructible_v<M3, M3&&, const A2&>); |
| 45 | } |
| 46 | { |
| 47 | std::pair<int, int> expected[] = {{1, 1}, {1, 2}, {2, 3}, {2, 2}, {3, 1}}; |
| 48 | using C = test_less<int>; |
| 49 | using A = test_allocator<int>; |
| 50 | using M = std::flat_multimap<int, int, C, std::vector<int, A>, std::deque<int, A>>; |
| 51 | auto mo = M(expected, expected + 5, C(5), A(7)); |
| 52 | auto m = M(std::move(mo), A(3)); |
| 53 | |
| 54 | assert(m.key_comp() == C(5)); |
| 55 | assert(m.size() == 5); |
| 56 | auto [keys, values] = std::move(m).extract(); |
| 57 | assert(keys.get_allocator() == A(3)); |
| 58 | assert(values.get_allocator() == A(3)); |
| 59 | assert(std::ranges::equal(keys, expected | std::views::elements<0>)); |
| 60 | assert(std::ranges::equal(values, expected | std::views::elements<1>)); |
| 61 | |
| 62 | // The original flat_multimap is moved-from. |
| 63 | assert(std::is_sorted(mo.begin(), mo.end(), mo.value_comp())); |
| 64 | assert(mo.empty()); |
| 65 | assert(mo.key_comp() == C(5)); |
| 66 | assert(mo.keys().get_allocator() == A(7)); |
| 67 | assert(mo.values().get_allocator() == A(7)); |
| 68 | } |
| 69 | { |
| 70 | // moved-from object maintains invariant if one of underlying container does not clear after move |
| 71 | using M = std::flat_multimap<int, int, std::less<>, std::vector<int>, CopyOnlyVector<int>>; |
| 72 | M m1 = M({1, 1, 3}, {1, 2, 3}); |
| 73 | M m2(std::move(m1), std::allocator<int>{}); |
| 74 | assert(m2.size() == 3); |
| 75 | check_invariant(m1); |
| 76 | LIBCPP_ASSERT(m1.empty()); |
| 77 | LIBCPP_ASSERT(m1.keys().size() == 0); |
| 78 | LIBCPP_ASSERT(m1.values().size() == 0); |
| 79 | } |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |