Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Chandler Carruth | 7c3769d | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 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 |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // test bitset(string, pos, n, zero, one); |
| 10 | |
| 11 | #include <bitset> |
| 12 | #include <cassert> |
Marshall Clow | 2af7d42 | 2015-07-16 22:13:26 +0000 | [diff] [blame] | 13 | #include <algorithm> // for 'min' and 'max' |
| 14 | #include <stdexcept> // for 'invalid_argument' |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 15 | |
Eric Fiselier | e98bd2a | 2016-06-02 04:03:31 +0000 | [diff] [blame] | 16 | #include "test_macros.h" |
Howard Hinnant | 97ecd64 | 2011-05-17 19:12:55 +0000 | [diff] [blame] | 17 | |
Stephan T. Lavavej | 5dbf234 | 2017-05-05 23:51:39 +0000 | [diff] [blame] | 18 | #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 Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 22 | template <std::size_t N> |
| 23 | void test_string_ctor() |
| 24 | { |
Eric Fiselier | e98bd2a | 2016-06-02 04:03:31 +0000 | [diff] [blame] | 25 | #ifndef TEST_HAS_NO_EXCEPTIONS |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 26 | { |
Eric Fiselier | e98bd2a | 2016-06-02 04:03:31 +0000 | [diff] [blame] | 27 | 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 Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 57 | { |
| 58 | std::string str("xxx1010101010xxxx"); |
Eric Fiselier | e98bd2a | 2016-06-02 04:03:31 +0000 | [diff] [blame] | 59 | 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 Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 65 | } |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 66 | { |
| 67 | std::string str("xxxbababababaxxxx"); |
Eric Fiselier | e98bd2a | 2016-06-02 04:03:31 +0000 | [diff] [blame] | 68 | 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 Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
Eric Fiselier | b87e5f5 | 2019-07-01 19:59:34 +0000 | [diff] [blame] | 77 | struct Nonsense { |
| 78 | virtual ~Nonsense() {} |
| 79 | }; |
| 80 | |
| 81 | void 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 Bastien | e15dd4e | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 89 | int main(int, char**) |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 90 | { |
| 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 Fiselier | b87e5f5 | 2019-07-01 19:59:34 +0000 | [diff] [blame] | 100 | test_for_non_eager_instantiation(); |
JF Bastien | e15dd4e | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 101 | |
| 102 | return 0; |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 103 | } |