129 lines · cpp
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-exception6//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++2612// charT zero = charT('0'), charT one = charT('1')); // constexpr since C++2313 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 22TEST_MSVC_DIAGNOSTIC_IGNORED(6294) // Ill-defined for-loop: initial condition does not satisfy test. Loop body not executed.23 24template <std::size_t N>25TEST_CONSTEXPR_CXX23 void test_char_pointer_ctor()26{27#ifndef TEST_HAS_NO_EXCEPTIONS28 if (!TEST_IS_CONSTANT_EVALUATED) {29 try {30 std::bitset<N> v("xxx1010101010xxxx");31 assert(false);32 }33 catch (std::invalid_argument&) {}34 }35#endif36 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 // Verify that this constructor doesn't read over the given bound.76 // See https://llvm.org/PR14368477 {78 const char not_null_terminated[] = {'1', '0', '1', '0', '1', '0', '1', '0', '1', '0'};79 std::bitset<N> v(not_null_terminated, 10);80 std::size_t M = std::min<std::size_t>(v.size(), 10);81 for (std::size_t i = 0; i < M; ++i)82 assert(v[i] == (not_null_terminated[M - 1 - i] == '1'));83 for (std::size_t i = 10; i < v.size(); ++i)84 assert(!v[i]);85 }86 {87 const char not_null_terminated[] = {'1', 'a', '1', 'a', '1', 'a', '1', 'a', '1', 'a'};88 std::bitset<N> v(not_null_terminated, 10, 'a');89 std::size_t M = std::min<std::size_t>(v.size(), 10);90 for (std::size_t i = 0; i < M; ++i)91 assert(v[i] == (not_null_terminated[M - 1 - i] == '1'));92 for (std::size_t i = 10; i < v.size(); ++i)93 assert(!v[i]);94 }95 {96 const char not_null_terminated[] = {'b', 'a', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'a'};97 std::bitset<N> v(not_null_terminated, 10, 'a', 'b');98 std::size_t M = std::min<std::size_t>(v.size(), 10);99 for (std::size_t i = 0; i < M; ++i)100 assert(v[i] == (not_null_terminated[M - 1 - i] == 'b'));101 for (std::size_t i = 10; i < v.size(); ++i)102 assert(!v[i]);103 }104}105 106TEST_CONSTEXPR_CXX23 bool test() {107 test_char_pointer_ctor<0>();108 test_char_pointer_ctor<1>();109 test_char_pointer_ctor<31>();110 test_char_pointer_ctor<32>();111 test_char_pointer_ctor<33>();112 test_char_pointer_ctor<63>();113 test_char_pointer_ctor<64>();114 test_char_pointer_ctor<65>();115 test_char_pointer_ctor<1000>();116 117 return true;118}119 120int main(int, char**)121{122 test();123#if TEST_STD_VER > 20124 static_assert(test());125#endif126 127 return 0;128}129