count.pass.cpp revision f5256e16dfc425c1d466f6308d4026d529ce9e0b
1//===----------------------------------------------------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. 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
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_count()
28{
29    const std::bitset<N> v = make_bitset<N>();
30    std::size_t c1 = v.count();
31    std::size_t c2 = 0;
32    for (std::size_t i = 0; i < N; ++i)
33        if (v[i])
34            ++c2;
35    assert(c1 == c2);
36}
37
38
39int main()
40{
41    test_count<0>();
42    test_count<1>();
43    test_count<31>();
44    test_count<32>();
45    test_count<33>();
46    test_count<63>();
47    test_count<64>();
48    test_count<65>();
49    test_count<1000>();
50}
51