1// Copyright (C) 2000, 2001 Free Software Foundation, Inc.
2//
3// This file is part of the GNU ISO C++ Library.  This library is free
4// software; you can redistribute it and/or modify it under the
5// terms of the GNU General Public License as published by the
6// Free Software Foundation; either version 2, or (at your option)
7// any later version.
8
9// This library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License along
15// with this library; see the file COPYING.  If not, write to the Free
16// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17// USA.
18
19// 27.8.1.10 ofstream member functions
20// @require@ %-*.tst
21// @diff@ %-*.tst %-*.txt
22
23#include <ostream>
24#include <fstream>
25#include <testsuite_hooks.h>
26
27const char name_01[] = "ofstream_members-1.tst";
28const char name_02[] = "ofstream_members-1.txt";
29
30// http://gcc.gnu.org/ml/libstdc++/2000-06/msg00136.html
31bool test00()
32{
33  bool test = true;
34  std::ofstream ofs1;
35  ofs1.close();
36
37  // false as expected:
38  VERIFY( !ofs1.is_open() );
39   // this is now true:
40  VERIFY( !(ofs1) );
41
42  ofs1.open(name_02);
43  VERIFY( ofs1.is_open() );
44  // fail bit still true
45  VERIFY( !(ofs1) );
46  VERIFY( ofs1.rdstate() == std::ios_base::failbit );
47
48  ofs1.close();
49
50#ifdef DEBUG_ASSERT
51  assert(test);
52#endif
53
54  return test;
55}
56
57
58// http://gcc.gnu.org/ml/libstdc++/2000-07/msg00004.html
59bool test01()
60{
61  bool test = true;
62  const int more_than_max_open_files = 8200;
63
64  for(int i = 0; ++i < more_than_max_open_files;)
65    {
66      std::ofstream ifs(name_02);
67      VERIFY( static_cast<bool>(ifs) );
68    }
69
70#ifdef DEBUG_ASSERT
71  assert(test);
72#endif
73
74  return test;
75}
76
77void
78redirect_buffer(std::ios& stream, std::streambuf* new_buf)
79{ stream.rdbuf(new_buf); }
80
81std::streambuf*
82active_buffer(std::ios& stream)
83{ return stream.rdbuf(); }
84
85// libstdc++/2832
86void test02()
87{
88  bool test = true;
89  const char* strlit01 = "fuck war";
90  const char* strlit02 = "two less cars abstract riot crew, critical mass/SF";
91  const std::string str00;
92  const std::string str01(strlit01);
93  std::string str02;
94  std::filebuf fbuf;
95  std::streambuf* pbasebuf0 = &fbuf;
96
97  std::ofstream sstrm1;
98  // derived rdbuf() always returns original streambuf, even though
99  // it's no longer associated with the stream.
100  std::filebuf* const buf1 = sstrm1.rdbuf();
101  // base rdbuf() returns the currently associated streambuf
102  std::streambuf* pbasebuf1 = active_buffer(sstrm1);
103  redirect_buffer(sstrm1, &fbuf);
104  std::filebuf* const buf2 = sstrm1.rdbuf();
105  std::streambuf* pbasebuf2 = active_buffer(sstrm1);
106  VERIFY( buf1 == buf2 );
107  VERIFY( pbasebuf1 != pbasebuf2 );
108  VERIFY( pbasebuf2 == pbasebuf0 );
109
110  // How confusing and non-intuitive is this?
111  // These semantics are a joke, a serious defect, and incredibly lame.
112}
113
114int main()
115{
116  test00();
117  test01();
118
119  test02();
120  return 0;
121}
122
123
124
125