Skip to content

Commit 43e3c15

Browse files
committed
Address review comments
1 parent 3dbebae commit 43e3c15

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

libcxx/test/std/strings/basic.string/string.ops/string_substr/subview.pass.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include <cassert>
1616
#include <string>
17+
#include <utility>
1718

1819
#include "constexpr_char_traits.h"
1920
#include "make_string.h"
@@ -29,28 +30,67 @@ constexpr void test() {
2930

3031
{ // With a default position and a character length.
3132
std::same_as<std::basic_string_view<CharT, TraitsT>> decltype(auto) sv = s.subview();
33+
3234
assert(sv == CS("Hello cruel world!"));
35+
// Also check if subview() is a const-qualified.
36+
assert(std::as_const(s).subview() == CS("Hello cruel world!"));
3337
}
3438

3539
{ // With a explict position and a character length.
3640
std::same_as<std::basic_string_view<CharT, TraitsT>> decltype(auto) sv = s.subview(6, 5);
41+
3742
assert(sv == CS("cruel"));
3843
}
3944

4045
{ // From the beginning of the string with a explicit character length.
4146
std::same_as<std::basic_string_view<CharT, TraitsT>> decltype(auto) sv = s.subview(0, 5);
47+
4248
assert(sv == CS("Hello"));
4349
}
4450

4551
{ // To the end of string with the default character length.
4652
std::same_as<std::basic_string_view<CharT, TraitsT>> decltype(auto) sv = s.subview(12);
53+
4754
assert(sv == CS("world!"));
4855
}
4956

5057
{ // From the beginning to the end of the string with explicit values.
5158
std::same_as<std::basic_string_view<CharT, TraitsT>> decltype(auto) sv = s.subview(0, s.size());
59+
5260
assert(sv == CS("Hello cruel world!"));
5361
}
62+
63+
// Test if exceptions are thrown correctly.
64+
#ifndef TEST_HAS_NO_EXCEPTIONS
65+
if (!std::is_constant_evaluated()) {
66+
{ // With a position that is out of range.
67+
try {
68+
s.subview(s.size() + 1);
69+
assert(false && "Expected std::out_of_range exception");
70+
} catch (const std::out_of_range&) {
71+
// Expected exception
72+
}
73+
}
74+
75+
{ // With a position that is out of range and a 0 character length.
76+
try {
77+
s.subview(s.size() + 1, 0);
78+
assert(false && "Expected std::out_of_range exception");
79+
} catch (const std::out_of_range&) {
80+
// Expected exception
81+
}
82+
}
83+
84+
{ // With a position that is out of range and a some character length.
85+
try {
86+
s.subview(s.size() + 1, 1);
87+
assert(false && "Expected std::out_of_range exception");
88+
} catch (const std::out_of_range&) {
89+
// Expected exception
90+
}
91+
}
92+
}
93+
#endif
5494
}
5595

5696
template <typename CharT>

libcxx/test/std/strings/string.view/string.view.ops/substr.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ void testSubs(const CharT* s) {
8181
testCases<CharT, &std::basic_string_view<CharT>::substr>(s);
8282
#if TEST_STD_VER >= 26
8383
testCases<CharT, &std::basic_string_view<CharT>::subview>(s);
84-
#endif // TEST_STD_VER >= 26
84+
#endif
8585
}
8686

8787
void test() {

0 commit comments

Comments
 (0)