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// <ostream>
10
11// template <class charT, class traits = char_traits<charT> >
12// class basic_ostream;
13
14// basic_ostream<charT,traits>& operator<<(basic_streambuf<charT,traits>* sb);
15
16#include <ostream>
17#include <cassert>
18
19#include "test_macros.h"
20
21template <class CharT>
22class testbuf
23 : public std::basic_streambuf<CharT>
24{
25 typedef std::basic_streambuf<CharT> base;
26 std::basic_string<CharT> str_;
27public:
28 testbuf()
29 {
30 }
31 testbuf(const std::basic_string<CharT>& str)
32 : str_(str)
33 {
34 base::setg(const_cast<CharT*>(str_.data()),
35 const_cast<CharT*>(str_.data()),
36 const_cast<CharT*>(str_.data() + str_.size()));
37 }
38
39 std::basic_string<CharT> str() const
40 {return std::basic_string<CharT>(base::pbase(), base::pptr());}
41
42protected:
43
44 virtual typename base::int_type
45 overflow(typename base::int_type ch = base::traits_type::eof())
46 {
47 if (ch != base::traits_type::eof())
48 {
49 int n = static_cast<int>(str_.size());
50 str_.push_back(static_cast<CharT>(ch));
51 str_.resize(str_.capacity());
52 base::setp(const_cast<CharT*>(str_.data()),
53 const_cast<CharT*>(str_.data() + str_.size()));
54 base::pbump(n+1);
55 }
56 return ch;
57 }
58};
59
60int main(int, char**)
61{
62 {
63 testbuf<char> sb;
64 std::ostream os(&sb);
65 testbuf<char> sb2("testing...");
66 assert(sb.str() == "");
67 os << &sb2;
68 assert(sb.str() == "testing...");
69 }
70#if TEST_STD_VER > 14
71// LWG 2221 - nullptr. This is not backported to older standards modes.
72// See https://reviews.llvm.org/D127033 for more info on the rationale.
73 {
74 testbuf<char> sb;
75 std::ostream os(&sb);
76 os << nullptr;
77 assert(sb.str().size() != 0);
78 LIBCPP_ASSERT(sb.str() == "nullptr");
79 }
80#endif
81
82 return 0;
83}
84

source code of libcxx/test/std/input.output/iostream.format/output.streams/ostream.formatted/ostream.inserters/streambuf.pass.cpp