13
13
// getline(basic_istream<charT,traits>& is,
14
14
// basic_string<charT,traits,Allocator>& str);
15
15
16
- #include < string>
17
- #include < sstream>
18
16
#include < cassert>
17
+ #include < sstream>
18
+ #include < string>
19
19
20
+ #include " make_string.h"
20
21
#include " min_allocator.h"
22
+ #include " stream_types.h"
21
23
#include " test_macros.h"
22
24
23
- template <template <class > class Alloc >
24
- void test_string () {
25
- using S = std::basic_string<char , std::char_traits<char >, Alloc<char > >;
26
- #ifndef TEST_HAS_NO_WIDE_CHARACTERS
27
- using WS = std::basic_string<wchar_t , std::char_traits<wchar_t >, Alloc<wchar_t > >;
28
- #endif
29
- {
30
- std::istringstream in (" abc\n def\n ghij" );
31
- S s (" initial text" );
32
- std::getline (in, s);
33
- assert (in.good ());
34
- assert (s == " abc" );
35
- std::getline (in, s);
36
- assert (in.good ());
37
- assert (s == " def" );
38
- std::getline (in, s);
39
- assert (in.eof ());
40
- assert (s == " ghij" );
41
- }
42
- #ifndef TEST_HAS_NO_WIDE_CHARACTERS
25
+ template <class CharT , class Alloc , class Stream , class Streambuf >
26
+ void test () {
27
+ using string_type = std::basic_string<CharT, std::char_traits<CharT>, Alloc>;
28
+ using stream_type = std::basic_istream<CharT>;
29
+ using streambuf_type = Streambuf;
30
+
43
31
{
44
- std::wistringstream in (L" abc\n def\n ghij" );
45
- WS s (L" initial text" );
32
+ streambuf_type sb (MAKE_CSTRING (CharT, " abc\n def\n ghij" ));
33
+ stream_type in (&sb);
34
+ string_type s (MAKE_CSTRING (CharT, " initial text" ));
46
35
std::getline (in, s);
47
36
assert (in.good ());
48
- assert (s == L " abc" );
37
+ assert (s == MAKE_CSTRING (CharT, " abc" ) );
49
38
std::getline (in, s);
50
39
assert (in.good ());
51
- assert (s == L " def" );
40
+ assert (s == MAKE_CSTRING (CharT, " def" ) );
52
41
std::getline (in, s);
53
42
assert (in.eof ());
54
- assert (s == L " ghij" );
43
+ assert (s == MAKE_CSTRING (CharT, " ghij" ) );
55
44
}
56
- #endif
57
-
58
45
#ifndef TEST_HAS_NO_EXCEPTIONS
59
46
{
60
- std::basic_stringbuf< char > sb (" hello" );
61
- std::basic_istream< char > is (&sb);
47
+ streambuf_type sb (MAKE_CSTRING (CharT, " hello" ) );
48
+ stream_type is (&sb);
62
49
is.exceptions (std::ios_base::eofbit);
63
50
64
- S s;
51
+ string_type s;
65
52
bool threw = false ;
66
53
try {
67
54
std::getline (is, s);
@@ -73,36 +60,14 @@ void test_string() {
73
60
assert (!is.fail ());
74
61
assert (is.eof ());
75
62
assert (threw);
76
- assert (s == " hello" );
63
+ assert (s == MAKE_CSTRING (CharT, " hello" ) );
77
64
}
78
- # ifndef TEST_HAS_NO_WIDE_CHARACTERS
79
65
{
80
- std::basic_stringbuf<wchar_t > sb (L" hello" );
81
- std::basic_istream<wchar_t > is (&sb);
82
- is.exceptions (std::ios_base::eofbit);
83
-
84
- WS s;
85
- bool threw = false ;
86
- try {
87
- std::getline (is, s);
88
- } catch (std::ios::failure const &) {
89
- threw = true ;
90
- }
91
-
92
- assert (!is.bad ());
93
- assert (!is.fail ());
94
- assert (is.eof ());
95
- assert (threw);
96
- assert (s == L" hello" );
97
- }
98
- # endif
99
-
100
- {
101
- std::basic_stringbuf<char > sb;
102
- std::basic_istream<char > is (&sb);
66
+ streambuf_type sb (MAKE_CSTRING (CharT, " " ));
67
+ stream_type is (&sb);
103
68
is.exceptions (std::ios_base::failbit);
104
69
105
- S s;
70
+ string_type s;
106
71
bool threw = false ;
107
72
try {
108
73
std::getline (is, s);
@@ -114,37 +79,48 @@ void test_string() {
114
79
assert (is.fail ());
115
80
assert (is.eof ());
116
81
assert (threw);
117
- assert (s == " " );
82
+ assert (s == MAKE_CSTRING (CharT, " " ) );
118
83
}
119
- # ifndef TEST_HAS_NO_WIDE_CHARACTERS
120
- {
121
- std::basic_stringbuf<wchar_t > sb;
122
- std::basic_istream<wchar_t > is (&sb);
123
- is.exceptions (std::ios_base::failbit);
84
+ #endif // TEST_HAS_NO_EXCEPTIONS
85
+ }
124
86
125
- WS s;
126
- bool threw = false ;
127
- try {
128
- std::getline (is, s);
129
- } catch (std::ios::failure const &) {
130
- threw = true ;
131
- }
87
+ template <template <class > class Alloc >
88
+ void test_alloc () {
89
+ test<char , Alloc<char >, std::basic_istringstream<char >, std::basic_stringbuf<char > >();
90
+ test<char , Alloc<char >, std::basic_istringstream<char >, non_buffering_streambuf<char > >();
91
+ #ifndef TEST_HAS_NO_WIDE_CHARACTERS
92
+ test<wchar_t , Alloc<wchar_t >, std::basic_istringstream<wchar_t >, std::basic_stringbuf<wchar_t > >();
93
+ test<wchar_t , Alloc<wchar_t >, std::basic_istringstream<wchar_t >, non_buffering_streambuf<wchar_t > >();
94
+ #endif
95
+ }
132
96
133
- assert (!is.bad ());
134
- assert (is.fail ());
135
- assert (is.eof ());
136
- assert (threw);
137
- assert (s == L" " );
97
+ void test_tiny_allocator () {
98
+ {
99
+ std::string in_str =
100
+ " this is a too long line for the string that has to be longer because the implementation is broken\n " ;
101
+ std::istringstream iss (in_str);
102
+ std::basic_string<char , std::char_traits<char >, tiny_size_allocator<40 , char > > str;
103
+ std::getline (iss, str);
104
+ assert (iss.rdstate () & std::ios::failbit);
105
+ assert (str == in_str.substr (0 , str.max_size ()).c_str ());
106
+ }
107
+ {
108
+ std::string in_str =
109
+ " this is a too long line for the string that has to be longer because the implementation is broken" ;
110
+ std::istringstream iss (in_str);
111
+ std::basic_string<char , std::char_traits<char >, tiny_size_allocator<40 , char > > str;
112
+ std::getline (iss, str);
113
+ assert (iss.rdstate () & std::ios::failbit);
114
+ assert (str == in_str.substr (0 , str.max_size ()).c_str ());
138
115
}
139
- # endif
140
- #endif // TEST_HAS_NO_EXCEPTIONS
141
116
}
142
117
143
118
int main (int , char **) {
144
- test_string <std::allocator>();
119
+ test_alloc <std::allocator>();
145
120
#if TEST_STD_VER >= 11
146
- test_string <min_allocator>();
121
+ test_alloc <min_allocator>();
147
122
#endif
123
+ test_tiny_allocator ();
148
124
149
125
return 0 ;
150
126
}
0 commit comments