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