libstdc++: Fix new <sstream> constructors
[gcc.git] / libstdc++-v3 / testsuite / 27_io / basic_ostringstream / cons / char / 1.cc
1 // Copyright (C) 2020 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17
18 // C++20 29.8.2.2 basic_stringbuf constructors [stringbuf.cons
19
20 // { dg-options "-std=gnu++2a" }
21 // { dg-do run { target c++2a } }
22 // { dg-require-effective-target cxx11-abi }
23
24 #include <sstream>
25 #include <string>
26 #include <testsuite_allocator.h>
27 #include <testsuite_hooks.h>
28
29 void
30 test01()
31 {
32 std::ostringstream::allocator_type a;
33 std::ostringstream stm(std::ios_base::in, a);
34 }
35
36 auto const cstr = "This is a test string";
37
38 void
39 test02()
40 {
41 std::string s1(cstr);
42 std::ostringstream stm(std::move(s1));
43 VERIFY( s1.empty() );
44
45 std::string s2(cstr);
46 VERIFY( stm.str() == s2 );
47 }
48
49 void
50 test03()
51 {
52 using alloc_type = __gnu_test::tracker_allocator<char>;
53 using str_type = std::basic_string<char, std::char_traits<char>, alloc_type>;
54
55 auto const mode = std::ios_base::out;
56 str_type s1(cstr);
57
58 {
59 std::ostringstream::allocator_type a;
60 std::ostringstream sbuf(s1, mode, a);
61 std::string s2(cstr);
62 VERIFY( sbuf.str() == s2 );
63 }
64
65 {
66 std::ostringstream sbuf(s1, mode);
67 std::string s2(cstr);
68 VERIFY( sbuf.str() == s2 );
69 }
70
71 {
72 std::ostringstream sbuf(s1);
73 std::string s2(cstr);
74 VERIFY( sbuf.str() == s2 );
75 }
76 }
77
78 // A minimal allocator with no default constructor
79 template<typename T>
80 struct NoDefaultCons : __gnu_test::SimpleAllocator<T>
81 {
82 using __gnu_test::SimpleAllocator<T>::SimpleAllocator;
83
84 NoDefaultCons() = delete;
85
86 NoDefaultCons(int) { }
87 };
88
89 void
90 test04()
91 {
92 using sstream = std::basic_ostringstream<char, std::char_traits<char>,
93 NoDefaultCons<char>>;
94
95 NoDefaultCons<char> a(1);
96 const std::string str(cstr);
97
98 sstream ss1(str, a);
99 VERIFY( ss1.str() == cstr );
100
101 sstream ss2(str, std::ios::in, a);
102 VERIFY( ss2.str() == cstr );
103 VERIFY( bool(ss2 << "That") );
104 VERIFY( ss2.str() == "That is a test string" );
105
106 sstream ss3(std::string(str), std::ios::ate, a);
107 VERIFY( ss3.str() == cstr );
108 VERIFY( bool(ss3 << "y thing") );
109 VERIFY( ss3.str() == "This is a test stringy thing" );
110 }
111
112 int
113 main()
114 {
115 test01();
116 test02();
117 test03();
118 test04();
119 }