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