1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // XFAIL: libcpp-no-exceptions
11 // test bitset(string, pos, n, zero, one);
12 
13 #include <bitset>
14 #include <cassert>
15 #include <algorithm> // for 'min' and 'max'
16 #include <stdexcept> // for 'invalid_argument'
17 
18 #if defined(__clang__)
19 #pragma clang diagnostic ignored "-Wtautological-compare"
20 #endif
21 
22 template <std::size_t N>
23 void test_string_ctor()
24 {
25     {
26     try
27     {
28         std::string str("xxx1010101010xxxx");
29         std::bitset<N> v(str, str.size()+1, 10);
30         assert(false);
31     }
32     catch (std::out_of_range&)
33     {
34     }
35     }
36 
37     {
38     try
39     {
40         std::string str("xxx1010101010xxxx");
41         std::bitset<N> v(str, 2, 10);
42         assert(false);
43     }
44     catch (std::invalid_argument&)
45     {
46     }
47     }
48 
49     {
50     std::string str("xxx1010101010xxxx");
51     std::bitset<N> v(str, 3, 10);
52     std::size_t M = std::min<std::size_t>(N, 10);
53     for (std::size_t i = 0; i < M; ++i)
54         assert(v[i] == (str[3 + M - 1 - i] == '1'));
55     for (std::size_t i = 10; i < N; ++i)
56         assert(v[i] == false);
57     }
58 
59     {
60     try
61     {
62         std::string str("xxxbababababaxxxx");
63         std::bitset<N> v(str, 2, 10, 'a', 'b');
64         assert(false);
65     }
66     catch (std::invalid_argument&)
67     {
68     }
69     }
70 
71     {
72     std::string str("xxxbababababaxxxx");
73     std::bitset<N> v(str, 3, 10, 'a', 'b');
74     std::size_t M = std::min<std::size_t>(N, 10);
75     for (std::size_t i = 0; i < M; ++i)
76         assert(v[i] == (str[3 + M - 1 - i] == 'b'));
77     for (std::size_t i = 10; i < N; ++i)
78         assert(v[i] == false);
79     }
80 }
81 
82 int main()
83 {
84     test_string_ctor<0>();
85     test_string_ctor<1>();
86     test_string_ctor<31>();
87     test_string_ctor<32>();
88     test_string_ctor<33>();
89     test_string_ctor<63>();
90     test_string_ctor<64>();
91     test_string_ctor<65>();
92     test_string_ctor<1000>();
93 }
94