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