testsuite.h 2.3 KB

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