| 1 | // { dg-require-fileio "" } |
| 2 | |
| 3 | // Copyright (C) 2010-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 | // 27.8.1.4 Overridden virtual functions |
| 21 | |
| 22 | #include <fstream> |
| 23 | #include <cstring> |
| 24 | #include <testsuite_hooks.h> |
| 25 | |
| 26 | void test01() |
| 27 | { |
| 28 | using namespace std; |
| 29 | |
| 30 | typedef filebuf::pos_type pos_type; |
| 31 | const char name[] = "tmp_seekoff-4.tst"; |
| 32 | |
| 33 | const size_t size = 12; |
| 34 | char buf[size]; |
| 35 | streamsize n; |
| 36 | |
| 37 | filebuf fb; |
| 38 | fb.open(name, ios_base::in | ios_base::out | ios_base::trunc); |
| 39 | |
| 40 | n = fb.sputn("abcd", 4); |
| 41 | VERIFY( n == 4 ); |
| 42 | |
| 43 | fb.pubseekoff(0, ios_base::beg); |
| 44 | n = fb.sgetn(buf, 3); |
| 45 | VERIFY( n == 3 ); |
| 46 | VERIFY( !memcmp(buf, "abc", 3) ); |
| 47 | |
| 48 | // Check read => write without pubseekoff(0, ios_base::cur) |
| 49 | |
| 50 | n = fb.sputn("ef", 2); |
| 51 | VERIFY( n == 2 ); |
| 52 | |
| 53 | fb.pubseekoff(0, ios_base::beg); |
| 54 | |
| 55 | n = fb.sgetn(buf, size); |
| 56 | VERIFY( n == 5 ); |
| 57 | VERIFY( !memcmp(buf, "abcef", 5) ); |
| 58 | |
| 59 | fb.pubseekoff(0, ios_base::beg); |
| 60 | n = fb.sputn("gh", 2); |
| 61 | VERIFY( n == 2 ); |
| 62 | |
| 63 | // Check write => read without pubseekoff(0, ios_base::cur) |
| 64 | |
| 65 | n = fb.sgetn( buf, 3 ); |
| 66 | VERIFY( !memcmp(buf, "cef", 3) ); |
| 67 | |
| 68 | n = fb.sputn("ijkl", 4); |
| 69 | VERIFY( n == 4 ); |
| 70 | |
| 71 | fb.pubseekoff(0, ios_base::beg); |
| 72 | n = fb.sgetn(buf, 2); |
| 73 | VERIFY( n == 2 ); |
| 74 | VERIFY( !memcmp(buf, "gh", 2) ); |
| 75 | |
| 76 | fb.pubseekoff(0, ios_base::end); |
| 77 | n = fb.sputn("mno", 3); |
| 78 | VERIFY( n == 3 ); |
| 79 | |
| 80 | fb.pubseekoff(0, ios_base::beg); |
| 81 | n = fb.sgetn(buf, size); |
| 82 | VERIFY( n == 12 ); |
| 83 | VERIFY( !memcmp(buf, "ghcefijklmno", 12) ); |
| 84 | |
| 85 | fb.close(); |
| 86 | } |
| 87 | |
| 88 | int main() |
| 89 | { |
| 90 | test01(); |
| 91 | return 0; |
| 92 | } |