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