1 /* Test counting of trailing zeros.
2 Copyright (C) 2013-2020 Free Software Foundation, Inc.
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.
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.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Paul Eggert. */
21 #include "count-trailing-zeros.h"
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)
33 main (int argc
, char *argv
[])
37 #define TEST_COUNT_TRAILING_ZEROS(FUNC, TYPE, BITS, MAX, ONE) \
38 ASSERT (FUNC (0) == BITS); \
39 for (i = 0; i < BITS; i++) \
41 ASSERT (FUNC (ONE << i) == i); \
42 for (j = 0; j < i; j++) \
43 ASSERT (FUNC ((ONE << i) | (ONE << j)) == j); \
45 for (i = 0; i < 1000; i++) \
47 /* RAND_MAX is most often 0x7fff or 0x7fffffff. */ \
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)); \
60 for (j = BITS - 1; 0 <= j; j--) \
61 if (value & (ONE << j)) \
63 ASSERT (count == FUNC (value)); \
65 ASSERT (FUNC (MAX) == 0);
67 TEST_COUNT_TRAILING_ZEROS (count_trailing_zeros
, unsigned int,
68 UINT_BIT
, UINT_MAX
, 1U);
69 TEST_COUNT_TRAILING_ZEROS (count_trailing_zeros_l
, unsigned long int,
70 ULONG_BIT
, ULONG_MAX
, 1UL);
71 TEST_COUNT_TRAILING_ZEROS (count_trailing_zeros_ll
, unsigned long long int,
72 ULLONG_BIT
, ULLONG_MAX
, 1ULL);