| 1 | // 1999-09-20 bkoz |
| 2 | |
| 3 | // Copyright (C) 1999-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 | |
| 21 | // 27.4.3 template class fpos |
| 22 | |
| 23 | #include <cwchar> // for mbstate_t |
| 24 | #include <ios> |
| 25 | #include <cstring> |
| 26 | #include <testsuite_hooks.h> |
| 27 | |
| 28 | void test01() |
| 29 | { |
| 30 | typedef std::mbstate_t state_type; |
| 31 | // Use zero-initialization of the underlying memory so that padding |
| 32 | // bytes, if any, stand a better chance of comparing the same. |
| 33 | // Zero-initialized memory is guaranteed to be a valid initial |
| 34 | // state. This doesn't quite guarantee that any padding bits won't |
| 35 | // be overwritten when copying from other instances that haven't |
| 36 | // been fully initialized: this data type is compatible with C, so |
| 37 | // it is likely plain old data, but it could have a default ctor |
| 38 | // that initializes only the relevant fields, whereas copy-ctor and |
| 39 | // operator= could be implemented as a full-object memcpy, including |
| 40 | // padding bits, rather than fieldwise copying. However, since |
| 41 | // we're comparing two values copied from the same state_type |
| 42 | // instance, if padding bits are copied, we'll get the same for both |
| 43 | // of them, and if they aren't, we'll keep the values we initialized |
| 44 | // them with, so this should be good. |
| 45 | state_type state[2]; |
| 46 | std::memset(state, 0, sizeof (state)); |
| 47 | |
| 48 | |
| 49 | std::streampos pos01(0); |
| 50 | |
| 51 | // 27.4.3.1 fpos members |
| 52 | // void state(state_type s); |
| 53 | // state_type state(); |
| 54 | |
| 55 | // XXX Need to have better sanity checking for the mbstate_t type, |
| 56 | // or whatever the instantiating type for class fpos happens to be |
| 57 | // for streampos, as things like equality operators and assignment |
| 58 | // operators, increment and deincrement operators need to be in |
| 59 | // place. |
| 60 | pos01.state(state[1]); |
| 61 | state[0] = pos01.state(); |
| 62 | VERIFY( std::memcmp(&state[0], &state[1], sizeof(state_type)) == 0 ); |
| 63 | } |
| 64 | |
| 65 | int main() |
| 66 | { |
| 67 | test01(); |
| 68 | return 0; |
| 69 | } |