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