kselftest.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * kselftest.h: kselftest framework return codes to include from
  3. * selftests.
  4. *
  5. * Copyright (c) 2014 Shuah Khan <shuahkh@osg.samsung.com>
  6. * Copyright (c) 2014 Samsung Electronics Co., Ltd.
  7. *
  8. * This file is released under the GPLv2.
  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. };
  29. static struct ksft_count ksft_cnt;
  30. static inline int ksft_test_num(void)
  31. {
  32. return ksft_cnt.ksft_pass + ksft_cnt.ksft_fail +
  33. ksft_cnt.ksft_xfail + ksft_cnt.ksft_xpass +
  34. ksft_cnt.ksft_xskip;
  35. }
  36. static inline void ksft_inc_pass_cnt(void) { ksft_cnt.ksft_pass++; }
  37. static inline void ksft_inc_fail_cnt(void) { ksft_cnt.ksft_fail++; }
  38. static inline void ksft_inc_xfail_cnt(void) { ksft_cnt.ksft_xfail++; }
  39. static inline void ksft_inc_xpass_cnt(void) { ksft_cnt.ksft_xpass++; }
  40. static inline void ksft_inc_xskip_cnt(void) { ksft_cnt.ksft_xskip++; }
  41. static inline void ksft_print_header(void)
  42. {
  43. printf("TAP version 13\n");
  44. }
  45. static inline void ksft_print_cnts(void)
  46. {
  47. printf("1..%d\n", ksft_test_num());
  48. }
  49. static inline void ksft_print_msg(const char *msg, ...)
  50. {
  51. va_list args;
  52. va_start(args, msg);
  53. printf("# ");
  54. vprintf(msg, args);
  55. va_end(args);
  56. }
  57. static inline void ksft_test_result_pass(const char *msg, ...)
  58. {
  59. va_list args;
  60. ksft_cnt.ksft_pass++;
  61. va_start(args, msg);
  62. printf("ok %d ", ksft_test_num());
  63. vprintf(msg, args);
  64. va_end(args);
  65. }
  66. static inline void ksft_test_result_fail(const char *msg, ...)
  67. {
  68. va_list args;
  69. ksft_cnt.ksft_fail++;
  70. va_start(args, msg);
  71. printf("not ok %d ", ksft_test_num());
  72. vprintf(msg, args);
  73. va_end(args);
  74. }
  75. static inline void ksft_test_result_skip(const char *msg, ...)
  76. {
  77. va_list args;
  78. ksft_cnt.ksft_xskip++;
  79. va_start(args, msg);
  80. printf("ok %d # skip ", ksft_test_num());
  81. vprintf(msg, args);
  82. va_end(args);
  83. }
  84. static inline int ksft_exit_pass(void)
  85. {
  86. ksft_print_cnts();
  87. exit(KSFT_PASS);
  88. }
  89. static inline int ksft_exit_fail(void)
  90. {
  91. printf("Bail out!\n");
  92. ksft_print_cnts();
  93. exit(KSFT_FAIL);
  94. }
  95. static inline int ksft_exit_fail_msg(const char *msg, ...)
  96. {
  97. va_list args;
  98. va_start(args, msg);
  99. printf("Bail out! ");
  100. vprintf(msg, args);
  101. va_end(args);
  102. ksft_print_cnts();
  103. exit(KSFT_FAIL);
  104. }
  105. static inline int ksft_exit_xfail(void)
  106. {
  107. ksft_print_cnts();
  108. exit(KSFT_XFAIL);
  109. }
  110. static inline int ksft_exit_xpass(void)
  111. {
  112. ksft_print_cnts();
  113. exit(KSFT_XPASS);
  114. }
  115. static inline int ksft_exit_skip(const char *msg, ...)
  116. {
  117. if (msg) {
  118. va_list args;
  119. va_start(args, msg);
  120. printf("1..%d # Skipped: ", ksft_test_num());
  121. vprintf(msg, args);
  122. va_end(args);
  123. } else {
  124. ksft_print_cnts();
  125. }
  126. exit(KSFT_SKIP);
  127. }
  128. #endif /* __KSELFTEST_H */