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 // test bitset(string, pos, n, zero, one);
10
11 #include <bitset>
12 #include <algorithm> // for 'min' and 'max'
13 #include <cassert>
14 #include <stdexcept> // for 'invalid_argument'
15 #include <string>
16
17 #include "test_macros.h"
18
19 template <std::size_t N>
test_string_ctor()20 void test_string_ctor() {
21 #ifndef TEST_HAS_NO_EXCEPTIONS
22 {
23 try {
24 std::string s("xxx1010101010xxxx");
25 std::bitset<N> v(s, s.size()+1, 10);
26 assert(false);
27 }
28 catch (std::out_of_range&)
29 {
30 }
31 }
32 {
33 try {
34 std::string s("xxx1010101010xxxx");
35 std::bitset<N> v(s, 2, 10);
36 assert(false);
37 }
38 catch (std::invalid_argument&)
39 {
40 }
41 }
42 {
43 try {
44 std::string s("xxxbababababaxxxx");
45 std::bitset<N> v(s, 2, 10, 'a', 'b');
46 assert(false);
47 }
48 catch (std::invalid_argument&)
49 {
50 }
51 }
52 #endif // TEST_HAS_NO_EXCEPTIONS
53 {
54 std::string s("xxx1010101010xxxx");
55 std::bitset<N> v(s, 3, 10);
56 std::size_t M = std::min<std::size_t>(v.size(), 10);
57 for (std::size_t i = 0; i < M; ++i)
58 assert(v[i] == (s[3 + M - 1 - i] == '1'));
59 for (std::size_t i = 10; i < v.size(); ++i)
60 assert(v[i] == false);
61 }
62 {
63 std::string s("xxxbababababaxxxx");
64 std::bitset<N> v(s, 3, 10, 'a', 'b');
65 std::size_t M = std::min<std::size_t>(v.size(), 10);
66 for (std::size_t i = 0; i < M; ++i)
67 assert(v[i] == (s[3 + M - 1 - i] == 'b'));
68 for (std::size_t i = 10; i < v.size(); ++i)
69 assert(v[i] == false);
70 }
71 }
72
73 struct Nonsense {
~NonsenseNonsense74 virtual ~Nonsense() {}
75 };
76
test_for_non_eager_instantiation()77 void test_for_non_eager_instantiation() {
78 // Ensure we don't accidentally instantiate `std::basic_string<Nonsense>`
79 // since it may not be well formed and can cause an error in the
80 // non-immediate context.
81 static_assert(!std::is_constructible<std::bitset<3>, Nonsense*>::value, "");
82 static_assert(!std::is_constructible<std::bitset<3>, Nonsense*, size_t, Nonsense&, Nonsense&>::value, "");
83 }
84
main(int,char **)85 int main(int, char**) {
86 test_string_ctor<0>();
87 test_string_ctor<1>();
88 test_string_ctor<31>();
89 test_string_ctor<32>();
90 test_string_ctor<33>();
91 test_string_ctor<63>();
92 test_string_ctor<64>();
93 test_string_ctor<65>();
94 test_string_ctor<1000>();
95 test_for_non_eager_instantiation();
96
97 return 0;
98 }
99