xref: /llvm-project/libcxx/test/std/strings/basic.string/string.nonmembers/string.io/stream_extract.pass.cpp (revision a40bada91aeda276a772acfbcae6e8de26755a11)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // <string>
10 
11 // template<class charT, class traits, class Allocator>
12 //   basic_istream<charT,traits>&
13 //   operator>>(basic_istream<charT,traits>& is,
14 //              basic_string<charT,traits,Allocator>& str);
15 
16 #include <string>
17 #include <sstream>
18 #include <cassert>
19 
20 #include "min_allocator.h"
21 #include "test_macros.h"
22 
23 int main(int, char**) {
24   {
25     std::istringstream in("a bc defghij");
26     std::string s("initial text");
27     in >> s;
28     assert(in.good());
29     assert(s == "a");
30     assert(in.peek() == ' ');
31     in >> s;
32     assert(in.good());
33     assert(s == "bc");
34     assert(in.peek() == ' ');
35     in.width(3);
36     in >> s;
37     assert(in.good());
38     assert(s == "def");
39     assert(in.peek() == 'g');
40     in >> s;
41     assert(in.eof());
42     assert(s == "ghij");
43     in >> s;
44     assert(in.fail());
45   }
46 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
47   {
48     std::wistringstream in(L"a bc defghij");
49     std::wstring s(L"initial text");
50     in >> s;
51     assert(in.good());
52     assert(s == L"a");
53     assert(in.peek() == L' ');
54     in >> s;
55     assert(in.good());
56     assert(s == L"bc");
57     assert(in.peek() == L' ');
58     in.width(3);
59     in >> s;
60     assert(in.good());
61     assert(s == L"def");
62     assert(in.peek() == L'g');
63     in >> s;
64     assert(in.eof());
65     assert(s == L"ghij");
66     in >> s;
67     assert(in.fail());
68   }
69 #endif
70 #ifndef TEST_HAS_NO_EXCEPTIONS
71   {
72     std::stringbuf sb;
73     std::istream is(&sb);
74     is.exceptions(std::ios::failbit);
75 
76     bool threw = false;
77     try {
78       std::string s;
79       is >> s;
80     } catch (std::ios::failure const&) {
81       threw = true;
82     }
83 
84     assert(!is.bad());
85     assert(is.fail());
86     assert(is.eof());
87     assert(threw);
88   }
89   {
90     std::stringbuf sb;
91     std::istream is(&sb);
92     is.exceptions(std::ios::eofbit);
93 
94     bool threw = false;
95     try {
96       std::string s;
97       is >> s;
98     } catch (std::ios::failure const&) {
99       threw = true;
100     }
101 
102     assert(!is.bad());
103     assert(is.fail());
104     assert(is.eof());
105     assert(threw);
106   }
107 #endif // TEST_HAS_NO_EXCEPTIONS
108 #if TEST_STD_VER >= 11
109   {
110     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
111     std::istringstream in("a bc defghij");
112     S s("initial text");
113     in >> s;
114     assert(in.good());
115     assert(s == "a");
116     assert(in.peek() == ' ');
117     in >> s;
118     assert(in.good());
119     assert(s == "bc");
120     assert(in.peek() == ' ');
121     in.width(3);
122     in >> s;
123     assert(in.good());
124     assert(s == "def");
125     assert(in.peek() == 'g');
126     in >> s;
127     assert(in.eof());
128     assert(s == "ghij");
129     in >> s;
130     assert(in.fail());
131   }
132 #  ifndef TEST_HAS_NO_WIDE_CHARACTERS
133   {
134     typedef std::basic_string<wchar_t, std::char_traits<wchar_t>, min_allocator<wchar_t>> S;
135     std::wistringstream in(L"a bc defghij");
136     S s(L"initial text");
137     in >> s;
138     assert(in.good());
139     assert(s == L"a");
140     assert(in.peek() == L' ');
141     in >> s;
142     assert(in.good());
143     assert(s == L"bc");
144     assert(in.peek() == L' ');
145     in.width(3);
146     in >> s;
147     assert(in.good());
148     assert(s == L"def");
149     assert(in.peek() == L'g');
150     in >> s;
151     assert(in.eof());
152     assert(s == L"ghij");
153     in >> s;
154     assert(in.fail());
155   }
156 #  endif // TEST_HAS_NO_WIDE_CHARACTERS
157 #endif   // TEST_STD_VER >= 11
158 
159   return 0;
160 }
161