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