testsuite.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. and Erik Andersen
  6. * Copyright (C) 2000,2001 by Erik Andersen <andersen@uclibc.org>
  7. * Written by Erik Andersen <andersen@uclibc.org>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU Library General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  17. * for more details.
  18. *
  19. * You should have received a copy of the GNU Library General Public License
  20. * along with this program; if not, write to the Free Software Foundation,
  21. * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. */
  24. #ifndef TESTSUITE_H
  25. #define TESTSUITE_H
  26. #ifdef __NO_TESTCODE__
  27. extern size_t test_number;
  28. extern void init_testsuite(const char* testname);
  29. extern void done_testing(void) __attribute__((noreturn));
  30. extern void success_msg(int result, const char* command);
  31. extern void error_msg(int result, int line, const char* file, const char* command);
  32. #else
  33. size_t test_number = 0;
  34. static int failures = 0;
  35. void error_msg(int result, int line, const char* file, const char* command)
  36. {
  37. failures++;
  38. printf("\nFAILED TEST %d: \n\t%s\n", test_number, command);
  39. printf("AT LINE: %d, FILE: %s\n\n", line, file);
  40. }
  41. void success_msg(int result, const char* command)
  42. {
  43. #if 0
  44. printf("passed test: %s == 0\n", command);
  45. #endif
  46. }
  47. void done_testing(void)
  48. {
  49. if (0 < failures) {
  50. printf("Failed %d tests\n", failures);
  51. exit(EXIT_FAILURE);
  52. } else {
  53. printf("All functions tested sucessfully\n");
  54. exit( EXIT_SUCCESS );
  55. }
  56. }
  57. void init_testsuite(const char* testname)
  58. {
  59. printf("%s", testname);
  60. test_number = 0;
  61. failures = 0;
  62. atexit(done_testing);
  63. }
  64. #endif
  65. #define TEST_STRING_OUTPUT( command, expected_result ) \
  66. do { \
  67. int result=strcmp( command, expected_result); \
  68. test_number++; \
  69. if ( result == expected_result ) { \
  70. success_msg( result, "command"); \
  71. } else { \
  72. error_msg(result, __LINE__, __FILE__, command); \
  73. }; \
  74. } while (0)
  75. #define TEST_NUMERIC( command, expected_result ) \
  76. do { \
  77. int result=(command); \
  78. test_number++; \
  79. if ( result == expected_result ) { \
  80. success_msg( result, # command); \
  81. } else { \
  82. error_msg(result, __LINE__, __FILE__, # command); \
  83. }; \
  84. } while (0)
  85. #define TEST(command) \
  86. do { \
  87. int result=(command); \
  88. test_number++; \
  89. if ( result == 1) { \
  90. success_msg( result, # command); \
  91. } else { \
  92. error_msg(result, __LINE__, __FILE__, # command ); \
  93. }; \
  94. } while (0)
  95. #define STR_CMD(cmd) cmd
  96. #endif /* TESTSUITE_H */