bootstrap: fix handling of various perl --version formats
[gnulib.git] / tests / test-count-one-bits.c
1 /*
2  * Copyright (C) 2007-2008 Free Software Foundation
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Ben Pfaff.  */
18
19 #include <config.h>
20
21 #include "count-one-bits.h"
22
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #define ASSERT(expr)                                            \
28   do                                                            \
29     {                                                           \
30       if (!(expr))                                              \
31         {                                                       \
32           fprintf (stderr, "%s:%d: assertion \"%s\" failed\n",  \
33                    __FILE__, __LINE__, #expr);                  \
34           fflush (stderr);                                      \
35           abort ();                                             \
36         }                                                       \
37     }                                                           \
38   while (0)
39
40 #define UINT_BIT (sizeof (unsigned int) * CHAR_BIT)
41 #define ULONG_BIT (sizeof (unsigned long int) * CHAR_BIT)
42 #define ULLONG_BIT (sizeof (unsigned long long int) * CHAR_BIT)
43
44 #ifndef ULLONG_MAX
45 # define HALF (1ULL << (sizeof (unsigned long long int) * CHAR_BIT - 1))
46 # define ULLONG_MAX (HALF - 1 + HALF)
47 #endif
48
49 int
50 main (int argc, char *argv[])
51 {
52   int i, j;
53
54 #define TEST_COUNT_ONE_BITS(FUNC, TYPE, BITS, MAX, ONE) \
55   ASSERT (FUNC (0) == 0);                               \
56   for (i = 0; i < BITS; i++)                            \
57     {                                                   \
58       ASSERT (FUNC (ONE << i) == 1);                    \
59       for (j = i + 1; j < BITS; j++)                    \
60         ASSERT (FUNC ((ONE << i) | (ONE << j)) == 2);   \
61     }                                                   \
62   for (i = 0; i < 1000; i++)                            \
63     {                                                   \
64       TYPE value = rand () ^ (rand () << 31 << 1);      \
65       int count = 0;                                    \
66       for (j = 0; j < BITS; j++)                        \
67         count += (value & (ONE << j)) != 0;             \
68       ASSERT (count == FUNC (value));                   \
69     }                                                   \
70   ASSERT (FUNC (MAX) == BITS);
71
72   TEST_COUNT_ONE_BITS (count_one_bits, unsigned int, UINT_BIT, UINT_MAX, 1U);
73   TEST_COUNT_ONE_BITS (count_one_bits_l, unsigned long int,
74                        ULONG_BIT, ULONG_MAX, 1UL);
75 #ifdef HAVE_UNSIGNED_LONG_LONG_INT
76   TEST_COUNT_ONE_BITS (count_one_bits_ll,
77                        unsigned long long int, ULLONG_BIT, ULLONG_MAX, 1ULL);
78 #endif
79
80   return 0;
81 }