aboutsummaryrefslogtreecommitdiff
path: root/libcxx/test/std/utilities/optional/optional.iterator/end.pass.cpp
blob: 966c3e74418800404a496cdd1fb96e4aafe0164c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// REQUIRES: std-at-least-c++26

// <optional>

// constexpr iterator optional::end() noexcept;
// constexpr const_iterator optional::end() const noexcept;

#include <cassert>
#include <iterator>
#include <optional>
#include <ranges>
#include <utility>

template <typename T>
constexpr bool test() {
  std::optional<T> disengaged{std::nullopt};

  { // end() is marked noexcept
    static_assert(noexcept(disengaged.end()));
    static_assert(noexcept(std::as_const(disengaged).end()));
  }

  { // end() == begin() and end() == end() if the optional is disengaged
    auto it  = disengaged.end();
    auto it2 = std::as_const(disengaged).end();

    assert(it == disengaged.begin());
    assert(disengaged.begin() == it);
    assert(it == disengaged.end());

    assert(it2 == std::as_const(disengaged).begin());
    assert(std::as_const(disengaged).begin() == it2);
    assert(it2 == std::as_const(disengaged).end());
  }

  std::optional<T> engaged{T{}};

  { // end() != begin() if the optional is engaged
    auto it  = engaged.end();
    auto it2 = std::as_const(engaged).end();

    assert(it != engaged.begin());
    assert(engaged.begin() != it);

    assert(it2 != std::as_const(engaged).begin());
    assert(std::as_const(engaged).begin() != it2);
  }

  return true;
}

constexpr bool tests() {
  assert(test<int>());
  assert(test<char>());
  assert(test<const int>());
  assert(test<const char>());

  return true;
}

int main(int, char**) {
  assert(tests());
  static_assert(tests());

  return 0;
}