]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/27_io/basic_ostream/inserters_other/char/4.cc
locale_facets.tcc: Tweak to avoid warnings.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 27_io / basic_ostream / inserters_other / char / 4.cc
1 // 1999-08-16 bkoz
2 // 1999-11-01 bkoz
3
4 // Copyright (C) 1999, 2000, 2001, 2002, 2003 Free Software Foundation
5 //
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 2, or (at your option)
10 // any later version.
11
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.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 // USA.
21
22 // 27.6.2.5.4 basic_ostream character inserters
23 // @require@ %-*.tst %-*.txt
24 // @diff@ %-*.tst %-*.txt
25
26 #include <ostream>
27 #include <sstream>
28 #include <fstream>
29 #include <testsuite_hooks.h>
30
31 class test_buffer_1 : public std::streambuf
32 {
33 public:
34 test_buffer_1(const std::string& s) : str(s), it(str.begin()) { }
35
36 protected:
37 virtual int underflow() { return (it != str.end() ? *it : EOF); }
38 virtual int uflow() { return (it != str.end() ? *it++ : EOF); }
39
40 private:
41 const std::string str;
42 std::string::const_iterator it;
43 };
44
45
46 class test_buffer_2 : public std::streambuf
47 {
48 public:
49 test_buffer_2(const std::string& s) : str(s), it(str.begin()) { }
50
51 protected:
52 virtual int underflow() { return (it != str.end() ? *it : EOF); }
53 virtual int uflow() { return (it != str.end() ? *it++ : EOF); }
54 virtual std::streamsize showmanyc() { return std::distance(it, str.end()); }
55 private:
56 const std::string str;
57 std::string::const_iterator it;
58 };
59
60
61 class test_buffer_3 : public std::streambuf
62 {
63 public:
64 test_buffer_3(const std::string& s) : str(s), it(str.begin()) { }
65
66 protected:
67 virtual int underflow() { return (it != str.end() ? *it : EOF); }
68 virtual int uflow() { return (it != str.end() ? *it++ : EOF); }
69 virtual std::streamsize showmanyc()
70 {
71 std::streamsize ret = std::distance(it, str.end());
72 return ret > 0 ? ret : -1;
73 }
74 private:
75 const std::string str;
76 std::string::const_iterator it;
77 };
78
79 class test_buffer_4 : public std::streambuf {
80 public:
81 test_buffer_4(const std::string& s) : str(s), it(str.begin())
82 {
83 if (it != str.end()) {
84 buf[0] = *it++;
85 setg(buf, buf, buf+1);
86 }
87 }
88
89 protected:
90 virtual int underflow() { return (it != str.end() ? *it : EOF); }
91 virtual int uflow() { return (it != str.end() ? *it++ : EOF); }
92 virtual std::streamsize showmanyc() {
93 std::streamsize ret = std::distance(it, str.end());
94 return ret > 0 ? ret : -1;
95 }
96 private:
97 const std::string str;
98 std::string::const_iterator it;
99 char buf[1];
100 };
101
102 void test(const std::string& str, std::streambuf& buf)
103 {
104 bool test __attribute__((unused)) = true;
105
106 std::ostringstream out;
107 std::istream in(&buf);
108
109 out << in.rdbuf();
110
111 if (out.str() != str)
112 VERIFY( false );
113 }
114
115 // libstdc++/6745
116 // libstdc++/8071
117 // libstdc++/8127
118 // Jonathan Lennox <lennox@cs.columbia.edu>
119 void test05()
120 {
121 std::string string_a("Hello, world!");
122 std::string string_b("");
123
124 test_buffer_1 buf1a(string_a);
125 test_buffer_1 buf1b(string_b);
126
127 test_buffer_2 buf2a(string_a);
128 test_buffer_2 buf2b(string_b);
129
130 test_buffer_3 buf3a(string_a);
131 test_buffer_3 buf3b(string_b);
132
133 test_buffer_4 buf4a(string_a);
134 test_buffer_4 buf4b(string_b);
135
136 test(string_a, buf1a);
137 test(string_b, buf1b);
138
139 test(string_a, buf2a);
140 test(string_b, buf2b);
141
142 test(string_a, buf3a);
143 test(string_b, buf3b);
144
145 test(string_a, buf4a);
146 test(string_b, buf4b);
147 }
148
149 int
150 main()
151 {
152 test05();
153 return 0;
154 }