]> Git Repo - J-u-boot.git/blame - include/test/ut.h
dm: devres: Create a new devres header file
[J-u-boot.git] / include / test / ut.h
CommitLineData
83d290c5 1/* SPDX-License-Identifier: GPL-2.0+ */
2e7d35d2 2/*
e721b882 3 * Simple unit test library
2e7d35d2
SG
4 *
5 * Copyright (c) 2013 Google, Inc
2e7d35d2
SG
6 */
7
e721b882
JH
8#ifndef __TEST_UT_H
9#define __TEST_UT_H
2e7d35d2 10
85aeda4a
SG
11#include <linux/err.h>
12
e721b882 13struct unit_test_state;
2e7d35d2
SG
14
15/**
16 * ut_fail() - Record failure of a unit test
17 *
e721b882 18 * @uts: Test state
eae4b2b6
VC
19 * @fname: Filename where the error occurred
20 * @line: Line number where the error occurred
21 * @func: Function name where the error occurred
2e7d35d2
SG
22 * @cond: The condition that failed
23 */
e721b882 24void ut_fail(struct unit_test_state *uts, const char *fname, int line,
2e7d35d2
SG
25 const char *func, const char *cond);
26
27/**
28 * ut_failf() - Record failure of a unit test
29 *
e721b882 30 * @uts: Test state
eae4b2b6
VC
31 * @fname: Filename where the error occurred
32 * @line: Line number where the error occurred
33 * @func: Function name where the error occurred
2e7d35d2
SG
34 * @cond: The condition that failed
35 * @fmt: printf() format string for the error, followed by args
36 */
e721b882 37void ut_failf(struct unit_test_state *uts, const char *fname, int line,
2e7d35d2
SG
38 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)) { \
e721b882 45 ut_fail(uts, __FILE__, __LINE__, __func__, #cond); \
fe3f6a65 46 return CMD_RET_FAILURE; \
2e7d35d2
SG
47 }
48
49/* Assert that a condition is non-zero, with printf() string */
50#define ut_assertf(cond, fmt, args...) \
51 if (!(cond)) { \
e721b882 52 ut_failf(uts, __FILE__, __LINE__, __func__, #cond, \
2e7d35d2 53 fmt, ##args); \
fe3f6a65 54 return CMD_RET_FAILURE; \
2e7d35d2
SG
55 }
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) { \
e721b882 62 ut_failf(uts, __FILE__, __LINE__, __func__, \
2e7d35d2 63 #expr1 " == " #expr2, \
91a8c956
SG
64 "Expected %#x (%d), got %#x (%d)", val1, val1, \
65 val2, val2); \
fe3f6a65 66 return CMD_RET_FAILURE; \
2e7d35d2
SG
67 } \
68}
69
70/* Assert that two string expressions are equal */
71#define ut_asserteq_str(expr1, expr2) { \
72 const char *val1 = (expr1), *val2 = (expr2); \
73 \
74 if (strcmp(val1, val2)) { \
e721b882 75 ut_failf(uts, __FILE__, __LINE__, __func__, \
2e7d35d2
SG
76 #expr1 " = " #expr2, \
77 "Expected \"%s\", got \"%s\"", val1, val2); \
fe3f6a65 78 return CMD_RET_FAILURE; \
2e7d35d2
SG
79 } \
80}
81
41f67e3b
MS
82/* Assert that two memory areas are equal */
83#define ut_asserteq_mem(expr1, expr2, len) { \
84 const u8 *val1 = (u8 *)(expr1), *val2 = (u8 *)(expr2); \
85 const uint __len = len; \
86 \
87 if (memcmp(val1, val2, __len)) { \
88 char __buf1[64 + 1] = "\0"; \
89 char __buf2[64 + 1] = "\0"; \
90 bin2hex(__buf1, val1, min(__len, (uint)32)); \
91 bin2hex(__buf2, val2, min(__len, (uint)32)); \
92 ut_failf(uts, __FILE__, __LINE__, __func__, \
93 #expr1 " = " #expr2, \
94 "Expected \"%s\", got \"%s\"", \
95 __buf1, __buf2); \
96 return CMD_RET_FAILURE; \
97 } \
98}
99
2e7d35d2
SG
100/* Assert that two pointers are equal */
101#define ut_asserteq_ptr(expr1, expr2) { \
102 const void *val1 = (expr1), *val2 = (expr2); \
103 \
104 if (val1 != val2) { \
e721b882 105 ut_failf(uts, __FILE__, __LINE__, __func__, \
2e7d35d2
SG
106 #expr1 " = " #expr2, \
107 "Expected %p, got %p", val1, val2); \
fe3f6a65 108 return CMD_RET_FAILURE; \
2e7d35d2
SG
109 } \
110}
111
8d545790
RF
112/* Assert that a pointer is NULL */
113#define ut_assertnull(expr) { \
114 const void *val = (expr); \
115 \
116 if (val != NULL) { \
117 ut_failf(uts, __FILE__, __LINE__, __func__, \
118 #expr " != NULL", \
119 "Expected NULL, got %p", val); \
120 return CMD_RET_FAILURE; \
121 } \
122}
123
ecc2ed55
SG
124/* Assert that a pointer is not NULL */
125#define ut_assertnonnull(expr) { \
126 const void *val = (expr); \
127 \
128 if (val == NULL) { \
e721b882 129 ut_failf(uts, __FILE__, __LINE__, __func__, \
ecc2ed55
SG
130 #expr " = NULL", \
131 "Expected non-null, got NULL"); \
fe3f6a65 132 return CMD_RET_FAILURE; \
ecc2ed55
SG
133 } \
134}
135
85aeda4a 136/* Assert that a pointer is not an error pointer */
fe3c0b5b 137#define ut_assertok_ptr(expr) { \
85aeda4a
SG
138 const void *val = (expr); \
139 \
140 if (IS_ERR(val)) { \
141 ut_failf(uts, __FILE__, __LINE__, __func__, \
142 #expr " = NULL", \
143 "Expected pointer, got error %ld", \
144 PTR_ERR(val)); \
145 return CMD_RET_FAILURE; \
146 } \
147}
148
2e7d35d2
SG
149/* Assert that an operation succeeds (returns 0) */
150#define ut_assertok(cond) ut_asserteq(0, cond)
151
152#endif
This page took 0.519741 seconds and 4 git commands to generate.