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