* tests/test-ffs.c (NBITS): New macro.
(main): Add more tests.
* tests/test-ffsl.c (NBITS): New macro.
(main): Add more tests.
* tests/test-ffsll.c (NBITS): New macro.
(main): Add more tests.
+2011-07-15 Bruno Haible <bruno@clisp.org>
+
+ ffs: More tests.
+ * tests/test-ffs.c (NBITS): New macro.
+ (main): Add more tests.
+ * tests/test-ffsl.c (NBITS): New macro.
+ (main): Add more tests.
+ * tests/test-ffsll.c (NBITS): New macro.
+ (main): Add more tests.
+
2011-07-15 Eric Blake <eblake@redhat.com>
ffsl, ffsll: new modules
#include "macros.h"
+#define NBITS (sizeof (int) * CHAR_BIT)
+
static int
naive (int i)
{
unsigned int j;
- for (j = 0; j < CHAR_BIT * sizeof i; j++)
+ for (j = 0; j < NBITS; j++)
if (i & (1U << j))
return j + 1;
return 0;
int
main (int argc, char *argv[])
{
+ int x;
int i;
- for (i = -128; i <= 128; i++)
- ASSERT (ffs (i) == naive (i));
- for (i = 0; i < CHAR_BIT * sizeof i; i++)
+ for (x = -128; x <= 128; x++)
+ ASSERT (ffs (x) == naive (x));
+ for (i = 0; i < NBITS; i++)
{
ASSERT (ffs (1U << i) == naive (1U << i));
ASSERT (ffs (1U << i) == i + 1);
+ ASSERT (ffs (-1U << i) == i + 1);
+ }
+ for (i = 0; i < NBITS - 1; i++)
+ {
+ ASSERT (ffs (3U << i) == i + 1);
+ ASSERT (ffs (-3U << i) == i + 1);
+ }
+ for (i = 0; i < NBITS - 2; i++)
+ {
+ ASSERT (ffs (5U << i) == i + 1);
+ ASSERT (ffs (-5U << i) == i + 1);
+ ASSERT (ffs (7U << i) == i + 1);
+ ASSERT (ffs (-7U << i) == i + 1);
}
return 0;
}
#include "macros.h"
+#define NBITS (sizeof (long int) * CHAR_BIT)
+
static int
naive (long int i)
{
unsigned long int j;
- for (j = 0; j < CHAR_BIT * sizeof i; j++)
+ for (j = 0; j < NBITS; j++)
if (i & (1UL << j))
return j + 1;
return 0;
int
main (int argc, char *argv[])
{
- long int i;
+ long int x;
+ int i;
for (i = -128; i <= 128; i++)
ASSERT (ffsl (i) == naive (i));
- for (i = 0; i < CHAR_BIT * sizeof i; i++)
+ for (i = 0; i < NBITS; i++)
{
ASSERT (ffsl (1UL << i) == naive (1UL << i));
ASSERT (ffsl (1UL << i) == i + 1);
+ ASSERT (ffsl (-1UL << i) == i + 1);
+ }
+ for (i = 0; i < NBITS - 1; i++)
+ {
+ ASSERT (ffsl (3UL << i) == i + 1);
+ ASSERT (ffsl (-3UL << i) == i + 1);
+ }
+ for (i = 0; i < NBITS - 2; i++)
+ {
+ ASSERT (ffsl (5UL << i) == i + 1);
+ ASSERT (ffsl (-5UL << i) == i + 1);
+ ASSERT (ffsl (7UL << i) == i + 1);
+ ASSERT (ffsl (-7UL << i) == i + 1);
}
return 0;
}
#include "macros.h"
+#define NBITS (sizeof (long long int) * CHAR_BIT)
+
static int
naive (long long int i)
{
unsigned long long int j;
- for (j = 0; j < CHAR_BIT * sizeof i; j++)
+ for (j = 0; j < NBITS; j++)
if (i & (1ULL << j))
return j + 1;
return 0;
int
main (int argc, char *argv[])
{
- long long int i;
+ long long int x;
+ int i;
- for (i = -128; i <= 128; i++)
- ASSERT (ffsll (i) == naive (i));
- for (i = 0; i < CHAR_BIT * sizeof i; i++)
+ for (x = -128; x <= 128; x++)
+ ASSERT (ffsll (x) == naive (x));
+ for (i = 0; i < NBITS; i++)
{
ASSERT (ffsll (1ULL << i) == naive (1ULL << i));
ASSERT (ffsll (1ULL << i) == i + 1);
+ ASSERT (ffsll (-1ULL << i) == i + 1);
+ }
+ for (i = 0; i < NBITS - 1; i++)
+ {
+ ASSERT (ffsll (3ULL << i) == i + 1);
+ ASSERT (ffsll (-3ULL << i) == i + 1);
+ }
+ for (i = 0; i < NBITS - 2; i++)
+ {
+ ASSERT (ffsll (5ULL << i) == i + 1);
+ ASSERT (ffsll (-5ULL << i) == i + 1);
+ ASSERT (ffsll (7ULL << i) == i + 1);
+ ASSERT (ffsll (-7ULL << i) == i + 1);
}
return 0;
}