brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.1 KiB · d97dcef Raw
86 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// <ostream>10 11// template <class charT, class traits = char_traits<charT> >12//   class basic_ostream;13 14// template <class charT, class traits>15//   basic_ostream<charT,traits>& endl(basic_ostream<charT,traits>& os);16 17#include <ostream>18#include <cassert>19 20#include "test_macros.h"21 22int sync_called = 0;23 24template <class CharT>25class testbuf26    : public std::basic_streambuf<CharT>27{28    typedef std::basic_streambuf<CharT> base;29    std::basic_string<CharT> str_;30public:31    testbuf()32    {33    }34 35    std::basic_string<CharT> str() const36        {return std::basic_string<CharT>(base::pbase(), base::pptr());}37 38protected:39 40    virtual typename base::int_type41        overflow(typename base::int_type ch = base::traits_type::eof())42        {43            if (ch != base::traits_type::eof())44            {45                int n = static_cast<int>(str_.size());46                str_.push_back(static_cast<CharT>(ch));47                str_.resize(str_.capacity());48                base::setp(const_cast<CharT*>(str_.data()),49                           const_cast<CharT*>(str_.data() + str_.size()));50                base::pbump(n+1);51            }52            return ch;53        }54 55    virtual int56        sync()57        {58            ++sync_called;59            return 0;60        }61};62 63int main(int, char**)64{65    {66        testbuf<char> sb;67        std::ostream os(&sb);68        std::endl(os);69        assert(sb.str() == "\n");70        assert(sync_called == 1);71        assert(os.good());72    }73#ifndef TEST_HAS_NO_WIDE_CHARACTERS74    {75        testbuf<wchar_t> sb;76        std::wostream os(&sb);77        std::endl(os);78        assert(sb.str() == L"\n");79        assert(sync_called == 2);80        assert(os.good());81    }82#endif83 84  return 0;85}86