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 16 #if defined(__clang__) 17 #pragma clang diagnostic ignored "-Wtautological-compare" 18 #endif 19 20 template <std::size_t N> 21 std::bitset<N> 22 make_bitset() 23 { 24 std::bitset<N> v; 25 for (std::size_t i = 0; i < N; ++i) 26 v[i] = static_cast<bool>(std::rand() & 1); 27 return v; 28 } 29 30 template <std::size_t N> 31 void test_index_const() 32 { 33 std::bitset<N> v1 = make_bitset<N>(); 34 if (N > 0) 35 { 36 assert(v1[N/2] == v1.test(N/2)); 37 typename std::bitset<N>::reference r = v1[N/2]; 38 assert(r == v1.test(N/2)); 39 typename std::bitset<N>::reference r2 = v1[N/2]; 40 r = r2; 41 assert(r == v1.test(N/2)); 42 r = false; 43 assert(r == false); 44 assert(v1.test(N/2) == false); 45 r = true; 46 assert(r == true); 47 assert(v1.test(N/2) == true); 48 bool b = ~r; 49 assert(r == true); 50 assert(v1.test(N/2) == true); 51 assert(b == false); 52 r.flip(); 53 assert(r == false); 54 assert(v1.test(N/2) == false); 55 } 56 } 57 58 int main() 59 { 60 test_index_const<0>(); 61 test_index_const<1>(); 62 test_index_const<31>(); 63 test_index_const<32>(); 64 test_index_const<33>(); 65 test_index_const<63>(); 66 test_index_const<64>(); 67 test_index_const<65>(); 68 test_index_const<1000>(); 69 } 70