14
14
15
15
#include < cassert>
16
16
#include < string>
17
+ #include < utility>
17
18
18
19
#include " constexpr_char_traits.h"
19
20
#include " make_string.h"
@@ -29,28 +30,67 @@ constexpr void test() {
29
30
30
31
{ // With a default position and a character length.
31
32
std::same_as<std::basic_string_view<CharT, TraitsT>> decltype (auto ) sv = s.subview ();
33
+
32
34
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!" ));
33
37
}
34
38
35
39
{ // With a explict position and a character length.
36
40
std::same_as<std::basic_string_view<CharT, TraitsT>> decltype (auto ) sv = s.subview (6 , 5 );
41
+
37
42
assert (sv == CS (" cruel" ));
38
43
}
39
44
40
45
{ // From the beginning of the string with a explicit character length.
41
46
std::same_as<std::basic_string_view<CharT, TraitsT>> decltype (auto ) sv = s.subview (0 , 5 );
47
+
42
48
assert (sv == CS (" Hello" ));
43
49
}
44
50
45
51
{ // To the end of string with the default character length.
46
52
std::same_as<std::basic_string_view<CharT, TraitsT>> decltype (auto ) sv = s.subview (12 );
53
+
47
54
assert (sv == CS (" world!" ));
48
55
}
49
56
50
57
{ // From the beginning to the end of the string with explicit values.
51
58
std::same_as<std::basic_string_view<CharT, TraitsT>> decltype (auto ) sv = s.subview (0 , s.size ());
59
+
52
60
assert (sv == CS (" Hello cruel world!" ));
53
61
}
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
54
94
}
55
95
56
96
template <typename CharT>
0 commit comments