| 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 | // UNSUPPORTED: availability-pmr-missing |
| 11 | |
| 12 | // <flat_map> |
| 13 | |
| 14 | #include <algorithm> |
| 15 | #include <cassert> |
| 16 | #include <climits> |
| 17 | #include <deque> |
| 18 | #include <initializer_list> |
| 19 | #include <list> |
| 20 | #include <flat_map> |
| 21 | #include <functional> |
| 22 | #include <memory_resource> |
| 23 | #include <ranges> |
| 24 | #include <type_traits> |
| 25 | #include <utility> |
| 26 | #include <vector> |
| 27 | |
| 28 | #include "test_allocator.h" |
| 29 | |
| 30 | using P = std::pair<int, long>; |
| 31 | using PC = std::pair<const int, long>; |
| 32 | |
| 33 | void test_containers() { |
| 34 | std::deque<int, test_allocator<int>> ks({1, 2, 1, 2, 2, INT_MAX, 3}, test_allocator<int>(0, 42)); |
| 35 | std::deque<short, test_allocator<short>> vs({1, 2, 3, 4, 5, 3, 4}, test_allocator<int>(0, 43)); |
| 36 | std::deque<int, test_allocator<int>> sorted_ks({1, 1, 2, 2, 2, 3, INT_MAX}, test_allocator<int>(0, 42)); |
| 37 | std::deque<short, test_allocator<short>> sorted_vs({1, 3, 2, 4, 5, 4, 3}, test_allocator<int>(0, 43)); |
| 38 | const std::pair<int, short> expected[] = {{1, 1}, {1, 3}, {2, 2}, {2, 4}, {2, 5}, {3, 4}, {INT_MAX, 3}}; |
| 39 | { |
| 40 | std::pmr::monotonic_buffer_resource mr; |
| 41 | std::pmr::monotonic_buffer_resource mr2; |
| 42 | std::pmr::deque<int> pks(ks.begin(), ks.end(), &mr); |
| 43 | std::pmr::deque<short> pvs(vs.begin(), vs.end(), &mr); |
| 44 | std::flat_multimap s(std::move(pks), std::move(pvs), &mr2); |
| 45 | |
| 46 | ASSERT_SAME_TYPE( |
| 47 | decltype(s), std::flat_multimap<int, short, std::less<int>, std::pmr::deque<int>, std::pmr::deque<short>>); |
| 48 | assert(std::ranges::equal(s, expected)); |
| 49 | assert(s.keys().get_allocator().resource() == &mr2); |
| 50 | assert(s.values().get_allocator().resource() == &mr2); |
| 51 | } |
| 52 | { |
| 53 | std::pmr::monotonic_buffer_resource mr; |
| 54 | std::pmr::monotonic_buffer_resource mr2; |
| 55 | std::pmr::deque<int> pks(sorted_ks.begin(), sorted_ks.end(), &mr); |
| 56 | std::pmr::deque<short> pvs(sorted_vs.begin(), sorted_vs.end(), &mr); |
| 57 | std::flat_multimap s(std::sorted_equivalent, std::move(pks), std::move(pvs), &mr2); |
| 58 | |
| 59 | ASSERT_SAME_TYPE( |
| 60 | decltype(s), std::flat_multimap<int, short, std::less<int>, std::pmr::deque<int>, std::pmr::deque<short>>); |
| 61 | assert(std::ranges::equal(s, expected)); |
| 62 | assert(s.keys().get_allocator().resource() == &mr2); |
| 63 | assert(s.values().get_allocator().resource() == &mr2); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | void test_containers_compare() { |
| 68 | std::deque<int, test_allocator<int>> ks({1, 2, 1, 2, 2, INT_MAX, 3}, test_allocator<int>(0, 42)); |
| 69 | std::deque<short, test_allocator<short>> vs({1, 2, 3, 4, 5, 3, 4}, test_allocator<int>(0, 43)); |
| 70 | std::deque<int, test_allocator<int>> sorted_ks({INT_MAX, 3, 2, 2, 2, 1, 1}, test_allocator<int>(0, 42)); |
| 71 | std::deque<short, test_allocator<short>> sorted_vs({3, 4, 2, 4, 5, 1, 3}, test_allocator<int>(0, 43)); |
| 72 | const std::pair<int, short> expected[] = {{INT_MAX, 3}, {3, 4}, {2, 2}, {2, 4}, {2, 5}, {1, 1}, {1, 3}}; |
| 73 | |
| 74 | { |
| 75 | std::pmr::monotonic_buffer_resource mr; |
| 76 | std::pmr::monotonic_buffer_resource mr2; |
| 77 | std::pmr::deque<int> pks(ks.begin(), ks.end(), &mr); |
| 78 | std::pmr::deque<short> pvs(vs.begin(), vs.end(), &mr); |
| 79 | std::flat_multimap s(std::move(pks), std::move(pvs), std::greater<int>(), &mr2); |
| 80 | |
| 81 | ASSERT_SAME_TYPE( |
| 82 | decltype(s), std::flat_multimap<int, short, std::greater<int>, std::pmr::deque<int>, std::pmr::deque<short>>); |
| 83 | assert(std::ranges::equal(s, expected)); |
| 84 | assert(s.keys().get_allocator().resource() == &mr2); |
| 85 | assert(s.values().get_allocator().resource() == &mr2); |
| 86 | } |
| 87 | { |
| 88 | std::pmr::monotonic_buffer_resource mr; |
| 89 | std::pmr::monotonic_buffer_resource mr2; |
| 90 | std::pmr::deque<int> pks(sorted_ks.begin(), sorted_ks.end(), &mr); |
| 91 | std::pmr::deque<short> pvs(sorted_vs.begin(), sorted_vs.end(), &mr); |
| 92 | std::flat_multimap s(std::sorted_equivalent, std::move(pks), std::move(pvs), std::greater<int>(), &mr2); |
| 93 | |
| 94 | ASSERT_SAME_TYPE( |
| 95 | decltype(s), std::flat_multimap<int, short, std::greater<int>, std::pmr::deque<int>, std::pmr::deque<short>>); |
| 96 | assert(std::ranges::equal(s, expected)); |
| 97 | assert(s.keys().get_allocator().resource() == &mr2); |
| 98 | assert(s.values().get_allocator().resource() == &mr2); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | int main(int, char**) { |
| 103 | test_containers(); |
| 104 | test_containers_compare(); |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |