[gn build] Port f3c4b58f4b0d
[llvm-project.git] / libcxx / test / std / input.output / iostream.format / output.streams / ostream.seeks / seekp.pass.cpp
blobfcb6c8e5d73e3a164d741870db3552b715e61307
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 //===----------------------------------------------------------------------===//
9 // <ostream>
11 // template <class charT, class traits = char_traits<charT> >
12 // class basic_ostream;
14 // basic_ostream<charT,traits>& seekp(pos_type pos);
16 #include <ostream>
17 #include <cassert>
19 #include "test_macros.h"
21 int seekpos_called = 0;
23 template <class CharT>
24 struct testbuf
25 : public std::basic_streambuf<CharT>
27 typedef std::basic_streambuf<CharT> base;
28 testbuf() {}
30 protected:
32 typename base::pos_type
33 seekpos(typename base::pos_type sp, std::ios_base::openmode which)
35 ++seekpos_called;
36 assert(which == std::ios_base::out);
37 return sp;
41 int main(int, char**)
44 seekpos_called = 0;
45 std::ostream os((std::streambuf*)0);
46 assert(&os.seekp(5) == &os);
47 assert(seekpos_called == 0);
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());
60 { // See https://llvm.org/PR21361
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);
70 return 0;