Mercurial > hg > octave-jordi > gnulib-hg
annotate tests/test-count-one-bits.c @ 18079:4c948fd76734 default tip master
autoupdate
author | Karl Berry <karl@freefriends.org> |
---|---|
date | Mon, 24 Aug 2015 06:09:19 -0700 |
parents | ab58d4870664 |
children |
rev | line source |
---|---|
9122
f547a45baada
Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff
changeset
|
1 /* |
17848 | 2 * Copyright (C) 2007-2015 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 |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
9122
diff
changeset
|
15 * along with this program. If not, see <http://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 #ifndef ULLONG_MAX |
f547a45baada
Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff
changeset
|
33 # define HALF (1ULL << (sizeof (unsigned long long int) * CHAR_BIT - 1)) |
f547a45baada
Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff
changeset
|
34 # define ULLONG_MAX (HALF - 1 + HALF) |
f547a45baada
Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff
changeset
|
35 #endif |
f547a45baada
Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff
changeset
|
36 |
f547a45baada
Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff
changeset
|
37 int |
f547a45baada
Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff
changeset
|
38 main (int argc, char *argv[]) |
f547a45baada
Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff
changeset
|
39 { |
f547a45baada
Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff
changeset
|
40 int i, j; |
f547a45baada
Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff
changeset
|
41 |
f547a45baada
Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff
changeset
|
42 #define TEST_COUNT_ONE_BITS(FUNC, TYPE, BITS, MAX, ONE) \ |
f547a45baada
Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff
changeset
|
43 ASSERT (FUNC (0) == 0); \ |
f547a45baada
Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff
changeset
|
44 for (i = 0; i < BITS; i++) \ |
f547a45baada
Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff
changeset
|
45 { \ |
f547a45baada
Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff
changeset
|
46 ASSERT (FUNC (ONE << i) == 1); \ |
f547a45baada
Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff
changeset
|
47 for (j = i + 1; j < BITS; j++) \ |
f547a45baada
Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff
changeset
|
48 ASSERT (FUNC ((ONE << i) | (ONE << j)) == 2); \ |
f547a45baada
Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff
changeset
|
49 } \ |
f547a45baada
Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff
changeset
|
50 for (i = 0; i < 1000; i++) \ |
f547a45baada
Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff
changeset
|
51 { \ |
f547a45baada
Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff
changeset
|
52 TYPE value = rand () ^ (rand () << 31 << 1); \ |
f547a45baada
Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff
changeset
|
53 int count = 0; \ |
f547a45baada
Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff
changeset
|
54 for (j = 0; j < BITS; j++) \ |
f547a45baada
Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff
changeset
|
55 count += (value & (ONE << j)) != 0; \ |
f547a45baada
Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff
changeset
|
56 ASSERT (count == FUNC (value)); \ |
f547a45baada
Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff
changeset
|
57 } \ |
f547a45baada
Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff
changeset
|
58 ASSERT (FUNC (MAX) == BITS); |
f547a45baada
Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff
changeset
|
59 |
f547a45baada
Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff
changeset
|
60 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
|
61 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
|
62 ULONG_BIT, ULONG_MAX, 1UL); |
f547a45baada
Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff
changeset
|
63 #ifdef HAVE_UNSIGNED_LONG_LONG_INT |
f547a45baada
Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff
changeset
|
64 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
|
65 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
|
66 #endif |
f547a45baada
Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff
changeset
|
67 |
f547a45baada
Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff
changeset
|
68 return 0; |
f547a45baada
Improve name: "count-one-bits" is better than "popcount".
Ben Pfaff <blp@gnu.org>
parents:
diff
changeset
|
69 } |