1/*
2 *  BCUnit - A Unit testing framework library for C.
3 *  Copyright (C) 2001       Anil Kumar
4 *  Copyright (C) 2004-2006  Anil Kumar, Jerry St.Clair
5 *
6 *  This library is free software; you can redistribute it and/or
7 *  modify it under the terms of the GNU Library General Public
8 *  License as published by the Free Software Foundation; either
9 *  version 2 of the License, or (at your option) any later version.
10 *
11 *  This library is distributed in the hope that it will be useful,
12 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 *  Library General Public License for more details.
15 *
16 *  You should have received a copy of the GNU Library General Public
17 *  License along with this library; if not, write to the Free Software
18 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21/*
22 *  ASSERT Macro definitions and general BCUnit configuration definitions.
23 *
24 *  09/Aug/2001   ASSERT definitions. (AK)
25 *
26 *  12/Mar/2003   New Assert definitions. (AK)
27 *
28 *  27/Jul/2003   Modified ASSERT_XXX Macro definitions. (AK)
29 *
30 *  15-Jul-2004   New interface, changed action on assert failure to not
31 *                return, provided _FATAL versions of assertions to return
32 *                from test function on failure. (JDS)
33 *
34 *  01-Sep-2004   Modified assertions for setjmp/longjmp mechanism of
35 *                aborting test runs, added CU_FAIL and CU_PASS macros. (JDS)
36 *
37 *  07-May-2005   Added CU_ prefix to remaining BCUnit defines (BOOL, TRUE,
38 *                FALSE, MAX_...).  Added CU_UNREFERENCED_PARAMETER() define. (JDS)
39 */
40
41/** @file
42 * Basic BCUnit include file for user and system code.
43 * Defines macros for assertions for use in user test cases.
44 * Basic system macro definitions also appear here.
45 */
46/** @addtogroup Framework
47 * @{
48 */
49
50#ifndef BCUNIT_BCUNIT_H_SEEN
51#define BCUNIT_BCUNIT_H_SEEN
52
53#include <string.h>
54#include <math.h>
55
56/** BCUnit version number. */
57#define CU_VERSION "@VERSION@-@RELEASE@"
58
59/*  Max string lengths for names (includes terminating NULL. */
60/** Maximum length of a test name string. */
61#define CU_MAX_TEST_NAME_LENGTH 256
62/** Maximim length of a suite name string. */
63#define CU_MAX_SUITE_NAME_LENGTH 256
64
65/* Global type Definitions to be used for boolean operators. */
66#ifndef CU_BOOL
67  /** Boolean type for BCUnit use. */
68  #define CU_BOOL int
69#endif
70
71#ifndef CU_TRUE
72  /** Boolean TRUE for BCUnit use. */
73  #define CU_TRUE 1
74#endif
75
76#ifndef CU_FALSE
77  /** Boolean FALSE for BCUnit use. */
78  #define CU_FALSE 0
79#endif
80
81#ifndef CU_UNREFERENCED_PARAMETER
82  /** Consistent approach to referencing unused parameters. */
83  #define CU_UNREFERENCED_PARAMETER(x) (void)x
84#endif
85
86#ifndef CU_MAX
87#  define CU_MAX(a,b) (((a) >= (b)) ? (a) : (b))
88#endif
89
90#ifndef CU_MIN
91#  define CU_MIN(a,b) (((a) >= (b)) ? (b) : (a))
92#endif
93
94#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) || defined(__WIN32__)
95#  ifdef CU_DLL
96#    ifdef CU_BUILD_DLL
97#      define CU_EXPORT __declspec(dllexport)
98#    else
99#      define CU_EXPORT __declspec(dllimport)
100#    endif
101#  else
102#    define CU_EXPORT
103#  endif
104#else
105#  define CU_EXPORT
106#endif  /* WIN32 */
107
108#include "CUError.h"
109#include "TestDB.h"   /* not needed here - included for user convenience */
110#include "TestRun.h"  /* not needed here - include (after BOOL define) for user convenience */
111
112#ifdef _MSC_VER
113#ifndef snprintf
114#define snprintf _snprintf
115#endif
116#endif
117
118/** Record a pass condition without performing a logical test. */
119#define CU_PASS(msg) \
120  { CU_assertImplementation(CU_TRUE, __LINE__, ("CU_PASS(" #msg ")"), __FILE__, "", CU_FALSE); }
121
122/** Simple assertion.
123 *  Reports failure with no other action.
124 */
125#define CU_ASSERT(value) \
126  { CU_assertImplementation((value), __LINE__, #value, __FILE__, "", CU_FALSE); }
127
128/** Simple assertion.
129 *  Reports failure and causes test to abort.
130 */
131#define CU_ASSERT_FATAL(value) \
132  { CU_assertImplementation((value), __LINE__, #value, __FILE__, "", CU_TRUE); }
133
134/** Simple assertion.
135 *  Reports failure with no other action.
136 */
137#define CU_TEST(value) \
138  { CU_assertImplementation((value), __LINE__, #value, __FILE__, "", CU_FALSE); }
139
140/** Simple assertion.
141 *  Reports failure and causes test to abort.
142 */
143#define CU_TEST_FATAL(value) \
144  { CU_assertImplementation((value), __LINE__, #value, __FILE__, "", CU_TRUE); }
145
146/** Record a failure without performing a logical test. */
147#define CU_FAIL(msg) \
148  { CU_assertImplementation(CU_FALSE, __LINE__, ("CU_FAIL(" #msg ")"), __FILE__, "", CU_FALSE); }
149
150/** Record a failure without performing a logical test, and abort test. */
151#define CU_FAIL_FATAL(msg) \
152  { CU_assertImplementation(CU_FALSE, __LINE__, ("CU_FAIL_FATAL(" #msg ")"), __FILE__, "", CU_TRUE); }
153
154/** Asserts that value is CU_TRUE.
155 *  Reports failure with no other action.
156 */
157#define CU_ASSERT_TRUE(value) \
158  { CU_assertImplementation((value), __LINE__, ("CU_ASSERT_TRUE(" #value ")"), __FILE__, "", CU_FALSE); }
159
160/** Asserts that value is CU_TRUE.
161 *  Reports failure and causes test to abort.
162 */
163#define CU_ASSERT_TRUE_FATAL(value) \
164  { CU_assertImplementation((value), __LINE__, ("CU_ASSERT_TRUE_FATAL(" #value ")"), __FILE__, "", CU_TRUE); }
165
166/** Asserts that value is CU_FALSE.
167 *  Reports failure with no other action.
168 */
169#define CU_ASSERT_FALSE(value) \
170  { CU_assertImplementation(!(value), __LINE__, ("CU_ASSERT_FALSE(" #value ")"), __FILE__, "", CU_FALSE); }
171
172/** Asserts that value is CU_FALSE.
173 *  Reports failure and causes test to abort.
174 */
175#define CU_ASSERT_FALSE_FATAL(value) \
176  { CU_assertImplementation(!(value), __LINE__, ("CU_ASSERT_FALSE_FATAL(" #value ")"), __FILE__, "", CU_TRUE); }
177
178/** Asserts that actual == expected.
179 *  Reports failure with no other action.
180 */
181#define CU_ASSERT_EQUAL(actual, expected) \
182  { CU_assertImplementation(((actual) == (expected)), __LINE__, ("CU_ASSERT_EQUAL(" #actual "," #expected ")"), __FILE__, "", CU_FALSE); }
183
184/** Asserts that actual == expected.
185 *  Reports failure and causes test to abort.
186 */
187#define CU_ASSERT_EQUAL_FATAL(actual, expected) \
188  { CU_assertImplementation(((actual) == (expected)), __LINE__, ("CU_ASSERT_EQUAL_FATAL(" #actual "," #expected ")"), __FILE__, "", CU_TRUE); }
189
190/** Asserts that actual != expected.
191 *  Reports failure with no other action.
192 */
193#define CU_ASSERT_NOT_EQUAL(actual, expected) \
194  { CU_assertImplementation(((actual) != (expected)), __LINE__, ("CU_ASSERT_NOT_EQUAL(" #actual "," #expected ")"), __FILE__, "", CU_FALSE); }
195
196/** Asserts that actual != expected.
197 *  Reports failure and causes test to abort.
198 */
199#define CU_ASSERT_NOT_EQUAL_FATAL(actual, expected) \
200  { CU_assertImplementation(((actual) != (expected)), __LINE__, ("CU_ASSERT_NOT_EQUAL_FATAL(" #actual "," #expected ")"), __FILE__, "", CU_TRUE); }
201
202/** Asserts that pointers actual == expected.
203 *  Reports failure with no other action.
204 */
205#define CU_ASSERT_PTR_EQUAL(actual, expected) \
206  { CU_assertImplementation(((const void*)(actual) == (const void*)(expected)), __LINE__, ("CU_ASSERT_PTR_EQUAL(" #actual "," #expected ")"), __FILE__, "", CU_FALSE); }
207
208/** Asserts that pointers actual == expected.
209 * Reports failure and causes test to abort.
210 */
211#define CU_ASSERT_PTR_EQUAL_FATAL(actual, expected) \
212  { CU_assertImplementation(((const void*)(actual) == (const void*)(expected)), __LINE__, ("CU_ASSERT_PTR_EQUAL_FATAL(" #actual "," #expected ")"), __FILE__, "", CU_TRUE); }
213
214/** Asserts that pointers actual != expected.
215 *  Reports failure with no other action.
216 */
217#define CU_ASSERT_PTR_NOT_EQUAL(actual, expected) \
218  { CU_assertImplementation(((const void*)(actual) != (const void*)(expected)), __LINE__, ("CU_ASSERT_PTR_NOT_EQUAL(" #actual "," #expected ")"), __FILE__, "", CU_FALSE); }
219
220/** Asserts that pointers actual != expected.
221 *  Reports failure and causes test to abort.
222 */
223#define CU_ASSERT_PTR_NOT_EQUAL_FATAL(actual, expected) \
224  { CU_assertImplementation(((const void*)(actual) != (const void*)(expected)), __LINE__, ("CU_ASSERT_PTR_NOT_EQUAL_FATAL(" #actual "," #expected ")"), __FILE__, "", CU_TRUE); }
225
226/** Asserts that pointer value is NULL.
227 *  Reports failure with no other action.
228 */
229#define CU_ASSERT_PTR_NULL(value) \
230  { CU_assertImplementation((NULL == (const void*)(value)), __LINE__, ("CU_ASSERT_PTR_NULL(" #value")"), __FILE__, "", CU_FALSE); }
231
232/** Asserts that pointer value is NULL.
233 *  Reports failure and causes test to abort.
234 */
235#define CU_ASSERT_PTR_NULL_FATAL(value) \
236  { CU_assertImplementation((NULL == (const void*)(value)), __LINE__, ("CU_ASSERT_PTR_NULL_FATAL(" #value")"), __FILE__, "", CU_TRUE); }
237
238/** Asserts that pointer value is not NULL.
239 *  Reports failure with no other action.
240 */
241#define CU_ASSERT_PTR_NOT_NULL(value) \
242  { CU_assertImplementation((NULL != (const void*)(value)), __LINE__, ("CU_ASSERT_PTR_NOT_NULL(" #value")"), __FILE__, "", CU_FALSE); }
243
244/** Asserts that pointer value is not NULL.
245 *  Reports failure and causes test to abort.
246 */
247#define CU_ASSERT_PTR_NOT_NULL_FATAL(value) \
248  { CU_assertImplementation((NULL != (const void*)(value)), __LINE__, ("CU_ASSERT_PTR_NOT_NULL_FATAL(" #value")"), __FILE__, "", CU_TRUE); }
249
250/** Asserts that string actual == expected.
251 *  Reports failure with no other action.
252 */
253#define CU_ASSERT_STRING_EQUAL(actual, expected) \
254  { CU_assertImplementation(!(strcmp((const char*)(actual), (const char*)(expected))), __LINE__, ("CU_ASSERT_STRING_EQUAL(" #actual ","  #expected ")"), __FILE__, "", CU_FALSE); }
255
256/** Asserts that string actual == expected.
257 *  Reports failure and causes test to abort.
258 */
259#define CU_ASSERT_STRING_EQUAL_FATAL(actual, expected) \
260  { CU_assertImplementation(!(strcmp((const char*)(actual), (const char*)(expected))), __LINE__, ("CU_ASSERT_STRING_EQUAL_FATAL(" #actual ","  #expected ")"), __FILE__, "", CU_TRUE); }
261
262/** Asserts that string actual != expected.
263 *  Reports failure with no other action.
264 */
265#define CU_ASSERT_STRING_NOT_EQUAL(actual, expected) \
266  { CU_assertImplementation((strcmp((const char*)(actual), (const char*)(expected))), __LINE__, ("CU_ASSERT_STRING_NOT_EQUAL(" #actual ","  #expected ")"), __FILE__, "", CU_FALSE); }
267
268/** Asserts that string actual != expected.
269 *  Reports failure and causes test to abort.
270 */
271#define CU_ASSERT_STRING_NOT_EQUAL_FATAL(actual, expected) \
272  { CU_assertImplementation((strcmp((const char*)(actual), (const char*)(expected))), __LINE__, ("CU_ASSERT_STRING_NOT_EQUAL_FATAL(" #actual ","  #expected ")"), __FILE__, "", CU_TRUE); }
273
274/** Asserts that string actual == expected with length specified.
275 *  The comparison is limited to count characters.
276 *  Reports failure with no other action.
277 */
278#define CU_ASSERT_NSTRING_EQUAL(actual, expected, count) \
279  { CU_assertImplementation(!(strncmp((const char*)(actual), (const char*)(expected), (size_t)(count))), __LINE__, ("CU_ASSERT_NSTRING_EQUAL(" #actual ","  #expected "," #count ")"), __FILE__, "", CU_FALSE); }
280
281/** Asserts that string actual == expected with length specified.
282 *  The comparison is limited to count characters.
283 *  Reports failure and causes test to abort.
284 */
285#define CU_ASSERT_NSTRING_EQUAL_FATAL(actual, expected, count) \
286  { CU_assertImplementation(!(strncmp((const char*)(actual), (const char*)(expected), (size_t)(count))), __LINE__, ("CU_ASSERT_NSTRING_EQUAL_FATAL(" #actual ","  #expected "," #count ")"), __FILE__, "", CU_TRUE); }
287
288/** Asserts that string actual != expected with length specified.
289 *  The comparison is limited to count characters.
290 *  Reports failure with no other action.
291 */
292#define CU_ASSERT_NSTRING_NOT_EQUAL(actual, expected, count) \
293  { CU_assertImplementation((strncmp((const char*)(actual), (const char*)(expected), (size_t)(count))), __LINE__, ("CU_ASSERT_NSTRING_NOT_EQUAL(" #actual ","  #expected "," #count ")"), __FILE__, "", CU_FALSE); }
294
295/** Asserts that string actual != expected with length specified.
296 *  The comparison is limited to count characters.
297 *  Reports failure and causes test to abort.
298 */
299#define CU_ASSERT_NSTRING_NOT_EQUAL_FATAL(actual, expected, count) \
300  { CU_assertImplementation((strncmp((const char*)(actual), (const char*)(expected), (size_t)(count))), __LINE__, ("CU_ASSERT_NSTRING_NOT_EQUAL_FATAL(" #actual ","  #expected "," #count ")"), __FILE__, "", CU_TRUE); }
301
302/** Asserts that double actual == expected within the specified tolerance.
303 *  If actual is within granularity of expected, the assertion passes.
304 *  Reports failure with no other action.
305 */
306#define CU_ASSERT_DOUBLE_EQUAL(actual, expected, granularity) \
307  { CU_assertImplementation(((fabs((double)(actual) - (expected)) <= fabs((double)(granularity)))), __LINE__, ("CU_ASSERT_DOUBLE_EQUAL(" #actual ","  #expected "," #granularity ")"), __FILE__, "", CU_FALSE); }
308
309/** Asserts that double actual == expected within the specified tolerance.
310 *  If actual is within granularity of expected, the assertion passes.
311 *  Reports failure and causes test to abort.
312 */
313#define CU_ASSERT_DOUBLE_EQUAL_FATAL(actual, expected, granularity) \
314  { CU_assertImplementation(((fabs((double)(actual) - (expected)) <= fabs((double)(granularity)))), __LINE__, ("CU_ASSERT_DOUBLE_EQUAL_FATAL(" #actual ","  #expected "," #granularity ")"), __FILE__, "", CU_TRUE); }
315
316/** Asserts that double actual != expected within the specified tolerance.
317 *  If actual is within granularity of expected, the assertion fails.
318 *  Reports failure with no other action.
319 */
320#define CU_ASSERT_DOUBLE_NOT_EQUAL(actual, expected, granularity) \
321  { CU_assertImplementation(((fabs((double)(actual) - (expected)) > fabs((double)(granularity)))), __LINE__, ("CU_ASSERT_DOUBLE_NOT_EQUAL(" #actual ","  #expected "," #granularity ")"), __FILE__, "", CU_FALSE); }
322
323/** Asserts that double actual != expected within the specified tolerance.
324 *  If actual is within granularity of expected, the assertion fails.
325 *  Reports failure and causes test to abort.
326 */
327#define CU_ASSERT_DOUBLE_NOT_EQUAL_FATAL(actual, expected, granularity) \
328  { CU_assertImplementation(((fabs((double)(actual) - (expected)) > fabs((double)(granularity)))), __LINE__, ("CU_ASSERT_DOUBLE_NOT_EQUAL_FATAL(" #actual ","  #expected "," #granularity ")"), __FILE__, "", CU_TRUE); }
329
330#ifdef USE_DEPRECATED_BCUNIT_NAMES
331
332#ifndef BOOL
333  /** Deprecated (version 2.0-2). @deprecated Use CU_BOOL. */
334  #define BOOL int
335#endif
336
337#ifndef TRUE
338  /** Deprecated (version 2.0-2). @deprecated Use CU_TRUE. */
339  #define TRUE 1
340#endif
341
342#ifndef FALSE
343  /** Deprecated (version 2.0-2). @deprecated Use CU_FALSE. */
344  #define FALSE	0
345#endif
346
347/** Deprecated (version 2.0-2). @deprecated Use CU_MAX_TEST_NAME_LENGTH. */
348#define MAX_TEST_NAME_LENGTH	256
349/** Deprecated (version 2.0-2). @deprecated Use CU_MAX_SUITE_NAME_LENGTH. */
350#define MAX_SUITE_NAME_LENGTH	256
351
352/** Deprecated (version 1). @deprecated Use CU_ASSERT_FATAL. */
353#define ASSERT(value) { if (FALSE == (int)(value)) { CU_assertImplementation((BOOL)value, __LINE__, #value, __FILE__, "", FALSE); return; }}
354/** Deprecated (version 1). @deprecated Use CU_ASSERT_TRUE_FATAL. */
355#define ASSERT_TRUE(value) { if (FALSE == (value)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_TRUE(" #value ")"), __FILE__, "", FALSE); return; }}
356/** Deprecated (version 1). @deprecated Use CU_ASSERT_FALSE_FATAL. */
357#define ASSERT_FALSE(value) { if (FALSE != (value)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_FALSE(" #value ")"), __FILE__, "", FALSE); return; }}
358/** Deprecated (version 1). @deprecated Use CU_ASSERT_EQUAL_FATAL. */
359#define ASSERT_EQUAL(actual, expected) { if ((actual) != (expected)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_EQUAL(" #actual "," #expected ")"), __FILE__, "", FALSE); return; }}
360/** Deprecated (version 1). @deprecated Use CU_ASSERT_NOT_EQUAL_FATAL. */
361#define ASSERT_NOT_EQUAL(actual, expected) { if ((void*)(actual) == (void*)(expected)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_NOT_EQUAL(" #actual "," #expected ")"), __FILE__, "", FALSE); return; }}
362/** Deprecated (version 1). @deprecated Use CU_ASSERT_PTR_EQUAL_FATAL. */
363#define ASSERT_PTR_EQUAL(actual, expected) { if ((void*)(actual) != (void*)(expected)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_PTR_EQUAL(" #actual "," #expected ")"), __FILE__, "", FALSE); return; }}
364/** Deprecated (version 1). @deprecated Use CU_ASSERT_PTR_NOT_EQUAL_FATAL. */
365#define ASSERT_PTR_NOT_EQUAL(actual, expected) { if ((void*)(actual) == (void*)(expected)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_PTR_NOT_EQUAL(" #actual "," #expected ")"), __FILE__, "", FALSE); return; }}
366/** Deprecated (version 1). @deprecated Use CU_ASSERT_PTR_NULL_FATAL. */
367#define ASSERT_PTR_NULL(value)  { if (NULL != (void*)(value)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_PTR_NULL(" #value")"), __FILE__, "", FALSE); return; }}
368/** Deprecated (version 1). @deprecated Use CU_ASSERT_PTR_NOT_NULL_FATAL. */
369#define ASSERT_PTR_NOT_NULL(value) { if (NULL == (void*)(value)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_PTR_NOT_NULL(" #value")"), __FILE__, "", FALSE); return; }}
370/** Deprecated (version 1). @deprecated Use CU_ASSERT_STRING_EQUAL_FATAL. */
371#define ASSERT_STRING_EQUAL(actual, expected) { if (strcmp((const char*)actual, (const char*)expected)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_STRING_EQUAL(" #actual ","  #expected ")"), __FILE__, "", FALSE); return; }}
372/** Deprecated (version 1). @deprecated Use CU_ASSERT_STRING_NOT_EQUAL_FATAL. */
373#define ASSERT_STRING_NOT_EQUAL(actual, expected) { if (!strcmp((const char*)actual, (const char*)expected)) { CU_assertImplementation(TRUE, __LINE__, ("ASSERT_STRING_NOT_EQUAL(" #actual ","  #expected ")"), __FILE__, "", FALSE); return; }}
374/** Deprecated (version 1). @deprecated Use CU_ASSERT_NSTRING_EQUAL_FATAL. */
375#define ASSERT_NSTRING_EQUAL(actual, expected, count) { if (strncmp((const char*)actual, (const char*)expected, (size_t)count)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_NSTRING_EQUAL(" #actual ","  #expected "," #count ")"), __FILE__, "", FALSE); return; }}
376/** Deprecated (version 1). @deprecated Use CU_ASSERT_NSTRING_NOT_EQUAL_FATAL. */
377#define ASSERT_NSTRING_NOT_EQUAL(actual, expected, count) { if (!strncmp((const char*)actual, (const char*)expected, (size_t)count)) { CU_assertImplementation(TRUE, __LINE__, ("ASSERT_NSTRING_NOT_EQUAL(" #actual ","  #expected "," #count ")"), __FILE__, "", FALSE); return; }}
378/** Deprecated (version 1). @deprecated Use CU_ASSERT_DOUBLE_EQUAL_FATAL. */
379#define ASSERT_DOUBLE_EQUAL(actual, expected, granularity) { if ((fabs((double)actual - expected) > fabs((double)granularity))) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_DOUBLE_EQUAL(" #actual ","  #expected "," #granularity ")"), __FILE__, "", FALSE); return; }}
380/** Deprecated (version 1). @deprecated Use CU_ASSERT_DOUBLE_NOT_EQUAL_FATAL. */
381#define ASSERT_DOUBLE_NOT_EQUAL(actual, expected, granularity) { if ((fabs((double)actual - expected) <= fabs((double)granularity))) { CU_assertImplementation(TRUE, __LINE__, ("ASSERT_DOUBLE_NOT_EQUAL(" #actual ","  #expected "," #granularity ")"), __FILE__, "", FALSE); return; }}
382#endif  /* USE_DEPRECATED_BCUNIT_NAMES */
383
384#endif  /*  BCUNIT_BCUNIT_H_SEEN  */
385
386/** @} */
387