testsuite.h 2.3 KB

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