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