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