Home | History | Annotate | Download | only in conversions.buffer
      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 // <locale>
     11 
     12 // wbuffer_convert<Codecvt, Elem, Tr>
     13 
     14 // wbuffer_convert(streambuf *bytebuf = 0, Codecvt *pcvt = new Codecvt,
     15 //                 state_type state = state_type());
     16 
     17 #include <locale>
     18 #include <codecvt>
     19 #include <sstream>
     20 #include <cassert>
     21 #include <new>
     22 
     23 int new_called = 0;
     24 
     25 void* operator new(std::size_t s) throw(std::bad_alloc)
     26 {
     27     ++new_called;
     28     return std::malloc(s);
     29 }
     30 
     31 void  operator delete(void* p) throw()
     32 {
     33     --new_called;
     34     std::free(p);
     35 }
     36 
     37 int main()
     38 {
     39     typedef std::wbuffer_convert<std::codecvt_utf8<wchar_t> > B;
     40 #if _LIBCPP_STD_VER > 11
     41     static_assert(!std::is_convertible<std::streambuf*, B>::value, "");
     42     static_assert( std::is_constructible<B, std::streambuf*>::value, "");
     43 #endif
     44     {
     45         B b;
     46         assert(b.rdbuf() == nullptr);
     47         assert(new_called != 0);
     48     }
     49     assert(new_called == 0);
     50     {
     51         std::stringstream s;
     52         B b(s.rdbuf());
     53         assert(b.rdbuf() == s.rdbuf());
     54         assert(new_called != 0);
     55     }
     56     assert(new_called == 0);
     57     {
     58         std::stringstream s;
     59         B b(s.rdbuf(), new std::codecvt_utf8<wchar_t>);
     60         assert(b.rdbuf() == s.rdbuf());
     61         assert(new_called != 0);
     62     }
     63     assert(new_called == 0);
     64     {
     65         std::stringstream s;
     66         B b(s.rdbuf(), new std::codecvt_utf8<wchar_t>, std::mbstate_t());
     67         assert(b.rdbuf() == s.rdbuf());
     68         assert(new_called != 0);
     69     }
     70     assert(new_called == 0);
     71 }
     72