| 1 | // Copyright (C) 2004-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 | |
| 19 | // 27.6.1.3 unformatted input functions |
| 20 | |
| 21 | #include <cstring> // for strlen |
| 22 | #include <istream> |
| 23 | #include <testsuite_hooks.h> |
| 24 | |
| 25 | class Inbuf : public std::streambuf |
| 26 | { |
| 27 | static const char buf[]; |
| 28 | const char* current; |
| 29 | int size; |
| 30 | |
| 31 | public: |
| 32 | Inbuf() |
| 33 | { |
| 34 | current = buf; |
| 35 | size = std::strlen(buf); |
| 36 | } |
| 37 | |
| 38 | int_type underflow() |
| 39 | { |
| 40 | if (current < buf + size) |
| 41 | return traits_type::to_int_type(*current); |
| 42 | return traits_type::eof(); |
| 43 | } |
| 44 | |
| 45 | int_type uflow() |
| 46 | { |
| 47 | if (current < buf + size) |
| 48 | return traits_type::to_int_type(*current++); |
| 49 | return traits_type::eof(); |
| 50 | } |
| 51 | }; |
| 52 | |
| 53 | const char Inbuf::buf[] = "1234567890abcdefghij"; |
| 54 | |
| 55 | void test01() |
| 56 | { |
| 57 | using namespace std; |
| 58 | |
| 59 | typedef char_traits<char> traits_type; |
| 60 | |
| 61 | Inbuf inbuf1; |
| 62 | istream is(&inbuf1); |
| 63 | |
| 64 | char buffer[10]; |
| 65 | traits_type::assign(buffer, sizeof(buffer), 'X'); |
| 66 | |
| 67 | is.getline(buffer, sizeof(buffer), '0'); |
| 68 | VERIFY( is.rdstate() == ios_base::goodbit ); |
| 69 | VERIFY( !traits_type::compare(buffer, "123456789\0", sizeof(buffer)) ); |
| 70 | VERIFY( is.gcount() == 10 ); |
| 71 | |
| 72 | is.clear(); |
| 73 | traits_type::assign(buffer, sizeof(buffer), 'X'); |
| 74 | is.getline(buffer, sizeof(buffer)); |
| 75 | VERIFY( is.rdstate() == ios_base::failbit ); |
| 76 | VERIFY( !traits_type::compare(buffer, "abcdefghi\0", sizeof(buffer)) ); |
| 77 | VERIFY( is.gcount() == 9 ); |
| 78 | |
| 79 | is.clear(); |
| 80 | traits_type::assign(buffer, sizeof(buffer), 'X'); |
| 81 | is.getline(buffer, sizeof(buffer)); |
| 82 | VERIFY( is.rdstate() == ios_base::eofbit ); |
| 83 | VERIFY( !traits_type::compare(buffer, "j\0XXXXXXXX", sizeof(buffer)) ); |
| 84 | VERIFY( is.gcount() == 1 ); |
| 85 | |
| 86 | is.clear(); |
| 87 | traits_type::assign(buffer, sizeof(buffer), 'X'); |
| 88 | is.getline(buffer, sizeof(buffer)); |
| 89 | VERIFY( is.rdstate() == (ios_base::eofbit | ios_base::failbit) ); |
| 90 | VERIFY( !traits_type::compare(buffer, "\0XXXXXXXXX", sizeof(buffer)) ); |
| 91 | VERIFY( is.gcount() == 0 ); |
| 92 | } |
| 93 | |
| 94 | int main() |
| 95 | { |
| 96 | test01(); |
| 97 | return 0; |
| 98 | } |