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