]> gcc.gnu.org Git - gcc.git/blob - libstdc++-v3/testsuite/27_io/filebuf_members.cc
filebuf.cc: Tweak.
[gcc.git] / libstdc++-v3 / testsuite / 27_io / filebuf_members.cc
1 // Copyright (C) 2000 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.3 filebuf member functions
20
21 // various tests for filebuf::open() and filebuf::close() including
22 // the non-portable functionality in the libstdc++-v3 IO library
23
24 #include <fstream>
25 #include <cassert>
26 #include <unistd.h>
27 #include <fcntl.h>
28
29
30 // verify that std::filebuf doesn't close files that it didn't open
31 // when using the following std::filebuf ctor:
32 //
33 // std::filebuf(int __fd,
34 // const char* __unused,
35 // ios_base::openmode __mode);
36 //
37 // thanks to "George T. Talbot" <george@moberg.com> for uncovering
38 // this bug/situation.
39
40 const char name_01[] = "testsuite/filebuf_members-1.tst";
41 const char name_02[] = "testsuite/filebuf_members-1.txt";
42
43 int
44 test_01()
45 {
46 bool test = true;
47 int close_num;
48
49 // read (ext)
50 int fd = open(name_01, O_RDONLY);
51 test &= fd >= 0;
52
53 {
54 std::filebuf fb(fd, "double_read", std::ios_base::in);
55 }
56
57 close_num = close(fd);
58 test &= close_num == 0;
59
60
61 // read (standard)
62 FILE* f = fopen(name_01, "r");
63 test &= f != NULL;
64
65 {
66 std::ifstream ifstream1(name_01);
67 test &= ifstream1.is_open();
68 std::ios_base::iostate st01 = ifstream1.rdstate();
69 test &= st01 == std::ios_base::goodbit;
70 }
71
72 close_num = fclose(f);
73 test &= close_num == 0;
74
75
76 #ifdef DEBUG_ASSERT
77 assert(test);
78 #endif
79
80 return test;
81 }
82
83
84 int
85 main()
86 {
87 test_01();
88 return 0;
89 }
90
91
92
93
This page took 0.038883 seconds and 5 git commands to generate.