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