testsuite.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Some simple macros for use in test applications.
  4. *
  5. * Copyright (C) 2000, 2001 by Lineo, inc.
  6. * Written by Erik Andersen <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. #ifndef TESTSUITE_H
  24. #define TESTSUITE_H
  25. size_t test_number = 0;
  26. static int failures = 0;
  27. void init_testsuite(const char* testname);
  28. void done_testing(void) __attribute__((noreturn));
  29. void success_msg(int result, const char* command);
  30. void error_msg(int result, int line, const char* file, const char* command);
  31. #define TEST_STRING_OUTPUT( command, expected_result ) \
  32. do { \
  33. int result=strcmp( command, expected_result); \
  34. test_number++; \
  35. if ( result == expected_result ) { \
  36. success_msg( result, "command"); \
  37. } else { \
  38. error_msg(result, __LINE__, __FILE__, command); \
  39. }; \
  40. } while (0)
  41. #define TEST_NUMERIC( command, expected_result ) \
  42. do { \
  43. int result=(command); \
  44. test_number++; \
  45. if ( result == expected_result ) { \
  46. success_msg( result, # command); \
  47. } else { \
  48. error_msg(result, __LINE__, __FILE__, # command); \
  49. }; \
  50. } while (0)
  51. #define TEST(command) \
  52. do { \
  53. int result=(command); \
  54. test_number++; \
  55. if ( result == 1) { \
  56. success_msg( result, # command); \
  57. } else { \
  58. error_msg(result, __LINE__, __FILE__, # command ); \
  59. }; \
  60. } while (0)
  61. #define STR_CMD(cmd) cmd
  62. void error_msg(int result, int line, const char* file, const char* command)
  63. {
  64. failures++;
  65. printf("\nFAILED TEST %d: \n\t%s\n", test_number, command);
  66. printf("AT LINE: %d, FILE: %s\n\n", line, file);
  67. }
  68. void success_msg(int result, const char* command)
  69. {
  70. #if 0
  71. printf("passed test: %s == 0\n", command);
  72. #endif
  73. }
  74. void done_testing(void)
  75. {
  76. if (0 < failures) {
  77. printf("Failed %d tests\n", failures);
  78. exit(EXIT_FAILURE);
  79. } else {
  80. printf("All functions tested sucessfully\n");
  81. exit( EXIT_SUCCESS );
  82. }
  83. }
  84. void init_testsuite(const char* testname)
  85. {
  86. printf("%s", testname);
  87. test_number = 0;
  88. failures = 0;
  89. atexit(done_testing);
  90. }
  91. #endif /* TESTSUITE_H */