testsuite.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Some simple macros for use in test applications.
  4. *
  5. * Copyright (C) 2000 by Lineo, inc. Written by Erik Andersen
  6. * <andersen@lineo.com>, <andersee@debian.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU Library General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  16. * for more details.
  17. *
  18. * You should have received a copy of the GNU Library General Public License
  19. * along with this program; if not, write to the Free Software Foundation,
  20. * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. void error_msg(int result, int line, const char* file, const char* msg, ...);
  24. void success_msg(int result, const char* msg, ...);
  25. void stop_testing(void) __attribute__((noreturn));;
  26. static int failures = 0;
  27. #define TEST_STRING_OUTPUT( command, expected_result, message, args...) \
  28. do { \
  29. int result=strcmp( command, expected_result); \
  30. if ( result != 0 ) \
  31. success_msg( result, message, ## args); \
  32. else \
  33. error_msg(result, __LINE__, __FILE__, message, ## args); \
  34. } while (0)
  35. #define TEST_NUMERIC_OUTPUT( command, expected_result, message, args...) \
  36. do { \
  37. int result=(command); \
  38. if ( result == expected_result ) \
  39. success_msg( result, message, ## args); \
  40. else \
  41. error_msg(result, __LINE__, __FILE__, message, ## args); \
  42. } while (0)
  43. #define TEST_SUCCESS(command, message, args...) TEST_NUMERIC_OUTPUT( command, EXIT_SUCCESS, message, ## args)
  44. void error_msg(int result, int line, const char* file, const char* msg, ...)
  45. {
  46. va_list p;
  47. failures++;
  48. va_start(p, msg);
  49. printf("FAILED TEST ");
  50. vprintf(msg, p);
  51. va_end(p);
  52. printf(" AT LINE: %d, FILE: %s\n", line, file);
  53. }
  54. void success_msg(int result, const char* msg, ...)
  55. {
  56. #if 0
  57. va_list p;
  58. va_start(p, msg);
  59. printf("passed test: ");
  60. vprintf(msg, p);
  61. va_end(p);
  62. printf("\n");
  63. #endif
  64. }
  65. void stop_testing(void)
  66. {
  67. if (0 < failures) {
  68. printf("Failed %d tests\n", failures);
  69. exit(EXIT_FAILURE);
  70. } else {
  71. printf("All functions tested sucessfully\n");
  72. exit( EXIT_SUCCESS );
  73. }
  74. }