streambuf.pass.cpp revision f5256e16dfc425c1d466f6308d4026d529ce9e0b
1//===----------------------------------------------------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// <ostream>
11
12// template <class charT, class traits = char_traits<charT> >
13//   class basic_ostream;
14
15// basic_ostream<charT,traits>& operator<<(basic_streambuf<charT,traits>* sb);
16
17#include <ostream>
18#include <cassert>
19
20template <class CharT>
21class testbuf
22    : public std::basic_streambuf<CharT>
23{
24    typedef std::basic_streambuf<CharT> base;
25    std::basic_string<CharT> str_;
26public:
27    testbuf()
28    {
29    }
30    testbuf(const std::basic_string<CharT>& str)
31        : str_(str)
32    {
33        base::setg(const_cast<CharT*>(str_.data()),
34                   const_cast<CharT*>(str_.data()),
35                   const_cast<CharT*>(str_.data() + str_.size()));
36    }
37
38    std::basic_string<CharT> str() const
39        {return std::basic_string<CharT>(base::pbase(), base::pptr());}
40
41protected:
42
43    virtual typename base::int_type
44        overflow(typename base::int_type __c = base::traits_type::eof())
45        {
46            if (__c != base::traits_type::eof())
47            {
48                int n = str_.size();
49                str_.push_back(__c);
50                str_.resize(str_.capacity());
51                base::setp(const_cast<CharT*>(str_.data()),
52                           const_cast<CharT*>(str_.data() + str_.size()));
53                base::pbump(n+1);
54            }
55            return __c;
56        }
57};
58
59int main()
60{
61    {
62        testbuf<char> sb;
63        std::ostream os(&sb);
64        testbuf<char> sb2("testing...");
65        assert(sb.str() == "");
66        os << &sb2;
67        assert(sb.str() == "testing...");
68    }
69}
70