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
23template <template <class> class Alloc>
24void test_string() {
25 using S = std::basic_string<char, std::char_traits<char>, Alloc<char> >;
26 {
27 std::istringstream in("a bc defghij");
28 S s("initial text");
29 in >> s;
30 assert(in.good());
31 assert(s == "a");
32 assert(in.peek() == ' ');
33 in >> s;
34 assert(in.good());
35 assert(s == "bc");
36 assert(in.peek() == ' ');
37 in.width(wide: 3);
38 in >> s;
39 assert(in.good());
40 assert(s == "def");
41 assert(in.peek() == 'g');
42 in >> s;
43 assert(in.eof());
44 assert(s == "ghij");
45 in >> s;
46 assert(in.fail());
47 }
48#ifndef TEST_HAS_NO_WIDE_CHARACTERS
49 {
50 using WS = std::basic_string<wchar_t, std::char_traits<wchar_t>, Alloc<wchar_t> >;
51 std::wistringstream in(L"a bc defghij");
52 WS s(L"initial text");
53 in >> s;
54 assert(in.good());
55 assert(s == L"a");
56 assert(in.peek() == L' ');
57 in >> s;
58 assert(in.good());
59 assert(s == L"bc");
60 assert(in.peek() == L' ');
61 in.width(wide: 3);
62 in >> s;
63 assert(in.good());
64 assert(s == L"def");
65 assert(in.peek() == L'g');
66 in >> s;
67 assert(in.eof());
68 assert(s == L"ghij");
69 in >> s;
70 assert(in.fail());
71 }
72#endif
73
74#ifndef TEST_HAS_NO_EXCEPTIONS
75 {
76 std::stringbuf sb;
77 std::istream is(&sb);
78 is.exceptions(except: std::ios::failbit);
79
80 bool threw = false;
81 try {
82 S s;
83 is >> s;
84 } catch (std::ios::failure const&) {
85 threw = true;
86 }
87
88 assert(!is.bad());
89 assert(is.fail());
90 assert(is.eof());
91 assert(threw);
92 }
93 {
94 std::stringbuf sb;
95 std::istream is(&sb);
96 is.exceptions(except: std::ios::eofbit);
97
98 bool threw = false;
99 try {
100 S s;
101 is >> s;
102 } catch (std::ios::failure const&) {
103 threw = true;
104 }
105
106 assert(!is.bad());
107 assert(is.fail());
108 assert(is.eof());
109 assert(threw);
110 }
111#endif // TEST_HAS_NO_EXCEPTIONS
112}
113
114int main(int, char**) {
115 test_string<std::allocator>();
116#if TEST_STD_VER >= 11
117 test_string<min_allocator>();
118#endif
119
120 return 0;
121}
122

source code of libcxx/test/std/strings/basic.string/string.nonmembers/string.io/stream_extract.pass.cpp