| 1 | // 1999-08-11 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 | // 27.6.1.3 unformatted input functions |
| 21 | |
| 22 | #include <istream> |
| 23 | #include <sstream> |
| 24 | #include <testsuite_hooks.h> |
| 25 | |
| 26 | void |
| 27 | test02() |
| 28 | { |
| 29 | typedef std::char_traits<char> traits_type; |
| 30 | |
| 31 | const char str_lit01[] = "\t\t\t sun*ra \n" |
| 32 | " " |
| 33 | "and his myth science arkestra present\n" |
| 34 | " " |
| 35 | "angles and demons @ play\n" |
| 36 | " " |
| 37 | "the nubians of plutonia"; |
| 38 | std::string str01(str_lit01); |
| 39 | std::string strtmp; |
| 40 | |
| 41 | std::stringbuf sbuf_04(str01, std::ios_base::in); |
| 42 | |
| 43 | std::istream is_00(0); |
| 44 | std::istream is_04(&sbuf_04); |
| 45 | std::ios_base::iostate state1, state2, statefail, stateeof; |
| 46 | statefail = std::ios_base::failbit; |
| 47 | stateeof = std::ios_base::eofbit; |
| 48 | char carray1[400] = ""; |
| 49 | |
| 50 | // istream& getline(char* s, streamsize n, char delim) |
| 51 | // istream& getline(char* s, streamsize n) |
| 52 | state1 = is_00.rdstate(); |
| 53 | is_00.getline(carray1, 20, '*'); |
| 54 | state2 = is_00.rdstate(); |
| 55 | // make sure failbit was set, since we couldn't extract |
| 56 | // from the null streambuf... |
| 57 | VERIFY( state1 != state2 ); |
| 58 | VERIFY( static_cast<bool>(state2 & statefail) ); |
| 59 | |
| 60 | VERIFY( is_04.gcount() == 0 ); |
| 61 | state1 = is_04.rdstate(); |
| 62 | is_04.getline(carray1, 1, '\t'); // extracts, throws away |
| 63 | state2 = is_04.rdstate(); |
| 64 | VERIFY( is_04.gcount() == 1 ); |
| 65 | VERIFY( state1 == state2 ); |
| 66 | VERIFY( state1 == 0 ); |
| 67 | VERIFY( !traits_type::compare("", carray1, 1) ); |
| 68 | |
| 69 | state1 = is_04.rdstate(); |
| 70 | is_04.getline(carray1, 20, '*'); |
| 71 | state2 = is_04.rdstate(); |
| 72 | VERIFY( is_04.gcount() == 10 ); |
| 73 | VERIFY( state1 == state2 ); |
| 74 | VERIFY( state1 == 0 ); |
| 75 | VERIFY( !traits_type::compare("\t\t sun", carray1, 10) ); |
| 76 | |
| 77 | state1 = is_04.rdstate(); |
| 78 | is_04.getline(carray1, 20); |
| 79 | state2 = is_04.rdstate(); |
| 80 | VERIFY( is_04.gcount() == 4 ); |
| 81 | VERIFY( state1 == state2 ); |
| 82 | VERIFY( state1 == 0 ); |
| 83 | VERIFY( !traits_type::compare("ra ", carray1, 4) ); |
| 84 | |
| 85 | state1 = is_04.rdstate(); |
| 86 | is_04.getline(carray1, 65); |
| 87 | state2 = is_04.rdstate(); |
| 88 | VERIFY( is_04.gcount() == 64 ); |
| 89 | VERIFY( state1 != state2 ); |
| 90 | VERIFY( state2 == statefail ); |
| 91 | VERIFY( !traits_type::compare( |
| 92 | " and his myth science arkestra presen", |
| 93 | carray1, 65) ); |
| 94 | |
| 95 | is_04.clear(); |
| 96 | state1 = is_04.rdstate(); |
| 97 | is_04.getline(carray1, 120, '|'); |
| 98 | state2 = is_04.rdstate(); |
| 99 | VERIFY( is_04.gcount() == 106 ); |
| 100 | VERIFY( state1 != state2 ); |
| 101 | VERIFY( state2 == stateeof ); |
| 102 | |
| 103 | is_04.clear(); |
| 104 | state1 = is_04.rdstate(); |
| 105 | is_04.getline(carray1, 100, '|'); |
| 106 | state2 = is_04.rdstate(); |
| 107 | VERIFY( is_04.gcount() == 0 ); |
| 108 | VERIFY( state1 != state2 ); |
| 109 | VERIFY( static_cast<bool>(state2 & stateeof) ); |
| 110 | VERIFY( static_cast<bool>(state2 & statefail) ); |
| 111 | } |
| 112 | |
| 113 | int |
| 114 | main() |
| 115 | { |
| 116 | test02(); |
| 117 | return 0; |
| 118 | } |