Skip to content

Commit 718af43

Browse files
committed
Use std::as_const pattern for all tests
1 parent c0ff08a commit 718af43

File tree

3 files changed

+24
-27
lines changed

3 files changed

+24
-27
lines changed

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// <optional>
1212

1313
// constexpr iterator optional::begin() noexcept;
14-
// constexpr const_iterator optional::begin() noexcept;
14+
// constexpr const_iterator optional::begin() const noexcept;
1515

1616
#include <cassert>
1717
#include <iterator>
@@ -21,21 +21,20 @@
2121

2222
template <typename T>
2323
constexpr bool test() {
24-
const std::optional<T> opt{T{}};
25-
std::optional<T> nonconst_opt{T{}};
24+
std::optional<T> opt{T{}};
2625

2726
{ // begin() is marked noexcept
2827
static_assert(noexcept(opt.begin()));
29-
static_assert(noexcept(nonconst_opt.begin()));
28+
static_assert(noexcept(std::as_const(opt).begin()));
3029
}
3130

3231
{ // Dereferencing an iterator at the beginning == indexing the 0th element, and that calling begin() again return the same iterator.
3332
auto iter1 = opt.begin();
34-
auto iter2 = nonconst_opt.begin();
33+
auto iter2 = std::as_const(opt).begin();
3534
assert(*iter1 == iter1[0]);
3635
assert(*iter2 == iter2[0]);
3736
assert(iter1 == opt.begin());
38-
assert(iter2 == nonconst_opt.begin());
37+
assert(iter2 == std::as_const(opt).begin());
3938
}
4039

4140
{ // Calling begin() multiple times on a disengaged optional returns the same iterator.

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,48 +11,47 @@
1111
// <optional>
1212

1313
// constexpr iterator optional::end() noexcept;
14-
// constexpr const_iterator optional::end() noexcept;
14+
// constexpr const_iterator optional::end() const noexcept;
1515

1616
#include <cassert>
1717
#include <iterator>
18-
#include <ranges>
1918
#include <optional>
19+
#include <ranges>
20+
#include <utility>
2021

2122
template <typename T>
2223
constexpr bool test() {
2324
std::optional<T> disengaged{std::nullopt};
24-
const std::optional<T> disengaged2{std::nullopt};
2525

2626
{ // end() is marked noexcept
2727
static_assert(noexcept(disengaged.end()));
28-
static_assert(noexcept(disengaged2.end()));
28+
static_assert(noexcept(std::as_const(disengaged).end()));
2929
}
3030

3131
{ // end() == begin() and end() == end() if the optional is disengaged
3232
auto it = disengaged.end();
33-
auto it2 = disengaged2.end();
33+
auto it2 = std::as_const(disengaged).end();
3434

3535
assert(it == disengaged.begin());
3636
assert(disengaged.begin() == it);
3737
assert(it == disengaged.end());
3838

39-
assert(it2 == disengaged2.begin());
40-
assert(disengaged2.begin() == it2);
41-
assert(it2 == disengaged2.end());
39+
assert(it2 == std::as_const(disengaged).begin());
40+
assert(std::as_const(disengaged).begin() == it2);
41+
assert(it2 == std::as_const(disengaged).end());
4242
}
4343

4444
std::optional<T> engaged{T{}};
45-
const std::optional<T> engaged2{T{}};
4645

4746
{ // end() != begin() if the optional is engaged
4847
auto it = engaged.end();
49-
auto it2 = engaged2.end();
48+
auto it2 = std::as_const(engaged).end();
5049

5150
assert(it != engaged.begin());
5251
assert(engaged.begin() != it);
5352

54-
assert(it2 != engaged2.begin());
55-
assert(engaged2.begin() != it2);
53+
assert(it2 != std::as_const(engaged).begin());
54+
assert(std::as_const(engaged).begin() != it2);
5655
}
5756

5857
return true;

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,18 @@
2121

2222
template <typename T, T __val>
2323
constexpr bool test() {
24-
const std::optional<T> opt{__val};
25-
std::optional<T> nonconst_opt{__val};
24+
std::optional<T> opt{__val};
2625

2726
{ // Dereferencing an iterator of an engaged optional will return the same value that the optional holds.
2827
auto it = opt.begin();
29-
auto it2 = nonconst_opt.begin();
28+
auto it2 = std::as_const(opt).begin();
3029
assert(*it == *opt);
31-
assert(*it2 == *nonconst_opt);
30+
assert(*it2 == *std::as_const(opt));
3231
}
3332

3433
{ // optional::iterator and optional::const_iterator satisfy the Cpp17RandomAccessIterator and contiguous iterator.
3534
auto it = opt.begin();
36-
auto it2 = nonconst_opt.begin();
35+
auto it2 = std::as_const(opt).begin();
3736
assert(std::contiguous_iterator<decltype(it)>);
3837
assert(std::contiguous_iterator<decltype(it2)>);
3938

@@ -43,18 +42,18 @@ constexpr bool test() {
4342

4443
{ // 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&
4544
auto it = opt.begin();
46-
auto it2 = nonconst_opt.begin();
45+
auto it2 = std::as_const(opt).begin();
4746
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&>));
47+
assert((std::is_same_v<typename decltype(it)::reference, T&>));
4948
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&>));
49+
assert((std::is_same_v<typename decltype(it2)::reference, const T&>));
5150
}
5251

5352
{ // std::ranges::size for an engaged optional<T> == 1, disengaged optional<T> == 0
5453
const std::optional<T> disengaged{std::nullopt};
5554
std::optional<T> disengaged2{std::nullopt};
5655
assert(std::ranges::size(opt) == 1);
57-
assert(std::ranges::size(nonconst_opt) == 1);
56+
assert(std::ranges::size(std::as_const(opt)) == 1);
5857

5958
assert(std::ranges::size(disengaged) == 0);
6059
assert(std::ranges::size(disengaged2) == 0);

0 commit comments

Comments
 (0)