OSDN Git Service

c172f32b9f5375424f383a6891fac2e860071c74
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / 27_io / stringbuf_virtuals.cc
1 // 2001-05-21 Benjamin Kosnik  <bkoz@redhat.com>
2
3 // Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING.  If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20
21 // 27.7.1.3 Overridden virtual functions
22
23 #include <sstream>
24 #include <testsuite_hooks.h>
25
26 void test01()
27 {
28   using namespace std;
29
30   bool test = true;
31   char buf[512];
32   const char* strlit = "how to tell a story and other essays: mark twain";
33   const size_t strlitsize = std::strlen(strlit);
34   stringbuf sbuf(ios_base::out);
35
36   sbuf.pubsetbuf(buf, strlitsize);
37   sbuf.sputn(strlit, strlitsize);
38   VERIFY( std::strncmp(strlit, buf, strlitsize) != 0 );
39 }
40
41 void test02(std::stringbuf& in, bool pass)
42 {
43   using namespace std;
44   typedef streambuf::pos_type pos_type;
45   typedef streambuf::off_type off_type;
46   pos_type bad = pos_type(off_type(-1));
47   pos_type p = 0;
48
49   // seekoff
50   p = in.pubseekoff(0, ios_base::beg, ios_base::in);
51   if (pass)
52     VERIFY( p != bad );
53
54   p = in.pubseekoff(0, ios_base::beg, ios_base::out); 
55   VERIFY( p == bad );
56
57   p = in.pubseekoff(0, ios_base::beg); 
58   VERIFY( p == bad );
59
60
61   // seekpos
62   p = in.pubseekpos(0, ios_base::in);
63   if (pass)
64     VERIFY( p != bad );
65
66   p = in.pubseekpos(0, ios_base::out); 
67   VERIFY( p == bad );
68
69   p = in.pubseekpos(0); 
70   VERIFY( p == bad );
71 }
72
73 // libstdc++/9322
74 void test08()
75 {
76   using std::locale;
77   bool test = true;
78
79   locale loc;
80   std::stringbuf ob;
81   VERIFY( ob.getloc() == loc );
82
83   locale::global(locale("en_US"));
84   VERIFY( ob.getloc() == loc );
85
86   locale loc_de ("de_DE");
87   locale ret = ob.pubimbue(loc_de);
88   VERIFY( ob.getloc() == loc_de );
89   VERIFY( ret == loc );
90
91   locale::global(loc);
92   VERIFY( ob.getloc() == loc_de );
93 }
94
95 bool over_called;
96
97 class Derived_stringbuf : public std::stringbuf
98 {
99 public:
100   int_type overflow(int_type c)
101   {
102     over_called = true;
103     return std::stringbuf::overflow(c);
104   }
105   
106   const char_type* pub_epptr() const
107   {
108     return epptr();
109   }
110   
111   const char_type* pub_pptr() const
112   {
113     return pptr();
114   }
115 };
116
117 // libstdc++/9404
118 void test09()
119 {
120   bool test = true;
121
122   bool over_expected;
123
124   // sputc
125   Derived_stringbuf dsbuf_01;
126   over_called = false;
127   dsbuf_01.sputc('i');
128   VERIFY( over_called );
129   over_expected = dsbuf_01.pub_epptr() == dsbuf_01.pub_pptr();
130   over_called = false;
131   dsbuf_01.sputc('v');
132   VERIFY( (!over_expected && !over_called)
133           || (over_expected && over_called) );
134   dsbuf_01.sputc('i');
135   VERIFY( dsbuf_01.str() == "ivi" ); // Sanity check.
136
137   // sputn
138   Derived_stringbuf dsbuf_02;
139   over_called = false;
140   dsbuf_02.sputn("sonne's", 7);
141   VERIFY( over_called );
142   over_expected = dsbuf_02.pub_epptr() == dsbuf_02.pub_pptr();
143   over_called = false;
144   dsbuf_02.sputn(" peak", 5);
145   VERIFY( (!over_expected && !over_called)
146           || (over_expected && over_called) );
147   VERIFY( dsbuf_02.str() == "sonne's peak" ); // Sanity check.
148 }
149
150 int main() 
151 {
152   using namespace std;
153   test01();
154
155   // movie star, submarine scientist!
156   stringbuf in1("Hedy Lamarr", ios_base::in);
157   stringbuf in2(ios_base::in);
158   stringbuf in3("", ios_base::in);
159   test02(in1, true);
160   test02(in2, false);
161   test02(in3, false);
162
163   __gnu_cxx_test::run_test_wrapped_generic_locale_exception_catcher(test08);
164   test09();
165   return 0;
166 }