macros.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* macros.h -- Common macros used by JWhois tests.
  2. Copyright (C) 2006-2016 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. /* Define ASSERT_STREAM before including this file if ASSERT must
  16. target a stream other than stderr. */
  17. #ifndef ASSERT_STREAM
  18. # define ASSERT_STREAM stderr
  19. #endif
  20. /* ASSERT (condition);
  21. verifies that the specified condition is fulfilled. If not, a message
  22. is printed to ASSERT_STREAM if defined (defaulting to stderr if
  23. undefined) and the program is terminated with an error code.
  24. This macro has the following properties:
  25. - The programmer specifies the expected condition, not the failure
  26. condition. This simplifies thinking.
  27. - The condition is tested always, regardless of compilation flags.
  28. (Unlike the macro from <assert.h>.)
  29. - On Unix platforms, the tester can debug the test program with a
  30. debugger (provided core dumps are enabled: "ulimit -c unlimited").
  31. - For the sake of platforms where no debugger is available (such as
  32. some mingw systems), an error message is printed on the error
  33. stream that includes the source location of the ASSERT invocation.
  34. */
  35. #define ASSERT(expr) \
  36. do \
  37. { \
  38. if (!(expr)) \
  39. { \
  40. fprintf (ASSERT_STREAM, "%s:%d: assertion '%s' failed\n", \
  41. __FILE__, __LINE__, #expr); \
  42. fflush (ASSERT_STREAM); \
  43. abort (); \
  44. } \
  45. } \
  46. while (0)
  47. /* SIZEOF (array)
  48. returns the number of elements of an array. It works for arrays that are
  49. declared outside functions and for local variables of array type. It does
  50. *not* work for function parameters of array type, because they are actually
  51. parameters of pointer type. */
  52. #define SIZEOF(array) (sizeof (array) / sizeof (array[0]))