xref: /minix3/external/bsd/libc++/dist/libcxx/test/utilities/template.bitset/bitset.members/count.pass.cpp (revision 4684ddb6aab0b36791c8099bc705d6140b3d05d0)
1*4684ddb6SLionel Sambuc //===----------------------------------------------------------------------===//
2*4684ddb6SLionel Sambuc //
3*4684ddb6SLionel Sambuc //                     The LLVM Compiler Infrastructure
4*4684ddb6SLionel Sambuc //
5*4684ddb6SLionel Sambuc // This file is dual licensed under the MIT and the University of Illinois Open
6*4684ddb6SLionel Sambuc // Source Licenses. See LICENSE.TXT for details.
7*4684ddb6SLionel Sambuc //
8*4684ddb6SLionel Sambuc //===----------------------------------------------------------------------===//
9*4684ddb6SLionel Sambuc 
10*4684ddb6SLionel Sambuc // test size_t count() const;
11*4684ddb6SLionel Sambuc 
12*4684ddb6SLionel Sambuc #include <bitset>
13*4684ddb6SLionel Sambuc #include <cstdlib>
14*4684ddb6SLionel Sambuc #include <cassert>
15*4684ddb6SLionel Sambuc 
16*4684ddb6SLionel Sambuc #pragma clang diagnostic ignored "-Wtautological-compare"
17*4684ddb6SLionel Sambuc 
18*4684ddb6SLionel Sambuc template <std::size_t N>
19*4684ddb6SLionel Sambuc std::bitset<N>
make_bitset()20*4684ddb6SLionel Sambuc make_bitset()
21*4684ddb6SLionel Sambuc {
22*4684ddb6SLionel Sambuc     std::bitset<N> v;
23*4684ddb6SLionel Sambuc     for (std::size_t i = 0; i < N; ++i)
24*4684ddb6SLionel Sambuc         v[i] = static_cast<bool>(std::rand() & 1);
25*4684ddb6SLionel Sambuc     return v;
26*4684ddb6SLionel Sambuc }
27*4684ddb6SLionel Sambuc 
28*4684ddb6SLionel Sambuc template <std::size_t N>
test_count()29*4684ddb6SLionel Sambuc void test_count()
30*4684ddb6SLionel Sambuc {
31*4684ddb6SLionel Sambuc     const std::bitset<N> v = make_bitset<N>();
32*4684ddb6SLionel Sambuc     std::size_t c1 = v.count();
33*4684ddb6SLionel Sambuc     std::size_t c2 = 0;
34*4684ddb6SLionel Sambuc     for (std::size_t i = 0; i < N; ++i)
35*4684ddb6SLionel Sambuc         if (v[i])
36*4684ddb6SLionel Sambuc             ++c2;
37*4684ddb6SLionel Sambuc     assert(c1 == c2);
38*4684ddb6SLionel Sambuc }
39*4684ddb6SLionel Sambuc 
main()40*4684ddb6SLionel Sambuc int main()
41*4684ddb6SLionel Sambuc {
42*4684ddb6SLionel Sambuc     test_count<0>();
43*4684ddb6SLionel Sambuc     test_count<1>();
44*4684ddb6SLionel Sambuc     test_count<31>();
45*4684ddb6SLionel Sambuc     test_count<32>();
46*4684ddb6SLionel Sambuc     test_count<33>();
47*4684ddb6SLionel Sambuc     test_count<63>();
48*4684ddb6SLionel Sambuc     test_count<64>();
49*4684ddb6SLionel Sambuc     test_count<65>();
50*4684ddb6SLionel Sambuc     test_count<1000>();
51*4684ddb6SLionel Sambuc }
52