kselftest.h 3.9 KB

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