| 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 | // 27.6.1.3 unformatted input functions |
| 19 | |
| 20 | #include <cwchar> // for wcslen |
| 21 | #include <istream> |
| 22 | #include <sstream> |
| 23 | #include <testsuite_hooks.h> |
| 24 | |
| 25 | // [patch] bits/istream.tcc - getline(char_type*,streamsize,char_type) |
| 26 | // http://gcc.gnu.org/ml/libstdc++/2000-07/msg00003.html |
| 27 | void |
| 28 | test05() |
| 29 | { |
| 30 | const wchar_t* charray = L"\n" |
| 31 | L"a\n" |
| 32 | L"aa\n" |
| 33 | L"aaa\n" |
| 34 | L"aaaa\n" |
| 35 | L"aaaaa\n" |
| 36 | L"aaaaaa\n" |
| 37 | L"aaaaaaa\n" |
| 38 | L"aaaaaaaa\n" |
| 39 | L"aaaaaaaaa\n" |
| 40 | L"aaaaaaaaaa\n" |
| 41 | L"aaaaaaaaaaa\n" |
| 42 | L"aaaaaaaaaaaa\n" |
| 43 | L"aaaaaaaaaaaaa\n" |
| 44 | L"aaaaaaaaaaaaaa\n"; |
| 45 | |
| 46 | const std::streamsize it = 5; |
| 47 | std::streamsize br = 0; |
| 48 | wchar_t tmp[it]; |
| 49 | std::wstringbuf sb(charray, std::ios_base::in); |
| 50 | std::wistream ifs(&sb); |
| 51 | std::streamsize blen = std::wcslen(charray); |
| 52 | VERIFY(!(!ifs)); |
| 53 | while(ifs.getline(tmp, it) || ifs.gcount()) |
| 54 | { |
| 55 | br += ifs.gcount(); |
| 56 | if(ifs.eof()) |
| 57 | { |
| 58 | // Just sanity checks to make sure we've extracted the same |
| 59 | // number of chars that were in the streambuf |
| 60 | VERIFY( br == blen ); |
| 61 | // Also, we should only set the failbit if we could |
| 62 | // _extract_ no chars from the stream, i.e. the first read |
| 63 | // returned EOF. |
| 64 | VERIFY( ifs.fail() && ifs.gcount() == 0 ); |
| 65 | } |
| 66 | else if(ifs.fail()) |
| 67 | { |
| 68 | // delimiter not read |
| 69 | // |
| 70 | // either |
| 71 | // -> extracted no characters |
| 72 | // or |
| 73 | // -> n - 1 characters are stored |
| 74 | ifs.clear(ifs.rdstate() & ~std::ios::failbit); |
| 75 | VERIFY( (ifs.gcount() == 0) || (std::wcslen(tmp) == it - 1) ); |
| 76 | VERIFY( !(!ifs) ); |
| 77 | continue; |
| 78 | } |
| 79 | else |
| 80 | { |
| 81 | // delimiter was read. |
| 82 | // |
| 83 | // -> wcslen(__s) < n - 1 |
| 84 | // -> delimiter was seen -> gcount() > wcslen(__s) |
| 85 | VERIFY( ifs.gcount() == static_cast<std::streamsize>(std::wcslen(tmp) |
| 86 | + 1) ); |
| 87 | continue; |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | int |
| 93 | main() |
| 94 | { |
| 95 | test05(); |
| 96 | return 0; |
| 97 | } |