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

source code of libcxx/test/std/containers/container.adaptors/flat.map/flat.map.observers/comp.pass.cpp