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 | // operator<<(unsigned long long val); |
15 | |
16 | #include <ostream> |
17 | #include <cassert> |
18 | |
19 | #include "test_macros.h" |
20 | |
21 | template <class CharT> |
22 | class testbuf |
23 | : public std::basic_streambuf<CharT> |
24 | { |
25 | typedef std::basic_streambuf<CharT> base; |
26 | std::basic_string<CharT> str_; |
27 | public: |
28 | testbuf() |
29 | { |
30 | } |
31 | |
32 | std::basic_string<CharT> str() const |
33 | {return std::basic_string<CharT>(base::pbase(), base::pptr());} |
34 | |
35 | protected: |
36 | |
37 | virtual typename base::int_type |
38 | overflow(typename base::int_type ch = base::traits_type::eof()) |
39 | { |
40 | if (ch != base::traits_type::eof()) |
41 | { |
42 | int n = static_cast<int>(str_.size()); |
43 | str_.push_back(static_cast<CharT>(ch)); |
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 ch; |
50 | } |
51 | }; |
52 | |
53 | int main(int, char**) |
54 | { |
55 | { |
56 | std::ostream os((std::streambuf*)0); |
57 | unsigned long long n = 0; |
58 | os << n; |
59 | assert(os.bad()); |
60 | assert(os.fail()); |
61 | } |
62 | { |
63 | testbuf<char> sb; |
64 | std::ostream os(&sb); |
65 | unsigned long long n = 0; |
66 | os << n; |
67 | assert(sb.str() == "0"); |
68 | } |
69 | { |
70 | testbuf<char> sb; |
71 | std::ostream os(&sb); |
72 | unsigned long long n = 10; |
73 | os << n; |
74 | assert(sb.str() == "10"); |
75 | } |
76 | { |
77 | testbuf<char> sb; |
78 | std::ostream os(&sb); |
79 | std::hex(base&: os); |
80 | unsigned long long n = static_cast<unsigned long long>(-10); |
81 | os << n; |
82 | assert(sb.str() == "fffffffffffffff6"); |
83 | } |
84 | |
85 | return 0; |
86 | } |
87 |