This source file includes following definitions.
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #include <config.h>
20
21 #include "count-one-bits.h"
22
23 #include <limits.h>
24 #include <stdio.h>
25
26 #include "macros.h"
27
28 #define UINT_BIT (sizeof (unsigned int) * CHAR_BIT)
29 #define ULONG_BIT (sizeof (unsigned long int) * CHAR_BIT)
30 #define ULLONG_BIT (sizeof (unsigned long long int) * CHAR_BIT)
31
32 int
33 main (int argc, char *argv[])
34 {
35 int i, j;
36
37 #define TEST_COUNT_ONE_BITS(FUNC, TYPE, BITS, MAX, ONE) \
38 ASSERT (FUNC (0) == 0); \
39 for (i = 0; i < BITS; i++) \
40 { \
41 ASSERT (FUNC (ONE << i) == 1); \
42 for (j = i + 1; j < BITS; j++) \
43 ASSERT (FUNC ((ONE << i) | (ONE << j)) == 2); \
44 } \
45 for (i = 0; i < 1000; i++) \
46 { \
47 \
48 TYPE value = \
49 (RAND_MAX <= 0xffff \
50 ? ((TYPE) rand () >> 3) \
51 ^ (((TYPE) rand () >> 3) << 12) \
52 ^ (((TYPE) rand () >> 3) << 24) \
53 ^ (((TYPE) rand () >> 3) << 30 << 6) \
54 ^ (((TYPE) rand () >> 3) << 30 << 18) \
55 ^ (((TYPE) rand () >> 3) << 30 << 30) \
56 : ((TYPE) rand () >> 3) \
57 ^ (((TYPE) rand () >> 3) << 22) \
58 ^ (((TYPE) rand () >> 3) << 22 << 22)); \
59 int count = 0; \
60 for (j = 0; j < BITS; j++) \
61 count += (value & (ONE << j)) != 0; \
62 ASSERT (count == FUNC (value)); \
63 } \
64 ASSERT (FUNC (MAX) == BITS);
65
66 TEST_COUNT_ONE_BITS (count_one_bits, unsigned int, UINT_BIT, UINT_MAX, 1U);
67 TEST_COUNT_ONE_BITS (count_one_bits_l, unsigned long int,
68 ULONG_BIT, ULONG_MAX, 1UL);
69 TEST_COUNT_ONE_BITS (count_one_bits_ll, unsigned long long int,
70 ULLONG_BIT, ULLONG_MAX, 1ULL);
71
72 return 0;
73 }