Debian Codegraph @

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