| 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 | // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23 |
| 10 | |
| 11 | // template<class charT, class traits> |
| 12 | // explicit bitset( |
| 13 | // const basic_string_view<charT,traits>& str, |
| 14 | // typename basic_string_view<charT,traits>::size_type pos = 0, |
| 15 | // typename basic_string_view<charT,traits>::size_type n = basic_string_view<charT,traits>::npos, |
| 16 | // charT zero = charT('0'), charT one = charT('1')); |
| 17 | |
| 18 | #include <algorithm> // for 'min' and 'max' |
| 19 | #include <bitset> |
| 20 | #include <cassert> |
| 21 | #include <stdexcept> // for 'invalid_argument' |
| 22 | #include <string_view> |
| 23 | #include <type_traits> |
| 24 | |
| 25 | #include "test_macros.h" |
| 26 | |
| 27 | template <std::size_t N> |
| 28 | constexpr void test_string_ctor() { |
| 29 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 30 | if (!TEST_IS_CONSTANT_EVALUATED) { |
| 31 | try { |
| 32 | std::string_view s("xxx1010101010xxxx" ); |
| 33 | std::bitset<N> v(s, s.size() + 1); |
| 34 | assert(false); |
| 35 | } catch (std::out_of_range&) { |
| 36 | } |
| 37 | try { |
| 38 | std::string_view s("xxx1010101010xxxx" ); |
| 39 | std::bitset<N> v(s, s.size() + 1, 10); |
| 40 | assert(false); |
| 41 | } catch (std::out_of_range&) { |
| 42 | } |
| 43 | try { |
| 44 | std::string_view s("xxx1010101010xxxx" ); |
| 45 | std::bitset<N> v(s); |
| 46 | assert(false); |
| 47 | } catch (std::invalid_argument&) { |
| 48 | } |
| 49 | try { |
| 50 | std::string_view s("xxx1010101010xxxx" ); |
| 51 | std::bitset<N> v(s, 2); |
| 52 | assert(false); |
| 53 | } catch (std::invalid_argument&) { |
| 54 | } |
| 55 | try { |
| 56 | std::string_view s("xxx1010101010xxxx" ); |
| 57 | std::bitset<N> v(s, 2, 10); |
| 58 | assert(false); |
| 59 | } catch (std::invalid_argument&) { |
| 60 | } |
| 61 | try { |
| 62 | std::string_view s("xxxbababababaxxxx" ); |
| 63 | std::bitset<N> v(s, 2, 10, 'a', 'b'); |
| 64 | assert(false); |
| 65 | } catch (std::invalid_argument&) { |
| 66 | } |
| 67 | } |
| 68 | #endif // TEST_HAS_NO_EXCEPTIONS |
| 69 | |
| 70 | static_assert(!std::is_convertible_v<std::string_view, std::bitset<N>>); |
| 71 | static_assert(std::is_constructible_v<std::bitset<N>, std::string_view>); |
| 72 | { |
| 73 | std::string_view s("1010101010" ); |
| 74 | std::bitset<N> v(s); |
| 75 | std::size_t M = std::min<std::size_t>(v.size(), 10); |
| 76 | for (std::size_t i = 0; i < M; ++i) |
| 77 | assert(v[i] == (s[M - 1 - i] == '1')); |
| 78 | for (std::size_t i = 10; i < v.size(); ++i) |
| 79 | assert(v[i] == false); |
| 80 | } |
| 81 | { |
| 82 | std::string_view s("xxx1010101010" ); |
| 83 | std::bitset<N> v(s, 3); |
| 84 | std::size_t M = std::min<std::size_t>(v.size(), 10); |
| 85 | for (std::size_t i = 0; i < M; ++i) |
| 86 | assert(v[i] == (s[3 + M - 1 - i] == '1')); |
| 87 | for (std::size_t i = 10; i < v.size(); ++i) |
| 88 | assert(v[i] == false); |
| 89 | } |
| 90 | { |
| 91 | std::string_view s("xxx1010101010xxxx" ); |
| 92 | std::bitset<N> v(s, 3, 10); |
| 93 | std::size_t M = std::min<std::size_t>(v.size(), 10); |
| 94 | for (std::size_t i = 0; i < M; ++i) |
| 95 | assert(v[i] == (s[3 + M - 1 - i] == '1')); |
| 96 | for (std::size_t i = 10; i < v.size(); ++i) |
| 97 | assert(v[i] == false); |
| 98 | } |
| 99 | { |
| 100 | std::string_view s("xxx1a1a1a1a1axxxx" ); |
| 101 | std::bitset<N> v(s, 3, 10, 'a'); |
| 102 | std::size_t M = std::min<std::size_t>(v.size(), 10); |
| 103 | for (std::size_t i = 0; i < M; ++i) |
| 104 | assert(v[i] == (s[3 + M - 1 - i] == '1')); |
| 105 | for (std::size_t i = 10; i < v.size(); ++i) |
| 106 | assert(v[i] == false); |
| 107 | } |
| 108 | { |
| 109 | std::string_view s("xxxbababababaxxxx" ); |
| 110 | std::bitset<N> v(s, 3, 10, 'a', 'b'); |
| 111 | std::size_t M = std::min<std::size_t>(v.size(), 10); |
| 112 | for (std::size_t i = 0; i < M; ++i) |
| 113 | assert(v[i] == (s[3 + M - 1 - i] == 'b')); |
| 114 | for (std::size_t i = 10; i < v.size(); ++i) |
| 115 | assert(v[i] == false); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | struct Nonsense { |
| 120 | virtual ~Nonsense() {} |
| 121 | }; |
| 122 | |
| 123 | constexpr void test_for_non_eager_instantiation() { |
| 124 | // Ensure we don't accidentally instantiate `std::basic_string_view<Nonsense>` |
| 125 | // since it may not be well formed and can cause an error in the |
| 126 | // non-immediate context. |
| 127 | static_assert(!std::is_constructible<std::bitset<3>, Nonsense*>::value, "" ); |
| 128 | static_assert(!std::is_constructible<std::bitset<3>, Nonsense*, std::size_t, Nonsense&, Nonsense&>::value, "" ); |
| 129 | } |
| 130 | |
| 131 | constexpr bool test() { |
| 132 | test_string_ctor<0>(); |
| 133 | test_string_ctor<1>(); |
| 134 | test_string_ctor<31>(); |
| 135 | test_string_ctor<32>(); |
| 136 | test_string_ctor<33>(); |
| 137 | test_string_ctor<63>(); |
| 138 | test_string_ctor<64>(); |
| 139 | test_string_ctor<65>(); |
| 140 | test_string_ctor<1000>(); |
| 141 | test_for_non_eager_instantiation(); |
| 142 | |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | int main(int, char**) { |
| 147 | test(); |
| 148 | static_assert(test()); |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |