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// <optional>
12
13// [optional.relops], relational operators
14
15// template<class T, three_way_comparable_with<T> U>
16// constexpr compare_three_way_result_t<T, U>
17// operator<=>(const optional<T>&, const optional<U>&);
18
19#include <cassert>
20#include <compare>
21#include <optional>
22
23#include "test_comparisons.h"
24
25constexpr bool test() {
26 {
27 std::optional<int> op1;
28 std::optional<int> op2;
29
30 assert((op1 <=> op2) == std::strong_ordering::equal);
31 assert(testOrder(op1, op2, std::strong_ordering::equal));
32 }
33 {
34 std::optional<int> op1{3};
35 std::optional<int> op2{3};
36 assert((op1 <=> op1) == std::strong_ordering::equal);
37 assert(testOrder(op1, op1, std::strong_ordering::equal));
38 assert((op1 <=> op2) == std::strong_ordering::equal);
39 assert(testOrder(op1, op2, std::strong_ordering::equal));
40 assert((op2 <=> op1) == std::strong_ordering::equal);
41 assert(testOrder(op2, op1, std::strong_ordering::equal));
42 }
43 {
44 std::optional<int> op;
45 std::optional<int> op1{2};
46 std::optional<int> op2{3};
47 assert((op <=> op2) == std::strong_ordering::less);
48 assert(testOrder(op, op2, std::strong_ordering::less));
49 assert((op1 <=> op2) == std::strong_ordering::less);
50 assert(testOrder(op1, op2, std::strong_ordering::less));
51 }
52 {
53 std::optional<int> op;
54 std::optional<int> op1{3};
55 std::optional<int> op2{2};
56 assert((op1 <=> op) == std::strong_ordering::greater);
57 assert(testOrder(op1, op, std::strong_ordering::greater));
58 assert((op1 <=> op2) == std::strong_ordering::greater);
59 assert(testOrder(op1, op2, std::strong_ordering::greater));
60 }
61
62 return true;
63}
64
65int main(int, char**) {
66 assert(test());
67 static_assert(test());
68 return 0;
69}
70

source code of libcxx/test/std/utilities/optional/optional.relops/compare.three_way.pass.cpp