xref: /llvm-project/libcxx/test/std/input.output/iostreams.base/ios/basic.ios.members/set_rdbuf.pass.cpp (revision 2df59c50688c122bbcae7467d3eaf862c3ea3088)
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 // <ios>
10 
11 // template <class charT, class traits> class basic_ios
12 
13 // void set_rdbuf(basic_streambuf<charT, traits>* sb);
14 
15 #include <ios>
16 #include <streambuf>
17 #include <cassert>
18 
19 #include "test_macros.h"
20 
21 struct testbuf
22     : public std::streambuf
23 {
24 };
25 
26 struct testios
27     : public std::ios
28 {
testiostestios29     testios(std::streambuf* p) : std::ios(p) {}
set_rdbuftestios30     void set_rdbuf(std::streambuf* x) {std::ios::set_rdbuf(x);}
31 };
32 
main(int,char **)33 int main(int, char**)
34 {
35     testbuf sb1;
36     testbuf sb2;
37     testios ios(&sb1);
38 #ifndef TEST_HAS_NO_EXCEPTIONS
39     try
40     {
41         ios.setstate(std::ios::badbit);
42         ios.exceptions(std::ios::badbit);
43         assert(false);
44     }
45     catch (...)
46     {
47     }
48 #endif
49     ios.set_rdbuf(&sb2);
50     assert(ios.rdbuf() == &sb2);
51 #ifndef TEST_HAS_NO_EXCEPTIONS
52     try
53     {
54         ios.setstate(std::ios::badbit);
55         ios.exceptions(std::ios::badbit);
56     }
57     catch (...)
58     {
59     }
60 #endif
61     ios.set_rdbuf(0);
62     assert(ios.rdbuf() == 0);
63 
64   return 0;
65 }
66