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