Skip to content

Commit d5927a7

Browse files
committed
Update iterator tests
1 parent 6a1545f commit d5927a7

File tree

3 files changed

+124
-1
lines changed

3 files changed

+124
-1
lines changed

libcxx/test/libcxx/utilities/optional/optional.iterator/iterator.compile.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
// REQUIRES: std-at-least-c++26
10-
// UNSUPPORTED: generic-hardening
10+
1111
// <optional>
1212

1313
// template <class T> class optional::iterator;

libcxx/test/std/utilities/optional/optional.iterator/iterator.pass.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,30 @@ constexpr bool test() {
8686
assert(*(val.begin()) == v);
8787
}
8888

89+
// [container.reqmts] operator-
90+
{
91+
std::optional<T> val(v);
92+
auto it1 = val.begin();
93+
auto it2 = val.begin();
94+
auto it3 = val.end();
95+
96+
auto cit1 = std::as_const(val).begin();
97+
auto cit2 = std::as_const(val).begin();
98+
auto cit3 = std::as_const(val).end();
99+
100+
assert(it1 - it2 == 0);
101+
assert(cit1 - cit2 == 0);
102+
assert(it1 - cit1 == 0);
103+
assert(it3 - it1 == 1);
104+
assert(it1 - it3 == -1);
105+
106+
assert(cit3 - cit1 == 1);
107+
assert(cit1 - cit3 == -1);
108+
assert(cit3 - cit3 == 0);
109+
assert(cit3 - it1 == 1);
110+
assert(it1 - cit3 == -1);
111+
}
112+
89113
return true;
90114
}
91115

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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

Comments
 (0)