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// friend bool operator==(const flat_multimap& x, const flat_multimap& y);
16// friend synth-three-way-result<value_type>
17// operator<=>(const flat_multimap& x, const flat_multimap& y);
18
19#include <algorithm>
20#include <cassert>
21#include <deque>
22#include <compare>
23#include <flat_map>
24#include <functional>
25#include <limits>
26#include <vector>
27
28#include "MinSequenceContainer.h"
29#include "test_macros.h"
30#include "min_allocator.h"
31#include "test_allocator.h"
32#include "test_comparisons.h"
33#include "test_container_comparisons.h"
34
35template <class KeyContainer, class ValueContainer>
36void test() {
37 using Key = typename KeyContainer::value_type;
38 using Value = typename ValueContainer::value_type;
39
40 {
41 using C = std::flat_multimap<Key, Value>;
42 C s1 = {{1, 1}};
43 C s2 = {{2, 0}}; // {{1,1}} versus {{2,0}}
44 ASSERT_SAME_TYPE(decltype(s1 <=> s2), std::strong_ordering);
45 AssertComparisonsReturnBool<C>();
46 assert(testComparisons(s1, s2, false, true));
47 s2 = {{1, 1}}; // {{1,1}} versus {{1,1}}
48 assert(testComparisons(s1, s2, true, false));
49 s2 = {{1, 1}, {2, 0}}; // {{1,1}} versus {{1,1},{2,0}}
50 assert(testComparisons(s1, s2, false, true));
51 s1 = {{0, 0}, {1, 1}, {2, 2}}; // {{0,0},{1,1},{2,2}} versus {{1,1},{2,0}}
52 assert(testComparisons(s1, s2, false, true));
53 s2 = {{0, 0}, {1, 1}, {2, 3}}; // {{0,0},{1,1},{2,2}} versus {{0,0},{1,1},{2,3}}
54 assert(testComparisons(s1, s2, false, true));
55
56 s1 = {{1, 1}, {1, 1}};
57 s2 = {{1, 1}, {1, 1}};
58 assert(testComparisons(s1, s2, true, false));
59
60 s2 = {{1, 1}, {1, 1}, {2, 2}};
61 assert(testComparisons(s1, s2, false, true));
62
63 s2 = {{1, 1}, {2, 2}, {2, 2}};
64 assert(testComparisons(s1, s2, false, true));
65
66 s2 = {{0, 0}, {1, 1}, {1, 1}};
67 assert(testComparisons(s1, s2, false, false));
68 }
69 {
70 // Comparisons use value_type's native operators, not the comparator
71 using C = std::flat_multimap<Key, Value, std::greater<Key>>;
72 C s1 = {{1, 1}};
73 C s2 = {{2, 0}}; // {{1,1}} versus {{2,0}}
74 ASSERT_SAME_TYPE(decltype(s1 <=> s2), std::strong_ordering);
75 AssertComparisonsReturnBool<C>();
76 assert(testComparisons(s1, s2, false, true));
77 s2 = {{1, 1}}; // {{1,1}} versus {{1,1}}
78 assert(testComparisons(s1, s2, true, false));
79 s2 = {{1, 1}, {2, 0}}; // {{1,1}} versus {{2,0},{1,1}}
80 assert(testComparisons(s1, s2, false, true));
81 s1 = {{0, 0}, {1, 1}, {2, 2}}; // {{2,2},{1,1},{0,0}} versus {2,0},{1,1}}
82 assert(testComparisons(s1, s2, false, false));
83 s2 = {{0, 0}, {1, 1}, {2, 3}}; // {{2,2},{1,1},{0,0}} versus {{2,3},{1,1},{0,0}}
84 assert(testComparisons(s1, s2, false, true));
85 }
86}
87
88int main(int, char**) {
89 test<std::vector<int>, std::vector<int>>();
90 test<std::deque<int>, std::deque<int>>();
91 test<MinSequenceContainer<int>, MinSequenceContainer<int>>();
92 test<std::vector<int, min_allocator<int>>, std::vector<int, min_allocator<int>>>();
93 test<std::vector<int, min_allocator<int>>, std::vector<int, min_allocator<int>>>();
94
95 {
96 using C = std::flat_multimap<double, int>;
97 C s1 = {{1, 1}};
98 C s2 = C(std::sorted_equivalent, {{std::numeric_limits<double>::quiet_NaN(), 2}});
99 ASSERT_SAME_TYPE(decltype(s1 <=> s2), std::partial_ordering);
100 AssertComparisonsReturnBool<C>();
101 assert(testComparisonsComplete(s1, s2, false, false, false));
102 }
103 {
104 using C = std::flat_multimap<int, double>;
105 C s1 = {{1, 1}};
106 C s2 = C(std::sorted_equivalent, {{2, std::numeric_limits<double>::quiet_NaN()}});
107 ASSERT_SAME_TYPE(decltype(s1 <=> s2), std::partial_ordering);
108 AssertComparisonsReturnBool<C>();
109 assert(testComparisonsComplete(s1, s2, false, true, false));
110 s2 = C(std::sorted_equivalent, {{1, std::numeric_limits<double>::quiet_NaN()}});
111 assert(testComparisonsComplete(s1, s2, false, false, false));
112 }
113 {
114 // Comparisons use value_type's native operators, not the comparator
115 struct StrongComp {
116 bool operator()(double a, double b) const { return std::strong_order(a, b) < 0; }
117 };
118 using C = std::flat_multimap<double, double, StrongComp>;
119 C s1 = {{1, 1}};
120 C s2 = {{std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN()}};
121 ASSERT_SAME_TYPE(decltype(s1 <=> s2), std::partial_ordering);
122 AssertComparisonsReturnBool<C>();
123 assert(testComparisonsComplete(s1, s2, false, false, false));
124 s1 = {{{1, 1}, {std::numeric_limits<double>::quiet_NaN(), 1}}};
125 s2 = {{{std::numeric_limits<double>::quiet_NaN(), 1}, {1, 1}}};
126 assert(std::lexicographical_compare_three_way(
127 s1.keys().begin(), s1.keys().end(), s2.keys().begin(), s2.keys().end(), std::strong_order) ==
128 std::strong_ordering::equal);
129 assert(s1 != s2);
130 assert((s1 <=> s2) == std::partial_ordering::unordered);
131 }
132 return 0;
133}
134

source code of libcxx/test/std/containers/container.adaptors/flat.multimap/op_compare.pass.cpp