1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // <ostream>
10 
11 // template <class charT, class traits = char_traits<charT> >
12 //   class basic_ostream;
13 
14 // basic_ostream<charT,traits>& seekp(pos_type pos);
15 
16 #include <ostream>
17 #include <cassert>
18 
19 #include "test_macros.h"
20 
21 int seekpos_called = 0;
22 
23 template <class CharT>
24 struct testbuf
25     : public std::basic_streambuf<CharT>
26 {
27     typedef std::basic_streambuf<CharT> base;
testbuftestbuf28     testbuf() {}
29 
30 protected:
31 
32     typename base::pos_type
seekpostestbuf33     seekpos(typename base::pos_type sp, std::ios_base::openmode which)
34     {
35         ++seekpos_called;
36         assert(which == std::ios_base::out);
37         return sp;
38     }
39 };
40 
main(int,char **)41 int main(int, char**)
42 {
43     {
44         seekpos_called = 0;
45         std::ostream os((std::streambuf*)0);
46         assert(&os.seekp(5) == &os);
47         assert(seekpos_called == 0);
48     }
49     {
50         seekpos_called = 0;
51         testbuf<char> sb;
52         std::ostream os(&sb);
53         assert(&os.seekp(10) == &os);
54         assert(seekpos_called == 1);
55         assert(os.good());
56         assert(&os.seekp(-1) == &os);
57         assert(seekpos_called == 2);
58         assert(os.fail());
59     }
60     { // See https://bugs.llvm.org/show_bug.cgi?id=21361
61         seekpos_called = 0;
62         testbuf<char> sb;
63         std::ostream os(&sb);
64         os.setstate(std::ios_base::eofbit);
65         assert(&os.seekp(10) == &os);
66         assert(seekpos_called == 1);
67         assert(os.rdstate() == std::ios_base::eofbit);
68     }
69 
70   return 0;
71 }
72