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     {
     41         B b;
     42         assert(b.rdbuf() == nullptr);
     43         assert(new_called != 0);
     44     }
     45     assert(new_called == 0);
     46     {
     47         std::stringstream s;
     48         B b(s.rdbuf());
     49         assert(b.rdbuf() == s.rdbuf());
     50         assert(new_called != 0);
     51     }
     52     assert(new_called == 0);
     53     {
     54         std::stringstream s;
     55         B b(s.rdbuf(), new std::codecvt_utf8<wchar_t>);
     56         assert(b.rdbuf() == s.rdbuf());
     57         assert(new_called != 0);
     58     }
     59     assert(new_called == 0);
     60     {
     61         std::stringstream s;
     62         B b(s.rdbuf(), new std::codecvt_utf8<wchar_t>, std::mbstate_t());
     63         assert(b.rdbuf() == s.rdbuf());
     64         assert(new_called != 0);
     65     }
     66     assert(new_called == 0);
     67 }
     68