kselftest.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * kselftest.h: kselftest framework return codes to include from
  4. * selftests.
  5. *
  6. * Copyright (c) 2014 Shuah Khan <shuahkh@osg.samsung.com>
  7. * Copyright (c) 2014 Samsung Electronics Co., Ltd.
  8. *
  9. */
  10. #ifndef __KSELFTEST_H
  11. #define __KSELFTEST_H
  12. #include <stdlib.h>
  13. #include <unistd.h>
  14. #include <stdarg.h>
  15. /* define kselftest exit codes */
  16. #define KSFT_PASS 0
  17. #define KSFT_FAIL 1
  18. #define KSFT_XFAIL 2
  19. #define KSFT_XPASS 3
  20. /* Treat skip as pass */
  21. #define KSFT_SKIP 4
  22. /* counters */
  23. struct ksft_count {
  24. unsigned int ksft_pass;
  25. unsigned int ksft_fail;
  26. unsigned int ksft_xfail;
  27. unsigned int ksft_xpass;
  28. unsigned int ksft_xskip;
  29. unsigned int ksft_error;
  30. };
  31. static struct ksft_count ksft_cnt;
  32. static inline int ksft_test_num(void)
  33. {
  34. return ksft_cnt.ksft_pass + ksft_cnt.ksft_fail +
  35. ksft_cnt.ksft_xfail + ksft_cnt.ksft_xpass +
  36. ksft_cnt.ksft_xskip + ksft_cnt.ksft_error;
  37. }
  38. static inline void ksft_inc_pass_cnt(void) { ksft_cnt.ksft_pass++; }
  39. static inline void ksft_inc_fail_cnt(void) { ksft_cnt.ksft_fail++; }
  40. static inline void ksft_inc_xfail_cnt(void) { ksft_cnt.ksft_xfail++; }
  41. static inline void ksft_inc_xpass_cnt(void) { ksft_cnt.ksft_xpass++; }
  42. static inline void ksft_inc_xskip_cnt(void) { ksft_cnt.ksft_xskip++; }
  43. static inline void ksft_inc_error_cnt(void) { ksft_cnt.ksft_error++; }
  44. static inline int ksft_get_pass_cnt(void) { return ksft_cnt.ksft_pass; }
  45. static inline int ksft_get_fail_cnt(void) { return ksft_cnt.ksft_fail; }
  46. static inline int ksft_get_xfail_cnt(void) { return ksft_cnt.ksft_xfail; }
  47. static inline int ksft_get_xpass_cnt(void) { return ksft_cnt.ksft_xpass; }
  48. static inline int ksft_get_xskip_cnt(void) { return ksft_cnt.ksft_xskip; }
  49. static inline int ksft_get_error_cnt(void) { return ksft_cnt.ksft_error; }
  50. static inline void ksft_print_header(void)
  51. {
  52. if (!(getenv("KSFT_TAP_LEVEL")))
  53. printf("TAP version 13\n");
  54. }
  55. static inline void ksft_print_cnts(void)
  56. {
  57. printf("Pass %d Fail %d Xfail %d Xpass %d Skip %d Error %d\n",
  58. ksft_cnt.ksft_pass, ksft_cnt.ksft_fail,
  59. ksft_cnt.ksft_xfail, ksft_cnt.ksft_xpass,
  60. ksft_cnt.ksft_xskip, ksft_cnt.ksft_error);
  61. printf("1..%d\n", ksft_test_num());
  62. }
  63. static inline void ksft_print_msg(const char *msg, ...)
  64. {
  65. va_list args;
  66. va_start(args, msg);
  67. printf("# ");
  68. vprintf(msg, args);
  69. va_end(args);
  70. }
  71. static inline void ksft_test_result_pass(const char *msg, ...)
  72. {
  73. va_list args;
  74. ksft_cnt.ksft_pass++;
  75. va_start(args, msg);
  76. printf("ok %d ", ksft_test_num());
  77. vprintf(msg, args);
  78. va_end(args);
  79. }
  80. static inline void ksft_test_result_fail(const char *msg, ...)
  81. {
  82. va_list args;
  83. ksft_cnt.ksft_fail++;
  84. va_start(args, msg);
  85. printf("not ok %d ", ksft_test_num());
  86. vprintf(msg, args);
  87. va_end(args);
  88. }
  89. static inline void ksft_test_result_skip(const char *msg, ...)
  90. {
  91. va_list args;
  92. ksft_cnt.ksft_xskip++;
  93. va_start(args, msg);
  94. printf("ok %d # skip ", ksft_test_num());
  95. vprintf(msg, args);
  96. va_end(args);
  97. }
  98. static inline void ksft_test_result_error(const char *msg, ...)
  99. {
  100. va_list args;
  101. ksft_cnt.ksft_error++;
  102. va_start(args, msg);
  103. printf("not ok %d # error ", ksft_test_num());
  104. vprintf(msg, args);
  105. va_end(args);
  106. }
  107. static inline int ksft_exit_pass(void)
  108. {
  109. ksft_print_cnts();
  110. exit(KSFT_PASS);
  111. }
  112. static inline int ksft_exit_fail(void)
  113. {
  114. printf("Bail out!\n");
  115. ksft_print_cnts();
  116. exit(KSFT_FAIL);
  117. }
  118. static inline int ksft_exit_fail_msg(const char *msg, ...)
  119. {
  120. va_list args;
  121. va_start(args, msg);
  122. printf("Bail out! ");
  123. vprintf(msg, args);
  124. va_end(args);
  125. ksft_print_cnts();
  126. exit(KSFT_FAIL);
  127. }
  128. static inline int ksft_exit_xfail(void)
  129. {
  130. ksft_print_cnts();
  131. exit(KSFT_XFAIL);
  132. }
  133. static inline int ksft_exit_xpass(void)
  134. {
  135. ksft_print_cnts();
  136. exit(KSFT_XPASS);
  137. }
  138. static inline int ksft_exit_skip(const char *msg, ...)
  139. {
  140. if (msg) {
  141. va_list args;
  142. va_start(args, msg);
  143. printf("1..%d # Skipped: ", ksft_test_num());
  144. vprintf(msg, args);
  145. va_end(args);
  146. } else {
  147. ksft_print_cnts();
  148. }
  149. exit(KSFT_SKIP);
  150. }
  151. #endif /* __KSELFTEST_H */