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 <streambuf>
19
20#include "test_macros.h"
21
22template <class CharT>
23struct testbuf
24 : public std::basic_streambuf<CharT>
25{
26 typedef std::basic_string<CharT> string_type;
27 typedef std::basic_streambuf<CharT> base;
28private:
29 string_type str_;
30public:
31
32 testbuf() {}
33 testbuf(const string_type& str)
34 : str_(str)
35 {
36 base::setg(const_cast<CharT*>(str_.data()),
37 const_cast<CharT*>(str_.data()),
38 const_cast<CharT*>(str_.data()) + str_.size());
39 }
40
41 CharT* eback() const {return base::eback();}
42 CharT* gptr() const {return base::gptr();}
43 CharT* egptr() const {return base::egptr();}
44};
45
46int main(int, char**)
47{
48 {
49 testbuf<char> sb(" 1\n2345\n6");
50 std::istream is(&sb);
51 assert(is.peek() == ' ');
52 assert(!is.eof());
53 assert(!is.fail());
54 assert(is.gcount() == 0);
55 is.get();
56 assert(is.peek() == '1');
57 assert(!is.eof());
58 assert(!is.fail());
59 assert(is.gcount() == 0);
60 }
61#ifndef TEST_HAS_NO_WIDE_CHARACTERS
62 {
63 testbuf<wchar_t> sb(L" 1\n2345\n6");
64 std::wistream is(&sb);
65 assert(is.peek() == L' ');
66 assert(!is.eof());
67 assert(!is.fail());
68 assert(is.gcount() == 0);
69 is.get();
70 assert(is.peek() == L'1');
71 assert(!is.eof());
72 assert(!is.fail());
73 assert(is.gcount() == 0);
74 }
75#endif
76#ifndef TEST_HAS_NO_EXCEPTIONS
77 {
78 testbuf<char> sb;
79 std::basic_istream<char> is(&sb);
80 is.exceptions(std::ios_base::eofbit);
81 bool threw = false;
82 try {
83 is.peek();
84 } catch (std::ios_base::failure&) {
85 threw = true;
86 }
87 assert(threw);
88 assert(!is.bad());
89 assert( is.eof());
90 assert(!is.fail());
91 }
92#ifndef TEST_HAS_NO_WIDE_CHARACTERS
93 {
94 testbuf<wchar_t> sb;
95 std::basic_istream<wchar_t> is(&sb);
96 is.exceptions(std::ios_base::eofbit);
97 bool threw = false;
98 try {
99 is.peek();
100 } catch (std::ios_base::failure&) {
101 threw = true;
102 }
103 assert(threw);
104 assert(!is.bad());
105 assert( is.eof());
106 assert(!is.fail());
107 }
108#endif
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.unformatted/peek.pass.cpp