205 lines · cpp
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9// REQUIRES: locale.en_US.UTF-810// REQUIRES: locale.fr_FR.UTF-811 12// <ios>13 14// template <class charT, class traits> class basic_ios15 16// basic_ios& copyfmt(const basic_ios& rhs);17 18#include <ios>19#include <memory>20#include <streambuf>21#include <sstream>22#include <cassert>23 24#include "platform_support.h" // locale name macros25 26#include "test_macros.h"27#include "operator_hijacker.h"28 29struct testbuf30 : public std::streambuf31{32};33 34bool f1_called = false;35bool f2_called = false;36 37bool g1_called = false;38bool g2_called = false;39bool g3_called = false;40 41void f1(std::ios_base::event ev, std::ios_base& stream, int index)42{43 if (ev == std::ios_base::erase_event)44 {45 assert(!f1_called);46 assert( f2_called);47 assert(!g1_called);48 assert(!g2_called);49 assert(!g3_called);50 assert(stream.getloc().name() == LOCALE_en_US_UTF_8);51 assert(index == 4);52 f1_called = true;53 }54}55 56void f2(std::ios_base::event ev, std::ios_base& stream, int index)57{58 if (ev == std::ios_base::erase_event)59 {60 assert(!f1_called);61 assert(!f2_called);62 assert(!g1_called);63 assert(!g2_called);64 assert(!g3_called);65 assert(stream.getloc().name() == LOCALE_en_US_UTF_8);66 assert(index == 5);67 f2_called = true;68 }69}70 71void g1(std::ios_base::event ev, std::ios_base& stream, int index)72{73 if (ev == std::ios_base::copyfmt_event)74 {75 assert( f1_called);76 assert( f2_called);77 assert(!g1_called);78 assert( g2_called);79 assert( g3_called);80 assert(stream.getloc().name() == LOCALE_fr_FR_UTF_8);81 assert(index == 7);82 g1_called = true;83 }84}85 86void g2(std::ios_base::event ev, std::ios_base& stream, int index)87{88 if (ev == std::ios_base::copyfmt_event)89 {90 assert( f1_called);91 assert( f2_called);92 assert(!g1_called);93 assert(!g2_called);94 assert( g3_called);95 assert(stream.getloc().name() == LOCALE_fr_FR_UTF_8);96 assert(index == 8);97 g2_called = true;98 }99}100 101void g3(std::ios_base::event ev, std::ios_base& stream, int index)102{103 if (ev == std::ios_base::copyfmt_event)104 {105 assert( f1_called);106 assert( f2_called);107 assert(!g1_called);108 assert(!g2_called);109 assert(!g3_called);110 assert(stream.getloc().name() == LOCALE_fr_FR_UTF_8);111 assert(index == 9);112 g3_called = true;113 }114}115 116int main(int, char**)117{118 testbuf sb1;119 std::ios ios1(&sb1);120 ios1.flags(std::ios::boolalpha | std::ios::dec | std::ios::fixed);121 ios1.precision(1);122 ios1.width(11);123 ios1.imbue(std::locale(LOCALE_en_US_UTF_8));124 ios1.exceptions(std::ios::failbit);125 ios1.setstate(std::ios::eofbit);126 ios1.register_callback(f1, 4);127 ios1.register_callback(f2, 5);128 ios1.iword(0) = 1;129 ios1.iword(1) = 2;130 ios1.iword(2) = 3;131 char c1, c2, c3;132 ios1.pword(0) = &c1;133 ios1.pword(1) = &c2;134 ios1.pword(2) = &c3;135 ios1.tie((std::ostream*)1);136 ios1.fill('1');137 138 testbuf sb2;139 std::ios ios2(&sb2);140 ios2.flags(std::ios::showpoint | std::ios::uppercase);141 ios2.precision(2);142 ios2.width(12);143 ios2.imbue(std::locale(LOCALE_fr_FR_UTF_8));144 ios2.exceptions(std::ios::eofbit);145 ios2.setstate(std::ios::goodbit);146 ios2.register_callback(g1, 7);147 ios2.register_callback(g2, 8);148 ios2.register_callback(g3, 9);149 ios2.iword(0) = 4;150 ios2.iword(1) = 5;151 ios2.iword(2) = 6;152 ios2.iword(3) = 7;153 ios2.iword(4) = 8;154 ios2.iword(5) = 9;155 char d1, d2;156 ios2.pword(0) = &d1;157 ios2.pword(1) = &d2;158 ios2.tie((std::ostream*)2);159 ios2.fill('2');160 161 ios1.copyfmt(ios1);162 assert(!f1_called);163 164#ifndef TEST_HAS_NO_EXCEPTIONS165 try166 {167 ios1.copyfmt(ios2);168 assert(false);169 }170 catch (std::ios_base::failure&)171 {172 }173 assert(ios1.rdstate() == std::ios::eofbit);174 assert(ios1.rdbuf() == &sb1);175 assert(ios1.flags() == (std::ios::showpoint | std::ios::uppercase));176 assert(ios1.precision() == 2);177 assert(ios1.width() == 12);178 assert(ios1.getloc().name() == LOCALE_fr_FR_UTF_8);179 assert(ios1.exceptions() == std::ios::eofbit);180 assert(f1_called);181 assert(f2_called);182 assert(g1_called);183 assert(g2_called);184 assert(g3_called);185 assert(ios1.iword(0) == 4);186 assert(ios1.iword(1) == 5);187 assert(ios1.iword(2) == 6);188 assert(ios1.iword(3) == 7);189 assert(ios1.iword(4) == 8);190 assert(ios1.iword(5) == 9);191 assert(ios1.pword(0) == &d1);192 assert(ios1.pword(1) == &d2);193 assert(ios1.tie() == (std::ostream*)2);194 assert(ios1.fill() == '2');195#endif196 197 {198 std::basic_stringbuf<char, operator_hijacker_char_traits<char> > sb;199 std::basic_ios<char, operator_hijacker_char_traits<char> > ios(std::addressof(sb));200 ios.copyfmt(ios);201 }202 203 return 0;204}205