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 | // template<class T2, class E2> requires (!is_void_v<T2>) |
12 | // friend constexpr bool operator==(const expected& x, const expected<T2, E2>& y); |
13 | |
14 | #include <cassert> |
15 | #include <concepts> |
16 | #include <expected> |
17 | #include <type_traits> |
18 | #include <utility> |
19 | |
20 | #include "test_macros.h" |
21 | #include "../../types.h" |
22 | |
23 | // Test constraint |
24 | static_assert(!CanCompare<NonComparable, NonComparable>); |
25 | |
26 | static_assert(CanCompare<std::expected<int, int>, std::expected<int, int>>); |
27 | static_assert(CanCompare<std::expected<int, int>, std::expected<short, short>>); |
28 | |
29 | #if TEST_STD_VER >= 26 |
30 | // https://wg21.link/P3379R0 |
31 | static_assert(!CanCompare<std::expected<int, int>, std::expected<void, int>>); |
32 | static_assert(CanCompare<std::expected<int, int>, std::expected<int, int>>); |
33 | static_assert(!CanCompare<std::expected<NonComparable, int>, std::expected<NonComparable, int>>); |
34 | static_assert(!CanCompare<std::expected<int, NonComparable>, std::expected<int, NonComparable>>); |
35 | static_assert(!CanCompare<std::expected<NonComparable, int>, std::expected<int, NonComparable>>); |
36 | static_assert(!CanCompare<std::expected<int, NonComparable>, std::expected<NonComparable, int>>); |
37 | #else |
38 | // Note this is true because other overloads in expected<non-void> are unconstrained |
39 | static_assert(CanCompare<std::expected<void, int>, std::expected<int, int>>); |
40 | #endif |
41 | constexpr bool test() { |
42 | // x.has_value() && y.has_value() |
43 | { |
44 | const std::expected<int, int> e1(5); |
45 | const std::expected<int, int> e2(10); |
46 | const std::expected<int, int> e3(5); |
47 | assert(e1 != e2); |
48 | assert(e1 == e3); |
49 | } |
50 | |
51 | // !x.has_value() && y.has_value() |
52 | { |
53 | const std::expected<int, int> e1(std::unexpect, 5); |
54 | const std::expected<int, int> e2(10); |
55 | const std::expected<int, int> e3(5); |
56 | assert(e1 != e2); |
57 | assert(e1 != e3); |
58 | } |
59 | |
60 | // x.has_value() && !y.has_value() |
61 | { |
62 | const std::expected<int, int> e1(5); |
63 | const std::expected<int, int> e2(std::unexpect, 10); |
64 | const std::expected<int, int> e3(std::unexpect, 5); |
65 | assert(e1 != e2); |
66 | assert(e1 != e3); |
67 | } |
68 | |
69 | // !x.has_value() && !y.has_value() |
70 | { |
71 | const std::expected<int, int> e1(std::unexpect, 5); |
72 | const std::expected<int, int> e2(std::unexpect, 10); |
73 | const std::expected<int, int> e3(std::unexpect, 5); |
74 | assert(e1 != e2); |
75 | assert(e1 == e3); |
76 | } |
77 | |
78 | return true; |
79 | } |
80 | |
81 | int main(int, char**) { |
82 | test(); |
83 | static_assert(test()); |
84 | return 0; |
85 | } |
86 | |