testsuite.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #ifdef __NO_TESTCODE__
  26. extern size_t test_number;
  27. extern void init_testsuite(const char* testname);
  28. extern void done_testing(void) __attribute__((noreturn));
  29. extern void success_msg(int result, const char* command);
  30. extern void error_msg(int result, int line, const char* file, const char* command);
  31. #else
  32. size_t test_number = 0;
  33. static int failures = 0;
  34. void error_msg(int result, int line, const char* file, const char* command)
  35. {
  36. failures++;
  37. printf("\nFAILED TEST %d: \n\t%s\n", test_number, command);
  38. printf("AT LINE: %d, FILE: %s\n\n", line, file);
  39. }
  40. void success_msg(int result, const char* command)
  41. {
  42. #if 0
  43. printf("passed test: %s == 0\n", command);
  44. #endif
  45. }
  46. void done_testing(void)
  47. {
  48. if (0 < failures) {
  49. printf("Failed %d tests\n", failures);
  50. exit(EXIT_FAILURE);
  51. } else {
  52. printf("All functions tested sucessfully\n");
  53. exit( EXIT_SUCCESS );
  54. }
  55. }
  56. void init_testsuite(const char* testname)
  57. {
  58. printf("%s", testname);
  59. test_number = 0;
  60. failures = 0;
  61. atexit(done_testing);
  62. }
  63. #endif
  64. #define TEST_STRING_OUTPUT( command, expected_result ) \
  65. do { \
  66. int result=strcmp( command, expected_result); \
  67. test_number++; \
  68. if ( result == expected_result ) { \
  69. success_msg( result, "command"); \
  70. } else { \
  71. error_msg(result, __LINE__, __FILE__, command); \
  72. }; \
  73. } while (0)
  74. #define TEST_NUMERIC( command, expected_result ) \
  75. do { \
  76. int result=(command); \
  77. test_number++; \
  78. if ( result == expected_result ) { \
  79. success_msg( result, # command); \
  80. } else { \
  81. error_msg(result, __LINE__, __FILE__, # command); \
  82. }; \
  83. } while (0)
  84. #define TEST(command) \
  85. do { \
  86. int result=(command); \
  87. test_number++; \
  88. if ( result == 1) { \
  89. success_msg( result, # command); \
  90. } else { \
  91. error_msg(result, __LINE__, __FILE__, # command ); \
  92. }; \
  93. } while (0)
  94. #define STR_CMD(cmd) cmd
  95. #endif /* TESTSUITE_H */