Debian Codegraph @

libcxx/test/std/input.output/iostream.format/input.streams/istream.unformatted/get.pass.cpp

raw
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 // Requires 396145d in the built library.
10 // XFAIL: using-built-library-before-llvm-9
11
12 // <istream>
13
14 // int_type get();
15
16 #include <istream>
17 #include <cassert>
18 #include "test_macros.h"
19
20 template <class CharT>
21 struct testbuf
22 : public std::basic_streambuf<CharT>
23 {
24 typedef std::basic_string<CharT> string_type;
25 typedef std::basic_streambuf<CharT> base;
26 private:
27 string_type str_;
28 public:
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
44 int main(int, char**)
45 {
46 {
47 testbuf<char> sb(" ");
48 std::istream is(&sb);
49 char c = static_cast<char>(is.get());
50 assert(!is.eof());
51 assert(!is.fail());
52 assert(c == ' ');
53 assert(is.gcount() == 1);
54 }
55 {
56 testbuf<char> sb(" abc");
57 std::istream is(&sb);
58 char c = static_cast<char>(is.get());
59 assert(!is.eof());
60 assert(!is.fail());
61 assert(c == ' ');
62 assert(is.gcount() == 1);
63 c = static_cast<char>(is.get());
64 assert(!is.eof());
65 assert(!is.fail());
66 assert(c == 'a');
67 assert(is.gcount() == 1);
68 c = static_cast<char>(is.get());
69 assert(!is.eof());
70 assert(!is.fail());
71 assert(c == 'b');
72 assert(is.gcount() == 1);
73 c = static_cast<char>(is.get());
74 assert(!is.eof());
75 assert(!is.fail());
76 assert(c == 'c');
77 assert(is.gcount() == 1);
78 }
79 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
80 {
81 testbuf<wchar_t> sb(L" abc");
82 std::wistream is(&sb);
83 wchar_t c = is.get();
84 assert(!is.eof());
85 assert(!is.fail());
86 assert(c == L' ');
87 assert(is.gcount() == 1);
88 c = is.get();
89 assert(!is.eof());
90 assert(!is.fail());
91 assert(c == L'a');
92 assert(is.gcount() == 1);
93 c = is.get();
94 assert(!is.eof());
95 assert(!is.fail());
96 assert(c == L'b');
97 assert(is.gcount() == 1);
98 c = is.get();
99 assert(!is.eof());
100 assert(!is.fail());
101 assert(c == L'c');
102 assert(is.gcount() == 1);
103 }
104 #endif
105 #ifndef TEST_HAS_NO_EXCEPTIONS
106 {
107 testbuf<char> sb("rrrrrrrrr");
108 std::basic_istream<char> is(&sb);
109 is.exceptions(std::ios_base::eofbit);
110
111 bool threw = false;
112 try {
113 while (true) {
114 is.get();
115 if (is.eof())
116 break;
117 }
118 } catch (std::ios_base::failure const&) {
119 threw = true;
120 }
121
122 assert(!is.bad());
123 assert( is.fail());
124 assert( is.eof());
125 assert(threw);
126 }
127 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
128 {
129 testbuf<wchar_t> sb(L"rrrrrrrrr");
130 std::basic_istream<wchar_t> is(&sb);
131 is.exceptions(std::ios_base::eofbit);
132
133 bool threw = false;
134 try {
135 while (true) {
136 is.get();
137 if (is.eof())
138 break;
139 }
140 } catch (std::ios_base::failure const&) {
141 threw = true;
142 }
143
144 assert(!is.bad());
145 assert( is.fail());
146 assert( is.eof());
147 assert(threw);
148 }
149 #endif
150 #endif // TEST_HAS_NO_EXCEPTIONS
151
152 return 0;
153 }