Skip to content

Commit b6c564a

Browse files
committed
Addressed comments
1 parent 43e3c15 commit b6c564a

File tree

2 files changed

+161
-93
lines changed

2 files changed

+161
-93
lines changed

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

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,35 +29,28 @@ constexpr void test() {
2929
std::basic_string<CharT, TraitsT, AllocT> s{CS("Hello cruel world!"), AllocT{}};
3030

3131
{ // With a default position and a character length.
32+
33+
// Check it the return type of subview() is correct.
3234
std::same_as<std::basic_string_view<CharT, TraitsT>> decltype(auto) sv = s.subview();
3335

3436
assert(sv == CS("Hello cruel world!"));
3537
// Also check if subview() is a const-qualified.
3638
assert(std::as_const(s).subview() == CS("Hello cruel world!"));
3739
}
3840

39-
{ // With a explict position and a character length.
40-
std::same_as<std::basic_string_view<CharT, TraitsT>> decltype(auto) sv = s.subview(6, 5);
41-
42-
assert(sv == CS("cruel"));
43-
}
44-
45-
{ // From the beginning of the string with a explicit character length.
46-
std::same_as<std::basic_string_view<CharT, TraitsT>> decltype(auto) sv = s.subview(0, 5);
41+
{ // Check with different position and length.
4742

48-
assert(sv == CS("Hello"));
49-
}
50-
51-
{ // To the end of string with the default character length.
52-
std::same_as<std::basic_string_view<CharT, TraitsT>> decltype(auto) sv = s.subview(12);
43+
// With a explict position and a character length.
44+
assert(s.subview(6, 5) == CS("cruel"));
5345

54-
assert(sv == CS("world!"));
55-
}
46+
// From the beginning of the string with a explicit character length.
47+
assert(sv = s.subview(0, 5) == CS("Hello"));
5648

57-
{ // From the beginning to the end of the string with explicit values.
58-
std::same_as<std::basic_string_view<CharT, TraitsT>> decltype(auto) sv = s.subview(0, s.size());
49+
// To the end of string with the default character length.
50+
assert(subview(12) == CS("world!"));
5951

60-
assert(sv == CS("Hello cruel world!"));
52+
// From the beginning to the end of the string with explicit values.
53+
assert(subview(0, s.size() == CS("Hello cruel world!"));
6154
}
6255

6356
// Test if exceptions are thrown correctly.
@@ -68,7 +61,7 @@ constexpr void test() {
6861
s.subview(s.size() + 1);
6962
assert(false && "Expected std::out_of_range exception");
7063
} catch (const std::out_of_range&) {
71-
// Expected exception
64+
// Expected exception...
7265
}
7366
}
7467

@@ -77,7 +70,7 @@ constexpr void test() {
7770
s.subview(s.size() + 1, 0);
7871
assert(false && "Expected std::out_of_range exception");
7972
} catch (const std::out_of_range&) {
80-
// Expected exception
73+
// Expected exception...
8174
}
8275
}
8376

@@ -86,7 +79,7 @@ constexpr void test() {
8679
s.subview(s.size() + 1, 1);
8780
assert(false && "Expected std::out_of_range exception");
8881
} catch (const std::out_of_range&) {
89-
// Expected exception
82+
// Expected exception...
9083
}
9184
}
9285
}

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

Lines changed: 147 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -20,44 +20,49 @@
2020
// Effects: Determines the effective length rlen of the string to reference as the smaller of n and size() - pos.
2121
// Returns: basic_string_view(data()+pos, rlen).
2222

23-
#include <algorithm>
24-
#include <cassert>
25-
#include <cstddef>
26-
#include <stdexcept>
27-
#include <string_view>
23+
#if 0
24+
# include <algorithm>
25+
# include <cassert>
26+
# include <string_view>
2827

29-
#include "test_macros.h"
28+
# include "make_string.h"
29+
# include "test_macros.h"
30+
31+
# define CS(S) MAKE_CSTRING(CharT, S)
3032

3133
template <typename CharT>
3234
struct Test {
3335
typedef std::basic_string_view<CharT> (std::basic_string_view<CharT>::*Sub)(
34-
typename std::basic_string_view<CharT>::size_type, typename std::basic_string_view<CharT>::size_type) const;
36+
typename std::basic_string_view<CharT>::size_type = 0, typename std::basic_string_view<CharT>::size_type = npos) const;
3537
};
3638

39+
3740
template <typename CharT, typename Test<CharT>::Sub TestSub>
38-
void testDetail(std::basic_string_view<CharT> sv, std::size_t n, size_t pos) {
41+
TEST_CONSTEXPR_CXX14 void testDetail(std::basic_string_view<CharT> sv, std::size_t n, size_t pos) {
3942
std::basic_string_view<CharT> sv1;
40-
#ifdef TEST_HAS_NO_EXCEPTIONS
43+
# ifdef TEST_HAS_NO_EXCEPTIONS
4144
if (pos > sv.size())
4245
return; // would throw if exceptions were enabled
4346
sv1 = (sv.*TestSub)(pos, n);
44-
#else
45-
try {
46-
sv1 = (sv.*TestSub)(pos, n);
47-
assert(pos <= sv.size());
48-
} catch (const std::out_of_range&) {
49-
assert(pos > sv.size());
50-
return;
47+
# else
48+
if (!TEST_IS_CONSTANT_EVALUATED) {
49+
try {
50+
sv1 = (sv.*TestSub)(pos, n);
51+
assert(pos <= sv.size());
52+
} catch (const std::out_of_range&) {
53+
assert(pos > sv.size());
54+
return;
55+
}
5156
}
52-
#endif
57+
# endif
5358
const std::size_t rlen = std::min(n, sv.size() - pos);
5459
assert(sv1.size() == rlen);
5560
for (std::size_t i = 0; i < rlen; ++i)
5661
assert(sv[pos + i] == sv1[i]);
5762
}
5863

5964
template <typename CharT, typename Test<CharT>::Sub TestSub>
60-
void testCases(const CharT* s) {
65+
TEST_CONSTEXPR_CXX14 void testCases(const CharT* s) {
6166
std::basic_string_view<CharT> sv(s);
6267

6368
testDetail<CharT, TestSub>(sv, 0, 0);
@@ -71,87 +76,157 @@ void testCases(const CharT* s) {
7176
testDetail<CharT, TestSub>(sv, 2, std::basic_string_view<CharT>::npos);
7277
testDetail<CharT, TestSub>(sv, sv.size(), std::basic_string_view<CharT>::npos);
7378

79+
// Test if exceptions are thrown correctly.
7480
testDetail<CharT, TestSub>(sv, sv.size() + 1, 0);
7581
testDetail<CharT, TestSub>(sv, sv.size() + 1, 1);
7682
testDetail<CharT, TestSub>(sv, sv.size() + 1, std::basic_string_view<CharT>::npos);
7783
}
7884

7985
template <typename CharT>
80-
void testSubs(const CharT* s) {
86+
TEST_CONSTEXPR_CXX14 void testSubs(const CharT* s) {
8187
testCases<CharT, &std::basic_string_view<CharT>::substr>(s);
82-
#if TEST_STD_VER >= 26
88+
# if TEST_STD_VER >= 26
8389
testCases<CharT, &std::basic_string_view<CharT>::subview>(s);
84-
#endif
90+
# endif
8591
}
8692

87-
void test() {
88-
testSubs("ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE");
89-
testSubs("ABCDE");
90-
testSubs("a");
91-
testSubs("");
93+
# define CS(S) MAKE_CSTRING(CharT, S)
9294

93-
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
95+
template <typename CharT>
96+
TEST_CONSTEXPR_CXX14 void test() {
9497
testSubs(
95-
L"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE");
96-
testSubs(L"ABCDE");
97-
testSubs(L"a");
98-
testSubs(L"");
99-
#endif
98+
CS("ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE"));
99+
testSubs(CS("ABCDE"));
100+
testSubs(CS("a"));
101+
testSubs(CS(""));
102+
}
100103

101-
#if TEST_STD_VER >= 11
102-
testSubs(
103-
u"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE");
104-
testSubs(u"ABCDE");
105-
testSubs(u"a");
106-
testSubs(u"");
104+
TEST_CONSTEXPR_CXX14 bool test() {
105+
test<char>();
106+
# ifndef TEST_HAS_NO_WIDE_CHARACTERS
107+
test<wchar_t>();
108+
# endif
109+
# if TEST_STD_VER >= 11
110+
# ifndef TEST_HAS_NO_CHAR8_T
111+
test<char8_t>();
112+
# endif
113+
test<char16_t>();
114+
test<char32_t>();
115+
# endif
107116

108-
testSubs(
109-
U"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE");
110-
testSubs(U"ABCDE");
111-
testSubs(U"a");
112-
testSubs(U"");
113-
#endif
117+
return true;
114118
}
115119

116-
#if TEST_STD_VER >= 14
117-
template <typename Test<char>::Sub TestSub>
118-
constexpr void testConstexprDetail() {
119-
constexpr std::string_view sv{"ABCDE", 5};
120-
{
121-
constexpr std::string_view sv2 = (sv.*TestSub)(0, 3);
122-
123-
static_assert(sv2.size() == 3, "");
124-
static_assert(sv2[0] == 'A', "");
125-
static_assert(sv2[1] == 'B', "");
126-
static_assert(sv2[2] == 'C', "");
127-
}
120+
int main(int, char**) {
121+
test();
122+
# if TEST_STD_VER >= 14
123+
// static_assert(test());
124+
# endif
128125

129-
{
130-
constexpr std::string_view sv2 = (sv.*TestSub)(3, 0);
131-
static_assert(sv2.size() == 0, "");
132-
}
126+
return 0;
127+
}
128+
#endif
129+
130+
#include <cassert>
131+
#include <string>
132+
#include <utility>
133+
134+
#include "constexpr_char_traits.h"
135+
#include "make_string.h"
136+
#include "test_macros.h"
137+
138+
#define CS(S) MAKE_CSTRING(CharT, S)
139+
140+
template <typename CharT, typename TraitsT>
141+
struct Test {
142+
typedef std::basic_string_view<CharT, TraitsT> (std::basic_string_view<CharT, TraitsT>::*Sub)(
143+
typename std::basic_string_view<CharT, TraitsT>::size_type,
144+
typename std::basic_string_view<CharT, TraitsT>::size_type) const;
145+
};
133146

134-
{
135-
constexpr std::string_view sv2 = (sv.*TestSub)(3, 3);
136-
static_assert(sv2.size() == 2, "");
137-
static_assert(sv2[0] == 'D', "");
138-
static_assert(sv2[1] == 'E', "");
147+
template <typename CharT, typename TraitsT, typename Test<CharT, TraitsT>::Sub TestSub>
148+
TEST_CONSTEXPR_CXX14 void test() {
149+
const std::basic_string_view<CharT, TraitsT> sv = CS("Hello cruel world!");
150+
151+
// With a default position and a character length.
152+
assert((sv.*TestSub)(0, std::basic_string_view<CharT, TraitsT>::npos) == CS("Hello cruel world!"));
153+
154+
// With a explict position and a character length.
155+
assert((sv.*TestSub)(6, 5) == CS("cruel"));
156+
157+
// From the beginning of the string with a explicit character length.
158+
assert((sv.*TestSub)(0, 5) == CS("Hello"));
159+
160+
// To the end of string with the default character length.
161+
assert((sv.*TestSub)(12, std::basic_string_view<CharT, TraitsT>::npos) == CS("world!"));
162+
163+
// From the beginning to the end of the string with explicit values.
164+
assert((sv.*TestSub)(0, sv.size()) == CS("Hello cruel world!"));
165+
166+
// Test if exceptions are thrown correctly.
167+
#ifndef TEST_HAS_NO_EXCEPTIONS
168+
if (!TEST_IS_CONSTANT_EVALUATED) {
169+
{ // With a position that is out of range.
170+
try {
171+
(sv.*TestSub)(sv.size() + 1, std::basic_string_view<CharT, TraitsT>::npos);
172+
assert(false && "Expected std::out_of_range exception");
173+
} catch (const std::out_of_range&) {
174+
// Expected exception
175+
}
176+
}
177+
178+
{ // With a position that is out of range and a 0 character length.
179+
try {
180+
(sv.*TestSub)(sv.size() + 1, 0);
181+
assert(false && "Expected std::out_of_range exception");
182+
} catch (const std::out_of_range&) {
183+
// Expected exception
184+
}
185+
}
186+
187+
{ // With a position that is out of range and a some character length.
188+
try {
189+
(sv.*TestSub)(sv.size() + 1, 1);
190+
assert(false && "Expected std::out_of_range exception");
191+
} catch (const std::out_of_range&) {
192+
// Expected exception
193+
}
194+
}
139195
}
196+
#endif
140197
}
141198

142-
void test_constexpr() {
143-
testConstexprDetail<&std::string_view::substr>();
144-
# if TEST_STD_VER >= 26
145-
testConstexprDetail<&std::string_view::subview>();
146-
# endif
199+
template <typename CharT>
200+
TEST_CONSTEXPR_CXX14 void test() {
201+
ASSERT_SAME_TYPE(std::basic_string_view<CharT>, decltype(std::declval<std::basic_string_view<CharT> >().substr()));
202+
test<CharT, std::char_traits<CharT>, &std::basic_string_view<CharT, std::char_traits<CharT> >::substr>();
203+
test<CharT, constexpr_char_traits<CharT>, &std::basic_string_view<CharT, constexpr_char_traits<CharT> >::substr>();
204+
#if TEST_STD_VER >= 26
205+
ASSERT_SAME_TYPE(std::basic_string_view<CharT>, decltype(std::declval<std::basic_string_view<CharT> >().subview()));
206+
test<CharT, std::char_traits<CharT>, &std::basic_string_view<CharT, std::char_traits<CharT>>::subview>();
207+
test<CharT, constexpr_char_traits<CharT>, &std::basic_string_view<CharT, constexpr_char_traits<CharT> >::subview>();
208+
#endif
147209
}
210+
211+
TEST_CONSTEXPR_CXX14 bool test() {
212+
test<char>();
213+
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
214+
test<wchar_t>();
215+
#endif
216+
#ifndef TEST_HAS_NO_CHAR8_T
217+
test<char8_t>();
148218
#endif
219+
test<char16_t>();
220+
test<char32_t>();
221+
222+
return true;
223+
}
149224

150225
int main(int, char**) {
151226
test();
152227
#if TEST_STD_VER >= 14
153-
test_constexpr();
228+
static_assert(test());
154229
#endif
155230

156231
return 0;
157-
}
232+
}

0 commit comments

Comments
 (0)