Debian Codegraph @

libcxx/test/std/utilities/expected/expected.void/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<void, int>, std::expected<void, int>>);
29 static_assert(CanCompare<std::expected<void, int>, std::expected<void, short>>);
30
31 // Note this is true because other overloads in expected<non-void> are unconstrained
32 static_assert(CanCompare<std::expected<void, int>, std::expected<int, int>>);
33
34 constexpr bool test() {
35 // x.has_value() && y.has_value()
36 {
37 const std::expected<void, int> e1;
38 const std::expected<void, int> e2;
39 assert(e1 == e2);
40 }
41
42 // !x.has_value() && y.has_value()
43 {
44 const std::expected<void, int> e1(std::unexpect, 5);
45 const std::expected<void, int> e2;
46 assert(e1 != e2);
47 }
48
49 // x.has_value() && !y.has_value()
50 {
51 const std::expected<void, int> e1;
52 const std::expected<void, int> e2(std::unexpect, 10);
53 const std::expected<void, int> e3(std::unexpect, 5);
54 assert(e1 != e2);
55 assert(e1 != e3);
56 }
57
58 // !x.has_value() && !y.has_value()
59 {
60 const std::expected<void, int> e1(std::unexpect, 5);
61 const std::expected<void, int> e2(std::unexpect, 10);
62 const std::expected<void, int> e3(std::unexpect, 5);
63 assert(e1 != e2);
64 assert(e1 == e3);
65 }
66
67 return true;
68 }
69
70 int main(int, char**) {
71 test();
72 static_assert(test());
73 return 0;
74 }