blob: b08449ce8eb7aa129bc61194f225620c22bee358 [file] [log] [blame]
Howard Hinnantc52f43e2010-08-22 00:59:46 +00001//===----------------------------------------------------------------------===//
2//
Chandler Carruth7c3769d2019-01-19 10:56:40 +00003// 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
Howard Hinnantc52f43e2010-08-22 00:59:46 +00006//
7//===----------------------------------------------------------------------===//
8
9// test bitset(string, pos, n, zero, one);
10
11#include <bitset>
12#include <cassert>
Marshall Clow2af7d422015-07-16 22:13:26 +000013#include <algorithm> // for 'min' and 'max'
14#include <stdexcept> // for 'invalid_argument'
Howard Hinnantc52f43e2010-08-22 00:59:46 +000015
Eric Fiseliere98bd2a2016-06-02 04:03:31 +000016#include "test_macros.h"
Howard Hinnant97ecd642011-05-17 19:12:55 +000017
Stephan T. Lavavej5dbf2342017-05-05 23:51:39 +000018#if defined(TEST_COMPILER_C1XX)
19#pragma warning(disable: 6294) // Ill-defined for-loop: initial condition does not satisfy test. Loop body not executed.
20#endif
21
Howard Hinnantc52f43e2010-08-22 00:59:46 +000022template <std::size_t N>
23void test_string_ctor()
24{
Eric Fiseliere98bd2a2016-06-02 04:03:31 +000025#ifndef TEST_HAS_NO_EXCEPTIONS
Howard Hinnantc52f43e2010-08-22 00:59:46 +000026 {
Eric Fiseliere98bd2a2016-06-02 04:03:31 +000027 try {
28 std::string str("xxx1010101010xxxx");
29 std::bitset<N> v(str, str.size()+1, 10);
30 assert(false);
31 }
32 catch (std::out_of_range&)
33 {
34 }
35 }
36 {
37 try {
38 std::string str("xxx1010101010xxxx");
39 std::bitset<N> v(str, 2, 10);
40 assert(false);
41 }
42 catch (std::invalid_argument&)
43 {
44 }
45 }
46 {
47 try {
48 std::string str("xxxbababababaxxxx");
49 std::bitset<N> v(str, 2, 10, 'a', 'b');
50 assert(false);
51 }
52 catch (std::invalid_argument&)
53 {
54 }
55 }
56#endif // TEST_HAS_NO_EXCEPTIONS
Howard Hinnantc52f43e2010-08-22 00:59:46 +000057 {
58 std::string str("xxx1010101010xxxx");
Eric Fiseliere98bd2a2016-06-02 04:03:31 +000059 std::bitset<N> v(str, 3, 10);
60 std::size_t M = std::min<std::size_t>(N, 10);
61 for (std::size_t i = 0; i < M; ++i)
62 assert(v[i] == (str[3 + M - 1 - i] == '1'));
63 for (std::size_t i = 10; i < N; ++i)
64 assert(v[i] == false);
Howard Hinnantc52f43e2010-08-22 00:59:46 +000065 }
Howard Hinnantc52f43e2010-08-22 00:59:46 +000066 {
67 std::string str("xxxbababababaxxxx");
Eric Fiseliere98bd2a2016-06-02 04:03:31 +000068 std::bitset<N> v(str, 3, 10, 'a', 'b');
69 std::size_t M = std::min<std::size_t>(N, 10);
70 for (std::size_t i = 0; i < M; ++i)
71 assert(v[i] == (str[3 + M - 1 - i] == 'b'));
72 for (std::size_t i = 10; i < N; ++i)
73 assert(v[i] == false);
Howard Hinnantc52f43e2010-08-22 00:59:46 +000074 }
75}
76
Eric Fiselierb87e5f52019-07-01 19:59:34 +000077struct Nonsense {
78 virtual ~Nonsense() {}
79};
80
81void test_for_non_eager_instantiation() {
82 // Ensure we don't accidentally instantiate `std::basic_string<Nonsense>`
83 // since it may not be well formed and can cause an error in the
84 // non-immediate context.
85 static_assert(!std::is_constructible<std::bitset<3>, Nonsense*>::value, "");
86 static_assert(!std::is_constructible<std::bitset<3>, Nonsense*, size_t, Nonsense&, Nonsense&>::value, "");
87}
88
JF Bastiene15dd4e2019-02-04 20:31:13 +000089int main(int, char**)
Howard Hinnantc52f43e2010-08-22 00:59:46 +000090{
91 test_string_ctor<0>();
92 test_string_ctor<1>();
93 test_string_ctor<31>();
94 test_string_ctor<32>();
95 test_string_ctor<33>();
96 test_string_ctor<63>();
97 test_string_ctor<64>();
98 test_string_ctor<65>();
99 test_string_ctor<1000>();
Eric Fiselierb87e5f52019-07-01 19:59:34 +0000100 test_for_non_eager_instantiation();
JF Bastiene15dd4e2019-02-04 20:31:13 +0000101
102 return 0;
Howard Hinnantc52f43e2010-08-22 00:59:46 +0000103}