1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // test bitset(string, pos, n, zero, one);
12 #include <algorithm> // for 'min' and 'max'
14 #include <stdexcept> // for 'invalid_argument'
17 #include "test_macros.h"
19 template <std::size_t N
>
20 void test_string_ctor() {
21 #ifndef TEST_HAS_NO_EXCEPTIONS
24 std::string
s("xxx1010101010xxxx");
25 std::bitset
<N
> v(s
, s
.size()+1, 10);
28 catch (std::out_of_range
&)
34 std::string
s("xxx1010101010xxxx");
35 std::bitset
<N
> v(s
, 2, 10);
38 catch (std::invalid_argument
&)
44 std::string
s("xxxbababababaxxxx");
45 std::bitset
<N
> v(s
, 2, 10, 'a', 'b');
48 catch (std::invalid_argument
&)
52 #endif // TEST_HAS_NO_EXCEPTIONS
54 std::string
s("xxx1010101010xxxx");
55 std::bitset
<N
> v(s
, 3, 10);
56 std::size_t M
= std::min
<std::size_t>(v
.size(), 10);
57 for (std::size_t i
= 0; i
< M
; ++i
)
58 assert(v
[i
] == (s
[3 + M
- 1 - i
] == '1'));
59 for (std::size_t i
= 10; i
< v
.size(); ++i
)
60 assert(v
[i
] == false);
63 std::string
s("xxxbababababaxxxx");
64 std::bitset
<N
> v(s
, 3, 10, 'a', 'b');
65 std::size_t M
= std::min
<std::size_t>(v
.size(), 10);
66 for (std::size_t i
= 0; i
< M
; ++i
)
67 assert(v
[i
] == (s
[3 + M
- 1 - i
] == 'b'));
68 for (std::size_t i
= 10; i
< v
.size(); ++i
)
69 assert(v
[i
] == false);
74 virtual ~Nonsense() {}
77 void test_for_non_eager_instantiation() {
78 // Ensure we don't accidentally instantiate `std::basic_string<Nonsense>`
79 // since it may not be well formed and can cause an error in the
80 // non-immediate context.
81 static_assert(!std::is_constructible
<std::bitset
<3>, Nonsense
*>::value
, "");
82 static_assert(!std::is_constructible
<std::bitset
<3>, Nonsense
*, size_t, Nonsense
&, Nonsense
&>::value
, "");
85 int main(int, char**) {
86 test_string_ctor
<0>();
87 test_string_ctor
<1>();
88 test_string_ctor
<31>();
89 test_string_ctor
<32>();
90 test_string_ctor
<33>();
91 test_string_ctor
<63>();
92 test_string_ctor
<64>();
93 test_string_ctor
<65>();
94 test_string_ctor
<1000>();
95 test_for_non_eager_instantiation();