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
19// 27.6.1.3 unformatted input functions
20
21#include <cwchar> // for wcslen
22#include <istream>
23#include <testsuite_hooks.h>
24
25class Inbuf : public std::wstreambuf
26{
27  static const wchar_t buf[];
28  const wchar_t* current;
29  int size;
30
31public:
32  Inbuf()
33  {
34    current = buf;
35    size = std::wcslen(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
53const wchar_t Inbuf::buf[] = L"1234567890abcdefghij";
54
55void test01()
56{
57  using namespace std;
58  bool test __attribute__((unused)) = true;
59
60  typedef char_traits<wchar_t>   traits_type;
61
62  Inbuf inbuf1;
63  wistream is(&inbuf1);
64
65  wchar_t buffer[10];
66  traits_type::assign(buffer, sizeof(buffer) / sizeof(wchar_t), L'X');
67
68  is.getline(buffer, sizeof(buffer) / sizeof(wchar_t), L'0');
69  VERIFY( is.rdstate() == ios_base::goodbit );
70  VERIFY( !traits_type::compare(buffer, L"123456789\0",
71				sizeof(buffer) / sizeof(wchar_t)) );
72  VERIFY( is.gcount() == 10 );
73
74  is.clear();
75  traits_type::assign(buffer, sizeof(buffer) / sizeof(wchar_t), L'X');
76  is.getline(buffer, sizeof(buffer) / sizeof(wchar_t));
77  VERIFY( is.rdstate() == ios_base::failbit );
78  VERIFY( !traits_type::compare(buffer, L"abcdefghi\0",
79				sizeof(buffer) / sizeof(wchar_t)) );
80  VERIFY( is.gcount() == 9 );
81
82  is.clear();
83  traits_type::assign(buffer, sizeof(buffer) / sizeof(wchar_t), L'X');
84  is.getline(buffer, sizeof(buffer) / sizeof(wchar_t));
85  VERIFY( is.rdstate() == ios_base::eofbit );
86  VERIFY( !traits_type::compare(buffer, L"j\0XXXXXXXX",
87				sizeof(buffer) / sizeof(wchar_t)) );
88  VERIFY( is.gcount() == 1 );
89
90  is.clear();
91  traits_type::assign(buffer, sizeof(buffer) / sizeof(wchar_t), L'X');
92  is.getline(buffer, sizeof(buffer) / sizeof(wchar_t));
93  VERIFY( is.rdstate() == (ios_base::eofbit | ios_base::failbit) );
94  VERIFY( !traits_type::compare(buffer, L"\0XXXXXXXXX",
95				sizeof(buffer) / sizeof(wchar_t)) );
96  VERIFY( is.gcount() == 0 );
97}
98
99int main()
100{
101  test01();
102  return 0;
103}
104