Mercurial > gnulib
annotate tests/test-count-leading-zeros.c @ 40246:c34f677e6117 default tip master
_Noreturn: GCC 4.7 does not support [[noreturn]] in C++11 mode
* lib/_Noreturn.h, m4/gnulib-common.m4: Don't use [[noreturn]] before
GCC 4.8.
author | Akim Demaille <akim.demaille@gmail.com> |
---|---|
date | Sun, 17 Mar 2019 19:27:20 +0100 |
parents | 452ab00796c7 |
children |
rev | line source |
---|---|
17037 | 1 /* |
40057
b06060465f09
maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents:
39823
diff
changeset
|
2 * Copyright (C) 2012-2019 Free Software Foundation, Inc. |
17037 | 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 | |
19190 | 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
17037 | 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 int | |
33 main (int argc, char *argv[]) | |
34 { | |
35 int i, j; | |
36 | |
40214 | 37 #define TEST_COUNT_LEADING_ZEROS(FUNC, TYPE, BITS, MAX, ONE) \ |
38 ASSERT (FUNC (0) == BITS); \ | |
39 for (i = 0; i < BITS; i++) \ | |
40 { \ | |
41 ASSERT (FUNC (ONE << i) == BITS - i - 1); \ | |
42 for (j = 0; j < i; j++) \ | |
43 ASSERT (FUNC ((ONE << i) | (ONE << j)) == BITS - i - 1); \ | |
44 } \ | |
45 for (i = 0; i < 1000; i++) \ | |
46 { \ | |
47 /* RAND_MAX is most often 0x7fff or 0x7fffffff. */ \ | |
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 if (value & (ONE << j)) \ | |
62 count = BITS - j - 1; \ | |
63 ASSERT (count == FUNC (value)); \ | |
64 } \ | |
17037 | 65 ASSERT (FUNC (MAX) == 0); |
66 | |
67 TEST_COUNT_LEADING_ZEROS (count_leading_zeros, unsigned int, | |
68 UINT_BIT, UINT_MAX, 1U); | |
69 TEST_COUNT_LEADING_ZEROS (count_leading_zeros_l, unsigned long int, | |
70 ULONG_BIT, ULONG_MAX, 1UL); | |
71 #ifdef HAVE_UNSIGNED_LONG_LONG_INT | |
72 TEST_COUNT_LEADING_ZEROS (count_leading_zeros_ll, unsigned long long int, | |
73 ULLONG_BIT, ULLONG_MAX, 1ULL); | |
74 #endif | |
75 | |
76 return 0; | |
77 } |