Skip to content

Commit b63f2a9

Browse files
committed
Split test file into three tests
1 parent f782e10 commit b63f2a9

File tree

4 files changed

+219
-164
lines changed

4 files changed

+219
-164
lines changed

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

Lines changed: 0 additions & 164 deletions
This file was deleted.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
// constexpr iterator optional::begin() noexcept;
14+
// constexpr const_iterator optional::begin() noexcept;
15+
16+
#include <cassert>
17+
#include <iterator>
18+
#include <optional>
19+
#include <type_traits>
20+
21+
template <typename T>
22+
constexpr bool test() {
23+
constexpr std::optional<T> opt{T{}};
24+
std::optional<T> nonconst_opt{T{}};
25+
26+
{ // begin() is marked noexcept
27+
assert(noexcept(opt.begin()));
28+
assert(noexcept(nonconst_opt.begin()));
29+
}
30+
31+
{ // Dereferencing an iterator at the beggining as indexing the 0th element, and that those iterators are the same value returned by calling begin() again.
32+
auto iter1 = opt.begin();
33+
auto iter2 = nonconst_opt.begin();
34+
assert(*iter1 == iter1[0]);
35+
assert(*iter2 == iter2[0]);
36+
assert(iter1 == opt.begin());
37+
assert(iter2 == nonconst_opt.begin());
38+
}
39+
40+
return true;
41+
}
42+
43+
constexpr bool tests() {
44+
assert(test<int>());
45+
assert(test<char>());
46+
assert(test<const int>());
47+
assert(test<const char>());
48+
return true;
49+
}
50+
51+
int main() {
52+
assert(tests());
53+
static_assert(tests());
54+
55+
return 0;
56+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
// constexpr iterator optional::end() noexcept;
14+
// constexpr const_iterator optional::end() noexcept;
15+
16+
#include <cassert>
17+
#include <iterator>
18+
#include <ranges>
19+
#include <optional>
20+
21+
template <typename T>
22+
constexpr bool test() {
23+
std::optional<T> unengaged{std::nullopt};
24+
constexpr std::optional<T> unengaged2{std::nullopt};
25+
26+
{ // end() == begin() and end() == end() if the optional is unengaged
27+
auto it = unengaged.end();
28+
auto it2 = unengaged2.end();
29+
30+
assert(it == unengaged.begin());
31+
assert(unengaged.begin() == it);
32+
assert(it == unengaged.end());
33+
34+
assert(it2 == unengaged2.begin());
35+
assert(unengaged2.begin() == it2);
36+
assert(it2 == unengaged2.end());
37+
}
38+
39+
std::optional<T> engaged{T{}};
40+
constexpr std::optional<T> engaged2{T{}};
41+
42+
{ // end() != begin() if the optional is engaged
43+
auto it = engaged.end();
44+
auto it2 = engaged2.end();
45+
46+
assert(it != engaged.begin());
47+
assert(engaged.begin() != it);
48+
49+
assert(it2 != engaged2.begin());
50+
assert(engaged2.begin() != it2);
51+
}
52+
53+
return true;
54+
}
55+
56+
constexpr bool tests() {
57+
assert(test<int>());
58+
assert(test<char>());
59+
assert(test<const int>());
60+
assert(test<const char>());
61+
62+
return true;
63+
}
64+
65+
int main() {
66+
assert(tests());
67+
static_assert(tests());
68+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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

Comments
 (0)