|
| 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 <iterator> |
| 18 | +#include <optional> |
| 19 | +#include <ranges> |
| 20 | +#include <type_traits> |
| 21 | + |
| 22 | +template <typename T, T __val> |
| 23 | +constexpr bool test() { |
| 24 | + constexpr std::optional<T> opt{__val}; |
| 25 | + std::optional<T> nonconst_opt{__val}; |
| 26 | + |
| 27 | + { // Dereferencing an iterator of an engaged optional will return the same value that the optional holds. |
| 28 | + auto it = opt.begin(); |
| 29 | + auto it2 = nonconst_opt.begin(); |
| 30 | + assert(*it == *opt); |
| 31 | + assert(*it2 == *nonconst_opt); |
| 32 | + } |
| 33 | + |
| 34 | + { // optional::iterator and optional::const_iterator satisfy the Cpp17RandomAccessIterator and contiguous iterator. |
| 35 | + auto it = opt.begin(); |
| 36 | + auto it2 = nonconst_opt.begin(); |
| 37 | + assert(std::contiguous_iterator<decltype(it)>); |
| 38 | + assert(std::contiguous_iterator<decltype(it2)>); |
| 39 | + |
| 40 | + assert(std::random_access_iterator<decltype(it)>); |
| 41 | + assert(std::random_access_iterator<decltype(it2)>); |
| 42 | + } |
| 43 | + |
| 44 | + { // const_iterator::value_type == std::remove_cv_t<T>, const_iterator::reference == const T&, iterator::value_type = std::remove_cv_t<T>, iterator::reference == T& |
| 45 | + auto it = opt.begin(); |
| 46 | + auto it2 = nonconst_opt.begin(); |
| 47 | + assert((std::is_same_v<typename decltype(it)::value_type, std::remove_cv_t<T>>)); |
| 48 | + assert((std::is_same_v<typename decltype(it)::reference, const T&>)); |
| 49 | + assert((std::is_same_v<typename decltype(it2)::value_type, std::remove_cv_t<T>>)); |
| 50 | + assert((std::is_same_v<typename decltype(it2)::reference, T&>)); |
| 51 | + } |
| 52 | + |
| 53 | + { // std::ranges for an engaged optional<T> == 1, unengaged optional<T> == 0 |
| 54 | + constexpr std::optional<T> unengaged{std::nullopt}; |
| 55 | + std::optional<T> unengaged2{std::nullopt}; |
| 56 | + assert(std::ranges::size(opt) == 1); |
| 57 | + assert(std::ranges::size(nonconst_opt) == 1); |
| 58 | + |
| 59 | + assert(std::ranges::size(unengaged) == 0); |
| 60 | + assert(std::ranges::size(unengaged2) == 0); |
| 61 | + } |
| 62 | + |
| 63 | + { // std::ranges::enable_view<optional<T>> == true, and std::format_kind<optional<T>> == true |
| 64 | + assert(std::ranges::enable_view<std::optional<T>> == true); |
| 65 | + assert(std::format_kind<std::optional<T>> == std::range_format::disabled); |
| 66 | + } |
| 67 | + |
| 68 | + // 8: An optional with value that is reset will have a begin() == end(), then when it is reassigned a value, begin() != end(), and *begin() will contain the new value. |
| 69 | + { |
| 70 | + std::optional<T> val{__val}; |
| 71 | + assert(val.begin() != val.end()); |
| 72 | + val.reset(); |
| 73 | + assert(val.begin() == val.end()); |
| 74 | + val.emplace(__val); |
| 75 | + assert(val.begin() != val.end()); |
| 76 | + assert(*(val.begin()) == __val); |
| 77 | + } |
| 78 | + |
| 79 | + return true; |
| 80 | +} |
| 81 | + |
| 82 | +constexpr bool tests() { |
| 83 | + assert((test<int, 1>())); |
| 84 | + assert((test<char, 'a'>())); |
| 85 | + assert((test<bool, true>())); |
| 86 | + assert((test<const int, 2>())); |
| 87 | + assert((test<const char, 'b'>())); |
| 88 | + |
| 89 | + return true; |
| 90 | +} |
| 91 | + |
| 92 | +int main() { |
| 93 | + assert(tests()); |
| 94 | + static_assert(tests()); |
| 95 | +} |
0 commit comments