1230557Sjimharris// Copyright (C) 2000, 2001 Free Software Foundation, Inc.
2230557Sjimharris//
3230557Sjimharris// This file is part of the GNU ISO C++ Library.  This library is free
4230557Sjimharris// software; you can redistribute it and/or modify it under the
5230557Sjimharris// terms of the GNU General Public License as published by the
6230557Sjimharris// Free Software Foundation; either version 2, or (at your option)
7230557Sjimharris// any later version.
8230557Sjimharris
9230557Sjimharris// This library is distributed in the hope that it will be useful,
10230557Sjimharris// but WITHOUT ANY WARRANTY; without even the implied warranty of
11230557Sjimharris// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12230557Sjimharris// GNU General Public License for more details.
13230557Sjimharris
14230557Sjimharris// You should have received a copy of the GNU General Public License along
15230557Sjimharris// with this library; see the file COPYING.  If not, write to the Free
16230557Sjimharris// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17230557Sjimharris// USA.
18230557Sjimharris
19230557Sjimharris// 27.8.1.7 ifstream member functions
20230557Sjimharris// @require@ %-*.tst %-*.txt
21230557Sjimharris// @diff@ %-*.tst %-*.txt
22230557Sjimharris
23230557Sjimharris#include <istream>
24230557Sjimharris#include <fstream>
25230557Sjimharris#include <testsuite_hooks.h>
26230557Sjimharris
27230557Sjimharrisconst char name_01[] = "ifstream_members-1.tst";
28230557Sjimharrisconst char name_02[] = "ifstream_members-1.txt";
29230557Sjimharris
30230557Sjimharris// http://gcc.gnu.org/ml/libstdc++/2000-06/msg00136.html
31230557Sjimharrisbool test00()
32230557Sjimharris{
33230557Sjimharris  bool test = true;
34230557Sjimharris  std::ifstream ifs1;
35230557Sjimharris  ifs1.close();
36230557Sjimharris
37230557Sjimharris  // false as expected:
38230557Sjimharris  VERIFY( !ifs1.is_open() );
39230557Sjimharris   // this is now true:
40230557Sjimharris  VERIFY( !(ifs1) );
41230557Sjimharris
42230557Sjimharris  ifs1.open(name_01);
43230557Sjimharris  VERIFY( ifs1.is_open() );
44230557Sjimharris  // fail bit still true
45230557Sjimharris  VERIFY( !(ifs1) );
46230557Sjimharris  VERIFY( ifs1.rdstate() == std::ios_base::failbit );
47230557Sjimharris
48230557Sjimharris  ifs1.close();
49230557Sjimharris
50230557Sjimharris#ifdef DEBUG_ASSERT
51230557Sjimharris  assert(test);
52230557Sjimharris#endif
53230557Sjimharris
54230557Sjimharris  return test;
55230557Sjimharris}
56230557Sjimharris
57230557Sjimharris// http://gcc.gnu.org/ml/libstdc++/2000-07/msg00004.html
58230557Sjimharrisbool test01()
59230557Sjimharris{
60230557Sjimharris  bool test = true;
61230557Sjimharris  const int more_than_max_open_files = 8200;
62230557Sjimharris
63230557Sjimharris  for(int i = 0; ++i < more_than_max_open_files;)
64230557Sjimharris    {
65230557Sjimharris      std::ifstream ifs(name_01);
66230557Sjimharris      VERIFY( static_cast<bool>(ifs) );
67230557Sjimharris    }
68230557Sjimharris
69230557Sjimharris#ifdef DEBUG_ASSERT
70230557Sjimharris  assert(test);
71230557Sjimharris#endif
72230557Sjimharris
73230557Sjimharris  return test;
74230557Sjimharris}
75230557Sjimharris
76230557Sjimharrisvoid
77230557Sjimharrisredirect_buffer(std::ios& stream, std::streambuf* new_buf)
78230557Sjimharris{ stream.rdbuf(new_buf); }
79230557Sjimharris
80230557Sjimharrisstd::streambuf*
81230557Sjimharrisactive_buffer(std::ios& stream)
82230557Sjimharris{ return stream.rdbuf(); }
83230557Sjimharris
84230557Sjimharris// libstdc++/2832
85230557Sjimharrisvoid test02()
86230557Sjimharris{
87230557Sjimharris  bool test = true;
88230557Sjimharris  const char* strlit01 = "fuck war";
89230557Sjimharris  const char* strlit02 = "two less cars abstract riot crew, critical mass/SF";
90230557Sjimharris  const std::string str00;
91230557Sjimharris  const std::string str01(strlit01);
92230557Sjimharris  std::string str02;
93230557Sjimharris  std::filebuf fbuf;
94230557Sjimharris  std::streambuf* pbasebuf0 = &fbuf;
95230557Sjimharris
96230557Sjimharris  std::ifstream sstrm1;
97230557Sjimharris  // derived rdbuf() always returns original streambuf, even though
98230557Sjimharris  // it's no longer associated with the stream.
99230557Sjimharris  std::filebuf* const buf1 = sstrm1.rdbuf();
100230557Sjimharris  // base rdbuf() returns the currently associated streambuf
101230557Sjimharris  std::streambuf* pbasebuf1 = active_buffer(sstrm1);
102230557Sjimharris  redirect_buffer(sstrm1, &fbuf);
103230557Sjimharris  std::filebuf* const buf2 = sstrm1.rdbuf();
104230557Sjimharris  std::streambuf* pbasebuf2 = active_buffer(sstrm1);
105230557Sjimharris  VERIFY( buf1 == buf2 );
106230557Sjimharris  VERIFY( pbasebuf1 != pbasebuf2 );
107230557Sjimharris  VERIFY( pbasebuf2 == pbasebuf0 );
108230557Sjimharris
109230557Sjimharris  // How confusing and non-intuitive is this?
110230557Sjimharris  // These semantics are a joke, a serious defect, and incredibly lame.
111230557Sjimharris}
112230557Sjimharris
113230557Sjimharrisint main()
114230557Sjimharris{
115230557Sjimharris  test00();
116230557Sjimharris  test01();
117230557Sjimharris
118230557Sjimharris  test02();
119230557Sjimharris  return 0;
120230557Sjimharris}
121230557Sjimharris
122230557Sjimharris
123230557Sjimharris
124230557Sjimharris