| 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 <algorithm> // for 'min' and 'max' |
| 16 | #include <cassert> |
| 17 | #include <stdexcept> // for 'invalid_argument' |
| 18 | #include <type_traits> |
| 19 | |
| 20 | #include "test_macros.h" |
| 21 | |
| 22 | TEST_MSVC_DIAGNOSTIC_IGNORED(6294) // Ill-defined for-loop: initial condition does not satisfy test. Loop body not executed. |
| 23 | |
| 24 | template <std::size_t N> |
| 25 | TEST_CONSTEXPR_CXX23 void test_char_pointer_ctor() |
| 26 | { |
| 27 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 28 | if (!TEST_IS_CONSTANT_EVALUATED) { |
| 29 | try { |
| 30 | std::bitset<N> v("xxx1010101010xxxx" ); |
| 31 | assert(false); |
| 32 | } |
| 33 | catch (std::invalid_argument&) {} |
| 34 | } |
| 35 | #endif |
| 36 | |
| 37 | static_assert(!std::is_convertible<const char*, std::bitset<N> >::value, "" ); |
| 38 | static_assert(std::is_constructible<std::bitset<N>, const char*>::value, "" ); |
| 39 | { |
| 40 | const char s[] = "1010101010" ; |
| 41 | std::bitset<N> v(s); |
| 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); |
| 47 | } |
| 48 | { |
| 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); |
| 56 | } |
| 57 | { |
| 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); |
| 65 | } |
| 66 | { |
| 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); |
| 74 | } |
| 75 | } |
| 76 | |
| 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>(); |
| 87 | |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | int main(int, char**) |
| 92 | { |
| 93 | test(); |
| 94 | #if TEST_STD_VER > 20 |
| 95 | static_assert(test()); |
| 96 | #endif |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |