1// 2000-12-19 bkoz
2
3// Copyright (C) 2000, 2002, 2003, 2004 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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// This fails on Darwin 8 because malloc doesn't return NULL even
29// if an allocation fails (filed as Radar 3884894).
30// { dg-do run { xfail *-*-darwin8* } }
31
32#include <sstream>
33#include <iostream>
34#include <testsuite_hooks.h>
35
36// libstdc++/3129
37void test02()
38{
39  bool test __attribute__((unused)) = true;
40  int max = std::numeric_limits<int>::max() - 1;
41  std::stringbuf        strbuf;
42  std::ios              ios(&strbuf);
43
44  ios.exceptions(std::ios::badbit);
45
46  long l = 0;
47  void* v = 0;
48
49  // pword
50  ios.pword(1) = v;
51  VERIFY( ios.pword(1) == v );
52
53  try
54    {
55      v = ios.pword(max);
56    }
57  catch(std::ios_base::failure& obj)
58    {
59      // Ok.
60      VERIFY( ios.bad() );
61    }
62  catch(...)
63    {
64      test = false;
65      VERIFY( test );
66    }
67  VERIFY( v == 0 );
68
69  VERIFY( ios.pword(1) == v );
70
71  // max is different code path from max-1
72  v = &test;
73  try
74    {
75      v = ios.pword(std::numeric_limits<int>::max());
76    }
77  catch(std::ios_base::failure& obj)
78    {
79      // Ok.
80      VERIFY( ios.bad() );
81    }
82  catch(...)
83    {
84      test = false;
85      VERIFY( test );
86    }
87  VERIFY( v == &test );
88
89  // iword
90  ios.iword(1) = 1;
91  VERIFY( ios.iword(1) == 1 );
92
93  try
94    {
95      l = ios.iword(max);
96    }
97  catch(std::ios_base::failure& obj)
98    {
99      // Ok.
100      VERIFY( ios.bad() );
101    }
102  catch(...)
103    {
104      test = false;
105      VERIFY( test );
106    }
107  VERIFY( l == 0 );
108
109  VERIFY( ios.iword(1) == 1 );
110
111  // max is different code path from max-1
112  l = 1;
113  try
114    {
115      l = ios.iword(std::numeric_limits<int>::max());
116    }
117  catch(std::ios_base::failure& obj)
118    {
119      // Ok.
120      VERIFY( ios.bad() );
121    }
122  catch(...)
123    {
124      test = false;
125      VERIFY( test );
126    }
127  VERIFY( l == 1 );
128
129}
130
131int main(void)
132{
133  __gnu_test::set_memory_limits();
134  test02();
135  return 0;
136}
137