testsuite.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 %lu: \n\t%s\n", (unsigned long)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. #if !defined(__UCLIBC__) || defined(__UCLIBC_DYNAMIC_ATEXIT__)
  63. atexit(done_testing);
  64. #endif
  65. }
  66. #endif /* __NO_TESTCODE__ */
  67. #define TEST_STRING_OUTPUT(command, expected_result) \
  68. do { \
  69. int result = strcmp(command, expected_result); \
  70. test_number++; \
  71. if (result == expected_result) { \
  72. success_msg(result, "command"); \
  73. } else { \
  74. error_msg(result, __LINE__, __FILE__, command); \
  75. }; \
  76. } while (0)
  77. #define TEST_NUMERIC(command, expected_result) \
  78. do { \
  79. int result = (command); \
  80. test_number++; \
  81. if (result == expected_result) { \
  82. success_msg(result, # command); \
  83. } else { \
  84. error_msg(result, __LINE__, __FILE__, # command); \
  85. }; \
  86. } while (0)
  87. #define TEST(command) \
  88. do { \
  89. int result = (command); \
  90. test_number++; \
  91. if (result == 1) { \
  92. success_msg(result, # command); \
  93. } else { \
  94. error_msg(result, __LINE__, __FILE__, # command); \
  95. }; \
  96. } while (0)
  97. #define STR_CMD(cmd) cmd
  98. #endif /* TESTSUITE_H */