xref: /llvm-project/libcxx/test/std/input.output/iostreams.base/ios/basic.ios.members/copyfmt.pass.cpp (revision b9a2658a3e8bd13b0f9e7a8a440832a95b377216)
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 // REQUIRES: locale.en_US.UTF-8
10 // REQUIRES: locale.fr_FR.UTF-8
11 
12 // <ios>
13 
14 // template <class charT, class traits> class basic_ios
15 
16 // basic_ios& copyfmt(const basic_ios& rhs);
17 
18 // XFAIL: FROZEN-CXX03-HEADERS-FIXME
19 
20 #include <ios>
21 #include <memory>
22 #include <streambuf>
23 #include <sstream>
24 #include <cassert>
25 
26 #include "platform_support.h" // locale name macros
27 
28 #include "test_macros.h"
29 #include "operator_hijacker.h"
30 
31 struct testbuf
32     : public std::streambuf
33 {
34 };
35 
36 bool f1_called = false;
37 bool f2_called = false;
38 
39 bool g1_called = false;
40 bool g2_called = false;
41 bool g3_called = false;
42 
43 void f1(std::ios_base::event ev, std::ios_base& stream, int index)
44 {
45     if (ev == std::ios_base::erase_event)
46     {
47         assert(!f1_called);
48         assert( f2_called);
49         assert(!g1_called);
50         assert(!g2_called);
51         assert(!g3_called);
52         assert(stream.getloc().name() == LOCALE_en_US_UTF_8);
53         assert(index == 4);
54         f1_called = true;
55     }
56 }
57 
58 void f2(std::ios_base::event ev, std::ios_base& stream, int index)
59 {
60     if (ev == std::ios_base::erase_event)
61     {
62         assert(!f1_called);
63         assert(!f2_called);
64         assert(!g1_called);
65         assert(!g2_called);
66         assert(!g3_called);
67         assert(stream.getloc().name() == LOCALE_en_US_UTF_8);
68         assert(index == 5);
69         f2_called = true;
70     }
71 }
72 
73 void g1(std::ios_base::event ev, std::ios_base& stream, int index)
74 {
75     if (ev == std::ios_base::copyfmt_event)
76     {
77         assert( f1_called);
78         assert( f2_called);
79         assert(!g1_called);
80         assert( g2_called);
81         assert( g3_called);
82         assert(stream.getloc().name() == LOCALE_fr_FR_UTF_8);
83         assert(index == 7);
84         g1_called = true;
85     }
86 }
87 
88 void g2(std::ios_base::event ev, std::ios_base& stream, int index)
89 {
90     if (ev == std::ios_base::copyfmt_event)
91     {
92         assert( f1_called);
93         assert( f2_called);
94         assert(!g1_called);
95         assert(!g2_called);
96         assert( g3_called);
97         assert(stream.getloc().name() == LOCALE_fr_FR_UTF_8);
98         assert(index == 8);
99         g2_called = true;
100     }
101 }
102 
103 void g3(std::ios_base::event ev, std::ios_base& stream, int index)
104 {
105     if (ev == std::ios_base::copyfmt_event)
106     {
107         assert( f1_called);
108         assert( f2_called);
109         assert(!g1_called);
110         assert(!g2_called);
111         assert(!g3_called);
112         assert(stream.getloc().name() == LOCALE_fr_FR_UTF_8);
113         assert(index == 9);
114         g3_called = true;
115     }
116 }
117 
118 int main(int, char**)
119 {
120     testbuf sb1;
121     std::ios ios1(&sb1);
122     ios1.flags(std::ios::boolalpha | std::ios::dec | std::ios::fixed);
123     ios1.precision(1);
124     ios1.width(11);
125     ios1.imbue(std::locale(LOCALE_en_US_UTF_8));
126     ios1.exceptions(std::ios::failbit);
127     ios1.setstate(std::ios::eofbit);
128     ios1.register_callback(f1, 4);
129     ios1.register_callback(f2, 5);
130     ios1.iword(0) = 1;
131     ios1.iword(1) = 2;
132     ios1.iword(2) = 3;
133     char c1, c2, c3;
134     ios1.pword(0) = &c1;
135     ios1.pword(1) = &c2;
136     ios1.pword(2) = &c3;
137     ios1.tie((std::ostream*)1);
138     ios1.fill('1');
139 
140     testbuf sb2;
141     std::ios ios2(&sb2);
142     ios2.flags(std::ios::showpoint | std::ios::uppercase);
143     ios2.precision(2);
144     ios2.width(12);
145     ios2.imbue(std::locale(LOCALE_fr_FR_UTF_8));
146     ios2.exceptions(std::ios::eofbit);
147     ios2.setstate(std::ios::goodbit);
148     ios2.register_callback(g1, 7);
149     ios2.register_callback(g2, 8);
150     ios2.register_callback(g3, 9);
151     ios2.iword(0) = 4;
152     ios2.iword(1) = 5;
153     ios2.iword(2) = 6;
154     ios2.iword(3) = 7;
155     ios2.iword(4) = 8;
156     ios2.iword(5) = 9;
157     char d1, d2;
158     ios2.pword(0) = &d1;
159     ios2.pword(1) = &d2;
160     ios2.tie((std::ostream*)2);
161     ios2.fill('2');
162 
163     ios1.copyfmt(ios1);
164     assert(!f1_called);
165 
166 #ifndef TEST_HAS_NO_EXCEPTIONS
167     try
168     {
169         ios1.copyfmt(ios2);
170         assert(false);
171     }
172     catch (std::ios_base::failure&)
173     {
174     }
175     assert(ios1.rdstate() == std::ios::eofbit);
176     assert(ios1.rdbuf() == &sb1);
177     assert(ios1.flags() == (std::ios::showpoint | std::ios::uppercase));
178     assert(ios1.precision() == 2);
179     assert(ios1.width() == 12);
180     assert(ios1.getloc().name() == LOCALE_fr_FR_UTF_8);
181     assert(ios1.exceptions() == std::ios::eofbit);
182     assert(f1_called);
183     assert(f2_called);
184     assert(g1_called);
185     assert(g2_called);
186     assert(g3_called);
187     assert(ios1.iword(0) == 4);
188     assert(ios1.iword(1) == 5);
189     assert(ios1.iword(2) == 6);
190     assert(ios1.iword(3) == 7);
191     assert(ios1.iword(4) == 8);
192     assert(ios1.iword(5) == 9);
193     assert(ios1.pword(0) == &d1);
194     assert(ios1.pword(1) == &d2);
195     assert(ios1.tie() == (std::ostream*)2);
196     assert(ios1.fill() == '2');
197 #endif
198 
199     {
200       std::basic_stringbuf<char, operator_hijacker_char_traits<char> > sb;
201       std::basic_ios<char, operator_hijacker_char_traits<char> > ios(std::addressof(sb));
202       ios.copyfmt(ios);
203     }
204 
205   return 0;
206 }
207