xref: /llvm-project/libcxx/test/std/strings/basic.string/string.nonmembers/string.io/stream_insert.pass.cpp (revision 6e1dcc9335116f650d68cdbed12bbb34a99b2d9b)
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 template <template <class> class Alloc>
test()24 void test() {
25   using S  = std::basic_string<char, std::char_traits<char>, Alloc<char> >;
26   using OS = std::basic_ostringstream<char, std::char_traits<char>, Alloc<char> >;
27   {
28     OS out;
29     S s("some text");
30     out << s;
31     assert(out.good());
32     assert(s == out.str());
33   }
34   {
35     OS out;
36     S s("some text");
37     out.width(12);
38     out << s;
39     assert(out.good());
40     assert("   " + s == out.str());
41   }
42 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
43   using WS  = std::basic_string<wchar_t, std::char_traits<wchar_t>, Alloc<wchar_t> >;
44   using WOS = std::basic_ostringstream<wchar_t, std::char_traits<wchar_t>, Alloc<wchar_t> >;
45   {
46     WOS out;
47     WS s(L"some text");
48     out << s;
49     assert(out.good());
50     assert(s == out.str());
51   }
52   {
53     WOS out;
54     WS s(L"some text");
55     out.width(12);
56     out << s;
57     assert(out.good());
58     assert(L"   " + s == out.str());
59   }
60 #endif
61 }
62 
main(int,char **)63 int main(int, char**) {
64   test<std::allocator>();
65 
66 #if TEST_STD_VER >= 11
67   test<min_allocator>();
68 #endif
69 
70   return 0;
71 }
72