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