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 <cassert> 13 #include <algorithm> // for 'min' and 'max' 14 #include <stdexcept> // for 'invalid_argument' 15 16 #include "test_macros.h" 17 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 22 template <std::size_t N> 23 void test_string_ctor() 24 { 25 #ifndef TEST_HAS_NO_EXCEPTIONS 26 { 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 57 { 58 std::string str("xxx1010101010xxxx"); 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); 65 } 66 { 67 std::string str("xxxbababababaxxxx"); 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); 74 } 75 } 76 77 int main(int, char**) 78 { 79 test_string_ctor<0>(); 80 test_string_ctor<1>(); 81 test_string_ctor<31>(); 82 test_string_ctor<32>(); 83 test_string_ctor<33>(); 84 test_string_ctor<63>(); 85 test_string_ctor<64>(); 86 test_string_ctor<65>(); 87 test_string_ctor<1000>(); 88 89 return 0; 90 } 91