index.pass.cpp revision b64f8b07c104c6cc986570ac8ee0ed16a9f23976
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>::reference operator[](size_t pos);
11
12#include <bitset>
13#include <cstdlib>
14#include <cassert>
15
16template <std::size_t N>
17std::bitset<N>
18make_bitset()
19{
20    std::bitset<N> v;
21    for (std::size_t i = 0; i < N; ++i)
22        v[i] = static_cast<bool>(std::rand() & 1);
23    return v;
24}
25
26template <std::size_t N>
27void test_index_const()
28{
29    std::bitset<N> v1 = make_bitset<N>();
30    if (N > 0)
31    {
32        assert(v1[N/2] == v1.test(N/2));
33        typename std::bitset<N>::reference r = v1[N/2];
34        assert(r == v1.test(N/2));
35        typename std::bitset<N>::reference r2 = v1[N/2];
36        r = r2;
37        assert(r == v1.test(N/2));
38        r = false;
39        assert(r == false);
40        assert(v1.test(N/2) == false);
41        r = true;
42        assert(r == true);
43        assert(v1.test(N/2) == true);
44        bool b = ~r;
45        assert(r == true);
46        assert(v1.test(N/2) == true);
47        assert(b == false);
48        r.flip();
49        assert(r == false);
50        assert(v1.test(N/2) == false);
51    }
52}
53
54int main()
55{
56    test_index_const<0>();
57    test_index_const<1>();
58    test_index_const<31>();
59    test_index_const<32>();
60    test_index_const<33>();
61    test_index_const<63>();
62    test_index_const<64>();
63    test_index_const<65>();
64    test_index_const<1000>();
65}
66