Debian Codegraph @

libcxx/test/std/utilities/expected/expected.expected/equality/equality.other_expected.pass.cpp

raw
1 //===----------------------------------------------------------------------===//
2 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3 // See https://llvm.org/LICENSE.txt for license information.
4 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5 //
6 //===----------------------------------------------------------------------===//
7
8 // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
9
10 // template<class T2, class E2> requires (!is_void_v<T2>)
11 // friend constexpr bool operator==(const expected& x, const expected<T2, E2>& y);
12
13 #include <cassert>
14 #include <concepts>
15 #include <expected>
16 #include <type_traits>
17 #include <utility>
18
19 #include "test_macros.h"
20
21 // Test constraint
22 template <class T1, class T2>
23 concept CanCompare = requires(T1 t1, T2 t2) { t1 == t2; };
24
25 struct Foo{};
26 static_assert(!CanCompare<Foo, Foo>);
27
28 static_assert(CanCompare<std::expected<int, int>, std::expected<int, int>>);
29 static_assert(CanCompare<std::expected<int, int>, std::expected<short, short>>);
30
31 // Note this is true because other overloads are unconstrained
32 static_assert(CanCompare<std::expected<int, int>, std::expected<void, int>>);
33
34 constexpr bool test() {
35 // x.has_value() && y.has_value()
36 {
37 const std::expected<int, int> e1(5);
38 const std::expected<int, int> e2(10);
39 const std::expected<int, int> e3(5);
40 assert(e1 != e2);
41 assert(e1 == e3);
42 }
43
44 // !x.has_value() && y.has_value()
45 {
46 const std::expected<int, int> e1(std::unexpect, 5);
47 const std::expected<int, int> e2(10);
48 const std::expected<int, int> e3(5);
49 assert(e1 != e2);
50 assert(e1 != e3);
51 }
52
53 // x.has_value() && !y.has_value()
54 {
55 const std::expected<int, int> e1(5);
56 const std::expected<int, int> e2(std::unexpect, 10);
57 const std::expected<int, int> e3(std::unexpect, 5);
58 assert(e1 != e2);
59 assert(e1 != e3);
60 }
61
62 // !x.has_value() && !y.has_value()
63 {
64 const std::expected<int, int> e1(std::unexpect, 5);
65 const std::expected<int, int> e2(std::unexpect, 10);
66 const std::expected<int, int> e3(std::unexpect, 5);
67 assert(e1 != e2);
68 assert(e1 == e3);
69 }
70
71 return true;
72 }
73
74 int main(int, char**) {
75 test();
76 static_assert(test());
77 return 0;
78 }