xref: /llvm-project/libcxx/test/std/utilities/optional/optional.nullops/compare.three_way.pass.cpp (revision 03cda77409023e64d6338dcb4e36bf1e40b33ea3)
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.nullops], comparison with nullopt
14 
15 // template<class T>
16 //   constexpr strong_ordering operator<=>(const optional<T>&, nullopt_t) noexcept;
17 
18 #include <cassert>
19 #include <compare>
20 #include <optional>
21 
22 #include "test_comparisons.h"
23 
test()24 constexpr bool test() {
25   {
26     std::optional<int> op;
27     assert((std::nullopt <=> op) == std::strong_ordering::equal);
28     assert(testOrder(std::nullopt, op, std::strong_ordering::equal));
29     assert((op <=> std::nullopt) == std::strong_ordering::equal);
30     assert(testOrder(op, std::nullopt, std::strong_ordering::equal));
31   }
32   {
33     std::optional<int> op{1};
34     assert((std::nullopt <=> op) == std::strong_ordering::less);
35     assert(testOrder(std::nullopt, op, std::strong_ordering::less));
36   }
37   {
38     std::optional<int> op{1};
39     assert((op <=> std::nullopt) == std::strong_ordering::greater);
40     assert(testOrder(op, std::nullopt, std::strong_ordering::greater));
41   }
42 
43   return true;
44 }
45 
main(int,char **)46 int main(int, char**) {
47   assert(test());
48   static_assert(test());
49   return 0;
50 }
51