xref: /llvm-project/libcxx/test/std/strings/basic.string/string.nonmembers/string.io/stream_extract.pass.cpp (revision 57b08b0944046a6a57ee9b7b479181f548a5b9b4)
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 
22 int main()
23 {
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     {
47         std::wistringstream in(L"a bc defghij");
48         std::wstring s(L"initial text");
49         in >> s;
50         assert(in.good());
51         assert(s == L"a");
52         assert(in.peek() == L' ');
53         in >> s;
54         assert(in.good());
55         assert(s == L"bc");
56         assert(in.peek() == L' ');
57         in.width(3);
58         in >> s;
59         assert(in.good());
60         assert(s == L"def");
61         assert(in.peek() == L'g');
62         in >> s;
63         assert(in.eof());
64         assert(s == L"ghij");
65         in >> s;
66         assert(in.fail());
67     }
68 #if TEST_STD_VER >= 11
69     {
70         typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
71         std::istringstream in("a bc defghij");
72         S s("initial text");
73         in >> s;
74         assert(in.good());
75         assert(s == "a");
76         assert(in.peek() == ' ');
77         in >> s;
78         assert(in.good());
79         assert(s == "bc");
80         assert(in.peek() == ' ');
81         in.width(3);
82         in >> s;
83         assert(in.good());
84         assert(s == "def");
85         assert(in.peek() == 'g');
86         in >> s;
87         assert(in.eof());
88         assert(s == "ghij");
89         in >> s;
90         assert(in.fail());
91     }
92     {
93         typedef std::basic_string<wchar_t, std::char_traits<wchar_t>, min_allocator<wchar_t>> S;
94         std::wistringstream in(L"a bc defghij");
95         S s(L"initial text");
96         in >> s;
97         assert(in.good());
98         assert(s == L"a");
99         assert(in.peek() == L' ');
100         in >> s;
101         assert(in.good());
102         assert(s == L"bc");
103         assert(in.peek() == L' ');
104         in.width(3);
105         in >> s;
106         assert(in.good());
107         assert(s == L"def");
108         assert(in.peek() == L'g');
109         in >> s;
110         assert(in.eof());
111         assert(s == L"ghij");
112         in >> s;
113         assert(in.fail());
114     }
115 #endif
116 }
117