]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/input.output/iostream.format/output.streams/ostream.formatted/ostream.inserters/streambuf.pass.cpp
Vendor import of libc++ trunk r290819:
[FreeBSD/FreeBSD.git] / test / std / input.output / iostream.format / output.streams / ostream.formatted / ostream.inserters / streambuf.pass.cpp
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 // <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
20 template <class CharT>
21 class testbuf
22     : public std::basic_streambuf<CharT>
23 {
24     typedef std::basic_streambuf<CharT> base;
25     std::basic_string<CharT> str_;
26 public:
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
41 protected:
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 = static_cast<int>(str_.size());
49                 str_.push_back(static_cast<CharT>(__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
59 int 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 }