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 // REQUIRES: locale.en_US.UTF-8
     11 
     12 // <ios>
     13 
     14 // template <class charT, class traits> class basic_ios
     15 
     16 // locale imbue(const locale& loc);
     17 
     18 #include <ios>
     19 #include <streambuf>
     20 #include <cassert>
     21 
     22 #include "platform_support.h" // locale name macros
     23 
     24 struct testbuf
     25     : public std::streambuf
     26 {
     27 };
     28 
     29 bool f1_called = false;
     30 bool f2_called = false;
     31 bool f3_called = false;
     32 
     33 void f1(std::ios_base::event ev, std::ios_base& stream, int index)
     34 {
     35     if (ev == std::ios_base::imbue_event)
     36     {
     37         assert(!f1_called);
     38         assert( f2_called);
     39         assert( f3_called);
     40         assert(stream.getloc().name() == LOCALE_en_US_UTF_8);
     41         assert(index == 4);
     42         f1_called = true;
     43     }
     44 }
     45 
     46 void f2(std::ios_base::event ev, std::ios_base& stream, int index)
     47 {
     48     if (ev == std::ios_base::imbue_event)
     49     {
     50         assert(!f1_called);
     51         assert(!f2_called);
     52         assert( f3_called);
     53         assert(stream.getloc().name() == LOCALE_en_US_UTF_8);
     54         assert(index == 5);
     55         f2_called = true;
     56     }
     57 }
     58 
     59 void f3(std::ios_base::event ev, std::ios_base& stream, int index)
     60 {
     61     if (ev == std::ios_base::imbue_event)
     62     {
     63         assert(!f1_called);
     64         assert(!f2_called);
     65         assert(!f3_called);
     66         assert(stream.getloc().name() == LOCALE_en_US_UTF_8);
     67         assert(index == 6);
     68         f3_called = true;
     69     }
     70 }
     71 
     72 int main()
     73 {
     74     {
     75         std::ios ios(0);
     76         ios.register_callback(f1, 4);
     77         ios.register_callback(f2, 5);
     78         ios.register_callback(f3, 6);
     79         std::locale l = ios.imbue(std::locale(LOCALE_en_US_UTF_8));
     80         assert(l.name() == std::string("C"));
     81         assert(ios.getloc().name() == std::string(LOCALE_en_US_UTF_8));
     82         assert(f1_called);
     83         assert(f2_called);
     84         assert(f3_called);
     85     }
     86     f1_called = false;
     87     f2_called = false;
     88     f3_called = false;
     89     {
     90         testbuf sb;
     91         std::ios ios(&sb);
     92         ios.register_callback(f1, 4);
     93         ios.register_callback(f2, 5);
     94         ios.register_callback(f3, 6);
     95         std::locale l = ios.imbue(std::locale(LOCALE_en_US_UTF_8));
     96         assert(l.name() == std::string("C"));
     97         assert(ios.getloc().name() == std::string(LOCALE_en_US_UTF_8));
     98         assert(sb.getloc().name() == std::string(LOCALE_en_US_UTF_8));
     99         assert(f1_called);
    100         assert(f2_called);
    101         assert(f3_called);
    102     }
    103 }
    104