1
2
3
4
5
6
7
8
9#ifndef __TEST_UT_H
10#define __TEST_UT_H
11
12#include <linux/err.h>
13
14struct unit_test_state;
15
16
17
18
19
20
21
22
23
24
25void ut_fail(struct unit_test_state *uts, const char *fname, int line,
26 const char *func, const char *cond);
27
28
29
30
31
32
33
34
35
36
37
38void ut_failf(struct unit_test_state *uts, const char *fname, int line,
39 const char *func, const char *cond, const char *fmt, ...)
40 __attribute__ ((format (__printf__, 6, 7)));
41
42
43
44#define ut_assert(cond) \
45 if (!(cond)) { \
46 ut_fail(uts, __FILE__, __LINE__, __func__, #cond); \
47 return CMD_RET_FAILURE; \
48 }
49
50
51#define ut_assertf(cond, fmt, args...) \
52 if (!(cond)) { \
53 ut_failf(uts, __FILE__, __LINE__, __func__, #cond, \
54 fmt, ##args); \
55 return CMD_RET_FAILURE; \
56 }
57
58
59#define ut_asserteq(expr1, expr2) { \
60 unsigned int val1 = (expr1), val2 = (expr2); \
61 \
62 if (val1 != val2) { \
63 ut_failf(uts, __FILE__, __LINE__, __func__, \
64 #expr1 " == " #expr2, \
65 "Expected %d, got %d", val1, val2); \
66 return CMD_RET_FAILURE; \
67 } \
68}
69
70
71#define ut_asserteq_str(expr1, expr2) { \
72 const char *val1 = (expr1), *val2 = (expr2); \
73 \
74 if (strcmp(val1, val2)) { \
75 ut_failf(uts, __FILE__, __LINE__, __func__, \
76 #expr1 " = " #expr2, \
77 "Expected \"%s\", got \"%s\"", val1, val2); \
78 return CMD_RET_FAILURE; \
79 } \
80}
81
82
83#define ut_asserteq_ptr(expr1, expr2) { \
84 const void *val1 = (expr1), *val2 = (expr2); \
85 \
86 if (val1 != val2) { \
87 ut_failf(uts, __FILE__, __LINE__, __func__, \
88 #expr1 " = " #expr2, \
89 "Expected %p, got %p", val1, val2); \
90 return CMD_RET_FAILURE; \
91 } \
92}
93
94
95#define ut_assertnonnull(expr) { \
96 const void *val = (expr); \
97 \
98 if (val == NULL) { \
99 ut_failf(uts, __FILE__, __LINE__, __func__, \
100 #expr " = NULL", \
101 "Expected non-null, got NULL"); \
102 return CMD_RET_FAILURE; \
103 } \
104}
105
106
107#define ut_assertok_ptr(expr) { \
108 const void *val = (expr); \
109 \
110 if (IS_ERR(val)) { \
111 ut_failf(uts, __FILE__, __LINE__, __func__, \
112 #expr " = NULL", \
113 "Expected pointer, got error %ld", \
114 PTR_ERR(val)); \
115 return CMD_RET_FAILURE; \
116 } \
117}
118
119
120#define ut_assertok(cond) ut_asserteq(0, cond)
121
122#endif
123