• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // test bitset(string, pos, n, zero, one);
11 
12 #include <bitset>
13 #include <cassert>
14 #include <algorithm> // for 'min' and 'max'
15 #include <stdexcept> // for 'invalid_argument'
16 
17 #include "test_macros.h"
18 
19 #if defined(TEST_COMPILER_C1XX)
20 #pragma warning(disable: 6294) // Ill-defined for-loop:  initial condition does not satisfy test.  Loop body not executed.
21 #endif
22 
23 template <std::size_t N>
test_string_ctor()24 void test_string_ctor()
25 {
26 #ifndef TEST_HAS_NO_EXCEPTIONS
27     {
28         try {
29             std::string str("xxx1010101010xxxx");
30             std::bitset<N> v(str, str.size()+1, 10);
31             assert(false);
32         }
33         catch (std::out_of_range&)
34         {
35         }
36     }
37     {
38         try {
39             std::string str("xxx1010101010xxxx");
40             std::bitset<N> v(str, 2, 10);
41             assert(false);
42         }
43         catch (std::invalid_argument&)
44         {
45         }
46     }
47     {
48         try {
49             std::string str("xxxbababababaxxxx");
50             std::bitset<N> v(str, 2, 10, 'a', 'b');
51             assert(false);
52         }
53         catch (std::invalid_argument&)
54         {
55         }
56     }
57 #endif // TEST_HAS_NO_EXCEPTIONS
58     {
59         std::string str("xxx1010101010xxxx");
60         std::bitset<N> v(str, 3, 10);
61         std::size_t M = std::min<std::size_t>(N, 10);
62         for (std::size_t i = 0; i < M; ++i)
63             assert(v[i] == (str[3 + M - 1 - i] == '1'));
64         for (std::size_t i = 10; i < N; ++i)
65             assert(v[i] == false);
66     }
67     {
68         std::string str("xxxbababababaxxxx");
69         std::bitset<N> v(str, 3, 10, 'a', 'b');
70         std::size_t M = std::min<std::size_t>(N, 10);
71         for (std::size_t i = 0; i < M; ++i)
72             assert(v[i] == (str[3 + M - 1 - i] == 'b'));
73         for (std::size_t i = 10; i < N; ++i)
74             assert(v[i] == false);
75     }
76 }
77 
main()78 int main()
79 {
80     test_string_ctor<0>();
81     test_string_ctor<1>();
82     test_string_ctor<31>();
83     test_string_ctor<32>();
84     test_string_ctor<33>();
85     test_string_ctor<63>();
86     test_string_ctor<64>();
87     test_string_ctor<65>();
88     test_string_ctor<1000>();
89 }
90