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
30using P = std::pair<int, long>;
31using PC = std::pair<const int, long>;
32
33void test_containers() {
34 std::deque<int, test_allocator<int>> ks({1, 2, 1, INT_MAX, 3}, test_allocator<int>(0, 42));
35 std::deque<short, test_allocator<short>> vs({1, 2, 1, 4, 5}, test_allocator<int>(0, 43));
36 std::deque<int, test_allocator<int>> sorted_ks({1, 2, 3, INT_MAX}, test_allocator<int>(0, 42));
37 std::deque<short, test_allocator<short>> sorted_vs({1, 2, 5, 4}, test_allocator<int>(0, 43));
38 const std::pair<int, short> expected[] = {{1, 1}, {2, 2}, {3, 5}, {INT_MAX, 4}};
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_map s(std::move(pks), std::move(pvs), &mr2);
45
46 ASSERT_SAME_TYPE(
47 decltype(s), std::flat_map<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_map s(std::sorted_unique, std::move(pks), std::move(pvs), &mr2);
58
59 ASSERT_SAME_TYPE(
60 decltype(s), std::flat_map<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
67void test_containers_compare() {
68 std::deque<int, test_allocator<int>> ks({1, 2, 1, INT_MAX, 3}, test_allocator<int>(0, 42));
69 std::deque<short, test_allocator<short>> vs({1, 2, 1, 4, 5}, test_allocator<int>(0, 43));
70 std::deque<int, test_allocator<int>> sorted_ks({INT_MAX, 3, 2, 1}, test_allocator<int>(0, 42));
71 std::deque<short, test_allocator<short>> sorted_vs({4, 5, 2, 1}, test_allocator<int>(0, 43));
72 const std::pair<int, short> expected[] = {{INT_MAX, 4}, {3, 5}, {2, 2}, {1, 1}};
73 {
74 std::pmr::monotonic_buffer_resource mr;
75 std::pmr::monotonic_buffer_resource mr2;
76 std::pmr::deque<int> pks(ks.begin(), ks.end(), &mr);
77 std::pmr::deque<short> pvs(vs.begin(), vs.end(), &mr);
78 std::flat_map s(std::move(pks), std::move(pvs), std::greater<int>(), &mr2);
79
80 ASSERT_SAME_TYPE(
81 decltype(s), std::flat_map<int, short, std::greater<int>, std::pmr::deque<int>, std::pmr::deque<short>>);
82 assert(std::ranges::equal(s, expected));
83 assert(s.keys().get_allocator().resource() == &mr2);
84 assert(s.values().get_allocator().resource() == &mr2);
85 }
86 {
87 std::pmr::monotonic_buffer_resource mr;
88 std::pmr::monotonic_buffer_resource mr2;
89 std::pmr::deque<int> pks(sorted_ks.begin(), sorted_ks.end(), &mr);
90 std::pmr::deque<short> pvs(sorted_vs.begin(), sorted_vs.end(), &mr);
91 std::flat_map s(std::sorted_unique, std::move(pks), std::move(pvs), std::greater<int>(), &mr2);
92
93 ASSERT_SAME_TYPE(
94 decltype(s), std::flat_map<int, short, std::greater<int>, std::pmr::deque<int>, std::pmr::deque<short>>);
95 assert(std::ranges::equal(s, expected));
96 assert(s.keys().get_allocator().resource() == &mr2);
97 assert(s.values().get_allocator().resource() == &mr2);
98 }
99}
100
101int main(int, char**) {
102 test_containers();
103 test_containers_compare();
104
105 return 0;
106}
107

source code of libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/deduct_pmr.pass.cpp