annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9122
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
1 /*
40057
b06060465f09 maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents: 39822
diff changeset
2 * Copyright (C) 2007-2019 Free Software Foundation, Inc.
9122
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
3 *
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 9122
diff changeset
4 * This program is free software: you can redistribute it and/or modify
9122
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 9122
diff changeset
6 * the Free Software Foundation; either version 3 of the License, or
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 9122
diff changeset
7 * (at your option) any later version.
9122
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
8 *
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
9 * This program is distributed in the hope that it will be useful,
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
12 * GNU General Public License for more details.
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
13 *
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
14 * You should have received a copy of the GNU General Public License
19190
9759915b2aca all: prefer https: URLs
Paul Eggert <eggert@cs.ucla.edu>
parents: 18626
diff changeset
15 * along with this program. If not, see <https://www.gnu.org/licenses/>. */
9122
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
16
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
17 /* Written by Ben Pfaff. */
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
18
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
19 #include <config.h>
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
20
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
21 #include "count-one-bits.h"
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
22
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
23 #include <limits.h>
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
24 #include <stdio.h>
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
25
12496
a48d3d749ca5 Refactor common macros used in tests.
Bruno Haible <bruno@clisp.org>
parents: 9889
diff changeset
26 #include "macros.h"
9122
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
27
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
28 #define UINT_BIT (sizeof (unsigned int) * CHAR_BIT)
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
29 #define ULONG_BIT (sizeof (unsigned long int) * CHAR_BIT)
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
30 #define ULLONG_BIT (sizeof (unsigned long long int) * CHAR_BIT)
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
31
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
32 int
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
33 main (int argc, char *argv[])
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
34 {
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
35 int i, j;
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
36
40214
452ab00796c7 Fix undefined behaviour.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
37 #define TEST_COUNT_ONE_BITS(FUNC, TYPE, BITS, MAX, ONE) \
452ab00796c7 Fix undefined behaviour.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
38 ASSERT (FUNC (0) == 0); \
452ab00796c7 Fix undefined behaviour.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
39 for (i = 0; i < BITS; i++) \
452ab00796c7 Fix undefined behaviour.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
40 { \
452ab00796c7 Fix undefined behaviour.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
41 ASSERT (FUNC (ONE << i) == 1); \
452ab00796c7 Fix undefined behaviour.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
42 for (j = i + 1; j < BITS; j++) \
452ab00796c7 Fix undefined behaviour.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
43 ASSERT (FUNC ((ONE << i) | (ONE << j)) == 2); \
452ab00796c7 Fix undefined behaviour.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
44 } \
452ab00796c7 Fix undefined behaviour.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
45 for (i = 0; i < 1000; i++) \
452ab00796c7 Fix undefined behaviour.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
46 { \
452ab00796c7 Fix undefined behaviour.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
47 /* RAND_MAX is most often 0x7fff or 0x7fffffff. */ \
452ab00796c7 Fix undefined behaviour.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
48 TYPE value = \
452ab00796c7 Fix undefined behaviour.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
49 (RAND_MAX <= 0xffff \
452ab00796c7 Fix undefined behaviour.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
50 ? ((TYPE) rand () >> 3) \
452ab00796c7 Fix undefined behaviour.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
51 ^ (((TYPE) rand () >> 3) << 12) \
452ab00796c7 Fix undefined behaviour.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
52 ^ (((TYPE) rand () >> 3) << 24) \
452ab00796c7 Fix undefined behaviour.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
53 ^ (((TYPE) rand () >> 3) << 30 << 6) \
452ab00796c7 Fix undefined behaviour.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
54 ^ (((TYPE) rand () >> 3) << 30 << 18) \
452ab00796c7 Fix undefined behaviour.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
55 ^ (((TYPE) rand () >> 3) << 30 << 30) \
452ab00796c7 Fix undefined behaviour.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
56 : ((TYPE) rand () >> 3) \
452ab00796c7 Fix undefined behaviour.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
57 ^ (((TYPE) rand () >> 3) << 22) \
452ab00796c7 Fix undefined behaviour.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
58 ^ (((TYPE) rand () >> 3) << 22 << 22)); \
452ab00796c7 Fix undefined behaviour.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
59 int count = 0; \
452ab00796c7 Fix undefined behaviour.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
60 for (j = 0; j < BITS; j++) \
452ab00796c7 Fix undefined behaviour.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
61 count += (value & (ONE << j)) != 0; \
452ab00796c7 Fix undefined behaviour.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
62 ASSERT (count == FUNC (value)); \
452ab00796c7 Fix undefined behaviour.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
63 } \
9122
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
64 ASSERT (FUNC (MAX) == BITS);
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
65
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
66 TEST_COUNT_ONE_BITS (count_one_bits, unsigned int, UINT_BIT, UINT_MAX, 1U);
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
67 TEST_COUNT_ONE_BITS (count_one_bits_l, unsigned long int,
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
68 ULONG_BIT, ULONG_MAX, 1UL);
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
69 #ifdef HAVE_UNSIGNED_LONG_LONG_INT
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
70 TEST_COUNT_ONE_BITS (count_one_bits_ll,
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
71 unsigned long long int, ULLONG_BIT, ULLONG_MAX, 1ULL);
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
72 #endif
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
73
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
74 return 0;
f547a45baada Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff changeset
75 }