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// template<class charT, class traits>
15// basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>& out, char c);
16
17// XFAIL: no-wide-characters
18
19#include <ostream>
20#include <cassert>
21
22#include "test_macros.h"
23
24template <class CharT>
25class testbuf
26 : 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() const
36 {return std::basic_string<CharT>(base::pbase(), base::pptr());}
37
38protected:
39
40 virtual typename base::int_type
41 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(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
56int main(int, char**)
57{
58 {
59 std::wostream os((std::wstreambuf*)0);
60 char c = 'a';
61 os << c;
62 assert(os.bad());
63 assert(os.fail());
64 }
65 {
66 testbuf<wchar_t> sb;
67 std::wostream os(&sb);
68 char c = 'a';
69 os << c;
70 assert(sb.str() == L"a");
71 }
72 {
73 testbuf<wchar_t> sb;
74 std::wostream os(&sb);
75 os.width(wide: 5);
76 char c = 'a';
77 os << c;
78 assert(sb.str() == L" a");
79 assert(os.width() == 0);
80 }
81 {
82 testbuf<wchar_t> sb;
83 std::wostream os(&sb);
84 os.width(wide: 5);
85 std::left(base&: os);
86 char c = 'a';
87 os << c;
88 assert(sb.str() == L"a ");
89 assert(os.width() == 0);
90 }
91
92 return 0;
93}
94

source code of libcxx/test/std/input.output/iostream.format/output.streams/ostream.formatted/ostream.inserters.character/char_to_wide.pass.cpp