maint: update copyright
[gnulib.git] / tests / test-count-leading-zeros.c
1 /*
2  * Copyright (C) 2012-2014 Free Software Foundation, Inc.
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 Eric Blake.  */
18
19 #include <config.h>
20
21 #include "count-leading-zeros.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 #ifndef ULLONG_MAX
33 # define HALF (1ULL << (sizeof (unsigned long long int) * CHAR_BIT - 1))
34 # define ULLONG_MAX (HALF - 1 + HALF)
35 #endif
36
37 int
38 main (int argc, char *argv[])
39 {
40   int i, j;
41
42 #define TEST_COUNT_LEADING_ZEROS(FUNC, TYPE, BITS, MAX, ONE)    \
43   ASSERT (FUNC (0) == BITS);                                    \
44   for (i = 0; i < BITS; i++)                                    \
45     {                                                           \
46       ASSERT (FUNC (ONE << i) == BITS - i - 1);                 \
47       for (j = 0; j < i; j++)                                   \
48         ASSERT (FUNC ((ONE << i) | (ONE << j)) == BITS - i - 1);\
49     }                                                           \
50   for (i = 0; i < 1000; i++)                                    \
51     {                                                           \
52       TYPE value = rand () ^ (rand () << 31 << 1);              \
53       int count = 0;                                            \
54       for (j = 0; j < BITS; j++)                                \
55         if (value & (ONE << j))                                 \
56           count = BITS - j - 1;                                 \
57       ASSERT (count == FUNC (value));                           \
58     }                                                           \
59   ASSERT (FUNC (MAX) == 0);
60
61   TEST_COUNT_LEADING_ZEROS (count_leading_zeros, unsigned int,
62                             UINT_BIT, UINT_MAX, 1U);
63   TEST_COUNT_LEADING_ZEROS (count_leading_zeros_l, unsigned long int,
64                             ULONG_BIT, ULONG_MAX, 1UL);
65 #ifdef HAVE_UNSIGNED_LONG_LONG_INT
66   TEST_COUNT_LEADING_ZEROS (count_leading_zeros_ll, unsigned long long int,
67                             ULLONG_BIT, ULLONG_MAX, 1ULL);
68 #endif
69
70   return 0;
71 }