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