Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // test bitset(string, pos, n, zero, one); |
| 11 | |
| 12 | #include <bitset> |
| 13 | #include <cassert> |
Marshall Clow | 2af7d42 | 2015-07-16 22:13:26 +0000 | [diff] [blame] | 14 | #include <algorithm> // for 'min' and 'max' |
| 15 | #include <stdexcept> // for 'invalid_argument' |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 16 | |
Howard Hinnant | 97ecd64 | 2011-05-17 19:12:55 +0000 | [diff] [blame] | 17 | #pragma clang diagnostic ignored "-Wtautological-compare" |
| 18 | |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 19 | template <std::size_t N> |
| 20 | void test_string_ctor() |
| 21 | { |
| 22 | { |
| 23 | try |
| 24 | { |
| 25 | std::string str("xxx1010101010xxxx"); |
| 26 | std::bitset<N> v(str, str.size()+1, 10); |
| 27 | assert(false); |
| 28 | } |
| 29 | catch (std::out_of_range&) |
| 30 | { |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | { |
| 35 | try |
| 36 | { |
| 37 | std::string str("xxx1010101010xxxx"); |
| 38 | std::bitset<N> v(str, 2, 10); |
| 39 | assert(false); |
| 40 | } |
| 41 | catch (std::invalid_argument&) |
| 42 | { |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | { |
| 47 | std::string str("xxx1010101010xxxx"); |
| 48 | std::bitset<N> v(str, 3, 10); |
| 49 | std::size_t M = std::min<std::size_t>(N, 10); |
| 50 | for (std::size_t i = 0; i < M; ++i) |
| 51 | assert(v[i] == (str[3 + M - 1 - i] == '1')); |
| 52 | for (std::size_t i = 10; i < N; ++i) |
| 53 | assert(v[i] == false); |
| 54 | } |
| 55 | |
| 56 | { |
| 57 | try |
| 58 | { |
| 59 | std::string str("xxxbababababaxxxx"); |
| 60 | std::bitset<N> v(str, 2, 10, 'a', 'b'); |
| 61 | assert(false); |
| 62 | } |
| 63 | catch (std::invalid_argument&) |
| 64 | { |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | { |
| 69 | std::string str("xxxbababababaxxxx"); |
| 70 | std::bitset<N> v(str, 3, 10, 'a', 'b'); |
| 71 | std::size_t M = std::min<std::size_t>(N, 10); |
| 72 | for (std::size_t i = 0; i < M; ++i) |
| 73 | assert(v[i] == (str[3 + M - 1 - i] == 'b')); |
| 74 | for (std::size_t i = 10; i < N; ++i) |
| 75 | assert(v[i] == false); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | int main() |
| 80 | { |
| 81 | test_string_ctor<0>(); |
| 82 | test_string_ctor<1>(); |
| 83 | test_string_ctor<31>(); |
| 84 | test_string_ctor<32>(); |
| 85 | test_string_ctor<33>(); |
| 86 | test_string_ctor<63>(); |
| 87 | test_string_ctor<64>(); |
| 88 | test_string_ctor<65>(); |
| 89 | test_string_ctor<1000>(); |
| 90 | } |