| 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 | // key_compare key_comp() const; |
| 16 | // value_compare value_comp() const; |
| 17 | |
| 18 | #include <cassert> |
| 19 | #include <flat_map> |
| 20 | #include <functional> |
| 21 | #include <utility> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include "test_macros.h" |
| 25 | |
| 26 | int main(int, char**) { |
| 27 | { |
| 28 | using M = std::flat_multimap<int, char>; |
| 29 | using Comp = std::less<int>; // the default |
| 30 | M m = {}; |
| 31 | ASSERT_SAME_TYPE(M::key_compare, Comp); |
| 32 | static_assert(!std::is_same_v<M::value_compare, Comp>); |
| 33 | ASSERT_SAME_TYPE(decltype(m.key_comp()), Comp); |
| 34 | ASSERT_SAME_TYPE(decltype(m.value_comp()), M::value_compare); |
| 35 | Comp kc = m.key_comp(); |
| 36 | assert(kc(1, 2)); |
| 37 | assert(!kc(2, 1)); |
| 38 | auto vc = m.value_comp(); |
| 39 | ASSERT_SAME_TYPE(decltype(vc(std::make_pair(1, 2), std::make_pair(1, 2))), bool); |
| 40 | assert(vc({1, '2'}, {2, '1'})); |
| 41 | assert(!vc({2, '1'}, {1, '2'})); |
| 42 | } |
| 43 | { |
| 44 | using Comp = std::function<bool(int, int)>; |
| 45 | using M = std::flat_multimap<int, int, Comp>; |
| 46 | Comp comp = std::greater<int>(); |
| 47 | M m({}, comp); |
| 48 | ASSERT_SAME_TYPE(M::key_compare, Comp); |
| 49 | ASSERT_SAME_TYPE(decltype(m.key_comp()), Comp); |
| 50 | ASSERT_SAME_TYPE(decltype(m.value_comp()), M::value_compare); |
| 51 | Comp kc = m.key_comp(); |
| 52 | assert(!kc(1, 2)); |
| 53 | assert(kc(2, 1)); |
| 54 | auto vc = m.value_comp(); |
| 55 | auto a = std::make_pair(1, 2); |
| 56 | ASSERT_SAME_TYPE(decltype(vc(a, a)), bool); |
| 57 | static_assert(!noexcept(vc(a, a))); |
| 58 | assert(!vc({1, 2}, {2, 1})); |
| 59 | assert(vc({2, 1}, {1, 2})); |
| 60 | } |
| 61 | { |
| 62 | using Comp = std::less<>; |
| 63 | using M = std::flat_multimap<int, int, Comp>; |
| 64 | M m = {}; |
| 65 | ASSERT_SAME_TYPE(M::key_compare, Comp); |
| 66 | ASSERT_SAME_TYPE(decltype(m.key_comp()), Comp); |
| 67 | ASSERT_SAME_TYPE(decltype(m.value_comp()), M::value_compare); |
| 68 | Comp kc = m.key_comp(); |
| 69 | assert(kc(1, 2)); |
| 70 | assert(!kc(2, 1)); |
| 71 | auto vc = m.value_comp(); |
| 72 | auto a = std::make_pair(1, 2); |
| 73 | ASSERT_SAME_TYPE(decltype(vc(a, a)), bool); |
| 74 | assert(vc({1, 2}, {2, 1})); |
| 75 | assert(!vc({2, 1}, {1, 2})); |
| 76 | } |
| 77 | { |
| 78 | using Comp = std::function<bool(const std::vector<int>&, const std::vector<int>&)>; |
| 79 | using M = std::flat_multimap<std::vector<int>, int, Comp>; |
| 80 | Comp comp = [i = 1](const auto& x, const auto& y) { return x[i] < y[i]; }; |
| 81 | M m({}, comp); |
| 82 | auto vc = m.value_comp(); |
| 83 | static_assert(sizeof(vc) >= sizeof(Comp)); |
| 84 | comp = nullptr; |
| 85 | m = M({}, nullptr); |
| 86 | assert(m.key_comp() == nullptr); |
| 87 | // At this point, m.key_comp() is disengaged. |
| 88 | // But the std::function captured by copy inside `vc` remains valid. |
| 89 | auto a = std::make_pair(std::vector<int>{2, 1, 4}, 42); |
| 90 | auto b = std::make_pair(std::vector<int>{1, 2, 3}, 42); |
| 91 | auto c = std::make_pair(std::vector<int>{0, 3, 2}, 42); |
| 92 | assert(vc(a, b)); |
| 93 | assert(vc(b, c)); |
| 94 | assert(!vc(b, a)); |
| 95 | assert(!vc(c, b)); |
| 96 | } |
| 97 | return 0; |
| 98 | } |
| 99 | |