ignore.pass.cpp revision b64f8b07c104c6cc986570ac8ee0ed16a9f23976
1//===----------------------------------------------------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// <istream>
11
12// basic_istream<charT,traits>&
13//    ignore(streamsize n = 1, int_type delim = traits::eof());
14
15#include <istream>
16#include <cassert>
17
18template <class CharT>
19struct testbuf
20    : public std::basic_streambuf<CharT>
21{
22    typedef std::basic_string<CharT> string_type;
23    typedef std::basic_streambuf<CharT> base;
24private:
25    string_type str_;
26public:
27
28    testbuf() {}
29    testbuf(const string_type& str)
30        : str_(str)
31    {
32        base::setg(const_cast<CharT*>(str_.data()),
33                   const_cast<CharT*>(str_.data()),
34                   const_cast<CharT*>(str_.data()) + str_.size());
35    }
36
37    CharT* eback() const {return base::eback();}
38    CharT* gptr() const {return base::gptr();}
39    CharT* egptr() const {return base::egptr();}
40};
41
42int main()
43{
44    {
45        testbuf<char> sb(" 1\n2345\n6");
46        std::istream is(&sb);
47        is.ignore();
48        assert(!is.eof());
49        assert(!is.fail());
50        assert(is.gcount() == 1);
51        is.ignore(5, '\n');
52        assert(!is.eof());
53        assert(!is.fail());
54        assert(is.gcount() == 2);
55        is.ignore(15);
56        assert( is.eof());
57        assert(!is.fail());
58        assert(is.gcount() == 6);
59    }
60    {
61        testbuf<wchar_t> sb(L" 1\n2345\n6");
62        std::wistream is(&sb);
63        is.ignore();
64        assert(!is.eof());
65        assert(!is.fail());
66        assert(is.gcount() == 1);
67        is.ignore(5, '\n');
68        assert(!is.eof());
69        assert(!is.fail());
70        assert(is.gcount() == 2);
71        is.ignore(15);
72        assert( is.eof());
73        assert(!is.fail());
74        assert(is.gcount() == 6);
75    }
76}
77