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