blob: 59b23a25a4de35d3c173d461bd7c7719b83b4f6f [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glassb2c1cac2014-02-26 15:59:21 -07002/*
Joe Hershberger3a77be52015-05-20 14:27:27 -05003 * Simple unit test library
Simon Glassb2c1cac2014-02-26 15:59:21 -07004 *
5 * Copyright (c) 2013 Google, Inc
Simon Glassb2c1cac2014-02-26 15:59:21 -07006 */
7
Joe Hershberger3a77be52015-05-20 14:27:27 -05008#ifndef __TEST_UT_H
9#define __TEST_UT_H
Simon Glassb2c1cac2014-02-26 15:59:21 -070010
Simon Glass7e1cebf2015-07-06 12:54:37 -060011#include <linux/err.h>
12
Joe Hershberger3a77be52015-05-20 14:27:27 -050013struct unit_test_state;
Simon Glassb2c1cac2014-02-26 15:59:21 -070014
15/**
16 * ut_fail() - Record failure of a unit test
17 *
Joe Hershberger3a77be52015-05-20 14:27:27 -050018 * @uts: Test state
Vagrant Cascadianedfdb992016-04-30 19:18:00 -070019 * @fname: Filename where the error occurred
20 * @line: Line number where the error occurred
21 * @func: Function name where the error occurred
Simon Glassb2c1cac2014-02-26 15:59:21 -070022 * @cond: The condition that failed
23 */
Joe Hershberger3a77be52015-05-20 14:27:27 -050024void ut_fail(struct unit_test_state *uts, const char *fname, int line,
Simon Glassb2c1cac2014-02-26 15:59:21 -070025 const char *func, const char *cond);
26
27/**
28 * ut_failf() - Record failure of a unit test
29 *
Joe Hershberger3a77be52015-05-20 14:27:27 -050030 * @uts: Test state
Vagrant Cascadianedfdb992016-04-30 19:18:00 -070031 * @fname: Filename where the error occurred
32 * @line: Line number where the error occurred
33 * @func: Function name where the error occurred
Simon Glassb2c1cac2014-02-26 15:59:21 -070034 * @cond: The condition that failed
35 * @fmt: printf() format string for the error, followed by args
36 */
Joe Hershberger3a77be52015-05-20 14:27:27 -050037void ut_failf(struct unit_test_state *uts, const char *fname, int line,
Simon Glassb2c1cac2014-02-26 15:59:21 -070038 const char *func, const char *cond, const char *fmt, ...)
39 __attribute__ ((format (__printf__, 6, 7)));
40
41
42/* Assert that a condition is non-zero */
43#define ut_assert(cond) \
44 if (!(cond)) { \
Joe Hershberger3a77be52015-05-20 14:27:27 -050045 ut_fail(uts, __FILE__, __LINE__, __func__, #cond); \
Joe Hershberger436cfc72015-05-20 14:27:34 -050046 return CMD_RET_FAILURE; \
Simon Glassb2c1cac2014-02-26 15:59:21 -070047 }
48
49/* Assert that a condition is non-zero, with printf() string */
50#define ut_assertf(cond, fmt, args...) \
51 if (!(cond)) { \
Joe Hershberger3a77be52015-05-20 14:27:27 -050052 ut_failf(uts, __FILE__, __LINE__, __func__, #cond, \
Simon Glassb2c1cac2014-02-26 15:59:21 -070053 fmt, ##args); \
Joe Hershberger436cfc72015-05-20 14:27:34 -050054 return CMD_RET_FAILURE; \
Simon Glassb2c1cac2014-02-26 15:59:21 -070055 }
56
57/* Assert that two int expressions are equal */
58#define ut_asserteq(expr1, expr2) { \
59 unsigned int val1 = (expr1), val2 = (expr2); \
60 \
61 if (val1 != val2) { \
Joe Hershberger3a77be52015-05-20 14:27:27 -050062 ut_failf(uts, __FILE__, __LINE__, __func__, \
Simon Glassb2c1cac2014-02-26 15:59:21 -070063 #expr1 " == " #expr2, \
64 "Expected %d, got %d", val1, val2); \
Joe Hershberger436cfc72015-05-20 14:27:34 -050065 return CMD_RET_FAILURE; \
Simon Glassb2c1cac2014-02-26 15:59:21 -070066 } \
67}
68
69/* Assert that two string expressions are equal */
70#define ut_asserteq_str(expr1, expr2) { \
71 const char *val1 = (expr1), *val2 = (expr2); \
72 \
73 if (strcmp(val1, val2)) { \
Joe Hershberger3a77be52015-05-20 14:27:27 -050074 ut_failf(uts, __FILE__, __LINE__, __func__, \
Simon Glassb2c1cac2014-02-26 15:59:21 -070075 #expr1 " = " #expr2, \
76 "Expected \"%s\", got \"%s\"", val1, val2); \
Joe Hershberger436cfc72015-05-20 14:27:34 -050077 return CMD_RET_FAILURE; \
Simon Glassb2c1cac2014-02-26 15:59:21 -070078 } \
79}
80
81/* Assert that two pointers are equal */
82#define ut_asserteq_ptr(expr1, expr2) { \
83 const void *val1 = (expr1), *val2 = (expr2); \
84 \
85 if (val1 != val2) { \
Joe Hershberger3a77be52015-05-20 14:27:27 -050086 ut_failf(uts, __FILE__, __LINE__, __func__, \
Simon Glassb2c1cac2014-02-26 15:59:21 -070087 #expr1 " = " #expr2, \
88 "Expected %p, got %p", val1, val2); \
Joe Hershberger436cfc72015-05-20 14:27:34 -050089 return CMD_RET_FAILURE; \
Simon Glassb2c1cac2014-02-26 15:59:21 -070090 } \
91}
92
Simon Glass7df766e2014-12-10 08:55:55 -070093/* Assert that a pointer is not NULL */
94#define ut_assertnonnull(expr) { \
95 const void *val = (expr); \
96 \
97 if (val == NULL) { \
Joe Hershberger3a77be52015-05-20 14:27:27 -050098 ut_failf(uts, __FILE__, __LINE__, __func__, \
Simon Glass7df766e2014-12-10 08:55:55 -070099 #expr " = NULL", \
100 "Expected non-null, got NULL"); \
Joe Hershberger436cfc72015-05-20 14:27:34 -0500101 return CMD_RET_FAILURE; \
Simon Glass7df766e2014-12-10 08:55:55 -0700102 } \
103}
104
Simon Glass7e1cebf2015-07-06 12:54:37 -0600105/* Assert that a pointer is not an error pointer */
Simon Glassd21afd52017-05-18 20:10:00 -0600106#define ut_assertok_ptr(expr) { \
Simon Glass7e1cebf2015-07-06 12:54:37 -0600107 const void *val = (expr); \
108 \
109 if (IS_ERR(val)) { \
110 ut_failf(uts, __FILE__, __LINE__, __func__, \
111 #expr " = NULL", \
112 "Expected pointer, got error %ld", \
113 PTR_ERR(val)); \
114 return CMD_RET_FAILURE; \
115 } \
116}
117
Simon Glassb2c1cac2014-02-26 15:59:21 -0700118/* Assert that an operation succeeds (returns 0) */
119#define ut_assertok(cond) ut_asserteq(0, cond)
120
121#endif