test-ffs.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* Copyright (C) 1994, 1997, 2000, 2004 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Joel Sherrill (jsherril@redstone-emh2.army.mil),
  4. On-Line Applications Research Corporation.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library 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 GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. int
  21. main (void)
  22. {
  23. int failures = 0;
  24. int i;
  25. auto void try (const char *name, long long int param, int value,
  26. int expected);
  27. void try (const char *name, long long int param, int value, int expected)
  28. {
  29. if (value != expected)
  30. {
  31. printf ("%s(%#llx) expected %d got %d\n",
  32. name, param, expected, value);
  33. ++failures;
  34. }
  35. else
  36. printf ("%s(%#llx) as expected %d\n", name, param, value);
  37. }
  38. #define TEST(fct, type) \
  39. try (#fct, 0, fct ((type) 0), 0); \
  40. for (i=0 ; i < 8 * sizeof (type); i++) \
  41. try (#fct, 1ll << i, fct (((type) 1) << i), i + 1); \
  42. for (i=0 ; i < 8 * sizeof (type) ; i++) \
  43. try (#fct, (~((type) 0) >> i) << i, fct ((~((type) 0) >> i) << i), i + 1);\
  44. try (#fct, 0x80008000, fct ((type) 0x80008000), 16)
  45. TEST (ffs, int);
  46. /* Not implemented in uClibc (yet?)
  47. TEST (ffsl, long int);
  48. TEST (ffsll, long long int);
  49. */
  50. if (failures)
  51. printf ("Test FAILED! %d failure%s.\n", failures, &"s"[failures == 1]);
  52. else
  53. puts ("Test succeeded.");
  54. return failures;
  55. }