1*4882a593Smuzhiyun /* 2*4882a593Smuzhiyun * Simple unit test library 3*4882a593Smuzhiyun * 4*4882a593Smuzhiyun * Copyright (c) 2013 Google, Inc 5*4882a593Smuzhiyun * 6*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+ 7*4882a593Smuzhiyun */ 8*4882a593Smuzhiyun 9*4882a593Smuzhiyun #ifndef __TEST_UT_H 10*4882a593Smuzhiyun #define __TEST_UT_H 11*4882a593Smuzhiyun 12*4882a593Smuzhiyun #include <linux/err.h> 13*4882a593Smuzhiyun 14*4882a593Smuzhiyun struct unit_test_state; 15*4882a593Smuzhiyun 16*4882a593Smuzhiyun /** 17*4882a593Smuzhiyun * ut_fail() - Record failure of a unit test 18*4882a593Smuzhiyun * 19*4882a593Smuzhiyun * @uts: Test state 20*4882a593Smuzhiyun * @fname: Filename where the error occurred 21*4882a593Smuzhiyun * @line: Line number where the error occurred 22*4882a593Smuzhiyun * @func: Function name where the error occurred 23*4882a593Smuzhiyun * @cond: The condition that failed 24*4882a593Smuzhiyun */ 25*4882a593Smuzhiyun void ut_fail(struct unit_test_state *uts, const char *fname, int line, 26*4882a593Smuzhiyun const char *func, const char *cond); 27*4882a593Smuzhiyun 28*4882a593Smuzhiyun /** 29*4882a593Smuzhiyun * ut_failf() - Record failure of a unit test 30*4882a593Smuzhiyun * 31*4882a593Smuzhiyun * @uts: Test state 32*4882a593Smuzhiyun * @fname: Filename where the error occurred 33*4882a593Smuzhiyun * @line: Line number where the error occurred 34*4882a593Smuzhiyun * @func: Function name where the error occurred 35*4882a593Smuzhiyun * @cond: The condition that failed 36*4882a593Smuzhiyun * @fmt: printf() format string for the error, followed by args 37*4882a593Smuzhiyun */ 38*4882a593Smuzhiyun void ut_failf(struct unit_test_state *uts, const char *fname, int line, 39*4882a593Smuzhiyun const char *func, const char *cond, const char *fmt, ...) 40*4882a593Smuzhiyun __attribute__ ((format (__printf__, 6, 7))); 41*4882a593Smuzhiyun 42*4882a593Smuzhiyun 43*4882a593Smuzhiyun /* Assert that a condition is non-zero */ 44*4882a593Smuzhiyun #define ut_assert(cond) \ 45*4882a593Smuzhiyun if (!(cond)) { \ 46*4882a593Smuzhiyun ut_fail(uts, __FILE__, __LINE__, __func__, #cond); \ 47*4882a593Smuzhiyun return CMD_RET_FAILURE; \ 48*4882a593Smuzhiyun } 49*4882a593Smuzhiyun 50*4882a593Smuzhiyun /* Assert that a condition is non-zero, with printf() string */ 51*4882a593Smuzhiyun #define ut_assertf(cond, fmt, args...) \ 52*4882a593Smuzhiyun if (!(cond)) { \ 53*4882a593Smuzhiyun ut_failf(uts, __FILE__, __LINE__, __func__, #cond, \ 54*4882a593Smuzhiyun fmt, ##args); \ 55*4882a593Smuzhiyun return CMD_RET_FAILURE; \ 56*4882a593Smuzhiyun } 57*4882a593Smuzhiyun 58*4882a593Smuzhiyun /* Assert that two int expressions are equal */ 59*4882a593Smuzhiyun #define ut_asserteq(expr1, expr2) { \ 60*4882a593Smuzhiyun unsigned int val1 = (expr1), val2 = (expr2); \ 61*4882a593Smuzhiyun \ 62*4882a593Smuzhiyun if (val1 != val2) { \ 63*4882a593Smuzhiyun ut_failf(uts, __FILE__, __LINE__, __func__, \ 64*4882a593Smuzhiyun #expr1 " == " #expr2, \ 65*4882a593Smuzhiyun "Expected %d, got %d", val1, val2); \ 66*4882a593Smuzhiyun return CMD_RET_FAILURE; \ 67*4882a593Smuzhiyun } \ 68*4882a593Smuzhiyun } 69*4882a593Smuzhiyun 70*4882a593Smuzhiyun /* Assert that two string expressions are equal */ 71*4882a593Smuzhiyun #define ut_asserteq_str(expr1, expr2) { \ 72*4882a593Smuzhiyun const char *val1 = (expr1), *val2 = (expr2); \ 73*4882a593Smuzhiyun \ 74*4882a593Smuzhiyun if (strcmp(val1, val2)) { \ 75*4882a593Smuzhiyun ut_failf(uts, __FILE__, __LINE__, __func__, \ 76*4882a593Smuzhiyun #expr1 " = " #expr2, \ 77*4882a593Smuzhiyun "Expected \"%s\", got \"%s\"", val1, val2); \ 78*4882a593Smuzhiyun return CMD_RET_FAILURE; \ 79*4882a593Smuzhiyun } \ 80*4882a593Smuzhiyun } 81*4882a593Smuzhiyun 82*4882a593Smuzhiyun /* Assert that two pointers are equal */ 83*4882a593Smuzhiyun #define ut_asserteq_ptr(expr1, expr2) { \ 84*4882a593Smuzhiyun const void *val1 = (expr1), *val2 = (expr2); \ 85*4882a593Smuzhiyun \ 86*4882a593Smuzhiyun if (val1 != val2) { \ 87*4882a593Smuzhiyun ut_failf(uts, __FILE__, __LINE__, __func__, \ 88*4882a593Smuzhiyun #expr1 " = " #expr2, \ 89*4882a593Smuzhiyun "Expected %p, got %p", val1, val2); \ 90*4882a593Smuzhiyun return CMD_RET_FAILURE; \ 91*4882a593Smuzhiyun } \ 92*4882a593Smuzhiyun } 93*4882a593Smuzhiyun 94*4882a593Smuzhiyun /* Assert that a pointer is not NULL */ 95*4882a593Smuzhiyun #define ut_assertnonnull(expr) { \ 96*4882a593Smuzhiyun const void *val = (expr); \ 97*4882a593Smuzhiyun \ 98*4882a593Smuzhiyun if (val == NULL) { \ 99*4882a593Smuzhiyun ut_failf(uts, __FILE__, __LINE__, __func__, \ 100*4882a593Smuzhiyun #expr " = NULL", \ 101*4882a593Smuzhiyun "Expected non-null, got NULL"); \ 102*4882a593Smuzhiyun return CMD_RET_FAILURE; \ 103*4882a593Smuzhiyun } \ 104*4882a593Smuzhiyun } 105*4882a593Smuzhiyun 106*4882a593Smuzhiyun /* Assert that a pointer is not an error pointer */ 107*4882a593Smuzhiyun #define ut_assertok_ptr(expr) { \ 108*4882a593Smuzhiyun const void *val = (expr); \ 109*4882a593Smuzhiyun \ 110*4882a593Smuzhiyun if (IS_ERR(val)) { \ 111*4882a593Smuzhiyun ut_failf(uts, __FILE__, __LINE__, __func__, \ 112*4882a593Smuzhiyun #expr " = NULL", \ 113*4882a593Smuzhiyun "Expected pointer, got error %ld", \ 114*4882a593Smuzhiyun PTR_ERR(val)); \ 115*4882a593Smuzhiyun return CMD_RET_FAILURE; \ 116*4882a593Smuzhiyun } \ 117*4882a593Smuzhiyun } 118*4882a593Smuzhiyun 119*4882a593Smuzhiyun /* Assert that an operation succeeds (returns 0) */ 120*4882a593Smuzhiyun #define ut_assertok(cond) ut_asserteq(0, cond) 121*4882a593Smuzhiyun 122*4882a593Smuzhiyun #endif 123