| 1 | // Copyright (C) 2010-2025 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 3, 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 COPYING3. If not see |
| 16 | // <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | // { dg-require-fileio "" } |
| 19 | |
| 20 | #include <fstream> |
| 21 | #include <testsuite_hooks.h> |
| 22 | #include <testsuite_character.h> |
| 23 | |
| 24 | const char name_01[] = "tmp_seekoff_45628.tst"; |
| 25 | |
| 26 | unsigned underflows, overflows; |
| 27 | |
| 28 | class my_filebuf |
| 29 | : public std::basic_filebuf<__gnu_test::pod_uchar> |
| 30 | { |
| 31 | virtual int_type |
| 32 | underflow() |
| 33 | { |
| 34 | ++underflows; |
| 35 | return std::basic_filebuf<__gnu_test::pod_uchar>::underflow(); |
| 36 | } |
| 37 | virtual int_type |
| 38 | overflow(int_type c) |
| 39 | { |
| 40 | ++overflows; |
| 41 | return std::basic_filebuf<__gnu_test::pod_uchar>::overflow(c); |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | // libstdc++/45628 |
| 46 | void test01() |
| 47 | { |
| 48 | using __gnu_test::pod_uchar; |
| 49 | std::locale loc(std::locale::classic(), |
| 50 | new std::codecvt<my_filebuf::traits_type::char_type, char, |
| 51 | my_filebuf::traits_type::state_type>); |
| 52 | |
| 53 | my_filebuf::pos_type opos[3], ipos[3]; |
| 54 | my_filebuf q; |
| 55 | q.pubimbue(loc); |
| 56 | |
| 57 | q.open(name_01, std::ios_base::in | std::ios_base::out |
| 58 | | std::ios_base::trunc); |
| 59 | |
| 60 | q.sputc(pod_uchar::from<char>('a')); |
| 61 | opos[0] = q.pubseekoff(0, std::ios_base::cur); |
| 62 | q.sputc(pod_uchar::from<char>('b')); |
| 63 | opos[1] = q.pubseekoff(0, std::ios_base::cur); |
| 64 | q.sputc(pod_uchar::from<char>('c')); |
| 65 | opos[2] = q.pubseekoff(0, std::ios_base::cur); |
| 66 | |
| 67 | VERIFY( overflows <= 9 ); // pubseekoff calls overflow twice if converting. |
| 68 | // NB: checking opos==ipos is not very rigorous as long as it flushes, since |
| 69 | // all positions will be at initial state. |
| 70 | q.pubseekoff(0, std::ios_base::beg); |
| 71 | |
| 72 | q.sbumpc(); |
| 73 | VERIFY( underflows == 1 ); |
| 74 | |
| 75 | ipos[0] = q.pubseekoff(0, std::ios_base::cur); |
| 76 | VERIFY( ipos[0] == opos[0] ); |
| 77 | q.sbumpc(); |
| 78 | ipos[1] = q.pubseekoff(0, std::ios_base::cur); |
| 79 | VERIFY( ipos[1] == opos[1] ); |
| 80 | q.sbumpc(); |
| 81 | ipos[2] = q.pubseekoff(0, std::ios_base::cur); |
| 82 | VERIFY( ipos[2] == opos[2] ); |
| 83 | |
| 84 | VERIFY( underflows == 1 ); // pubseekoff never flushes get area |
| 85 | |
| 86 | // Bonus test: check automatic mode switches. |
| 87 | q.sputc(pod_uchar::from<char>('d')); |
| 88 | |
| 89 | q.pubseekpos( ipos[1] ); |
| 90 | q.sputc(pod_uchar::from<char>('e')); |
| 91 | |
| 92 | VERIFY( my_filebuf::traits_type::eq( |
| 93 | my_filebuf::traits_type::to_char_type(q.sgetc()), |
| 94 | pod_uchar::from<char>('d')) ); |
| 95 | } |
| 96 | |
| 97 | int main() |
| 98 | { |
| 99 | test01(); |
| 100 | return 0; |
| 101 | } |