-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemunit_assertions.h
More file actions
292 lines (273 loc) · 10.6 KB
/
emunit_assertions.h
File metadata and controls
292 lines (273 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#ifndef EMUNIT_ASSERTIONS_H_INCLUDED
#define EMUNIT_ASSERTIONS_H_INCLUDED
/* EMUnit embedded unit test engine - Copyright (C) 2017 Radoslaw Koppel
*
* This program is free software: you can redistribute it and/or modify
* in under the terms of the GNU General Public license (version 3)
* as published by the Free Software Foundation AND MODIFIED BY the
* EMUnit exception.
*
* NOTE: The exception was added to the GPL to ensure
* that you can test any kind of software without being
* obligated to release the whole source code under the terms of GPL.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* and the EMUnit license exception along with this program.
* If not, it can be viewed in the original EMunit repository:
* <https://github.com/rkel/emunit>.
*/
#include "emunit_macros.h"
#include "emunit_types.h"
#include "emunit_port.h"
#include "emunit_assertions_delta.h"
#include "emunit_assertions_equal.h"
#include "emunit_assertions_range.h"
/**
* @file
* @brief EMUnit header with assertions
* @author Radosław Koppel <r.koppel\@k-el.com>
* @date 2017
*
* @sa emunit_assertions_group
*/
/**
* @defgroup emunit_assertions_group <emunit_assertions> EMUnit defined assertions
*
* Predefined assertion macros.
* This macros should be used directly in tests.
* @{
*/
/**
* @defgroup emunit_assertions_int_group <emunit_assertions_int> Internal assertion macros
*
* Internal macros that should not be used directly in the tests.
* This macros are used to construct usable assertion macros.
* @{
*/
/**
* @brief The auxiliary macro to call assertion function
*
* This macro creates assertion header, and passes it as a first
* argument to called assertion function.
*
* @param[in] nt Numeric type
* @param[in] func Assertion function
* @param[in] params Arguments for the assertion function in brackets
*/
#define EMUNIT_CALL_ASSERT(nt, func, params) \
do{ \
static const __flash char emunit_ca_file[] = __FILE__; \
static const __flash emunit_assert_head_t \
emunit_ca_head = { \
.p_file = emunit_ca_file, \
.line = __LINE__, \
.numtype = nt \
}; \
func(&emunit_ca_head, EMUNIT_DEBRACKET(params)); \
}while(0)
/**
* @brief The auxiliary macro to call assertion function with message
*
* This macro creates assertion header, and passes it as a first
* argument to called assertion function.
*
* @param[in] nt Numeric type
* @param[in] func Assertion function
* @param[in] params Arguments for the assertion function in brackets
* @param[in] fmt
* @param[in] ... Message format string followed by message parameters
*/
#define EMUNIT_CALL_ASSERT_MSG(nt, func, params, ...) \
do{ \
static const __flash char emunit_ca_file[] = __FILE__; \
static const __flash char emunit_ca_msg[] = EMUNIT_ARG1(__VA_ARGS__); \
static const __flash emunit_assert_head_t \
emunit_ca_head = { \
.p_file = emunit_ca_file, \
.line = __LINE__, \
.numtype = nt \
}; \
EMUNIT_IF_ARGCNT1((__VA_ARGS__), \
func ## _msg( \
&emunit_ca_head, \
EMUNIT_DEBRACKET(params), \
emunit_ca_msg \
) \
, \
func ## _msg( \
&emunit_ca_head, \
EMUNIT_DEBRACKET(params), \
emunit_ca_msg, \
EMUNIT_ARG_AFTER1(__VA_ARGS__) \
) \
); \
}while(0)
/** @} <!-- emunit_assertions_int_group --> */
/**
* @defgroup emunit_assertions_bool_group <emunit_assertions_bool> Boolean assertions
* @{
*
* Base assertions that operate on true and false values.
*/
/**
* @brief Base assertion
*
* This assertions expects the result of expression to be true.
* In the case of the error it usually prints the informations about the expression itself
* (but this may depend on the display port).
*
* @param exp The expression
*/
#define UT_ASSERT( exp) EMUNIT_CALL_ASSERT(EMUNIT_NUMTYPE_BOOL, ut_assert, (EMUNIT_FLASHSTR(#exp), (exp)))
/**
* @brief Expect true
*
* Functionally very similar to the @ref UT_ASSERT.
* But its printing behaviour is designed to be a candy for UT_ASSERT_EQUAL_BOOL,
* so it does not print the infromation about the expression but rather something like:
*
@verbatim
Expected: TRUE
Actual: FALSE
@endverbatim
*
* @param val The value to be checked
*/
#define UT_ASSERT_TRUE( val) UT_ASSERT_EQUAL_BOOL(true, (val))
/**
* @brief Expect false
*
* Assertion that expects the value to be false.
* Technically it is a candy for @ref UT_ASSERT_EQUAL_BOOL.
*
* @param val The value to be checked
*/
#define UT_ASSERT_FALSE( val) UT_ASSERT_EQUAL_BOOL(false, (val))
/**
* @brief Expect the pointer to be NULL
*
* Assertion that expects the pointer to be NULL.
* It is constructed that way that if it fails, the actual value of the pointer
* is displayed.
*
* @param ptr The pointer to be checked
*/
#define UT_ASSERT_NULL( ptr) EMUNIT_CN2(UT_ASSERT_EQUAL_HEX, EMUNIT_PTR_SIZE)((emunit_uintptr_t)NULL, (emunit_uintptr_t)(ptr))
/**
* @brief Expect the pointer not to be NULL
*
* Assertion that expects the pointer not to be NULL.
* If it fails it does not have to display the actual pointer value - it is obviously NULL.
*
* @param ptr The pointer to be checked
*/
#define UT_ASSERT_NOT_NULL(ptr) EMUNIT_CALL_ASSERT(EMUNIT_NUMTYPE_BOOL, ut_assert, (EMUNIT_FLASHSTR("(" #ptr ") != NULL"), ((ptr) != NULL)))
/**
* @brief Base assertion with message
*
* The message version of @ref UT_ASSERT.
*
* @param exp Expression to be checked
* @param ... Format string followed by the format values.
* Standard @c printf format is used.
*/
#define UT_ASSERT_MSG( exp, ...) EMUNIT_CALL_ASSERT_MSG(EMUNIT_NUMTYPE_BOOL, ut_assert, (EMUNIT_FLASHSTR(#exp), (exp)), __VA_ARGS__)
/**
* @brief Expect true with message
*
* The message version of @ref UT_ASSERT_TRUE.
*
* @param val The value to be checked
* @param ... Format string followed by the format values.
* Standard @c printf format is used.
*/
#define UT_ASSERT_TRUE_MSG( val, ...) UT_ASSERT_EQUAL_BOOL_MSG(true, (val), __VA_ARGS__)
/**
* @brief Expect false with message
*
* The message version of @ref UT_ASSERT_FALSE.
*
* @param val The value to be checked
* @param ... Format string followed by the format values.
* Standard @c printf format is used.
*/
#define UT_ASSERT_FALSE_MSG( val, ...) UT_ASSERT_EQUAL_BOOL_MSG(false, (val), __VA_ARGS__)
/**
* @brief Expect the pointer to be NULL with message
*
* The message version of @ref UT_ASSERT_NULL.
*
* @param ptr The pointer to be checked
* @param ... Format string followed by the format values.
* Standard @c printf format is used.
*/
#define UT_ASSERT_NULL_MSG( ptr, ...) EMUNIT_CN3(UT_ASSERT_EQUAL_HEX, EMUNIT_PTR_SIZE, _MSG)((emunit_uintptr_t)NULL, (emunit_uintptr_t)(ptr), __VA_ARGS__)
/**
* @brief Expect the pointer not to be NULL with message
*
* The message version of @ref UT_ASSERT_NOT_NULL.
*
* @param ptr The pointer to be checked
* @param ... Format string followed by the format values.
* Standard @c printf format is used.
*/
#define UT_ASSERT_NOT_NULL_MSG(ptr, ...) EMUNIT_CALL_ASSERT_MSG(EMUNIT_NUMTYPE_BOOL, ut_assert, (EMUNIT_FLASHSTR("(" #ptr ") != NULL"), ((ptr) != NULL)), __VA_ARGS__)
/** @} <!-- emunit_assertions_bool_group --> */
/**
* @defgroup emunit_assertions_str_group <emunit_assertions_str> String assertions
* @{
*
* Assertions that operates on strings.
*/
/**
* @brief Expect two strings to be the same
*
* This function checks the value of the strings.
* Strings may be placed in any memory (RAM, FLASH).
*
* @param e Expected string
* @param a Actual string
*/
#define UT_ASSERT_EQUAL_STR( e, a) UT_ASSERT_EQUAL_NSTR(EMUNIT_STRLEN_MAX, e, a)
/**
* @brief Expect two strings to be the same with message
*
* The message version of @ref UT_ASSERT_EQUAL_STR.
*
* @param e Expected string
* @param a Actual string
* @param ... Format string followed by the format values.
* Standard @c printf format is used.
*/
#define UT_ASSERT_EQUAL_STR_MSG(e, a, ...) UT_ASSERT_EQUAL_NSTR_MSG(EMUNIT_STRLEN_MAX, e, a, __VA_ARGS__)
/**
* @brief Expect up to N characters of two strings to be the same
*
* This function checks the value of the strings.
* Strings may be placed in any memory (RAM, FLASH).
*
* @param n Maximum number of characters to compare
* @param e Expected string
* @param a Actual string
*/
#define UT_ASSERT_EQUAL_NSTR( n, e, a) EMUNIT_CALL_ASSERT(EMUNIT_NUMTYPE_STR, ut_assert_nstr, (n, e, a))
/**
* @brief Expect up to N characters of two strings to be the same
*
* The message version of @ref UT_ASSERT_EQUAL_NSTR.
*
* @param n Maximum number of characters to compare
* @param e Expected string
* @param a Actual string
* @param ... Format string followed by the format values.
* Standard @c printf format is used.
*/
#define UT_ASSERT_EQUAL_NSTR_MSG(n, e, a, ...) EMUNIT_CALL_ASSERT_MSG(EMUNIT_NUMTYPE_STR, ut_assert_nstr, (n, e, a), __VA_ARGS__)
/** @} <!-- emunit_assertions_str_group --> */
/** @} <!-- emunit_assertions_group --> */
#endif /* EMUNIT_ASSERTIONS_H_INCLUDED */