Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / input.output / iostream.format / output.streams / ostream.rvalue / rvalue.pass.cpp
blob8ef9b6c77a0211eabb98b449cc76c7d2c9fc9135
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 //===----------------------------------------------------------------------===//
9 // <ostream>
11 // template <class Stream, class T>
12 // Stream&& operator<<(Stream&& os, const T& x);
14 #include <ostream>
15 #include <cassert>
17 #include "test_macros.h"
20 template <class CharT>
21 class testbuf
22 : public std::basic_streambuf<CharT>
24 typedef std::basic_streambuf<CharT> base;
25 std::basic_string<CharT> str_;
26 public:
27 testbuf()
31 std::basic_string<CharT> str() const
32 {return std::basic_string<CharT>(base::pbase(), base::pptr());}
34 protected:
36 virtual typename base::int_type
37 overflow(typename base::int_type ch = base::traits_type::eof())
39 if (ch != base::traits_type::eof())
41 int n = static_cast<int>(str_.size());
42 str_.push_back(static_cast<CharT>(ch));
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);
48 return ch;
52 struct Int {
53 int value;
54 template <class CharT>
55 friend void operator<<(std::basic_ostream<CharT>& os, Int const& self) {
56 os << self.value;
60 int main(int, char**)
63 testbuf<char> sb;
64 std::ostream os(&sb);
65 Int const i = {123};
66 std::ostream&& result = (std::move(os) << i);
67 assert(&result == &os);
68 assert(sb.str() == "123");
70 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
72 testbuf<wchar_t> sb;
73 std::wostream os(&sb);
74 Int const i = {123};
75 std::wostream&& result = (std::move(os) << i);
76 assert(&result == &os);
77 assert(sb.str() == L"123");
79 #endif
81 return 0;