ffsl, ffsll: new modules
[gnulib.git] / tests / test-ffsll.c
1 /*
2  * Copyright (C) 2011 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 /* Written by Eric Blake.  */
18 #include <config.h>
19
20 #include <string.h>
21
22 #include "signature.h"
23 SIGNATURE_CHECK (ffsll, int, (long long int));
24
25 #include <limits.h>
26
27 #include "macros.h"
28
29 static int
30 naive (long long int i)
31 {
32   unsigned long long int j;
33   for (j = 0; j < CHAR_BIT * sizeof i; j++)
34     if (i & (1ULL << j))
35       return j + 1;
36   return 0;
37 }
38
39 int
40 main (int argc, char *argv[])
41 {
42   long long int i;
43
44   for (i = -128; i <= 128; i++)
45     ASSERT (ffsll (i) == naive (i));
46   for (i = 0; i < CHAR_BIT * sizeof i; i++)
47     {
48       ASSERT (ffsll (1ULL << i) == naive (1ULL << i));
49       ASSERT (ffsll (1ULL << i) == i + 1);
50     }
51   return 0;
52 }