Mercurial > gnulib
annotate tests/test-count-trailing-zeros.c @ 37196:fe8f1c53b62b
New module 'count-trailing-zeros'.
* MODULES.html.sh: Mention it.
* lib/count-trailing-zeros.c, lib/count-trailing-zeros.h:
* m4/count-trailing-zeros.m4, modules/count-trailing-zeros:
* modules/count-trailing-zeros-tests:
* tests/test-count-trailing-zeros.c:
New files.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Sun, 06 Oct 2013 23:58:00 -0700 |
parents | |
children | 344018b6e5d7 |
rev | line source |
---|---|
37196
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
1 /* Test counting of trailing zeros. |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
2 Copyright (C) 2013 Free Software Foundation, Inc. |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
3 |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
4 This program is free software: you can redistribute it and/or modify |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
5 it under the terms of the GNU General Public License as published by |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
6 the Free Software Foundation; either version 3 of the License, or |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
7 (at your option) any later version. |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
8 |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
9 This program is distributed in the hope that it will be useful, |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
10 but WITHOUT ANY WARRANTY; without even the implied warranty of |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
12 GNU General Public License for more details. |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
13 |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
14 You should have received a copy of the GNU General Public License |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
16 |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
17 /* Written by Paul Eggert. */ |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
18 |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
19 #include <config.h> |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
20 |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
21 #include "count-trailing-zeros.h" |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
22 |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
23 #include <limits.h> |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
24 #include <stdio.h> |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
25 |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
26 #include "macros.h" |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
27 |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
28 #define UINT_BIT (sizeof (unsigned int) * CHAR_BIT) |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
29 #define ULONG_BIT (sizeof (unsigned long int) * CHAR_BIT) |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
30 #define ULLONG_BIT (sizeof (unsigned long long int) * CHAR_BIT) |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
31 |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
32 #ifndef ULLONG_MAX |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
33 # define HALF (1ULL << (sizeof (unsigned long long int) * CHAR_BIT - 1)) |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
34 # define ULLONG_MAX (HALF - 1 + HALF) |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
35 #endif |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
36 |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
37 int |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
38 main (int argc, char *argv[]) |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
39 { |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
40 int i, j; |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
41 |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
42 #define TEST_COUNT_TRAILING_ZEROS(FUNC, TYPE, BITS, MAX, ONE) \ |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
43 ASSERT (FUNC (0) == BITS); \ |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
44 for (i = 0; i < BITS; i++) \ |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
45 { \ |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
46 ASSERT (FUNC (ONE << i) == i); \ |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
47 for (j = 0; j < i; j++) \ |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
48 ASSERT (FUNC ((ONE << i) | (ONE << j)) == j); \ |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
49 } \ |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
50 for (i = 0; i < 1000; i++) \ |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
51 { \ |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
52 TYPE value = rand () ^ (rand () << 31 << 1); \ |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
53 int count = 0; \ |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
54 for (j = BITS - 1; 0 <= j; j--) \ |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
55 if (value & (ONE << j)) \ |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
56 count = j; \ |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
57 ASSERT (count == FUNC (value)); \ |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
58 } \ |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
59 ASSERT (FUNC (MAX) == 0); |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
60 |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
61 TEST_COUNT_TRAILING_ZEROS (count_trailing_zeros, unsigned int, |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
62 UINT_BIT, UINT_MAX, 1U); |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
63 TEST_COUNT_TRAILING_ZEROS (count_trailing_zeros_l, unsigned long int, |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
64 ULONG_BIT, ULONG_MAX, 1UL); |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
65 #ifdef HAVE_UNSIGNED_LONG_LONG_INT |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
66 TEST_COUNT_TRAILING_ZEROS (count_trailing_zeros_ll, unsigned long long int, |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
67 ULLONG_BIT, ULLONG_MAX, 1ULL); |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
68 #endif |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
69 |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
70 return 0; |
fe8f1c53b62b
New module 'count-trailing-zeros'.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff
changeset
|
71 } |