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     {
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     {
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 #ifndef TEST_HAS_NO_EXCEPTIONS
70     {
71         std::stringbuf sb;
72         std::istream is(&sb);
73         is.exceptions(std::ios::failbit);
74 
75         bool threw = false;
76         try {
77             std::string s;
78             is >> s;
79         } catch (std::ios::failure const&) {
80             threw = true;
81         }
82 
83         assert(!is.bad());
84         assert(is.fail());
85         assert(is.eof());
86         assert(threw);
87     }
88     {
89         std::stringbuf sb;
90         std::istream is(&sb);
91         is.exceptions(std::ios::eofbit);
92 
93         bool threw = false;
94         try {
95             std::string s;
96             is >> s;
97         } catch (std::ios::failure const&) {
98             threw = true;
99         }
100 
101         assert(!is.bad());
102         assert(is.fail());
103         assert(is.eof());
104         assert(threw);
105     }
106 #endif // TEST_HAS_NO_EXCEPTIONS
107 #if TEST_STD_VER >= 11
108     {
109         typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
110         std::istringstream in("a bc defghij");
111         S s("initial text");
112         in >> s;
113         assert(in.good());
114         assert(s == "a");
115         assert(in.peek() == ' ');
116         in >> s;
117         assert(in.good());
118         assert(s == "bc");
119         assert(in.peek() == ' ');
120         in.width(3);
121         in >> s;
122         assert(in.good());
123         assert(s == "def");
124         assert(in.peek() == 'g');
125         in >> s;
126         assert(in.eof());
127         assert(s == "ghij");
128         in >> s;
129         assert(in.fail());
130     }
131     {
132         typedef std::basic_string<wchar_t, std::char_traits<wchar_t>, min_allocator<wchar_t>> S;
133         std::wistringstream in(L"a bc defghij");
134         S s(L"initial text");
135         in >> s;
136         assert(in.good());
137         assert(s == L"a");
138         assert(in.peek() == L' ');
139         in >> s;
140         assert(in.good());
141         assert(s == L"bc");
142         assert(in.peek() == L' ');
143         in.width(3);
144         in >> s;
145         assert(in.good());
146         assert(s == L"def");
147         assert(in.peek() == L'g');
148         in >> s;
149         assert(in.eof());
150         assert(s == L"ghij");
151         in >> s;
152         assert(in.fail());
153     }
154 #endif
155 
156   return 0;
157 }
158