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

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