libcxx

libcxx mirror with random patches
git clone https://git.neptards.moe/neptards/libcxx.git
Log | Files | Refs

pbackfail.pass.cpp (1972B)


      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 pbackfail(int_type c = traits::eof());
     13 
     14 #include <fstream>
     15 #include <cassert>
     16 
     17 #include "test_macros.h"
     18 
     19 template <class CharT>
     20 struct test_buf
     21     : public std::basic_filebuf<CharT>
     22 {
     23     typedef std::basic_filebuf<CharT>  base;
     24     typedef typename base::char_type   char_type;
     25     typedef typename base::int_type    int_type;
     26     typedef typename base::traits_type traits_type;
     27 
     28     char_type* eback() const {return base::eback();}
     29     char_type* gptr()  const {return base::gptr();}
     30     char_type* egptr() const {return base::egptr();}
     31     void gbump(int n) {base::gbump(n);}
     32 
     33     virtual int_type pbackfail(int_type c = traits_type::eof()) {return base::pbackfail(c);}
     34 };
     35 
     36 int main()
     37 {
     38     {
     39         test_buf<char> f;
     40         assert(f.open("underflow.dat", std::ios_base::in) != 0);
     41         assert(f.is_open());
     42         assert(f.sbumpc() == '1');
     43         assert(f.sgetc() == '2');
     44         typename test_buf<char>::int_type pbackResult = f.pbackfail('a');
     45         LIBCPP_ASSERT(pbackResult == -1);
     46         if (pbackResult != -1) {
     47             assert(f.sbumpc() == 'a');
     48             assert(f.sgetc() == '2');
     49         }
     50     }
     51     {
     52         test_buf<char> f;
     53         assert(f.open("underflow.dat", std::ios_base::in | std::ios_base::out) != 0);
     54         assert(f.is_open());
     55         assert(f.sbumpc() == '1');
     56         assert(f.sgetc() == '2');
     57         typename test_buf<char>::int_type pbackResult = f.pbackfail('a');
     58         LIBCPP_ASSERT(pbackResult == 'a');
     59         if (pbackResult != -1) {
     60             assert(f.sbumpc() == 'a');
     61             assert(f.sgetc() == '2');
     62         }
     63     }
     64 }