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 // basic_istream<charT,traits>&
15 //    ignore(streamsize n = 1, int_type delim = traits::eof());
16 
17 #include <istream>
18 #include <cassert>
19 #include <streambuf>
20 
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         testbuf<char> sb(" 1\n2345\n6");
51         std::istream is(&sb);
52         is.ignore();
53         assert(!is.eof());
54         assert(!is.fail());
55         assert(is.gcount() == 1);
56         is.ignore(5, '\n');
57         assert(!is.eof());
58         assert(!is.fail());
59         assert(is.gcount() == 2);
60         is.ignore(15);
61         assert( is.eof());
62         assert(!is.fail());
63         assert(is.gcount() == 6);
64     }
65 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
66     {
67         testbuf<wchar_t> sb(L" 1\n2345\n6");
68         std::wistream is(&sb);
69         is.ignore();
70         assert(!is.eof());
71         assert(!is.fail());
72         assert(is.gcount() == 1);
73         is.ignore(5, '\n');
74         assert(!is.eof());
75         assert(!is.fail());
76         assert(is.gcount() == 2);
77         is.ignore(15);
78         assert( is.eof());
79         assert(!is.fail());
80         assert(is.gcount() == 6);
81     }
82 #endif
83 #ifndef TEST_HAS_NO_EXCEPTIONS
84     {
85         testbuf<char> sb(" ");
86         std::basic_istream<char> is(&sb);
87         is.exceptions(std::ios_base::eofbit);
88         bool threw = false;
89         try {
90             is.ignore(5);
91         } catch (std::ios_base::failure&) {
92             threw = true;
93         }
94         assert(threw);
95         assert(!is.bad());
96         assert( is.eof());
97         assert(!is.fail());
98     }
99 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
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 #endif // TEST_HAS_NO_EXCEPTIONS
117 
118     return 0;
119 }
120