| 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 |
// 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 |
| 13 |
|
| 14 |
#include <bitset> |
| 15 |
#include <cassert> |
| 16 |
#include <algorithm> // for 'min' and 'max' |
| 17 |
#include <stdexcept> // for 'invalid_argument' |
| 18 |
|
| 19 |
#include "test_macros.h" |
| 20 |
|
| 21 |
TEST_MSVC_DIAGNOSTIC_IGNORED(6294) // Ill-defined for-loop: initial condition does not satisfy test. Loop body not executed. |
| 22 |
|
| 23 |
template <std::size_t N> |
| 24 |
TEST_CONSTEXPR_CXX23 void test_char_pointer_ctor() |
| 25 |
{ |
| 26 |
#ifndef TEST_HAS_NO_EXCEPTIONS |
| 27 |
if (!TEST_IS_CONSTANT_EVALUATED) { |
| 28 |
try { |
| 29 |
std::bitset<N> v("xxx1010101010xxxx"); |
| 30 |
assert(false); |
| 31 |
} |
| 32 |
catch (std::invalid_argument&) {} |
| 33 |
} |
| 34 |
#endif |
| 35 |
|
| 36 |
static_assert(!std::is_convertible<const char*, std::bitset<N> >::value, ""); |
| 37 |
static_assert(std::is_constructible<std::bitset<N>, const char*>::value, ""); |
| 38 |
{ |
| 39 |
const char s[] = "1010101010"; |
| 40 |
std::bitset<N> v(s); |
| 41 |
std::size_t M = std::min<std::size_t>(v.size(), 10); |
| 42 |
for (std::size_t i = 0; i < M; ++i) |
| 43 |
assert(v[i] == (s[M - 1 - i] == '1')); |
| 44 |
for (std::size_t i = 10; i < v.size(); ++i) |
| 45 |
assert(v[i] == false); |
| 46 |
} |
| 47 |
{ |
| 48 |
const char s[] = "1010101010"; |
| 49 |
std::bitset<N> v(s, 10); |
| 50 |
std::size_t M = std::min<std::size_t>(v.size(), 10); |
| 51 |
for (std::size_t i = 0; i < M; ++i) |
| 52 |
assert(v[i] == (s[M - 1 - i] == '1')); |
| 53 |
for (std::size_t i = 10; i < v.size(); ++i) |
| 54 |
assert(v[i] == false); |
| 55 |
} |
| 56 |
{ |
| 57 |
const char s[] = "1a1a1a1a1a"; |
| 58 |
std::bitset<N> v(s, 10, 'a'); |
| 59 |
std::size_t M = std::min<std::size_t>(v.size(), 10); |
| 60 |
for (std::size_t i = 0; i < M; ++i) |
| 61 |
assert(v[i] == (s[M - 1 - i] == '1')); |
| 62 |
for (std::size_t i = 10; i < v.size(); ++i) |
| 63 |
assert(v[i] == false); |
| 64 |
} |
| 65 |
{ |
| 66 |
const char s[] = "bababababa"; |
| 67 |
std::bitset<N> v(s, 10, 'a', 'b'); |
| 68 |
std::size_t M = std::min<std::size_t>(v.size(), 10); |
| 69 |
for (std::size_t i = 0; i < M; ++i) |
| 70 |
assert(v[i] == (s[M - 1 - i] == 'b')); |
| 71 |
for (std::size_t i = 10; i < v.size(); ++i) |
| 72 |
assert(v[i] == false); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
TEST_CONSTEXPR_CXX23 bool test() { |
| 77 |
test_char_pointer_ctor<0>(); |
| 78 |
test_char_pointer_ctor<1>(); |
| 79 |
test_char_pointer_ctor<31>(); |
| 80 |
test_char_pointer_ctor<32>(); |
| 81 |
test_char_pointer_ctor<33>(); |
| 82 |
test_char_pointer_ctor<63>(); |
| 83 |
test_char_pointer_ctor<64>(); |
| 84 |
test_char_pointer_ctor<65>(); |
| 85 |
test_char_pointer_ctor<1000>(); |
| 86 |
|
| 87 |
return true; |
| 88 |
} |
| 89 |
|
| 90 |
int main(int, char**) |
| 91 |
{ |
| 92 |
test(); |
| 93 |
#if TEST_STD_VER > 20 |
| 94 |
static_assert(test()); |
| 95 |
#endif |
| 96 |
|
| 97 |
return 0; |
| 98 |
} |