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