| 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 | // class flat_multimap |
| 14 | |
| 15 | // void replace(key_container_type&& key_cont, mapped_container_type&& mapped_cont); |
| 16 | |
| 17 | #include <algorithm> |
| 18 | #include <deque> |
| 19 | #include <concepts> |
| 20 | #include <flat_map> |
| 21 | #include <functional> |
| 22 | |
| 23 | #include "MinSequenceContainer.h" |
| 24 | #include "../helpers.h" |
| 25 | #include "test_macros.h" |
| 26 | #include "min_allocator.h" |
| 27 | |
| 28 | template <class T, class... Args> |
| 29 | concept CanReplace = requires(T t, Args&&... args) { t.replace(std::forward<Args>(args)...); }; |
| 30 | |
| 31 | using Map = std::flat_multimap<int, int>; |
| 32 | static_assert(CanReplace<Map, std::vector<int>, std::vector<int>>); |
| 33 | static_assert(!CanReplace<Map, const std::vector<int>&, std::vector<int>>); |
| 34 | static_assert(!CanReplace<Map, std::vector<int>, const std::vector<int>&>); |
| 35 | static_assert(!CanReplace<Map, const std::vector<int>&, const std::vector<int>&>); |
| 36 | |
| 37 | template <class KeyContainer, class ValueContainer> |
| 38 | void test() { |
| 39 | using Key = typename KeyContainer::value_type; |
| 40 | using Value = typename ValueContainer::value_type; |
| 41 | using M = std::flat_multimap<Key, Value, std::less<Key>, KeyContainer, ValueContainer>; |
| 42 | |
| 43 | M m = M({1, 1, 3}, {4, 5, 6}); |
| 44 | KeyContainer new_keys = {7, 7}; |
| 45 | ValueContainer new_values = {9, 10}; |
| 46 | auto expected_keys = new_keys; |
| 47 | auto expected_values = new_values; |
| 48 | m.replace(std::move(new_keys), std::move(new_values)); |
| 49 | assert(m.size() == 2); |
| 50 | assert(std::ranges::equal(m.keys(), expected_keys)); |
| 51 | assert(std::ranges::equal(m.values(), expected_values)); |
| 52 | } |
| 53 | |
| 54 | int main(int, char**) { |
| 55 | test<std::vector<int>, std::vector<double>>(); |
| 56 | test<std::deque<int>, std::vector<double>>(); |
| 57 | test<MinSequenceContainer<int>, MinSequenceContainer<double>>(); |
| 58 | test<std::vector<int, min_allocator<int>>, std::vector<double, min_allocator<double>>>(); |
| 59 | |
| 60 | { |
| 61 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 62 | using KeyContainer = std::vector<int>; |
| 63 | using ValueContainer = ThrowOnMoveContainer<int>; |
| 64 | using M = std::flat_multimap<int, int, std::ranges::less, KeyContainer, ValueContainer>; |
| 65 | |
| 66 | M m; |
| 67 | m.emplace(1, 1); |
| 68 | m.emplace(2, 2); |
| 69 | try { |
| 70 | KeyContainer new_keys{3, 4}; |
| 71 | ValueContainer new_values{5, 6}; |
| 72 | m.replace(std::move(new_keys), std::move(new_values)); |
| 73 | assert(false); |
| 74 | } catch (int) { |
| 75 | check_invariant(m); |
| 76 | // In libc++, we clear the map |
| 77 | LIBCPP_ASSERT(m.size() == 0); |
| 78 | } |
| 79 | #endif |
| 80 | } |
| 81 | return 0; |
| 82 | } |
| 83 | |