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 // template <class charT>
10 // explicit bitset(const charT* str,
11 // typename basic_string_view<charT>::size_type n = basic_string_view<charT>::npos, // s/string/string_view since C++26
12 // charT zero = charT('0'), charT one = charT('1')); // constexpr since C++23
15 #include <algorithm> // for 'min' and 'max'
17 #include <stdexcept> // for 'invalid_argument'
18 #include <type_traits>
20 #include "test_macros.h"
22 TEST_MSVC_DIAGNOSTIC_IGNORED(6294) // Ill-defined for-loop: initial condition does not satisfy test. Loop body not executed.
24 template <std::size_t N
>
25 TEST_CONSTEXPR_CXX23
void test_char_pointer_ctor()
27 #ifndef TEST_HAS_NO_EXCEPTIONS
28 if (!TEST_IS_CONSTANT_EVALUATED
) {
30 std::bitset
<N
> v("xxx1010101010xxxx");
33 catch (std::invalid_argument
&) {}
37 static_assert(!std::is_convertible
<const char*, std::bitset
<N
> >::value
, "");
38 static_assert(std::is_constructible
<std::bitset
<N
>, const char*>::value
, "");
40 const char s
[] = "1010101010";
42 std::size_t M
= std::min
<std::size_t>(v
.size(), 10);
43 for (std::size_t i
= 0; i
< M
; ++i
)
44 assert(v
[i
] == (s
[M
- 1 - i
] == '1'));
45 for (std::size_t i
= 10; i
< v
.size(); ++i
)
46 assert(v
[i
] == false);
49 const char s
[] = "1010101010";
50 std::bitset
<N
> v(s
, 10);
51 std::size_t M
= std::min
<std::size_t>(v
.size(), 10);
52 for (std::size_t i
= 0; i
< M
; ++i
)
53 assert(v
[i
] == (s
[M
- 1 - i
] == '1'));
54 for (std::size_t i
= 10; i
< v
.size(); ++i
)
55 assert(v
[i
] == false);
58 const char s
[] = "1a1a1a1a1a";
59 std::bitset
<N
> v(s
, 10, 'a');
60 std::size_t M
= std::min
<std::size_t>(v
.size(), 10);
61 for (std::size_t i
= 0; i
< M
; ++i
)
62 assert(v
[i
] == (s
[M
- 1 - i
] == '1'));
63 for (std::size_t i
= 10; i
< v
.size(); ++i
)
64 assert(v
[i
] == false);
67 const char s
[] = "bababababa";
68 std::bitset
<N
> v(s
, 10, 'a', 'b');
69 std::size_t M
= std::min
<std::size_t>(v
.size(), 10);
70 for (std::size_t i
= 0; i
< M
; ++i
)
71 assert(v
[i
] == (s
[M
- 1 - i
] == 'b'));
72 for (std::size_t i
= 10; i
< v
.size(); ++i
)
73 assert(v
[i
] == false);
77 TEST_CONSTEXPR_CXX23
bool test() {
78 test_char_pointer_ctor
<0>();
79 test_char_pointer_ctor
<1>();
80 test_char_pointer_ctor
<31>();
81 test_char_pointer_ctor
<32>();
82 test_char_pointer_ctor
<33>();
83 test_char_pointer_ctor
<63>();
84 test_char_pointer_ctor
<64>();
85 test_char_pointer_ctor
<65>();
86 test_char_pointer_ctor
<1000>();
95 static_assert(test());