1 // Copyright (C) 2001, 2002 Free Software Foundation, Inc.
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)
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.
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,
19 // 27.8.1.3 filebuf member functions
20 // @require@ %-*.tst %-*.txt
21 // @diff@ %-*.tst %-*.txt
23 // various tests for filebuf::open() and filebuf::close() including
24 // the non-portable functionality in the libstdc++-v3 IO library
31 #include <sys/types.h>
33 #include <testsuite_hooks.h>
35 const char name_01[] = "filebuf_members-1.tst";
36 const char name_02[] = "filebuf_members-1.txt";
38 // Test member functions.
42 const char* name_03 = "filebuf_members-3"; // empty file, need to create
44 std::filebuf fb_01; // in
45 std::filebuf fb_02; // out
46 std::filebuf fb_03; // in | out
49 VERIFY( !fb_01.is_open() );
50 VERIFY( !fb_02.is_open() );
51 VERIFY( !fb_03.is_open() );
53 // filebuf_type* open(const char* __s, ios_base::openmode __mode)
54 fb_01.open(name_01, std::ios_base::in | std::ios_base::ate);
55 fb_02.open(name_02, std::ios_base::in | std::ios_base::out
56 | std::ios_base::trunc);
57 // Try to open two different files without closing the first:
58 // Should keep the old file attached, and disregard attempt to overthrow.
59 fb_02.open(name_03, std::ios_base::in | std::ios_base::out);
60 fb_03.open(name_03, std::ios_base::out | std::ios_base::trunc);
61 VERIFY( fb_01.is_open() );
62 VERIFY( fb_02.is_open() );
63 VERIFY( fb_03.is_open() );
65 // filebuf_type* close()
69 VERIFY( !fb_01.is_open() );
70 VERIFY( !fb_02.is_open() );
71 VERIFY( !fb_03.is_open() );
74 // Verify that std::filebuf doesn't close files that it didn't open
75 // when using the following std::filebuf ctor:
77 // std::filebuf(__c_file_type* __f,
78 // ios_base::openmode __mode,
81 // Thanks to "George T. Talbot" <george@moberg.com> for uncovering
82 // this bug/situation.
89 FILE* f2 = fopen(name_01, "r");
92 std::filebuf fb(f2, std::ios_base::in, 512);
94 close_num = fclose(f2);
95 VERIFY( close_num == 0 );
99 FILE* f = fopen(name_01, "r");
102 std::ifstream ifstream1(name_01);
103 VERIFY( ifstream1.is_open() );
104 std::ios_base::iostate st01 = ifstream1.rdstate();
105 VERIFY( st01 == std::ios_base::goodbit );
107 close_num = fclose(f);
108 VERIFY( close_num == 0 );
114 int first_fd = ::open(name_01, O_RDONLY);
115 VERIFY( first_fd != -1 );
116 FILE* first_file = ::fdopen(first_fd, "r");
117 VERIFY( first_file != NULL );
118 std::filebuf fb (first_file, std::ios_base::in);
120 int second_fd = fb.fd();
122 VERIFY( first_fd == second_fd );
125 // libstdc++/2913, libstdc++/4879
126 // John Fardo <jfardo@laurelnetworks.com>, Brad Garcia <garsh@attbi.com>
130 signal(SIGPIPE, SIG_IGN);
132 if (0 != mkfifo("xxx", S_IRWXU))
134 std::cerr << "failed to creat fifo" << std::endl;
141 std::cerr << "failed to fork" << std::endl;
147 std::ifstream ifs("xxx");
153 std::ofstream ofs("xxx");
158 * ISO/IED 14882:1998(E) 27.8.1.10.4
162 * Effects: Calls rdbuf()->close() and, if that function fails
163 * (returns a null pointer), calls setstate(failbit)...
166 if (!(ofs.rdstate() & std::ios::failbit))
168 std::cerr << "fail bit was not set!" << std::endl;
177 // Charles Leggett <CGLeggett@lbl.gov>
182 std::fstream scratch_file;
184 scratch_file.open("SCRATCH", std::ios::out);
185 scratch_file.close();
187 scratch_file.open("SCRATCH", std::ios::in);
188 scratch_file.close();
190 VERIFY(scratch_file);