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