Home | History | Annotate | Download | only in filebuf.virtuals
      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 // <fstream>
     13 
     14 // int_type overflow(int_type c = traits::eof());
     15 
     16 // This test is not entirely portable
     17 
     18 #include <fstream>
     19 #include <cassert>
     20 
     21 #include "platform_support.h" // locale name macros
     22 
     23 template <class CharT>
     24 struct test_buf
     25     : public std::basic_filebuf<CharT>
     26 {
     27     typedef std::basic_filebuf<CharT>  base;
     28     typedef typename base::char_type   char_type;
     29     typedef typename base::int_type    int_type;
     30     typedef typename base::traits_type traits_type;
     31 
     32     char_type* pbase() const {return base::pbase();}
     33     char_type* pptr()  const {return base::pptr();}
     34     char_type* epptr() const {return base::epptr();}
     35     void gbump(int n) {base::gbump(n);}
     36 
     37     virtual int_type overflow(int_type c = traits_type::eof()) {return base::overflow(c);}
     38 };
     39 
     40 int main()
     41 {
     42     {
     43         test_buf<char> f;
     44         assert(f.open("overflow.dat", std::ios_base::out) != 0);
     45         assert(f.is_open());
     46         assert(f.pbase() == 0);
     47         assert(f.pptr() == 0);
     48         assert(f.epptr() == 0);
     49         assert(f.overflow('a') == 'a');
     50         assert(f.pbase() != 0);
     51         assert(f.pptr() == f.pbase());
     52         assert(f.epptr() - f.pbase() == 4095);
     53     }
     54     {
     55         test_buf<char> f;
     56         assert(f.open("overflow.dat", std::ios_base::in) != 0);
     57         assert(f.is_open());
     58         assert(f.sgetc() == 'a');
     59     }
     60     std::remove("overflow.dat");
     61     {
     62         test_buf<char> f;
     63         f.pubsetbuf(0, 0);
     64         assert(f.open("overflow.dat", std::ios_base::out) != 0);
     65         assert(f.is_open());
     66         assert(f.pbase() == 0);
     67         assert(f.pptr() == 0);
     68         assert(f.epptr() == 0);
     69         assert(f.overflow('a') == 'a');
     70         assert(f.pbase() == 0);
     71         assert(f.pptr() == 0);
     72         assert(f.epptr() == 0);
     73     }
     74     {
     75         test_buf<char> f;
     76         assert(f.open("overflow.dat", std::ios_base::in) != 0);
     77         assert(f.is_open());
     78         assert(f.sgetc() == 'a');
     79     }
     80     std::remove("overflow.dat");
     81     {
     82         test_buf<wchar_t> f;
     83         assert(f.open("overflow.dat", std::ios_base::out) != 0);
     84         assert(f.is_open());
     85         assert(f.pbase() == 0);
     86         assert(f.pptr() == 0);
     87         assert(f.epptr() == 0);
     88         assert(f.overflow(L'a') == L'a');
     89         assert(f.pbase() != 0);
     90         assert(f.pptr() == f.pbase());
     91         assert(f.epptr() - f.pbase() == 4095);
     92     }
     93     {
     94         test_buf<wchar_t> f;
     95         assert(f.open("overflow.dat", std::ios_base::in) != 0);
     96         assert(f.is_open());
     97         assert(f.sgetc() == L'a');
     98     }
     99     std::remove("overflow.dat");
    100     {
    101         test_buf<wchar_t> f;
    102         f.pubsetbuf(0, 0);
    103         assert(f.open("overflow.dat", std::ios_base::out) != 0);
    104         assert(f.is_open());
    105         assert(f.pbase() == 0);
    106         assert(f.pptr() == 0);
    107         assert(f.epptr() == 0);
    108         assert(f.overflow(L'a') == L'a');
    109         assert(f.pbase() == 0);
    110         assert(f.pptr() == 0);
    111         assert(f.epptr() == 0);
    112     }
    113     {
    114         test_buf<wchar_t> f;
    115         assert(f.open("overflow.dat", std::ios_base::in) != 0);
    116         assert(f.is_open());
    117         assert(f.sgetc() == L'a');
    118     }
    119     std::remove("overflow.dat");
    120     {
    121         test_buf<wchar_t> f;
    122         f.pubimbue(std::locale(LOCALE_en_US_UTF_8));
    123         assert(f.open("overflow.dat", std::ios_base::out) != 0);
    124         assert(f.sputc(0x4E51) == 0x4E51);
    125         assert(f.sputc(0x4E52) == 0x4E52);
    126         assert(f.sputc(0x4E53) == 0x4E53);
    127     }
    128     {
    129         test_buf<char> f;
    130         assert(f.open("overflow.dat", std::ios_base::in) != 0);
    131         assert(f.is_open());
    132         assert(f.sbumpc() == 0xE4);
    133         assert(f.sbumpc() == 0xB9);
    134         assert(f.sbumpc() == 0x91);
    135         assert(f.sbumpc() == 0xE4);
    136         assert(f.sbumpc() == 0xB9);
    137         assert(f.sbumpc() == 0x92);
    138         assert(f.sbumpc() == 0xE4);
    139         assert(f.sbumpc() == 0xB9);
    140         assert(f.sbumpc() == 0x93);
    141         assert(f.sbumpc() == -1);
    142     }
    143     std::remove("overflow.dat");
    144 }
    145