testsuite.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Some simple macros for use in test applications.
  4. * Copyright (C) 2000-2006 by Erik Andersen <andersen@uclibc.org>
  5. *
  6. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  7. */
  8. #ifndef TESTSUITE_H
  9. #define TESTSUITE_H
  10. #ifdef __NO_TESTCODE__
  11. extern size_t test_number;
  12. extern void init_testsuite(const char* testname);
  13. extern void done_testing(void) __attribute__((noreturn));
  14. extern void success_msg(int result, const char* command);
  15. extern void error_msg(int result, int line, const char* file, const char* command);
  16. #else
  17. size_t test_number = 0;
  18. static int failures = 0;
  19. void error_msg(int result, int line, const char* file, const char* command)
  20. {
  21. failures++;
  22. printf("\nFAILED TEST %lu: \n\t%s\n", (unsigned long)test_number, command);
  23. printf("AT LINE: %d, FILE: %s\n\n", line, file);
  24. }
  25. void success_msg(int result, const char* command)
  26. {
  27. #if 0
  28. printf("passed test: %s == 0\n", command);
  29. #endif
  30. }
  31. void done_testing(void)
  32. {
  33. if (0 < failures) {
  34. printf("Failed %d tests\n", failures);
  35. exit(EXIT_FAILURE);
  36. } else {
  37. printf("All functions tested sucessfully\n");
  38. exit(EXIT_SUCCESS);
  39. }
  40. }
  41. void init_testsuite(const char* testname)
  42. {
  43. printf("%s", testname);
  44. test_number = 0;
  45. failures = 0;
  46. #if !defined(__UCLIBC__) || defined(__UCLIBC_DYNAMIC_ATEXIT__)
  47. atexit(done_testing);
  48. #endif
  49. }
  50. #endif /* __NO_TESTCODE__ */
  51. #define TEST_STRING_OUTPUT(command, expected_result) \
  52. do { \
  53. int result = strcmp(command, expected_result); \
  54. test_number++; \
  55. if (result == expected_result) { \
  56. success_msg(result, "command"); \
  57. } else { \
  58. error_msg(result, __LINE__, __FILE__, command); \
  59. }; \
  60. } while (0)
  61. #define TEST_NUMERIC(command, expected_result) \
  62. do { \
  63. int result = (command); \
  64. test_number++; \
  65. if (result == expected_result) { \
  66. success_msg(result, # command); \
  67. } else { \
  68. error_msg(result, __LINE__, __FILE__, # command); \
  69. }; \
  70. } while (0)
  71. #define TEST(command) \
  72. do { \
  73. int result = (command); \
  74. test_number++; \
  75. if (result == 1) { \
  76. success_msg(result, # command); \
  77. } else { \
  78. error_msg(result, __LINE__, __FILE__, # command); \
  79. }; \
  80. } while (0)
  81. #define STR_CMD(cmd) cmd
  82. #endif /* TESTSUITE_H */