xref: /llvm-project/libcxx/test/std/utilities/template.bitset/bitset.operators/stream_in.pass.cpp (revision a7f9895cc18995549c7facb96e72718da282a864)
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 // UNSUPPORTED: no-localization
10 
11 // test:
12 
13 // template <class charT, class traits, size_t N>
14 // basic_istream<charT, traits>&
15 // operator>>(basic_istream<charT, traits>& is, bitset<N>& x);
16 
17 #include <bitset>
18 #include <sstream>
19 #include <cassert>
20 #include "test_macros.h"
21 
main(int,char **)22 int main(int, char**)
23 {
24     {
25         std::istringstream in("01011010");
26         std::bitset<8> b;
27         in >> b;
28         assert(b.to_ulong() == 0x5A);
29     }
30     {
31         // Make sure that input-streaming an empty bitset does not cause the
32         // failbit to be set (LWG 3199).
33         std::istringstream in("01011010");
34         std::bitset<0> b;
35         in >> b;
36         assert(b.to_string() == "");
37         assert(!in.bad());
38         assert(!in.fail());
39         assert(!in.eof());
40         assert(in.good());
41     }
42 #ifndef TEST_HAS_NO_EXCEPTIONS
43     {
44         std::stringbuf sb;
45         std::istream is(&sb);
46         is.exceptions(std::ios::failbit);
47 
48         bool threw = false;
49         try {
50             std::bitset<8> b;
51             is >> b;
52         } catch (std::ios::failure const&) {
53             threw = true;
54         }
55 
56         assert(!is.bad());
57         assert(is.fail());
58         assert(is.eof());
59         assert(threw);
60     }
61     {
62         std::stringbuf sb;
63         std::istream is(&sb);
64         is.exceptions(std::ios::eofbit);
65 
66         bool threw = false;
67         try {
68             std::bitset<8> b;
69             is >> b;
70         } catch (std::ios::failure const&) {
71             threw = true;
72         }
73 
74         assert(!is.bad());
75         assert(is.fail());
76         assert(is.eof());
77         assert(threw);
78     }
79 #endif // TEST_HAS_NO_EXCEPTIONS
80 
81     return 0;
82 }
83