int.pass.cpp revision bc8d3f97eb5c958007f2713238472e0c1c8fe02c
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// operator<<(int val);
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
31    std::basic_string<CharT> str() const
32        {return std::basic_string<CharT>(base::pbase(), base::pptr());}
33
34protected:
35
36    virtual typename base::int_type
37        overflow(typename base::int_type __c = base::traits_type::eof())
38        {
39            if (__c != base::traits_type::eof())
40            {
41                int n = str_.size();
42                str_.push_back(__c);
43                str_.resize(str_.capacity());
44                base::setp(const_cast<CharT*>(str_.data()),
45                           const_cast<CharT*>(str_.data() + str_.size()));
46                base::pbump(n+1);
47            }
48            return __c;
49        }
50};
51
52int main()
53{
54    {
55        std::ostream os((std::streambuf*)0);
56        int n = 0;
57        os << n;
58        assert(os.bad());
59        assert(os.fail());
60    }
61    {
62        testbuf<char> sb;
63        std::ostream os(&sb);
64        int n = 0;
65        os << n;
66        assert(sb.str() == "0");
67    }
68    {
69        testbuf<char> sb;
70        std::ostream os(&sb);
71        int n = -10;
72        os << n;
73        assert(sb.str() == "-10");
74    }
75    {
76        testbuf<char> sb;
77        std::ostream os(&sb);
78        hex(os);
79        int n = -10;
80        os << n;
81        assert(sb.str() == "fffffff6");
82    }
83}
84