signed_char_pointer.pass.cpp revision 256813f4e7915d64776a4edd5f4765d893b9f062
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// template<class traits>
16//   basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>& out, const signed char* s);
17
18#include <ostream>
19#include <cassert>
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
32    std::basic_string<CharT> str() const
33        {return std::basic_string<CharT>(base::pbase(), base::pptr());}
34
35protected:
36
37    virtual typename base::int_type
38        overflow(typename base::int_type __c = base::traits_type::eof())
39        {
40            if (__c != base::traits_type::eof())
41            {
42                int n = str_.size();
43                str_.push_back(__c);
44                str_.resize(str_.capacity());
45                base::setp(const_cast<CharT*>(str_.data()),
46                           const_cast<CharT*>(str_.data() + str_.size()));
47                base::pbump(n+1);
48            }
49            return __c;
50        }
51};
52
53int main()
54{
55    {
56        std::ostream os((std::streambuf*)0);
57        const signed char* c = (const signed char*)"123";
58        os << c;
59        assert(os.bad());
60        assert(os.fail());
61    }
62    {
63        testbuf<char> sb;
64        std::ostream os(&sb);
65        const signed char* c = (const signed char*)"123";
66        os << c;
67        assert(sb.str() == "123");
68    }
69    {
70        testbuf<char> sb;
71        std::ostream os(&sb);
72        os.width(5);
73        const signed char* c = (const signed char*)"123";
74        os << c;
75        assert(sb.str() == "  123");
76        assert(os.width() == 0);
77    }
78    {
79        testbuf<char> sb;
80        std::ostream os(&sb);
81        os.width(5);
82        left(os);
83        const signed char* c = (const signed char*)"123";
84        os << c;
85        assert(sb.str() == "123  ");
86        assert(os.width() == 0);
87    }
88}
89