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& c);
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(" ");
48 std::istream is(&sb);
49 unsigned char c = 'z';
50 is >> c;
51 assert( is.eof());
52 assert( is.fail());
53 assert(c == 'z');
54 }
55 {
56 testbuf<char> sb(" abcdefghijk ");
57 std::istream is(&sb);
58 unsigned char c;
59 is >> c;
60 assert(!is.eof());
61 assert(!is.fail());
62 assert(c == 'a');
63 is >> c;
64 assert(!is.eof());
65 assert(!is.fail());
66 assert(c == 'b');
67 is >> c;
68 assert(!is.eof());
69 assert(!is.fail());
70 assert(c == 'c');
71 }
72#ifndef TEST_HAS_NO_EXCEPTIONS
73 {
74 testbuf<char> sb;
75 std::basic_istream<char> is(&sb);
76 is.exceptions(std::ios_base::failbit);
77
78 bool threw = false;
79 try {
80 unsigned char n = 0;
81 is >> n;
82 } catch (std::ios_base::failure const&) {
83 threw = true;
84 }
85
86 assert(!is.bad());
87 assert(is.fail());
88 assert(is.eof());
89 assert(threw);
90 }
91 {
92 testbuf<char> sb;
93 std::basic_istream<char> is(&sb);
94 is.exceptions(std::ios_base::eofbit);
95
96 bool threw = false;
97 try {
98 unsigned char n = 0;
99 is >> n;
100 } catch (std::ios_base::failure const&) {
101 threw = true;
102 }
103
104 assert(!is.bad());
105 assert(is.fail());
106 assert(is.eof());
107 assert(threw);
108 }
109#endif // TEST_HAS_NO_EXCEPTIONS
110
111 return 0;
112}
113

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