1/* 2 * CUnit - 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 CUnit 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 CUnit defines (BOOL, TRUE, 38 * FALSE, MAX_...). Added CU_UNREFERENCED_PARAMETER() define. (JDS) 39 */ 40 41/** @file 42 * Basic CUnit 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 CUNIT_CUNIT_H_SEEN 51#define CUNIT_CUNIT_H_SEEN 52 53#include <string.h> 54#include <math.h> 55 56/** CUnit 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 CUnit use. */ 68 #define CU_BOOL int 69#endif 70 71#ifndef CU_TRUE 72 /** Boolean TRUE for CUnit use. */ 73 #define CU_TRUE 1 74#endif 75 76#ifndef CU_FALSE 77 /** Boolean FALSE for CUnit 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# ifdef _MSC_VER 105# define snprintf _snprintf 106# endif 107#else 108# define CU_EXPORT 109#endif /* WIN32 */ 110 111#include "CUError.h" 112#include "TestDB.h" /* not needed here - included for user convenience */ 113#include "TestRun.h" /* not needed here - include (after BOOL define) for user convenience */ 114 115/** Record a pass condition without performing a logical test. */ 116#define CU_PASS(msg) \ 117 { CU_assertImplementation(CU_TRUE, __LINE__, ("CU_PASS(" #msg ")"), __FILE__, "", CU_FALSE); } 118 119/** Simple assertion. 120 * Reports failure with no other action. 121 */ 122#define CU_ASSERT(value) \ 123 { CU_assertImplementation((value), __LINE__, #value, __FILE__, "", CU_FALSE); } 124 125/** Simple assertion. 126 * Reports failure and causes test to abort. 127 */ 128#define CU_ASSERT_FATAL(value) \ 129 { CU_assertImplementation((value), __LINE__, #value, __FILE__, "", CU_TRUE); } 130 131/** Simple assertion. 132 * Reports failure with no other action. 133 */ 134#define CU_TEST(value) \ 135 { CU_assertImplementation((value), __LINE__, #value, __FILE__, "", CU_FALSE); } 136 137/** Simple assertion. 138 * Reports failure and causes test to abort. 139 */ 140#define CU_TEST_FATAL(value) \ 141 { CU_assertImplementation((value), __LINE__, #value, __FILE__, "", CU_TRUE); } 142 143/** Record a failure without performing a logical test. */ 144#define CU_FAIL(msg) \ 145 { CU_assertImplementation(CU_FALSE, __LINE__, ("CU_FAIL(" #msg ")"), __FILE__, "", CU_FALSE); } 146 147/** Record a failure without performing a logical test, and abort test. */ 148#define CU_FAIL_FATAL(msg) \ 149 { CU_assertImplementation(CU_FALSE, __LINE__, ("CU_FAIL_FATAL(" #msg ")"), __FILE__, "", CU_TRUE); } 150 151/** Asserts that value is CU_TRUE. 152 * Reports failure with no other action. 153 */ 154#define CU_ASSERT_TRUE(value) \ 155 { CU_assertImplementation((value), __LINE__, ("CU_ASSERT_TRUE(" #value ")"), __FILE__, "", CU_FALSE); } 156 157/** Asserts that value is CU_TRUE. 158 * Reports failure and causes test to abort. 159 */ 160#define CU_ASSERT_TRUE_FATAL(value) \ 161 { CU_assertImplementation((value), __LINE__, ("CU_ASSERT_TRUE_FATAL(" #value ")"), __FILE__, "", CU_TRUE); } 162 163/** Asserts that value is CU_FALSE. 164 * Reports failure with no other action. 165 */ 166#define CU_ASSERT_FALSE(value) \ 167 { CU_assertImplementation(!(value), __LINE__, ("CU_ASSERT_FALSE(" #value ")"), __FILE__, "", CU_FALSE); } 168 169/** Asserts that value is CU_FALSE. 170 * Reports failure and causes test to abort. 171 */ 172#define CU_ASSERT_FALSE_FATAL(value) \ 173 { CU_assertImplementation(!(value), __LINE__, ("CU_ASSERT_FALSE_FATAL(" #value ")"), __FILE__, "", CU_TRUE); } 174 175/** Asserts that actual == expected. 176 * Reports failure with no other action. 177 */ 178#define CU_ASSERT_EQUAL(actual, expected) \ 179 { CU_assertImplementation(((actual) == (expected)), __LINE__, ("CU_ASSERT_EQUAL(" #actual "," #expected ")"), __FILE__, "", CU_FALSE); } 180 181/** Asserts that actual == expected. 182 * Reports failure and causes test to abort. 183 */ 184#define CU_ASSERT_EQUAL_FATAL(actual, expected) \ 185 { CU_assertImplementation(((actual) == (expected)), __LINE__, ("CU_ASSERT_EQUAL_FATAL(" #actual "," #expected ")"), __FILE__, "", CU_TRUE); } 186 187/** Asserts that actual != expected. 188 * Reports failure with no other action. 189 */ 190#define CU_ASSERT_NOT_EQUAL(actual, expected) \ 191 { CU_assertImplementation(((actual) != (expected)), __LINE__, ("CU_ASSERT_NOT_EQUAL(" #actual "," #expected ")"), __FILE__, "", CU_FALSE); } 192 193/** Asserts that actual != expected. 194 * Reports failure and causes test to abort. 195 */ 196#define CU_ASSERT_NOT_EQUAL_FATAL(actual, expected) \ 197 { CU_assertImplementation(((actual) != (expected)), __LINE__, ("CU_ASSERT_NOT_EQUAL_FATAL(" #actual "," #expected ")"), __FILE__, "", CU_TRUE); } 198 199/** Asserts that pointers actual == expected. 200 * Reports failure with no other action. 201 */ 202#define CU_ASSERT_PTR_EQUAL(actual, expected) \ 203 { CU_assertImplementation(((const void*)(actual) == (const void*)(expected)), __LINE__, ("CU_ASSERT_PTR_EQUAL(" #actual "," #expected ")"), __FILE__, "", CU_FALSE); } 204 205/** Asserts that pointers actual == expected. 206 * Reports failure and causes test to abort. 207 */ 208#define CU_ASSERT_PTR_EQUAL_FATAL(actual, expected) \ 209 { CU_assertImplementation(((const void*)(actual) == (const void*)(expected)), __LINE__, ("CU_ASSERT_PTR_EQUAL_FATAL(" #actual "," #expected ")"), __FILE__, "", CU_TRUE); } 210 211/** Asserts that pointers actual != expected. 212 * Reports failure with no other action. 213 */ 214#define CU_ASSERT_PTR_NOT_EQUAL(actual, expected) \ 215 { CU_assertImplementation(((const void*)(actual) != (const void*)(expected)), __LINE__, ("CU_ASSERT_PTR_NOT_EQUAL(" #actual "," #expected ")"), __FILE__, "", CU_FALSE); } 216 217/** Asserts that pointers actual != expected. 218 * Reports failure and causes test to abort. 219 */ 220#define CU_ASSERT_PTR_NOT_EQUAL_FATAL(actual, expected) \ 221 { CU_assertImplementation(((const void*)(actual) != (const void*)(expected)), __LINE__, ("CU_ASSERT_PTR_NOT_EQUAL_FATAL(" #actual "," #expected ")"), __FILE__, "", CU_TRUE); } 222 223/** Asserts that pointer value is NULL. 224 * Reports failure with no other action. 225 */ 226#define CU_ASSERT_PTR_NULL(value) \ 227 { CU_assertImplementation((NULL == (const void*)(value)), __LINE__, ("CU_ASSERT_PTR_NULL(" #value")"), __FILE__, "", CU_FALSE); } 228 229/** Asserts that pointer value is NULL. 230 * Reports failure and causes test to abort. 231 */ 232#define CU_ASSERT_PTR_NULL_FATAL(value) \ 233 { CU_assertImplementation((NULL == (const void*)(value)), __LINE__, ("CU_ASSERT_PTR_NULL_FATAL(" #value")"), __FILE__, "", CU_TRUE); } 234 235/** Asserts that pointer value is not NULL. 236 * Reports failure with no other action. 237 */ 238#define CU_ASSERT_PTR_NOT_NULL(value) \ 239 { CU_assertImplementation((NULL != (const void*)(value)), __LINE__, ("CU_ASSERT_PTR_NOT_NULL(" #value")"), __FILE__, "", CU_FALSE); } 240 241/** Asserts that pointer value is not NULL. 242 * Reports failure and causes test to abort. 243 */ 244#define CU_ASSERT_PTR_NOT_NULL_FATAL(value) \ 245 { CU_assertImplementation((NULL != (const void*)(value)), __LINE__, ("CU_ASSERT_PTR_NOT_NULL_FATAL(" #value")"), __FILE__, "", CU_TRUE); } 246 247/** Asserts that string actual == expected. 248 * Reports failure with no other action. 249 */ 250#define CU_ASSERT_STRING_EQUAL(actual, expected) \ 251 { CU_assertImplementation(!(strcmp((const char*)(actual), (const char*)(expected))), __LINE__, ("CU_ASSERT_STRING_EQUAL(" #actual "," #expected ")"), __FILE__, "", CU_FALSE); } 252 253/** Asserts that string actual == expected. 254 * Reports failure and causes test to abort. 255 */ 256#define CU_ASSERT_STRING_EQUAL_FATAL(actual, expected) \ 257 { CU_assertImplementation(!(strcmp((const char*)(actual), (const char*)(expected))), __LINE__, ("CU_ASSERT_STRING_EQUAL_FATAL(" #actual "," #expected ")"), __FILE__, "", CU_TRUE); } 258 259/** Asserts that string actual != expected. 260 * Reports failure with no other action. 261 */ 262#define CU_ASSERT_STRING_NOT_EQUAL(actual, expected) \ 263 { CU_assertImplementation((strcmp((const char*)(actual), (const char*)(expected))), __LINE__, ("CU_ASSERT_STRING_NOT_EQUAL(" #actual "," #expected ")"), __FILE__, "", CU_FALSE); } 264 265/** Asserts that string actual != expected. 266 * Reports failure and causes test to abort. 267 */ 268#define CU_ASSERT_STRING_NOT_EQUAL_FATAL(actual, expected) \ 269 { CU_assertImplementation((strcmp((const char*)(actual), (const char*)(expected))), __LINE__, ("CU_ASSERT_STRING_NOT_EQUAL_FATAL(" #actual "," #expected ")"), __FILE__, "", CU_TRUE); } 270 271/** Asserts that string actual == expected with length specified. 272 * The comparison is limited to count characters. 273 * Reports failure with no other action. 274 */ 275#define CU_ASSERT_NSTRING_EQUAL(actual, expected, count) \ 276 { CU_assertImplementation(!(strncmp((const char*)(actual), (const char*)(expected), (size_t)(count))), __LINE__, ("CU_ASSERT_NSTRING_EQUAL(" #actual "," #expected "," #count ")"), __FILE__, "", CU_FALSE); } 277 278/** Asserts that string actual == expected with length specified. 279 * The comparison is limited to count characters. 280 * Reports failure and causes test to abort. 281 */ 282#define CU_ASSERT_NSTRING_EQUAL_FATAL(actual, expected, count) \ 283 { CU_assertImplementation(!(strncmp((const char*)(actual), (const char*)(expected), (size_t)(count))), __LINE__, ("CU_ASSERT_NSTRING_EQUAL_FATAL(" #actual "," #expected "," #count ")"), __FILE__, "", CU_TRUE); } 284 285/** Asserts that string actual != expected with length specified. 286 * The comparison is limited to count characters. 287 * Reports failure with no other action. 288 */ 289#define CU_ASSERT_NSTRING_NOT_EQUAL(actual, expected, count) \ 290 { CU_assertImplementation((strncmp((const char*)(actual), (const char*)(expected), (size_t)(count))), __LINE__, ("CU_ASSERT_NSTRING_NOT_EQUAL(" #actual "," #expected "," #count ")"), __FILE__, "", CU_FALSE); } 291 292/** Asserts that string actual != expected with length specified. 293 * The comparison is limited to count characters. 294 * Reports failure and causes test to abort. 295 */ 296#define CU_ASSERT_NSTRING_NOT_EQUAL_FATAL(actual, expected, count) \ 297 { 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); } 298 299/** Asserts that double actual == expected within the specified tolerance. 300 * If actual is within granularity of expected, the assertion passes. 301 * Reports failure with no other action. 302 */ 303#define CU_ASSERT_DOUBLE_EQUAL(actual, expected, granularity) \ 304 { CU_assertImplementation(((fabs((double)(actual) - (expected)) <= fabs((double)(granularity)))), __LINE__, ("CU_ASSERT_DOUBLE_EQUAL(" #actual "," #expected "," #granularity ")"), __FILE__, "", CU_FALSE); } 305 306/** Asserts that double actual == expected within the specified tolerance. 307 * If actual is within granularity of expected, the assertion passes. 308 * Reports failure and causes test to abort. 309 */ 310#define CU_ASSERT_DOUBLE_EQUAL_FATAL(actual, expected, granularity) \ 311 { CU_assertImplementation(((fabs((double)(actual) - (expected)) <= fabs((double)(granularity)))), __LINE__, ("CU_ASSERT_DOUBLE_EQUAL_FATAL(" #actual "," #expected "," #granularity ")"), __FILE__, "", CU_TRUE); } 312 313/** Asserts that double actual != expected within the specified tolerance. 314 * If actual is within granularity of expected, the assertion fails. 315 * Reports failure with no other action. 316 */ 317#define CU_ASSERT_DOUBLE_NOT_EQUAL(actual, expected, granularity) \ 318 { CU_assertImplementation(((fabs((double)(actual) - (expected)) > fabs((double)(granularity)))), __LINE__, ("CU_ASSERT_DOUBLE_NOT_EQUAL(" #actual "," #expected "," #granularity ")"), __FILE__, "", CU_FALSE); } 319 320/** Asserts that double actual != expected within the specified tolerance. 321 * If actual is within granularity of expected, the assertion fails. 322 * Reports failure and causes test to abort. 323 */ 324#define CU_ASSERT_DOUBLE_NOT_EQUAL_FATAL(actual, expected, granularity) \ 325 { CU_assertImplementation(((fabs((double)(actual) - (expected)) > fabs((double)(granularity)))), __LINE__, ("CU_ASSERT_DOUBLE_NOT_EQUAL_FATAL(" #actual "," #expected "," #granularity ")"), __FILE__, "", CU_TRUE); } 326 327#ifdef USE_DEPRECATED_CUNIT_NAMES 328 329#ifndef BOOL 330 /** Deprecated (version 2.0-2). @deprecated Use CU_BOOL. */ 331 #define BOOL int 332#endif 333 334#ifndef TRUE 335 /** Deprecated (version 2.0-2). @deprecated Use CU_TRUE. */ 336 #define TRUE 1 337#endif 338 339#ifndef FALSE 340 /** Deprecated (version 2.0-2). @deprecated Use CU_FALSE. */ 341 #define FALSE 0 342#endif 343 344/** Deprecated (version 2.0-2). @deprecated Use CU_MAX_TEST_NAME_LENGTH. */ 345#define MAX_TEST_NAME_LENGTH 256 346/** Deprecated (version 2.0-2). @deprecated Use CU_MAX_SUITE_NAME_LENGTH. */ 347#define MAX_SUITE_NAME_LENGTH 256 348 349/** Deprecated (version 1). @deprecated Use CU_ASSERT_FATAL. */ 350#define ASSERT(value) { if (FALSE == (int)(value)) { CU_assertImplementation((BOOL)value, __LINE__, #value, __FILE__, "", FALSE); return; }} 351/** Deprecated (version 1). @deprecated Use CU_ASSERT_TRUE_FATAL. */ 352#define ASSERT_TRUE(value) { if (FALSE == (value)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_TRUE(" #value ")"), __FILE__, "", FALSE); return; }} 353/** Deprecated (version 1). @deprecated Use CU_ASSERT_FALSE_FATAL. */ 354#define ASSERT_FALSE(value) { if (FALSE != (value)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_FALSE(" #value ")"), __FILE__, "", FALSE); return; }} 355/** Deprecated (version 1). @deprecated Use CU_ASSERT_EQUAL_FATAL. */ 356#define ASSERT_EQUAL(actual, expected) { if ((actual) != (expected)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_EQUAL(" #actual "," #expected ")"), __FILE__, "", FALSE); return; }} 357/** Deprecated (version 1). @deprecated Use CU_ASSERT_NOT_EQUAL_FATAL. */ 358#define ASSERT_NOT_EQUAL(actual, expected) { if ((void*)(actual) == (void*)(expected)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_NOT_EQUAL(" #actual "," #expected ")"), __FILE__, "", FALSE); return; }} 359/** Deprecated (version 1). @deprecated Use CU_ASSERT_PTR_EQUAL_FATAL. */ 360#define ASSERT_PTR_EQUAL(actual, expected) { if ((void*)(actual) != (void*)(expected)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_PTR_EQUAL(" #actual "," #expected ")"), __FILE__, "", FALSE); return; }} 361/** Deprecated (version 1). @deprecated Use CU_ASSERT_PTR_NOT_EQUAL_FATAL. */ 362#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; }} 363/** Deprecated (version 1). @deprecated Use CU_ASSERT_PTR_NULL_FATAL. */ 364#define ASSERT_PTR_NULL(value) { if (NULL != (void*)(value)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_PTR_NULL(" #value")"), __FILE__, "", FALSE); return; }} 365/** Deprecated (version 1). @deprecated Use CU_ASSERT_PTR_NOT_NULL_FATAL. */ 366#define ASSERT_PTR_NOT_NULL(value) { if (NULL == (void*)(value)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_PTR_NOT_NULL(" #value")"), __FILE__, "", FALSE); return; }} 367/** Deprecated (version 1). @deprecated Use CU_ASSERT_STRING_EQUAL_FATAL. */ 368#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; }} 369/** Deprecated (version 1). @deprecated Use CU_ASSERT_STRING_NOT_EQUAL_FATAL. */ 370#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; }} 371/** Deprecated (version 1). @deprecated Use CU_ASSERT_NSTRING_EQUAL_FATAL. */ 372#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; }} 373/** Deprecated (version 1). @deprecated Use CU_ASSERT_NSTRING_NOT_EQUAL_FATAL. */ 374#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; }} 375/** Deprecated (version 1). @deprecated Use CU_ASSERT_DOUBLE_EQUAL_FATAL. */ 376#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; }} 377/** Deprecated (version 1). @deprecated Use CU_ASSERT_DOUBLE_NOT_EQUAL_FATAL. */ 378#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; }} 379#endif /* USE_DEPRECATED_CUNIT_NAMES */ 380 381#endif /* CUNIT_CUNIT_H_SEEN */ 382 383/** @} */ 384