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