xref: /llvm-project/libcxx/test/std/strings/basic.string/string.nonmembers/string.io/stream_insert.pass.cpp (revision f4c1258d5633fcf06385ff3fd1f4bf57ab971964)
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 // <string>
10 
11 // template<class charT, class traits, class Allocator>
12 //   basic_ostream<charT, traits>&
13 //   operator<<(basic_ostream<charT, traits>& os,
14 //              const basic_string<charT,traits,Allocator>& str);
15 
16 #include <string>
17 #include <sstream>
18 #include <cassert>
19 
20 #include "test_macros.h"
21 #include "min_allocator.h"
22 
23 int main(int, char**)
24 {
25     {
26         std::ostringstream out;
27         std::string s("some text");
28         out << s;
29         assert(out.good());
30         assert(s == out.str());
31     }
32     {
33         std::ostringstream out;
34         std::string s("some text");
35         out.width(12);
36         out << s;
37         assert(out.good());
38         assert("   " + s == out.str());
39     }
40 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
41     {
42         std::wostringstream out;
43         std::wstring s(L"some text");
44         out << s;
45         assert(out.good());
46         assert(s == out.str());
47     }
48     {
49         std::wostringstream out;
50         std::wstring s(L"some text");
51         out.width(12);
52         out << s;
53         assert(out.good());
54         assert(L"   " + s == out.str());
55     }
56 #endif
57 #if TEST_STD_VER >= 11
58     {
59         typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
60         std::basic_ostringstream<S::value_type, S::traits_type, S::allocator_type> out;
61         S s("some text");
62         out << s;
63         assert(out.good());
64         assert(s == out.str());
65     }
66     {
67         typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
68         std::basic_ostringstream<S::value_type, S::traits_type, S::allocator_type> out;
69         S s("some text");
70         out.width(12);
71         out << s;
72         assert(out.good());
73         assert("   " + s == out.str());
74     }
75 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
76     {
77         typedef std::basic_string<wchar_t, std::char_traits<wchar_t>, min_allocator<wchar_t>> S;
78         std::basic_ostringstream<S::value_type, S::traits_type, S::allocator_type> out;
79         S s(L"some text");
80         out << s;
81         assert(out.good());
82         assert(s == out.str());
83     }
84     {
85         typedef std::basic_string<wchar_t, std::char_traits<wchar_t>, min_allocator<wchar_t>> S;
86         std::basic_ostringstream<S::value_type, S::traits_type, S::allocator_type> out;
87         S s(L"some text");
88         out.width(12);
89         out << s;
90         assert(out.good());
91         assert(L"   " + s == out.str());
92     }
93 #endif // TEST_HAS_NO_WIDE_CHARACTERS
94 #endif // TEST_STD_VER >= 11
95 
96   return 0;
97 }
98