]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/27_io/basic_ostream/inserters_other/char/4.cc
4 // Copyright (C) 1999-2013 Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING3. If not see
19 // <http://www.gnu.org/licenses/>.
21 // 27.6.2.5.4 basic_ostream character inserters
26 #include <testsuite_hooks.h>
28 class test_buffer_1
: public std::streambuf
31 test_buffer_1(const std::string
& s
) : str(s
), it(str
.begin()) { }
34 virtual int underflow() { return (it
!= str
.end() ? *it
: EOF
); }
35 virtual int uflow() { return (it
!= str
.end() ? *it
++ : EOF
); }
38 const std::string str
;
39 std::string::const_iterator it
;
43 class test_buffer_2
: public std::streambuf
46 test_buffer_2(const std::string
& s
) : str(s
), it(str
.begin()) { }
49 virtual int underflow() { return (it
!= str
.end() ? *it
: EOF
); }
50 virtual int uflow() { return (it
!= str
.end() ? *it
++ : EOF
); }
51 virtual std::streamsize
showmanyc() { return std::distance(it
, str
.end()); }
53 const std::string str
;
54 std::string::const_iterator it
;
58 class test_buffer_3
: public std::streambuf
61 test_buffer_3(const std::string
& s
) : str(s
), it(str
.begin()) { }
64 virtual int underflow() { return (it
!= str
.end() ? *it
: EOF
); }
65 virtual int uflow() { return (it
!= str
.end() ? *it
++ : EOF
); }
66 virtual std::streamsize
showmanyc()
68 std::streamsize ret
= std::distance(it
, str
.end());
69 return ret
> 0 ? ret
: -1;
72 const std::string str
;
73 std::string::const_iterator it
;
76 class test_buffer_4
: public std::streambuf
{
78 test_buffer_4(const std::string
& s
) : str(s
), it(str
.begin())
80 if (it
!= str
.end()) {
82 setg(buf
, buf
, buf
+1);
87 virtual int underflow() { return (it
!= str
.end() ? *it
: EOF
); }
88 virtual int uflow() { return (it
!= str
.end() ? *it
++ : EOF
); }
89 virtual std::streamsize
showmanyc() {
90 std::streamsize ret
= std::distance(it
, str
.end());
91 return ret
> 0 ? ret
: -1;
94 const std::string str
;
95 std::string::const_iterator it
;
99 void test(const std::string
& str
, std::streambuf
& buf
)
101 bool test
__attribute__((unused
)) = true;
103 std::ostringstream out
;
104 std::istream
in(&buf
);
108 if (out
.str() != str
)
115 // Jonathan Lennox <lennox@cs.columbia.edu>
118 std::string
string_a("Hello, world!");
119 std::string
string_b("");
121 test_buffer_1
buf1a(string_a
);
122 test_buffer_1
buf1b(string_b
);
124 test_buffer_2
buf2a(string_a
);
125 test_buffer_2
buf2b(string_b
);
127 test_buffer_3
buf3a(string_a
);
128 test_buffer_3
buf3b(string_b
);
130 test_buffer_4
buf4a(string_a
);
131 test_buffer_4
buf4b(string_b
);
133 test(string_a
, buf1a
);
134 test(string_b
, buf1b
);
136 test(string_a
, buf2a
);
137 test(string_b
, buf2b
);
139 test(string_a
, buf3a
);
140 test(string_b
, buf3b
);
142 test(string_a
, buf4a
);
143 test(string_b
, buf4b
);