Skip to content

Commit 1482740

Browse files
committed
Addressed comments
1 parent b6c564a commit 1482740

File tree

3 files changed

+179
-186
lines changed

3 files changed

+179
-186
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ constexpr void test() {
3030

3131
{ // With a default position and a character length.
3232

33+
// Also check if subview() is a const-qualified.
34+
assert(std::as_const(s).subview() == CS("Hello cruel world!"));
35+
3336
// Check it the return type of subview() is correct.
3437
std::same_as<std::basic_string_view<CharT, TraitsT>> decltype(auto) sv = s.subview();
35-
3638
assert(sv == CS("Hello cruel world!"));
37-
// Also check if subview() is a const-qualified.
38-
assert(std::as_const(s).subview() == CS("Hello cruel world!"));
3939
}
4040

4141
{ // Check with different position and length.
@@ -44,13 +44,13 @@ constexpr void test() {
4444
assert(s.subview(6, 5) == CS("cruel"));
4545

4646
// From the beginning of the string with a explicit character length.
47-
assert(sv = s.subview(0, 5) == CS("Hello"));
47+
assert(s.subview(0, 5) == CS("Hello"));
4848

4949
// To the end of string with the default character length.
50-
assert(subview(12) == CS("world!"));
50+
assert(s.subview(12) == CS("world!"));
5151

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

5656
// Test if exceptions are thrown correctly.

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

Lines changed: 71 additions & 178 deletions
Original file line numberDiff line numberDiff line change
@@ -11,222 +11,115 @@
1111
// <string_view>
1212

1313
// constexpr basic_string_view substr(size_type pos = 0, size_type n = npos) const;
14-
// constexpr basic_string_view subview(size_type pos = 0,
15-
// size_type n = npos) const; // freestanding-deleted
16-
17-
// subview is alternative name of substr
1814

1915
// Throws: out_of_range if pos > size().
2016
// Effects: Determines the effective length rlen of the string to reference as the smaller of n and size() - pos.
2117
// Returns: basic_string_view(data()+pos, rlen).
2218

23-
#if 0
24-
# include <algorithm>
25-
# include <cassert>
26-
# include <string_view>
27-
28-
# include "make_string.h"
29-
# include "test_macros.h"
19+
#include <algorithm>
20+
#include <cassert>
21+
#include <cstddef>
22+
#include <stdexcept>
23+
#include <string_view>
3024

31-
# define CS(S) MAKE_CSTRING(CharT, S)
25+
#include "test_macros.h"
3226

3327
template <typename CharT>
34-
struct Test {
35-
typedef std::basic_string_view<CharT> (std::basic_string_view<CharT>::*Sub)(
36-
typename std::basic_string_view<CharT>::size_type = 0, typename std::basic_string_view<CharT>::size_type = npos) const;
37-
};
38-
39-
40-
template <typename CharT, typename Test<CharT>::Sub TestSub>
41-
TEST_CONSTEXPR_CXX14 void testDetail(std::basic_string_view<CharT> sv, std::size_t n, size_t pos) {
28+
void test1(std::basic_string_view<CharT> sv, std::size_t n, size_t pos) {
4229
std::basic_string_view<CharT> sv1;
43-
# ifdef TEST_HAS_NO_EXCEPTIONS
30+
#ifdef TEST_HAS_NO_EXCEPTIONS
4431
if (pos > sv.size())
4532
return; // would throw if exceptions were enabled
46-
sv1 = (sv.*TestSub)(pos, n);
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-
}
33+
sv1 = sv.substr(pos, n);
34+
#else
35+
try {
36+
sv1 = sv.substr(pos, n);
37+
assert(pos <= sv.size());
38+
} catch (const std::out_of_range&) {
39+
assert(pos > sv.size());
40+
return;
5641
}
57-
# endif
42+
#endif
5843
const std::size_t rlen = std::min(n, sv.size() - pos);
5944
assert(sv1.size() == rlen);
6045
for (std::size_t i = 0; i < rlen; ++i)
6146
assert(sv[pos + i] == sv1[i]);
6247
}
6348

64-
template <typename CharT, typename Test<CharT>::Sub TestSub>
65-
TEST_CONSTEXPR_CXX14 void testCases(const CharT* s) {
66-
std::basic_string_view<CharT> sv(s);
49+
template <typename CharT>
50+
void test(const CharT* s) {
51+
typedef std::basic_string_view<CharT> string_view_t;
6752

68-
testDetail<CharT, TestSub>(sv, 0, 0);
69-
testDetail<CharT, TestSub>(sv, 1, 0);
70-
testDetail<CharT, TestSub>(sv, 20, 0);
71-
testDetail<CharT, TestSub>(sv, sv.size(), 0);
53+
string_view_t sv1(s);
7254

73-
testDetail<CharT, TestSub>(sv, 100, 3);
55+
test1(sv1, 0, 0);
56+
test1(sv1, 1, 0);
57+
test1(sv1, 20, 0);
58+
test1(sv1, sv1.size(), 0);
7459

75-
testDetail<CharT, TestSub>(sv, 0, std::basic_string_view<CharT>::npos);
76-
testDetail<CharT, TestSub>(sv, 2, std::basic_string_view<CharT>::npos);
77-
testDetail<CharT, TestSub>(sv, sv.size(), std::basic_string_view<CharT>::npos);
60+
test1(sv1, 0, 3);
61+
test1(sv1, 2, 3);
62+
test1(sv1, 100, 3);
7863

79-
// Test if exceptions are thrown correctly.
80-
testDetail<CharT, TestSub>(sv, sv.size() + 1, 0);
81-
testDetail<CharT, TestSub>(sv, sv.size() + 1, 1);
82-
testDetail<CharT, TestSub>(sv, sv.size() + 1, std::basic_string_view<CharT>::npos);
83-
}
64+
test1(sv1, 0, string_view_t::npos);
65+
test1(sv1, 2, string_view_t::npos);
66+
test1(sv1, sv1.size(), string_view_t::npos);
8467

85-
template <typename CharT>
86-
TEST_CONSTEXPR_CXX14 void testSubs(const CharT* s) {
87-
testCases<CharT, &std::basic_string_view<CharT>::substr>(s);
88-
# if TEST_STD_VER >= 26
89-
testCases<CharT, &std::basic_string_view<CharT>::subview>(s);
90-
# endif
91-
}
92-
93-
# define CS(S) MAKE_CSTRING(CharT, S)
94-
95-
template <typename CharT>
96-
TEST_CONSTEXPR_CXX14 void test() {
97-
testSubs(
98-
CS("ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE"));
99-
testSubs(CS("ABCDE"));
100-
testSubs(CS("a"));
101-
testSubs(CS(""));
102-
}
103-
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
116-
117-
return true;
68+
test1(sv1, sv1.size() + 1, 0);
69+
test1(sv1, sv1.size() + 1, 1);
70+
test1(sv1, sv1.size() + 1, string_view_t::npos);
11871
}
11972

12073
int main(int, char**) {
121-
test();
122-
# if TEST_STD_VER >= 14
123-
// static_assert(test());
124-
# endif
74+
test("ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE");
75+
test("ABCDE");
76+
test("a");
77+
test("");
12578

126-
return 0;
127-
}
79+
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
80+
test(L"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE");
81+
test(L"ABCDE");
82+
test(L"a");
83+
test(L"");
12884
#endif
12985

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"
86+
#if TEST_STD_VER >= 11
87+
test(u"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE");
88+
test(u"ABCDE");
89+
test(u"a");
90+
test(u"");
13791

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-
};
146-
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!"));
92+
test(U"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE");
93+
test(U"ABCDE");
94+
test(U"a");
95+
test(U"");
96+
#endif
16297

163-
// From the beginning to the end of the string with explicit values.
164-
assert((sv.*TestSub)(0, sv.size()) == CS("Hello cruel world!"));
98+
#if TEST_STD_VER > 11
99+
{
100+
constexpr std::string_view sv1{"ABCDE", 5};
165101

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-
}
102+
{
103+
constexpr std::string_view sv2 = sv1.substr(0, 3);
104+
static_assert(sv2.size() == 3, "");
105+
static_assert(sv2[0] == 'A', "");
106+
static_assert(sv2[1] == 'B', "");
107+
static_assert(sv2[2] == 'C', "");
176108
}
177109

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-
}
110+
{
111+
constexpr std::string_view sv2 = sv1.substr(3, 0);
112+
static_assert(sv2.size() == 0, "");
185113
}
186114

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-
}
115+
{
116+
constexpr std::string_view sv2 = sv1.substr(3, 3);
117+
static_assert(sv2.size() == 2, "");
118+
static_assert(sv2[0] == 'D', "");
119+
static_assert(sv2[1] == 'E', "");
194120
}
195121
}
196122
#endif
197-
}
198-
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
209-
}
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>();
218-
#endif
219-
test<char16_t>();
220-
test<char32_t>();
221-
222-
return true;
223-
}
224-
225-
int main(int, char**) {
226-
test();
227-
#if TEST_STD_VER >= 14
228-
static_assert(test());
229-
#endif
230123

231124
return 0;
232-
}
125+
}

0 commit comments

Comments
 (0)