]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/utilities/template.bitset/bitset.members/flip_one.pass.cpp
Vendor import of libc++ trunk r302418:
[FreeBSD/FreeBSD.git] / test / std / utilities / template.bitset / bitset.members / flip_one.pass.cpp
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<N>& flip(size_t pos);
11
12 #include <bitset>
13 #include <cstdlib>
14 #include <cassert>
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 std::bitset<N>
24 make_bitset()
25 {
26     std::bitset<N> v;
27     for (std::size_t i = 0; i < N; ++i)
28         v[i] = static_cast<bool>(std::rand() & 1);
29     return v;
30 }
31
32 template <std::size_t N>
33 void test_flip_one(bool test_throws)
34 {
35     std::bitset<N> v = make_bitset<N>();
36 #ifdef TEST_HAS_NO_EXCEPTIONS
37     if (test_throws) return;
38 #else
39     try
40     {
41 #endif
42         v.flip(50);
43         bool b = v[50];
44         if (50 >= v.size())
45             assert(false);
46         assert(v[50] == b);
47         v.flip(50);
48         assert(v[50] != b);
49         v.flip(50);
50         assert(v[50] == b);
51         assert(!test_throws);
52 #ifndef TEST_HAS_NO_EXCEPTIONS
53     }
54     catch (std::out_of_range&)
55     {
56         assert(test_throws);
57     }
58 #endif
59 }
60
61 int main()
62 {
63     test_flip_one<0>(true);
64     test_flip_one<1>(true);
65     test_flip_one<31>(true);
66     test_flip_one<32>(true);
67     test_flip_one<33>(true);
68     test_flip_one<63>(false);
69     test_flip_one<64>(false);
70     test_flip_one<65>(false);
71     test_flip_one<1000>(false);
72 }