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