xref: /llvm-project/libcxx/test/std/utilities/function.objects/range.cmp/not_equal_to.pass.cpp (revision 53406fb691db38b21decf233e091f648f8317b2d)
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
10 // UNSUPPORTED: libcpp-no-concepts
11 // UNSUPPORTED: libcpp-has-no-incomplete-ranges
12 
13 // <functional>
14 
15 // ranges::not_equal_to
16 
17 #include <functional>
18 #include <type_traits>
19 #include <cassert>
20 
21 #include "test_macros.h"
22 #include "compare_types.h"
23 #include "MoveOnly.h"
24 #include "pointer_comparison_test_helper.h"
25 
26 struct NotEqualityComparable {
27   friend bool operator==(const NotEqualityComparable&, const NotEqualityComparable&);
28   friend bool operator!=(const NotEqualityComparable&, const NotEqualityComparable&) = delete;
29 };
30 
31 static_assert(!std::is_invocable_v<std::ranges::equal_to, NotEqualityComparable, NotEqualityComparable>);
32 static_assert(!std::is_invocable_v<std::ranges::equal_to, int, MoveOnly>);
33 static_assert(std::is_invocable_v<std::ranges::equal_to, explicit_operators, explicit_operators>);
34 
35 static_assert(requires { typename std::ranges::not_equal_to::is_transparent; });
36 
37 struct PtrAndNotEqOperator {
38   constexpr operator void*() const { return nullptr; }
39   // We *don't* want operator!= to be picked here.
40   friend constexpr bool operator!=(PtrAndNotEqOperator, PtrAndNotEqOperator) { return true; }
41 };
42 
43 constexpr bool test() {
44   auto fn = std::ranges::not_equal_to();
45 
46   assert(fn(MoveOnly(41), MoveOnly(42)));
47 
48   // These are the opposite of other tests.
49   ForwardingTestObject a;
50   ForwardingTestObject b;
51   assert(fn(a, b));
52   assert(!fn(std::move(a), std::move(b)));
53 
54   assert(fn(1, 2));
55   assert(!fn(2, 2));
56   assert(fn(2, 1));
57 
58   assert(fn(2, 1L));
59 
60   // Make sure that "ranges::equal_to(x, y) == !ranges::not_equal_to(x, y)", even here.
61   assert(!fn(PtrAndNotEqOperator(), PtrAndNotEqOperator()));
62   assert(std::ranges::equal_to()(PtrAndNotEqOperator(), PtrAndNotEqOperator()));
63 
64   return true;
65 }
66 
67 int main(int, char**) {
68 
69   test();
70   static_assert(test());
71 
72   // test total ordering of int* for not_equal_to<int*> and not_equal_to<void>.
73   do_pointer_comparison_test(std::ranges::not_equal_to());
74 
75   return 0;
76 }
77