maint: update copyright
[gnulib.git] / tests / test-integer_length_l.c
1 /* Test of integer_length_l().
2    Copyright (C) 2011-2014 Free Software Foundation, Inc.
3
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.
8
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.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 #include <config.h>
18
19 #include "integer_length.h"
20
21 #include <limits.h>
22
23 #include "macros.h"
24
25 #define NBITS (sizeof (unsigned long) * CHAR_BIT)
26
27 static int
28 naive (unsigned long x)
29 {
30   int j;
31   for (j = NBITS - 1; j >= 0; j--)
32     if (x & (1UL << j))
33       return j + 1;
34   return 0;
35 }
36
37 int
38 main (int argc, char *argv[])
39 {
40   unsigned long x;
41   int i;
42
43   for (x = 0; x <= 256; x++)
44     ASSERT (integer_length_l (x) == naive (x));
45   for (i = 0; i < NBITS; i++)
46     {
47       ASSERT (integer_length_l (1UL << i) == naive (1UL << i));
48       ASSERT (integer_length_l (1UL << i) == i + 1);
49       ASSERT (integer_length_l (-1UL << i) == NBITS);
50     }
51   for (i = 0; i < NBITS - 1; i++)
52     ASSERT (integer_length_l (3UL << i) == i + 2);
53   for (i = 0; i < NBITS - 2; i++)
54     ASSERT (integer_length_l (-3UL << i) == NBITS);
55   for (i = 0; i < NBITS - 2; i++)
56     {
57       ASSERT (integer_length_l (5UL << i) == i + 3);
58       ASSERT (integer_length_l (7UL << i) == i + 3);
59     }
60   for (i = 0; i < NBITS - 3; i++)
61     {
62       ASSERT (integer_length_l (-5UL << i) == NBITS);
63       ASSERT (integer_length_l (-7UL << i) == NBITS);
64     }
65   return 0;
66 }