xref: /openbsd-src/gnu/lib/libstdc++/libstdc++/testsuite/27_io/ios_base_storage.cc (revision 03a78d155d6fff5698289342b62759a75b20d130)
1 // 2000-12-19 bkoz
2 
3 // Copyright (C) 2000, 2002, 2003 Free Software Foundation
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.4.2.5 ios_base storage functions
22 
23 // XXX This test will not work for some versions of irix6 because of
24 // XXX bug(s) in libc malloc for very large allocations.  However
25 // XXX -lmalloc seems to work.
26 // See http://gcc.gnu.org/ml/gcc/2002-05/msg01012.html
27 // { dg-options "-lmalloc" { target mips*-*-irix6* } }
28 
29 #include <sstream>
30 #include <iostream>
31 
32 #include <testsuite_hooks.h>
33 
34 // http://gcc.gnu.org/ml/gcc-bugs/2000-12/msg00413.html
test01()35 void test01()
36 {
37   bool test = true;
38 
39   using namespace std;
40 
41   long x1 = ios::xalloc();
42   long x2 = ios::xalloc();
43   long x3 = ios::xalloc();
44   long x4 = ios::xalloc();
45 
46   ostringstream out("the element of crime, lars von trier");
47   out.pword(++x4); // should not crash
48 }
49 
50 // libstdc++/3129
test02()51 void test02()
52 {
53   bool test = true;
54   int max = std::numeric_limits<int>::max() - 1;
55   std::stringbuf        strbuf;
56   std::ios              ios(&strbuf);
57 
58   ios.exceptions(std::ios::badbit);
59 
60   long l = 0;
61   void* v = 0;
62 
63   // pword
64   ios.pword(1) = v;
65   VERIFY( ios.pword(1) == v );
66 
67   try
68     {
69       v = ios.pword(max);
70     }
71   catch(std::ios_base::failure& obj)
72     {
73       // Ok.
74       VERIFY( ios.bad() );
75     }
76   catch(...)
77     {
78       VERIFY( test = false );
79     }
80   VERIFY( v == 0 );
81 
82   VERIFY( ios.pword(1) == v );
83 
84   // max is different code path from max-1
85   v = &test;
86   try
87     {
88       v = ios.pword(std::numeric_limits<int>::max());
89     }
90   catch(std::ios_base::failure& obj)
91     {
92       // Ok.
93       VERIFY( ios.bad() );
94     }
95   catch(...)
96     {
97       VERIFY( test = false );
98     }
99   VERIFY( v == &test );
100 
101   // iword
102   ios.iword(1) = 1;
103   VERIFY( ios.iword(1) == 1 );
104 
105   try
106     {
107       l = ios.iword(max);
108     }
109   catch(std::ios_base::failure& obj)
110     {
111       // Ok.
112       VERIFY( ios.bad() );
113     }
114   catch(...)
115     {
116       VERIFY( test = false );
117     }
118   VERIFY( l == 0 );
119 
120   VERIFY( ios.iword(1) == 1 );
121 
122   // max is different code path from max-1
123   l = 1;
124   try
125     {
126       l = ios.iword(std::numeric_limits<int>::max());
127     }
128   catch(std::ios_base::failure& obj)
129     {
130       // Ok.
131       VERIFY( ios.bad() );
132     }
133   catch(...)
134     {
135       VERIFY( test = false );
136     }
137   VERIFY( l == 1 );
138 
139 }
140 
141 class derived : public std::ios_base
142 {
143 public:
derived()144   derived() {}
145 };
146 
test03()147 void test03()
148 {
149   derived d;
150 
151   d.pword(0) = &d;
152   d.iword(0) = 1;
153 }
154 
main(void)155 int main(void)
156 {
157   __gnu_cxx_test::set_memory_limits();
158   test01();
159   test02();
160   test03();
161   return 0;
162 }
163