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 
main(int,char **)23 int main(int, char**)
24 {
25     {
26         std::istringstream in("a bc defghij");
27         std::string s("initial text");
28         in >> s;
29         assert(in.good());
30         assert(s == "a");
31         assert(in.peek() == ' ');
32         in >> s;
33         assert(in.good());
34         assert(s == "bc");
35         assert(in.peek() == ' ');
36         in.width(3);
37         in >> s;
38         assert(in.good());
39         assert(s == "def");
40         assert(in.peek() == 'g');
41         in >> s;
42         assert(in.eof());
43         assert(s == "ghij");
44         in >> s;
45         assert(in.fail());
46     }
47 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
48     {
49         std::wistringstream in(L"a bc defghij");
50         std::wstring s(L"initial text");
51         in >> s;
52         assert(in.good());
53         assert(s == L"a");
54         assert(in.peek() == L' ');
55         in >> s;
56         assert(in.good());
57         assert(s == L"bc");
58         assert(in.peek() == L' ');
59         in.width(3);
60         in >> s;
61         assert(in.good());
62         assert(s == L"def");
63         assert(in.peek() == L'g');
64         in >> s;
65         assert(in.eof());
66         assert(s == L"ghij");
67         in >> s;
68         assert(in.fail());
69     }
70 #endif
71 #ifndef TEST_HAS_NO_EXCEPTIONS
72     {
73         std::stringbuf sb;
74         std::istream is(&sb);
75         is.exceptions(std::ios::failbit);
76 
77         bool threw = false;
78         try {
79             std::string s;
80             is >> s;
81         } catch (std::ios::failure const&) {
82             threw = true;
83         }
84 
85         assert(!is.bad());
86         assert(is.fail());
87         assert(is.eof());
88         assert(threw);
89     }
90     {
91         std::stringbuf sb;
92         std::istream is(&sb);
93         is.exceptions(std::ios::eofbit);
94 
95         bool threw = false;
96         try {
97             std::string s;
98             is >> s;
99         } catch (std::ios::failure const&) {
100             threw = true;
101         }
102 
103         assert(!is.bad());
104         assert(is.fail());
105         assert(is.eof());
106         assert(threw);
107     }
108 #endif // TEST_HAS_NO_EXCEPTIONS
109 #if TEST_STD_VER >= 11
110     {
111         typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
112         std::istringstream in("a bc defghij");
113         S s("initial text");
114         in >> s;
115         assert(in.good());
116         assert(s == "a");
117         assert(in.peek() == ' ');
118         in >> s;
119         assert(in.good());
120         assert(s == "bc");
121         assert(in.peek() == ' ');
122         in.width(3);
123         in >> s;
124         assert(in.good());
125         assert(s == "def");
126         assert(in.peek() == 'g');
127         in >> s;
128         assert(in.eof());
129         assert(s == "ghij");
130         in >> s;
131         assert(in.fail());
132     }
133 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
134     {
135         typedef std::basic_string<wchar_t, std::char_traits<wchar_t>, min_allocator<wchar_t>> S;
136         std::wistringstream in(L"a bc defghij");
137         S s(L"initial text");
138         in >> s;
139         assert(in.good());
140         assert(s == L"a");
141         assert(in.peek() == L' ');
142         in >> s;
143         assert(in.good());
144         assert(s == L"bc");
145         assert(in.peek() == L' ');
146         in.width(3);
147         in >> s;
148         assert(in.good());
149         assert(s == L"def");
150         assert(in.peek() == L'g');
151         in >> s;
152         assert(in.eof());
153         assert(s == L"ghij");
154         in >> s;
155         assert(in.fail());
156     }
157 #endif // TEST_HAS_NO_WIDE_CHARACTERS
158 #endif // TEST_STD_VER >= 11
159 
160   return 0;
161 }
162