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