test-ffs.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. int
  20. main (void)
  21. {
  22. int failures = 0;
  23. int i;
  24. auto void try (const char *name, long long int param, int value,
  25. int expected);
  26. void try (const char *name, long long int param, int value, int expected)
  27. {
  28. if (value != expected)
  29. {
  30. printf ("%s(%#llx) expected %d got %d\n",
  31. name, param, expected, value);
  32. ++failures;
  33. }
  34. else
  35. printf ("%s(%#llx) as expected %d\n", name, param, value);
  36. }
  37. #define TEST(fct, type) \
  38. try (#fct, 0, fct ((type) 0), 0); \
  39. for (i=0 ; i < 8 * sizeof (type); i++) \
  40. try (#fct, 1ll << i, fct (((type) 1) << i), i + 1); \
  41. for (i=0 ; i < 8 * sizeof (type) ; i++) \
  42. try (#fct, (~((type) 0) >> i) << i, fct ((~((type) 0) >> i) << i), i + 1);\
  43. try (#fct, 0x80008000, fct ((type) 0x80008000), 16)
  44. TEST (ffs, int);
  45. /* Not implemented in uClibc (yet?)
  46. TEST (ffsl, long int);
  47. TEST (ffsll, long long int);
  48. */
  49. if (failures)
  50. printf ("Test FAILED! %d failure%s.\n", failures, &"s"[failures == 1]);
  51. else
  52. puts ("Test succeeded.");
  53. return failures;
  54. }