1// 1999-07-28 bkoz
2
3// Copyright (C) 1999-2015 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.2.3 basic_istream::operator>>
21
22#include <istream>
23#include <sstream>
24#include <testsuite_hooks.h>
25
26// stringbufs.
27void test01()
28{
29  typedef std::ios::traits_type ctraits_type;
30
31  bool test __attribute__((unused)) = true;
32  const std::string str_01;
33  const std::string str_02("art taylor kickin it on DAKAR");
34  std::string strtmp;
35
36  std::stringbuf isbuf_00(std::ios_base::in);
37  std::stringbuf isbuf_01(std::ios_base::in | std::ios_base::out);
38  std::stringbuf isbuf_02(str_01, std::ios_base::in);
39  std::stringbuf isbuf_03(str_01, std::ios_base::in | std::ios_base::out);
40  std::stringbuf isbuf_04(str_02, std::ios_base::in);
41  std::stringbuf isbuf_05(str_02, std::ios_base::in | std::ios_base::out);
42
43  std::istream is_00(0);
44  std::istream is_01(&isbuf_01);
45  std::istream is_02(&isbuf_02);
46  std::istream is_03(&isbuf_03);
47  std::istream is_04(&isbuf_04);
48  std::istream is_05(&isbuf_05);
49  std::ios_base::iostate state1, state2, statefail, stateeof;
50  statefail = std::ios_base::failbit;
51  stateeof = std::ios_base::eofbit;
52
53
54  // template<_CharT, _Traits>
55  //  basic_istream& operator>>(basic_streambuf*)
56
57  // null istream to empty in_buf
58  state1 = is_00.rdstate();
59  is_00 >> &isbuf_00;
60  state2 = is_00.rdstate();
61  VERIFY( state1 != state2 );
62  VERIFY( static_cast<bool>(state2 & statefail) );
63  VERIFY( isbuf_00.str() == str_01 );
64
65  // null istream to empty in_out_buf
66  is_00.clear(std::ios_base::goodbit);
67  state1 = is_00.rdstate();
68  is_00 >> &isbuf_01;
69  state2 = is_00.rdstate();
70  VERIFY( state1 != state2 );
71  VERIFY( static_cast<bool>(state2 & statefail) );
72  VERIFY( isbuf_01.str() == str_01 );
73
74  // null istream to full in_buf
75  is_00.clear(std::ios_base::goodbit);
76  state1 = is_00.rdstate();
77  is_00 >> &isbuf_04;
78  state2 = is_00.rdstate();
79  VERIFY( state1 != state2 );
80  VERIFY( static_cast<bool>(state2 & statefail) );
81  VERIFY( isbuf_04.str() == str_02 );
82
83  // null istream to full in_out_buf
84  is_00.clear(std::ios_base::goodbit);
85  state1 = is_00.rdstate();
86  is_00 >> &isbuf_05;
87  state2 = is_00.rdstate();
88  VERIFY( state1 != state2 );
89  VERIFY( static_cast<bool>(state2 & statefail) );
90  VERIFY( isbuf_05.str() == str_02 );
91
92  // empty but non-null istream to full in_buf
93  state1 = is_02.rdstate();
94  is_02 >> &isbuf_04;
95  state2 = is_02.rdstate();
96  VERIFY( state1 != state2 );
97  VERIFY( static_cast<bool>(state2 & statefail) );
98  VERIFY( isbuf_04.str() == str_02 ); // as only an "in" buffer
99  VERIFY( isbuf_04.sgetc() == 'a' );
100
101  // empty but non-null istream to full in_out_buf
102  is_02.clear(std::ios_base::goodbit);
103  state1 = is_02.rdstate();
104  is_02 >> &isbuf_05;
105  state2 = is_02.rdstate();
106  VERIFY( state1 != state2 );
107  VERIFY( static_cast<bool>(state2 & statefail) );
108  VERIFY( isbuf_05.str() == str_02 ); // as only an "in" buffer
109  VERIFY( isbuf_05.sgetc() == 'a' );
110
111  // full istream to empty in_buf (need out_buf, you know?)
112  state1 = is_04.rdstate();
113  is_04 >> &isbuf_02;
114  state2 = is_04.rdstate();
115  VERIFY( state1 != state2 );
116  VERIFY( static_cast<bool>(state2 & statefail) );
117  VERIFY( isbuf_02.str() == str_01 ); // as only an "in" buffer
118  VERIFY( isbuf_02.sgetc() == ctraits_type::eof() );
119  VERIFY( is_04.peek() == ctraits_type::eof() ); // as failed
120
121  // full istream to empty in_out_buf
122  is_04.clear(std::ios_base::goodbit);
123  state1 = is_04.rdstate();
124  is_04 >> &isbuf_03;
125  state2 = is_04.rdstate();
126  VERIFY( state1 != state2 );
127  VERIFY( !static_cast<bool>(state2 & statefail) );
128  VERIFY( state2 == stateeof );
129  strtmp = isbuf_03.str();
130  VERIFY( strtmp == str_02 ); // as only an "in" buffer
131  VERIFY( isbuf_03.sgetc() == 'a' );
132  VERIFY( is_04.peek() == ctraits_type::eof() );
133}
134
135int main()
136{
137  test01();
138  return 0;
139}
140