Home | History | Annotate | Download | only in ostream.seeks
      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 // <ostream>
     11 
     12 // template <class charT, class traits = char_traits<charT> >
     13 //   class basic_ostream;
     14 
     15 // basic_ostream<charT,traits>& seekp(pos_type pos);
     16 
     17 #include <ostream>
     18 #include <cassert>
     19 
     20 int seekpos_called = 0;
     21 
     22 template <class CharT>
     23 struct testbuf
     24     : public std::basic_streambuf<CharT>
     25 {
     26     typedef std::basic_streambuf<CharT> base;
     27     testbuf() {}
     28 
     29 protected:
     30 
     31     typename base::pos_type
     32     seekpos(typename base::pos_type sp, std::ios_base::openmode which)
     33     {
     34         ++seekpos_called;
     35         assert(which == std::ios_base::out);
     36         return sp;
     37     }
     38 };
     39 
     40 int main()
     41 {
     42     {
     43         seekpos_called = 0;
     44         std::ostream os((std::streambuf*)0);
     45         assert(&os.seekp(5) == &os);
     46         assert(seekpos_called == 0);
     47     }
     48     {
     49         seekpos_called = 0;
     50         testbuf<char> sb;
     51         std::ostream os(&sb);
     52         assert(&os.seekp(10) == &os);
     53         assert(seekpos_called == 1);
     54         assert(os.good());
     55         assert(&os.seekp(-1) == &os);
     56         assert(seekpos_called == 2);
     57         assert(os.fail());
     58     }
     59     { // See https://llvm.org/bugs/show_bug.cgi?id=21361
     60         seekpos_called = 0;
     61         testbuf<char> sb;
     62         std::ostream os(&sb);
     63         os.setstate(std::ios_base::eofbit);
     64         assert(&os.seekp(10) == &os);
     65         assert(seekpos_called == 1);
     66         assert(os.rdstate() == std::ios_base::eofbit);
     67     }
     68 }
     69