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 bitset(string, pos, n, zero, one);
10 
11 #include <bitset>
12 #include <cassert>
13 #include <algorithm> // for 'min' and 'max'
14 #include <stdexcept> // for 'invalid_argument'
15 
16 #include "test_macros.h"
17 
18 #if defined(TEST_COMPILER_C1XX)
19 #pragma warning(disable: 6294) // Ill-defined for-loop:  initial condition does not satisfy test.  Loop body not executed.
20 #endif
21 
22 template <std::size_t N>
23 void test_string_ctor()
24 {
25 #ifndef TEST_HAS_NO_EXCEPTIONS
26     {
27         try {
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         try {
38             std::string str("xxx1010101010xxxx");
39             std::bitset<N> v(str, 2, 10);
40             assert(false);
41         }
42         catch (std::invalid_argument&)
43         {
44         }
45     }
46     {
47         try {
48             std::string str("xxxbababababaxxxx");
49             std::bitset<N> v(str, 2, 10, 'a', 'b');
50             assert(false);
51         }
52         catch (std::invalid_argument&)
53         {
54         }
55     }
56 #endif // TEST_HAS_NO_EXCEPTIONS
57     {
58         std::string str("xxx1010101010xxxx");
59         std::bitset<N> v(str, 3, 10);
60         std::size_t M = std::min<std::size_t>(N, 10);
61         for (std::size_t i = 0; i < M; ++i)
62             assert(v[i] == (str[3 + M - 1 - i] == '1'));
63         for (std::size_t i = 10; i < N; ++i)
64             assert(v[i] == false);
65     }
66     {
67         std::string str("xxxbababababaxxxx");
68         std::bitset<N> v(str, 3, 10, 'a', 'b');
69         std::size_t M = std::min<std::size_t>(N, 10);
70         for (std::size_t i = 0; i < M; ++i)
71             assert(v[i] == (str[3 + M - 1 - i] == 'b'));
72         for (std::size_t i = 10; i < N; ++i)
73             assert(v[i] == false);
74     }
75 }
76 
77 struct Nonsense {
78     virtual ~Nonsense() {}
79 };
80 
81 void test_for_non_eager_instantiation() {
82     // Ensure we don't accidentally instantiate `std::basic_string<Nonsense>`
83     // since it may not be well formed and can cause an error in the
84     // non-immediate context.
85     static_assert(!std::is_constructible<std::bitset<3>, Nonsense*>::value, "");
86     static_assert(!std::is_constructible<std::bitset<3>, Nonsense*, size_t, Nonsense&, Nonsense&>::value, "");
87 }
88 
89 int main(int, char**)
90 {
91     test_string_ctor<0>();
92     test_string_ctor<1>();
93     test_string_ctor<31>();
94     test_string_ctor<32>();
95     test_string_ctor<33>();
96     test_string_ctor<63>();
97     test_string_ctor<64>();
98     test_string_ctor<65>();
99     test_string_ctor<1000>();
100     test_for_non_eager_instantiation();
101 
102   return 0;
103 }
104