Debian Codegraph @

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