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