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(off_type off, ios_base::seekdir dir);
     16 
     17 #include <ostream>
     18 #include <cassert>
     19 
     20 int seekoff_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     seekoff(typename base::off_type off, std::ios_base::seekdir way,
     33                                          std::ios_base::openmode which)
     34     {
     35         ++seekoff_called;
     36         assert(way == std::ios_base::beg);
     37         assert(which == std::ios_base::out);
     38         return off;
     39     }
     40 };
     41 
     42 int main()
     43 {
     44     {
     45         seekoff_called = 0;
     46         std::ostream os((std::streambuf*)0);
     47         assert(&os.seekp(5, std::ios_base::beg) == &os);
     48         assert(seekoff_called == 0);
     49     }
     50     {
     51         seekoff_called = 0;
     52         testbuf<char> sb;
     53         std::ostream os(&sb);
     54         assert(&os.seekp(10, std::ios_base::beg) == &os);
     55         assert(seekoff_called == 1);
     56         assert(os.good());
     57         assert(&os.seekp(-1, std::ios_base::beg) == &os);
     58         assert(seekoff_called == 2);
     59         assert(os.fail());
     60     }
     61     { // See https://bugs.llvm.org/show_bug.cgi?id=21361
     62         seekoff_called = 0;
     63         testbuf<char> sb;
     64         std::ostream os(&sb);
     65         os.setstate(std::ios_base::eofbit);
     66         assert(&os.seekp(10, std::ios_base::beg) == &os);
     67         assert(seekoff_called == 1);
     68         assert(os.rdstate() == std::ios_base::eofbit);
     69     }
     70 }
     71