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// <istream>
10
11// template<class traits>
12// basic_istream<char,traits>& operator>>(basic_istream<char,traits>&& in, unsigned char* s);
13
14#include <istream>
15#include <cassert>
16#include <streambuf>
17
18#include "test_macros.h"
19
20template <class CharT>
21struct testbuf
22 : public std::basic_streambuf<CharT>
23{
24 typedef std::basic_string<CharT> string_type;
25 typedef std::basic_streambuf<CharT> base;
26private:
27 string_type str_;
28public:
29
30 testbuf() {}
31 testbuf(const string_type& str)
32 : str_(str)
33 {
34 base::setg(const_cast<CharT*>(str_.data()),
35 const_cast<CharT*>(str_.data()),
36 const_cast<CharT*>(str_.data()) + str_.size());
37 }
38
39 CharT* eback() const {return base::eback();}
40 CharT* gptr() const {return base::gptr();}
41 CharT* egptr() const {return base::egptr();}
42};
43
44int main(int, char**)
45{
46 {
47 testbuf<char> sb(" abcdefghijk ");
48 std::istream is(&sb);
49 unsigned char s[20];
50 is >> s;
51 assert(!is.eof());
52 assert(!is.fail());
53 assert(std::string((char*)s) == "abcdefghijk");
54 }
55 {
56 testbuf<char> sb(" abcdefghijk ");
57 std::istream is(&sb);
58 is.width(wide: 4);
59 unsigned char s[20];
60 is >> s;
61 assert(!is.eof());
62 assert(!is.fail());
63 assert(std::string((char*)s) == "abc");
64 assert(is.width() == 0);
65 }
66#if TEST_STD_VER > 17
67 {
68 testbuf<char> sb(" abcdefghijk ");
69 std::istream is(&sb);
70 unsigned char s[4];
71 is >> s;
72 assert(!is.eof());
73 assert(!is.fail());
74 assert(std::string((char*)s) == "abc");
75 }
76#endif
77 {
78 testbuf<char> sb(" abcdefghijk");
79 std::istream is(&sb);
80 unsigned char s[20];
81 is >> s;
82 assert( is.eof());
83 assert(!is.fail());
84 assert(std::string((char*)s) == "abcdefghijk");
85 assert(is.width() == 0);
86 }
87 {
88 testbuf<char> sb(" abcdefghijk");
89 std::istream is(&sb);
90 unsigned char s[20];
91 is.width(wide: 1);
92 is >> s;
93 assert(!is.eof());
94 assert( is.fail());
95 assert(std::string((char*)s) == "");
96 assert(is.width() == 0);
97 }
98#if TEST_STD_VER > 17
99 {
100 testbuf<char> sb(" abcdefghijk");
101 std::istream is(&sb);
102 unsigned char s[1];
103 is >> s;
104 assert(!is.eof());
105 assert( is.fail());
106 assert(std::string((char*)s) == "");
107 }
108#endif
109#ifndef TEST_HAS_NO_EXCEPTIONS
110 {
111 testbuf<char> sb;
112 std::basic_istream<char> is(&sb);
113 is.exceptions(std::ios_base::failbit);
114
115 bool threw = false;
116 try {
117 unsigned char s[20];
118 is.width(10);
119 is >> s;
120 } catch (std::ios_base::failure const&) {
121 threw = true;
122 }
123
124 assert(!is.bad());
125 assert(is.fail());
126 assert(is.eof());
127 assert(threw);
128 }
129 {
130 testbuf<char> sb;
131 std::basic_istream<char> is(&sb);
132 is.exceptions(std::ios_base::eofbit);
133
134 bool threw = false;
135 try {
136 unsigned char s[20];
137 is.width(10);
138 is >> s;
139 } catch (std::ios_base::failure const&) {
140 threw = true;
141 }
142
143 assert(!is.bad());
144 assert(is.fail());
145 assert(is.eof());
146 assert(threw);
147 }
148#endif // TEST_HAS_NO_EXCEPTIONS
149
150 return 0;
151}
152

source code of libcxx/test/std/input.output/iostream.format/input.streams/istream.formatted/istream_extractors/unsigned_char_pointer.pass.cpp