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 // pos_type seekoff(off_type off, ios_base::seekdir way,
     13 //                  ios_base::openmode which = ios_base::in | ios_base::out);
     14 // pos_type seekpos(pos_type sp,
     15 //                  ios_base::openmode which = ios_base::in | ios_base::out);
     16 
     17 // This test is not entirely portable
     18 
     19 #include <fstream>
     20 #include <cassert>
     21 
     22 template <class CharT>
     23 struct test_buf
     24     : public std::basic_filebuf<CharT>
     25 {
     26     typedef std::basic_filebuf<CharT> base;
     27     typedef typename base::char_type  char_type;
     28     typedef typename base::int_type   int_type;
     29     typedef typename base::pos_type   pos_type;
     30 
     31     char_type* eback() const {return base::eback();}
     32     char_type* gptr()  const {return base::gptr();}
     33     char_type* egptr() const {return base::egptr();}
     34     void gbump(int n) {base::gbump(n);}
     35 
     36     virtual int_type underflow() {return base::underflow();}
     37 };
     38 
     39 int main()
     40 {
     41     {
     42         char buf[10];
     43         typedef std::filebuf::pos_type pos_type;
     44         std::filebuf f;
     45         f.pubsetbuf(buf, sizeof(buf));
     46         assert(f.open("seekoff.dat", std::ios_base::in | std::ios_base::out
     47                                                        | std::ios_base::trunc) != 0);
     48         assert(f.is_open());
     49         f.sputn("abcdefghijklmnopqrstuvwxyz", 26);
     50         assert(buf[0] == 'v');
     51         pos_type p = f.pubseekoff(-15, std::ios_base::cur);
     52         assert(p == 11);
     53         assert(f.sgetc() == 'l');
     54         f.pubseekoff(0, std::ios_base::beg);
     55         assert(f.sgetc() == 'a');
     56         f.pubseekoff(-1, std::ios_base::end);
     57         assert(f.sgetc() == 'z');
     58         assert(f.pubseekpos(p) == p);
     59         assert(f.sgetc() == 'l');
     60     }
     61     std::remove("seekoff.dat");
     62     {
     63         wchar_t buf[10];
     64         typedef std::filebuf::pos_type pos_type;
     65         std::wfilebuf f;
     66         f.pubsetbuf(buf, sizeof(buf)/sizeof(buf[0]));
     67         assert(f.open("seekoff.dat", std::ios_base::in | std::ios_base::out
     68                                                        | std::ios_base::trunc) != 0);
     69         assert(f.is_open());
     70         f.sputn(L"abcdefghijklmnopqrstuvwxyz", 26);
     71         assert(buf[0] == L'v');
     72         pos_type p = f.pubseekoff(-15, std::ios_base::cur);
     73         assert(p == 11);
     74         assert(f.sgetc() == L'l');
     75         f.pubseekoff(0, std::ios_base::beg);
     76         assert(f.sgetc() == L'a');
     77         f.pubseekoff(-1, std::ios_base::end);
     78         assert(f.sgetc() == L'z');
     79         assert(f.pubseekpos(p) == p);
     80         assert(f.sgetc() == L'l');
     81     }
     82     std::remove("seekoff.dat");
     83 }
     84