Debian Codegraph @

libcxx/test/std/input.output/iostream.format/input.streams/istream.unformatted/peek.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 peek();
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(" 1\n2345\n6");
48 std::istream is(&sb);
49 assert(is.peek() == ' ');
50 assert(!is.eof());
51 assert(!is.fail());
52 assert(is.gcount() == 0);
53 is.get();
54 assert(is.peek() == '1');
55 assert(!is.eof());
56 assert(!is.fail());
57 assert(is.gcount() == 0);
58 }
59 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
60 {
61 testbuf<wchar_t> sb(L" 1\n2345\n6");
62 std::wistream is(&sb);
63 assert(is.peek() == L' ');
64 assert(!is.eof());
65 assert(!is.fail());
66 assert(is.gcount() == 0);
67 is.get();
68 assert(is.peek() == L'1');
69 assert(!is.eof());
70 assert(!is.fail());
71 assert(is.gcount() == 0);
72 }
73 #endif
74 #ifndef TEST_HAS_NO_EXCEPTIONS
75 {
76 testbuf<char> sb;
77 std::basic_istream<char> is(&sb);
78 is.exceptions(std::ios_base::eofbit);
79 bool threw = false;
80 try {
81 is.peek();
82 } catch (std::ios_base::failure&) {
83 threw = true;
84 }
85 assert(threw);
86 assert(!is.bad());
87 assert( is.eof());
88 assert(!is.fail());
89 }
90 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
91 {
92 testbuf<wchar_t> sb;
93 std::basic_istream<wchar_t> is(&sb);
94 is.exceptions(std::ios_base::eofbit);
95 bool threw = false;
96 try {
97 is.peek();
98 } catch (std::ios_base::failure&) {
99 threw = true;
100 }
101 assert(threw);
102 assert(!is.bad());
103 assert( is.eof());
104 assert(!is.fail());
105 }
106 #endif
107 #endif // TEST_HAS_NO_EXCEPTIONS
108
109 return 0;
110 }