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 // XFAIL: with_system_cxx_lib=macosx10.14
10 // XFAIL: with_system_cxx_lib=macosx10.13
11 // XFAIL: with_system_cxx_lib=macosx10.12
12 // XFAIL: with_system_cxx_lib=macosx10.11
13 // XFAIL: with_system_cxx_lib=macosx10.10
14 // XFAIL: with_system_cxx_lib=macosx10.9
15 
16 // <istream>
17 
18 // int_type peek();
19 
20 #include <istream>
21 #include <cassert>
22 #include "test_macros.h"
23 
24 template <class CharT>
25 struct testbuf
26     : public std::basic_streambuf<CharT>
27 {
28     typedef std::basic_string<CharT> string_type;
29     typedef std::basic_streambuf<CharT> base;
30 private:
31     string_type str_;
32 public:
33 
testbuftestbuf34     testbuf() {}
testbuftestbuf35     testbuf(const string_type& str)
36         : str_(str)
37     {
38         base::setg(const_cast<CharT*>(str_.data()),
39                    const_cast<CharT*>(str_.data()),
40                    const_cast<CharT*>(str_.data()) + str_.size());
41     }
42 
ebacktestbuf43     CharT* eback() const {return base::eback();}
gptrtestbuf44     CharT* gptr() const {return base::gptr();}
egptrtestbuf45     CharT* egptr() const {return base::egptr();}
46 };
47 
main(int,char **)48 int main(int, char**)
49 {
50     {
51         testbuf<char> sb(" 1\n2345\n6");
52         std::istream is(&sb);
53         assert(is.peek() == ' ');
54         assert(!is.eof());
55         assert(!is.fail());
56         assert(is.gcount() == 0);
57         is.get();
58         assert(is.peek() == '1');
59         assert(!is.eof());
60         assert(!is.fail());
61         assert(is.gcount() == 0);
62     }
63     {
64         testbuf<wchar_t> sb(L" 1\n2345\n6");
65         std::wistream is(&sb);
66         assert(is.peek() == L' ');
67         assert(!is.eof());
68         assert(!is.fail());
69         assert(is.gcount() == 0);
70         is.get();
71         assert(is.peek() == L'1');
72         assert(!is.eof());
73         assert(!is.fail());
74         assert(is.gcount() == 0);
75     }
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     {
93         testbuf<wchar_t> sb;
94         std::basic_istream<wchar_t> is(&sb);
95         is.exceptions(std::ios_base::eofbit);
96         bool threw = false;
97         try {
98             is.peek();
99         } catch (std::ios_base::failure&) {
100             threw = true;
101         }
102         assert(threw);
103         assert(!is.bad());
104         assert( is.eof());
105         assert(!is.fail());
106     }
107 #endif
108 
109     return 0;
110 }
111