comparison tests/test-count-one-bits.c @ 40214:452ab00796c7

Fix undefined behaviour. * lib/bitrotate.h (rotl16, rotr16, rotl8, rotr8): Case x to 'unsigned int', to avoid shift operations on 'int'. * lib/xmemdup0.c (xmemdup0): Don't invoke memcpy with a zero size. * tests/test-count-leading-zeros.c (main): Use a random number that has as many bits as TYPE, not only 2*15 or 2*31 bits. * tests/test-count-trailing-zeros.c (main): Likewise. * tests/test-count-one-bits.c (main): Likewise. * tests/test-memmem.c: Don't include "null-ptr.h". (main): Use zerosize_ptr() instead of null_ptr(). * modules/memmem-tests (Files): Remove tests/null-ptr.h.
author Bruno Haible <bruno@clisp.org>
date Sat, 09 Mar 2019 20:32:25 +0100
parents b06060465f09
children
comparison
equal deleted inserted replaced
40213:cc3fed3b7788 40214:452ab00796c7
32 int 32 int
33 main (int argc, char *argv[]) 33 main (int argc, char *argv[])
34 { 34 {
35 int i, j; 35 int i, j;
36 36
37 #define TEST_COUNT_ONE_BITS(FUNC, TYPE, BITS, MAX, ONE) \ 37 #define TEST_COUNT_ONE_BITS(FUNC, TYPE, BITS, MAX, ONE) \
38 ASSERT (FUNC (0) == 0); \ 38 ASSERT (FUNC (0) == 0); \
39 for (i = 0; i < BITS; i++) \ 39 for (i = 0; i < BITS; i++) \
40 { \ 40 { \
41 ASSERT (FUNC (ONE << i) == 1); \ 41 ASSERT (FUNC (ONE << i) == 1); \
42 for (j = i + 1; j < BITS; j++) \ 42 for (j = i + 1; j < BITS; j++) \
43 ASSERT (FUNC ((ONE << i) | (ONE << j)) == 2); \ 43 ASSERT (FUNC ((ONE << i) | (ONE << j)) == 2); \
44 } \ 44 } \
45 for (i = 0; i < 1000; i++) \ 45 for (i = 0; i < 1000; i++) \
46 { \ 46 { \
47 TYPE value = rand () ^ (rand () << 31 << 1); \ 47 /* RAND_MAX is most often 0x7fff or 0x7fffffff. */ \
48 int count = 0; \ 48 TYPE value = \
49 for (j = 0; j < BITS; j++) \ 49 (RAND_MAX <= 0xffff \
50 count += (value & (ONE << j)) != 0; \ 50 ? ((TYPE) rand () >> 3) \
51 ASSERT (count == FUNC (value)); \ 51 ^ (((TYPE) rand () >> 3) << 12) \
52 } \ 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 } \
53 ASSERT (FUNC (MAX) == BITS); 64 ASSERT (FUNC (MAX) == BITS);
54 65
55 TEST_COUNT_ONE_BITS (count_one_bits, unsigned int, UINT_BIT, UINT_MAX, 1U); 66 TEST_COUNT_ONE_BITS (count_one_bits, unsigned int, UINT_BIT, UINT_MAX, 1U);
56 TEST_COUNT_ONE_BITS (count_one_bits_l, unsigned long int, 67 TEST_COUNT_ONE_BITS (count_one_bits_l, unsigned long int,
57 ULONG_BIT, ULONG_MAX, 1UL); 68 ULONG_BIT, ULONG_MAX, 1UL);