|
| 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 | +// REQUIRES: std-at-least-c++26 |
| 10 | + |
| 11 | +// <optional> |
| 12 | + |
| 13 | +// template <class T> class optional::iterator; |
| 14 | +// template <class T> class optional::const_iterator; |
| 15 | + |
| 16 | +#include <cassert> |
| 17 | +#include <compare> |
| 18 | +#include <concepts> |
| 19 | +#include <optional> |
| 20 | +#include <type_traits> |
| 21 | +#include <utility> |
| 22 | + |
| 23 | +template<typename T> |
| 24 | +constexpr bool test() { |
| 25 | + using Opt = std::optional<T>; |
| 26 | + using I = Opt::iterator; |
| 27 | + using CI = Opt::const_iterator; |
| 28 | + |
| 29 | + static_assert(std::three_way_comparable<I>); |
| 30 | + static_assert(std::three_way_comparable<CI>); |
| 31 | + |
| 32 | + std::remove_reference_t<T> t{}; |
| 33 | + Opt opt{t}; |
| 34 | + |
| 35 | + // [container.reqmts] tests for comparison operators of optional::iterator and optional::const_iterator |
| 36 | + auto it1 = opt.begin(); |
| 37 | + |
| 38 | + { |
| 39 | + auto it2 = opt.begin(); |
| 40 | + assert(it1 == it2); |
| 41 | + assert(!(it1 != it2)); |
| 42 | + |
| 43 | + static_assert(std::same_as<decltype(it1 <=> it2), std::strong_ordering>); |
| 44 | + assert(it1 <=> it2 == std::strong_ordering::equal); |
| 45 | + } |
| 46 | + |
| 47 | + { |
| 48 | + auto it3 = opt.end(); |
| 49 | + assert(it1 != it3); |
| 50 | + assert(it1 <= it3); |
| 51 | + assert(it1 < it3); |
| 52 | + assert(it3 >= it1); |
| 53 | + assert(it3 > it1); |
| 54 | + |
| 55 | + assert(it1 <=> it3 == std::strong_ordering::less); |
| 56 | + assert(it3 <=> it1 == std::strong_ordering::greater); |
| 57 | + } |
| 58 | + |
| 59 | + auto cit1 = std::as_const(opt).begin(); |
| 60 | + |
| 61 | + { |
| 62 | + auto cit2 = std::as_const(opt).begin(); |
| 63 | + assert(cit1 == cit2); |
| 64 | + assert(!(cit1 != cit2)); |
| 65 | + |
| 66 | + static_assert(std::same_as<decltype(cit1 <=> cit2), std::strong_ordering>); |
| 67 | + assert(cit1 <=> cit2 == std::strong_ordering::equal); |
| 68 | + } |
| 69 | + |
| 70 | + { |
| 71 | + auto cit3 = std::as_const(opt).end(); |
| 72 | + |
| 73 | + assert(cit1 <= cit3); |
| 74 | + assert(cit1 < cit3); |
| 75 | + assert(cit3 >= cit1); |
| 76 | + assert(cit3 > cit1); |
| 77 | + |
| 78 | + assert(cit1 <=> cit3 == std::strong_ordering::less); |
| 79 | + assert(cit3 <=> cit1 == std::strong_ordering::greater); |
| 80 | + } |
| 81 | + |
| 82 | + return true; |
| 83 | +} |
| 84 | + |
| 85 | + |
| 86 | +constexpr bool test() { |
| 87 | + test<int>(); |
| 88 | + test<char>(); |
| 89 | + test<int&>(); |
| 90 | + |
| 91 | + return true; |
| 92 | +} |
| 93 | + |
| 94 | +int main(int, char**) { |
| 95 | + assert(test()); |
| 96 | + static_assert(test()); |
| 97 | + |
| 98 | + return 0; |
| 99 | +} |
0 commit comments