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