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