| 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.4 Overridden virtual functions |
| 21 | |
| 22 | // { dg-require-fileio "" } |
| 23 | // { dg-additional-files "filebuf_virtuals-1.txt" } |
| 24 | |
| 25 | #include <fstream> |
| 26 | #include <testsuite_hooks.h> |
| 27 | |
| 28 | // NB: This test assumes that _M_buf_size == 40, and not the usual |
| 29 | // buffer_size length of BUFSIZ (8192), so that overflow/underflow can be |
| 30 | // simulated a bit more readily. |
| 31 | // NRB (Nota Really Bene): setting it to 40 breaks the test, as intended. |
| 32 | const int buffer_size = 8192; |
| 33 | //const int buffer_size = 40; |
| 34 | |
| 35 | const char name_01[] = "filebuf_virtuals-1.txt"; // file with data in it |
| 36 | const char name_02[] = "filebuf_virtuals-2.txt"; // empty file, need to create |
| 37 | const char name_03[] = "filebuf_virtuals-3.txt"; // empty file, need to create |
| 38 | |
| 39 | class derived_filebuf: public std::filebuf |
| 40 | { |
| 41 | public: |
| 42 | void |
| 43 | set_size(int_type __size) { _M_buf_size = __size; } |
| 44 | }; |
| 45 | |
| 46 | derived_filebuf fb_01; // in |
| 47 | derived_filebuf fb_02; // out |
| 48 | derived_filebuf fb_03; // in | out |
| 49 | |
| 50 | // Initialize filebufs to be the same size regardless of platform. |
| 51 | void test03() |
| 52 | { |
| 53 | fb_01.set_size(buffer_size); |
| 54 | fb_02.set_size(buffer_size); |
| 55 | fb_03.set_size(buffer_size); |
| 56 | } |
| 57 | |
| 58 | // Test overloaded virtual functions. |
| 59 | void test05() |
| 60 | { |
| 61 | typedef std::filebuf::int_type int_type; |
| 62 | typedef std::filebuf::traits_type traits_type; |
| 63 | typedef std::filebuf::pos_type pos_type; |
| 64 | typedef std::filebuf::off_type off_type; |
| 65 | typedef size_t size_type; |
| 66 | |
| 67 | std::filebuf f_tmp; |
| 68 | std::streamoff strmof_1, strmof_2; |
| 69 | |
| 70 | // GET |
| 71 | // int in_avail() |
| 72 | // if a read position is available, return _M_in_end - _M_in_cur. |
| 73 | // else return showmanyc. |
| 74 | strmof_1 = fb_01.in_avail(); |
| 75 | strmof_2 = fb_02.in_avail(); |
| 76 | VERIFY( strmof_1 == -1 ); |
| 77 | VERIFY( strmof_1 == strmof_2 ); //fail because not open |
| 78 | strmof_1 = fb_03.in_avail(); |
| 79 | VERIFY( strmof_1 == strmof_2 ); |
| 80 | fb_01.open(name_01, std::ios_base::in); |
| 81 | fb_02.open(name_02, std::ios_base::out | std::ios_base::trunc); |
| 82 | fb_03.open(name_03, std::ios_base::out | std::ios_base::in | std::ios_base::trunc); |
| 83 | strmof_1 = fb_01.in_avail(); |
| 84 | strmof_2 = fb_02.in_avail(); |
| 85 | VERIFY( strmof_1 != strmof_2 ); |
| 86 | VERIFY( strmof_1 >= 0 ); |
| 87 | VERIFY( strmof_2 == -1 ); // empty file |
| 88 | strmof_1 = fb_03.in_avail(); |
| 89 | VERIFY( strmof_1 == 0 ); // empty file |
| 90 | } |
| 91 | |
| 92 | int main() |
| 93 | { |
| 94 | test03(); |
| 95 | test05(); |
| 96 | return 0; |
| 97 | } |