From: Bruno Haible Date: Sat, 16 Jul 2011 00:36:07 +0000 (+0200) Subject: ffs: More tests. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f9c36381e1c5de5fcd6e46491ebd2f98a5e5c2ca;p=pspp 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. --- diff --git a/ChangeLog b/ChangeLog index 8104469a60..9ee9bea80b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2011-07-15 Bruno Haible + + 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 ffsl, ffsll: new modules diff --git a/tests/test-ffs.c b/tests/test-ffs.c index cfc4d0ec53..fb37f202ee 100644 --- a/tests/test-ffs.c +++ b/tests/test-ffs.c @@ -26,11 +26,13 @@ SIGNATURE_CHECK (ffs, int, (int)); #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; @@ -39,14 +41,28 @@ naive (int i) 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; } diff --git a/tests/test-ffsl.c b/tests/test-ffsl.c index 2da2f5c8ce..9c134c7bc1 100644 --- a/tests/test-ffsl.c +++ b/tests/test-ffsl.c @@ -26,11 +26,13 @@ SIGNATURE_CHECK (ffsl, int, (long int)); #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; @@ -39,14 +41,28 @@ naive (long int i) 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; } diff --git a/tests/test-ffsll.c b/tests/test-ffsll.c index c0fd426c94..37ce88e09e 100644 --- a/tests/test-ffsll.c +++ b/tests/test-ffsll.c @@ -26,11 +26,13 @@ SIGNATURE_CHECK (ffsll, int, (long long int)); #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; @@ -39,14 +41,28 @@ naive (long long int i) 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; }