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 setstate(iostate state);
14
15#include <ios>
16#include <streambuf>
17#include <cassert>
18
19#include "test_macros.h"
20
21struct testbuf : public std::streambuf {};
22
23int main(int, char**)
24{
25 {
26 std::ios ios(0);
27 ios.setstate(std::ios::goodbit);
28 assert(ios.rdstate() == std::ios::badbit);
29#ifndef TEST_HAS_NO_EXCEPTIONS
30 try
31 {
32 ios.exceptions(except: std::ios::badbit);
33 assert(false);
34 }
35 catch (...)
36 {
37 }
38 try
39 {
40 ios.setstate(std::ios::goodbit);
41 assert(false);
42 }
43 catch (std::ios::failure&)
44 {
45 assert(ios.rdstate() == std::ios::badbit);
46 }
47 try
48 {
49 ios.setstate(std::ios::eofbit);
50 assert(false);
51 }
52 catch (std::ios::failure&)
53 {
54 assert(ios.rdstate() == (std::ios::eofbit | std::ios::badbit));
55 }
56#endif
57 }
58 {
59 testbuf sb;
60 std::ios ios(&sb);
61 ios.setstate(std::ios::goodbit);
62 assert(ios.rdstate() == std::ios::goodbit);
63 ios.setstate(std::ios::eofbit);
64 assert(ios.rdstate() == std::ios::eofbit);
65 ios.setstate(std::ios::failbit);
66 assert(ios.rdstate() == (std::ios::eofbit | std::ios::failbit));
67 }
68
69 return 0;
70}
71

source code of libcxx/test/std/input.output/iostreams.base/ios/iostate.flags/setstate.pass.cpp