| 1 | // 2001-05-21 Benjamin Kosnik <[email protected]> |
| 2 | |
| 3 | // Copyright (C) 2001-2025 Free Software Foundation, Inc. |
| 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 3, 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 COPYING3. If not see |
| 18 | // <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | // C++98 27.8.1.3 filebuf member functions |
| 21 | |
| 22 | #include <fstream> |
| 23 | #include <testsuite_hooks.h> |
| 24 | |
| 25 | // { dg-additional-files "filebuf_virtuals-1.txt" } |
| 26 | |
| 27 | // NB: This test assumes that _M_buf_size == 40, and not the usual |
| 28 | // buffer_size length of BUFSIZ (8192), so that overflow/underflow can be |
| 29 | // simulated a bit more readily. |
| 30 | // NRB (Nota Really Bene): setting it to 40 breaks the test, as intended. |
| 31 | const int buffer_size = 8192; |
| 32 | //const int buffer_size = 40; |
| 33 | |
| 34 | const char name_01[] = "filebuf_virtuals-1.txt"; // file with data in it |
| 35 | const char name_02[] = "filebuf_virtuals-2.txt"; // empty file, need to create |
| 36 | const char name_03[] = "filebuf_virtuals-3.txt"; // empty file, need to create |
| 37 | |
| 38 | class derived_filebuf: public std::filebuf |
| 39 | { |
| 40 | public: |
| 41 | void |
| 42 | set_size(int_type __size) { _M_buf_size = __size; } |
| 43 | }; |
| 44 | |
| 45 | derived_filebuf fb_01; // in |
| 46 | derived_filebuf fb_02; // out |
| 47 | derived_filebuf fb_03; // in | out |
| 48 | |
| 49 | // Initialize filebufs to be the same size regardless of platform. |
| 50 | void test03() |
| 51 | { |
| 52 | fb_01.set_size(buffer_size); |
| 53 | fb_02.set_size(buffer_size); |
| 54 | fb_03.set_size(buffer_size); |
| 55 | } |
| 56 | |
| 57 | // Test overloaded virtual functions. |
| 58 | void test05() |
| 59 | { |
| 60 | typedef std::filebuf::int_type int_type; |
| 61 | typedef std::filebuf::traits_type traits_type; |
| 62 | typedef std::filebuf::pos_type pos_type; |
| 63 | typedef std::filebuf::off_type off_type; |
| 64 | typedef size_t size_type; |
| 65 | |
| 66 | std::filebuf f_tmp; |
| 67 | |
| 68 | fb_01.open(name_01, std::ios_base::in); |
| 69 | fb_02.open(name_02, std::ios_base::out | std::ios_base::trunc); |
| 70 | fb_03.open(name_03, std::ios_base::out | std::ios_base::in | std::ios_base::trunc); |
| 71 | |
| 72 | // NB Have to close these suckers. . . |
| 73 | // filebuf_type* close() |
| 74 | fb_01.close(); |
| 75 | fb_02.close(); |
| 76 | fb_03.close(); |
| 77 | VERIFY( !fb_01.is_open() ); |
| 78 | VERIFY( !fb_02.is_open() ); |
| 79 | VERIFY( !fb_03.is_open() ); |
| 80 | } |
| 81 | |
| 82 | int main() |
| 83 | { |
| 84 | test03(); |
| 85 | test05(); |
| 86 | return 0; |
| 87 | } |