[PowerPC][NFC] Cleanup PPCCTRLoopsVerify pass
[llvm-project.git] / libcxx / test / std / utilities / template.bitset / bitset.cons / string_ctor.pass.cpp
blob2c657b561c8fc8784833a0b520a4c76f625e5845
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 //===----------------------------------------------------------------------===//
9 // test bitset(string, pos, n, zero, one);
11 #include <bitset>
12 #include <algorithm> // for 'min' and 'max'
13 #include <cassert>
14 #include <stdexcept> // for 'invalid_argument'
15 #include <string>
17 #include "test_macros.h"
19 template <std::size_t N>
20 void test_string_ctor() {
21 #ifndef TEST_HAS_NO_EXCEPTIONS
23 try {
24 std::string s("xxx1010101010xxxx");
25 std::bitset<N> v(s, s.size()+1, 10);
26 assert(false);
28 catch (std::out_of_range&)
33 try {
34 std::string s("xxx1010101010xxxx");
35 std::bitset<N> v(s, 2, 10);
36 assert(false);
38 catch (std::invalid_argument&)
43 try {
44 std::string s("xxxbababababaxxxx");
45 std::bitset<N> v(s, 2, 10, 'a', 'b');
46 assert(false);
48 catch (std::invalid_argument&)
52 #endif // TEST_HAS_NO_EXCEPTIONS
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);
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);
73 struct Nonsense {
74 virtual ~Nonsense() {}
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, "");
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();
97 return 0;