20
20
// Effects: Determines the effective length rlen of the string to reference as the smaller of n and size() - pos.
21
21
// Returns: basic_string_view(data()+pos, rlen).
22
22
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>
28
27
29
- #include " test_macros.h"
28
+ # include "make_string.h"
29
+ # include "test_macros.h"
30
+
31
+ # define CS(S) MAKE_CSTRING(CharT, S)
30
32
31
33
template <typename CharT>
32
34
struct Test {
33
35
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;
35
37
};
36
38
39
+
37
40
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) {
39
42
std::basic_string_view<CharT> sv1;
40
- #ifdef TEST_HAS_NO_EXCEPTIONS
43
+ # ifdef TEST_HAS_NO_EXCEPTIONS
41
44
if (pos > sv.size())
42
45
return; // would throw if exceptions were enabled
43
46
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
+ }
51
56
}
52
- #endif
57
+ # endif
53
58
const std::size_t rlen = std::min(n, sv.size() - pos);
54
59
assert(sv1.size() == rlen);
55
60
for (std::size_t i = 0; i < rlen; ++i)
56
61
assert(sv[pos + i] == sv1[i]);
57
62
}
58
63
59
64
template <typename CharT, typename Test<CharT>::Sub TestSub>
60
- void testCases (const CharT* s) {
65
+ TEST_CONSTEXPR_CXX14 void testCases(const CharT* s) {
61
66
std::basic_string_view<CharT> sv(s);
62
67
63
68
testDetail<CharT, TestSub>(sv, 0, 0);
@@ -71,87 +76,157 @@ void testCases(const CharT* s) {
71
76
testDetail<CharT, TestSub>(sv, 2, std::basic_string_view<CharT>::npos);
72
77
testDetail<CharT, TestSub>(sv, sv.size(), std::basic_string_view<CharT>::npos);
73
78
79
+ // Test if exceptions are thrown correctly.
74
80
testDetail<CharT, TestSub>(sv, sv.size() + 1, 0);
75
81
testDetail<CharT, TestSub>(sv, sv.size() + 1, 1);
76
82
testDetail<CharT, TestSub>(sv, sv.size() + 1, std::basic_string_view<CharT>::npos);
77
83
}
78
84
79
85
template <typename CharT>
80
- void testSubs (const CharT* s) {
86
+ TEST_CONSTEXPR_CXX14 void testSubs(const CharT* s) {
81
87
testCases<CharT, &std::basic_string_view<CharT>::substr>(s);
82
- #if TEST_STD_VER >= 26
88
+ # if TEST_STD_VER >= 26
83
89
testCases<CharT, &std::basic_string_view<CharT>::subview>(s);
84
- #endif
90
+ # endif
85
91
}
86
92
87
- void test () {
88
- testSubs (" ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" );
89
- testSubs (" ABCDE" );
90
- testSubs (" a" );
91
- testSubs (" " );
93
+ # define CS(S) MAKE_CSTRING(CharT, S)
92
94
93
- #ifndef TEST_HAS_NO_WIDE_CHARACTERS
95
+ template <typename CharT>
96
+ TEST_CONSTEXPR_CXX14 void test() {
94
97
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
+ }
100
103
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
107
116
108
- testSubs (
109
- U" ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" );
110
- testSubs (U" ABCDE" );
111
- testSubs (U" a" );
112
- testSubs (U" " );
113
- #endif
117
+ return true;
114
118
}
115
119
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
128
125
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
+ };
133
146
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
+ }
139
195
}
196
+ #endif
140
197
}
141
198
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
147
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 >();
148
218
#endif
219
+ test<char16_t >();
220
+ test<char32_t >();
221
+
222
+ return true ;
223
+ }
149
224
150
225
int main (int , char **) {
151
226
test ();
152
227
#if TEST_STD_VER >= 14
153
- test_constexpr ( );
228
+ static_assert ( test () );
154
229
#endif
155
230
156
231
return 0 ;
157
- }
232
+ }
0 commit comments