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 size_t count() const; 11 12 #include <bitset> 13 #include <cstdlib> 14 #include <cassert> 15 16 #pragma clang diagnostic ignored "-Wtautological-compare" 17 18 template <std::size_t N> 19 std::bitset<N> make_bitset()20make_bitset() 21 { 22 std::bitset<N> v; 23 for (std::size_t i = 0; i < N; ++i) 24 v[i] = static_cast<bool>(std::rand() & 1); 25 return v; 26 } 27 28 template <std::size_t N> test_count()29void test_count() 30 { 31 const std::bitset<N> v = make_bitset<N>(); 32 std::size_t c1 = v.count(); 33 std::size_t c2 = 0; 34 for (std::size_t i = 0; i < N; ++i) 35 if (v[i]) 36 ++c2; 37 assert(c1 == c2); 38 } 39 main()40int main() 41 { 42 test_count<0>(); 43 test_count<1>(); 44 test_count<31>(); 45 test_count<32>(); 46 test_count<33>(); 47 test_count<63>(); 48 test_count<64>(); 49 test_count<65>(); 50 test_count<1000>(); 51 } 52