Main Page | Modules | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

CUnit.h

Go to the documentation of this file.
00001 /*
00002  *  CUnit - A Unit testing framework library for C.
00003  *  Copyright (C) 2001  Anil Kumar
00004  *  Copyright (C) 2004  Anil Kumar, Jerry St.Clair
00005  *
00006  *  This library is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU Library General Public
00008  *  License as published by the Free Software Foundation; either
00009  *  version 2 of the License, or (at your option) any later version.
00010  *
00011  *  This library is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  *  Library General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU Library General Public
00017  *  License along with this library; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 /*
00022  *  Contains ASSERT Macro definitions
00023  *
00024  *  Created By      : Anil Kumar on ...(in month of Aug 2001)
00025  *  Last Modified   : 09/Aug/2001
00026  *  Comment         : ASSERT definition
00027  *  EMail           : aksaharan@yahoo.com
00028  *
00029  *  Last Modified   : 12/Mar/2003
00030  *  Comment         : New Assert definitions
00031  *  EMail           : aksaharan@yahoo.com
00032  *
00033  *  Last Modified   : 27/Jul/2003
00034  *  Comment         : Modified ASSERT_XXX Macro definitions
00035  *  EMail           : aksaharan@yahoo.com
00036  *
00037  *  Last Modified   : 15-Jul-2004 (JDS)
00038  *  Comment         : New interface, changed action on assert failure to
00039  *                    not return, provided _FATAL versions of assertions
00040  *                    to return from test function on failure.
00041  *  EMail           : jds2@users.sourceforge.net
00042  *
00043  *  Last Modified   : 1-Sep-2004 (JDS)
00044  *  Comment         : Modified assertions for setjmp/longjmp mechanism of aborting
00045  *                    test runs, added CU_FAIL and CU_PASS macros.
00046  *  EMail           : jds2@users.sourceforge.net
00047  */
00048 
00058 #ifndef _CUNIT_CUNIT_H
00059 #define _CUNIT_CUNIT_H
00060 
00061 #include <string.h>
00062 #include <math.h>
00063 
00064 #include "CUError.h"
00065 #include "TestDB.h"   /* not needed here - included for user convenience */
00066 
00067 #ifdef __cplusplus
00068 extern "C" {
00069 #endif
00070 
00071 /*  Max string lengths for names (includes terminating NULL. */
00073 #define MAX_TEST_NAME_LENGTH    256
00074 
00075 #define MAX_SUITE_NAME_LENGTH   256
00076 
00077 /* Global type Definitions to be used for boolean operators. */
00078 #ifndef BOOL
00079 
00080   #define BOOL int
00081 #endif
00082 
00083 #ifndef TRUE
00084 
00085   #define TRUE 1
00086 #endif
00087 
00088 #ifndef FALSE
00089 
00090   #define FALSE 0
00091 #endif
00092 
00093 #ifndef CU_UNREFERENCED_PARAMETER
00094 
00095   #define CU_UNREFERENCED_PARAMETER(x) (void)x
00096 #endif
00097 
00098 #include "TestRun.h"  /* not needed here - include (after BOOL define) for user convenience */
00099 
00101 #define CU_PASS(msg) \
00102   { CU_assertImplementation(TRUE, __LINE__, ("CU_PASS(" #msg ")"), __FILE__, "", FALSE); }
00103 
00107 #define CU_ASSERT(value) \
00108   { CU_assertImplementation((value), __LINE__, #value, __FILE__, "", FALSE); }
00109 
00113 #define CU_ASSERT_FATAL(value) \
00114   { CU_assertImplementation((value), __LINE__, #value, __FILE__, "", TRUE); }
00115 
00119 #define CU_TEST(value) \
00120   { CU_assertImplementation((value), __LINE__, #value, __FILE__, "", FALSE); }
00121 
00125 #define CU_TEST_FATAL(value) \
00126   { CU_assertImplementation((value), __LINE__, #value, __FILE__, "", TRUE); }
00127 
00129 #define CU_FAIL(msg) \
00130   { CU_assertImplementation(FALSE, __LINE__, ("CU_FAIL(" #msg ")"), __FILE__, "", FALSE); }
00131 
00133 #define CU_FAIL_FATAL(msg) \
00134   { CU_assertImplementation(FALSE, __LINE__, ("CU_FAIL_FATAL(" #msg ")"), __FILE__, "", TRUE); }
00135 
00139 #define CU_ASSERT_TRUE(value) \
00140   { CU_assertImplementation((value), __LINE__, ("CU_ASSERT_TRUE(" #value ")"), __FILE__, "", FALSE); }
00141 
00145 #define CU_ASSERT_TRUE_FATAL(value) \
00146   { CU_assertImplementation((value), __LINE__, ("CU_ASSERT_TRUE_FATAL(" #value ")"), __FILE__, "", TRUE); }
00147 
00151 #define CU_ASSERT_FALSE(value) \
00152   { CU_assertImplementation(!(value), __LINE__, ("CU_ASSERT_FALSE(" #value ")"), __FILE__, "", FALSE); }
00153 
00157 #define CU_ASSERT_FALSE_FATAL(value) \
00158   { CU_assertImplementation(!(value), __LINE__, ("CU_ASSERT_FALSE_FATAL(" #value ")"), __FILE__, "", TRUE); }
00159 
00163 #define CU_ASSERT_EQUAL(actual, expected) \
00164   { CU_assertImplementation(((actual) == (expected)), __LINE__, ("CU_ASSERT_EQUAL(" #actual "," #expected ")"), __FILE__, "", FALSE); }
00165 
00169 #define CU_ASSERT_EQUAL_FATAL(actual, expected) \
00170   { CU_assertImplementation(((actual) == (expected)), __LINE__, ("CU_ASSERT_EQUAL_FATAL(" #actual "," #expected ")"), __FILE__, "", TRUE); }
00171 
00175 #define CU_ASSERT_NOT_EQUAL(actual, expected) \
00176   { CU_assertImplementation(((actual) != (expected)), __LINE__, ("CU_ASSERT_NOT_EQUAL(" #actual "," #expected ")"), __FILE__, "", FALSE); }
00177 
00181 #define CU_ASSERT_NOT_EQUAL_FATAL(actual, expected) \
00182   { CU_assertImplementation(((actual) != (expected)), __LINE__, ("CU_ASSERT_NOT_EQUAL_FATAL(" #actual "," #expected ")"), __FILE__, "", TRUE); }
00183 
00187 #define CU_ASSERT_PTR_EQUAL(actual, expected) \
00188   { CU_assertImplementation(((void*)(actual) == (void*)(expected)), __LINE__, ("CU_ASSERT_PTR_EQUAL(" #actual "," #expected ")"), __FILE__, "", FALSE); }
00189 
00193 #define CU_ASSERT_PTR_EQUAL_FATAL(actual, expected) \
00194   { CU_assertImplementation(((void*)(actual) == (void*)(expected)), __LINE__, ("CU_ASSERT_PTR_EQUAL_FATAL(" #actual "," #expected ")"), __FILE__, "", TRUE); }
00195 
00199 #define CU_ASSERT_PTR_NOT_EQUAL(actual, expected) \
00200   { CU_assertImplementation(((void*)(actual) != (void*)(expected)), __LINE__, ("CU_ASSERT_PTR_NOT_EQUAL(" #actual "," #expected ")"), __FILE__, "", FALSE); }
00201 
00205 #define CU_ASSERT_PTR_NOT_EQUAL_FATAL(actual, expected) \
00206   { CU_assertImplementation(((void*)(actual) != (void*)(expected)), __LINE__, ("CU_ASSERT_PTR_NOT_EQUAL_FATAL(" #actual "," #expected ")"), __FILE__, "", TRUE); }
00207 
00211 #define CU_ASSERT_PTR_NULL(value) \
00212   { CU_assertImplementation((NULL == (void*)(value)), __LINE__, ("CU_ASSERT_PTR_NULL(" #value")"), __FILE__, "", FALSE); }
00213 
00217 #define CU_ASSERT_PTR_NULL_FATAL(value) \
00218   { CU_assertImplementation((NULL == (void*)(value)), __LINE__, ("CU_ASSERT_PTR_NULL_FATAL(" #value")"), __FILE__, "", TRUE); }
00219 
00223 #define CU_ASSERT_PTR_NOT_NULL(value) \
00224   { CU_assertImplementation((NULL != (void*)(value)), __LINE__, ("CU_ASSERT_PTR_NOT_NULL(" #value")"), __FILE__, "", FALSE); }
00225 
00229 #define CU_ASSERT_PTR_NOT_NULL_FATAL(value) \
00230   { CU_assertImplementation((NULL != (void*)(value)), __LINE__, ("CU_ASSERT_PTR_NOT_NULL_FATAL(" #value")"), __FILE__, "", TRUE); }
00231 
00235 #define CU_ASSERT_STRING_EQUAL(actual, expected) \
00236   { CU_assertImplementation(!(strcmp((const char*)(actual), (const char*)(expected))), __LINE__, ("CU_ASSERT_STRING_EQUAL(" #actual ","  #expected ")"), __FILE__, "", FALSE); }
00237 
00241 #define CU_ASSERT_STRING_EQUAL_FATAL(actual, expected) \
00242   { CU_assertImplementation(!(strcmp((const char*)(actual), (const char*)(expected))), __LINE__, ("CU_ASSERT_STRING_EQUAL_FATAL(" #actual ","  #expected ")"), __FILE__, "", TRUE); }
00243 
00247 #define CU_ASSERT_STRING_NOT_EQUAL(actual, expected) \
00248   { CU_assertImplementation((strcmp((const char*)(actual), (const char*)(expected))), __LINE__, ("CU_ASSERT_STRING_NOT_EQUAL(" #actual ","  #expected ")"), __FILE__, "", FALSE); }
00249 
00253 #define CU_ASSERT_STRING_NOT_EQUAL_FATAL(actual, expected) \
00254   { CU_assertImplementation((strcmp((const char*)(actual), (const char*)(expected))), __LINE__, ("CU_ASSERT_STRING_NOT_EQUAL_FATAL(" #actual ","  #expected ")"), __FILE__, "", TRUE); }
00255 
00260 #define CU_ASSERT_NSTRING_EQUAL(actual, expected, count) \
00261   { CU_assertImplementation(!(strncmp((const char*)(actual), (const char*)(expected), (size_t)(count))), __LINE__, ("CU_ASSERT_NSTRING_EQUAL(" #actual ","  #expected "," #count ")"), __FILE__, "", FALSE); }
00262 
00267 #define CU_ASSERT_NSTRING_EQUAL_FATAL(actual, expected, count) \
00268   { CU_assertImplementation(!(strncmp((const char*)(actual), (const char*)(expected), (size_t)(count))), __LINE__, ("CU_ASSERT_NSTRING_EQUAL_FATAL(" #actual ","  #expected "," #count ")"), __FILE__, "", TRUE); }
00269 
00274 #define CU_ASSERT_NSTRING_NOT_EQUAL(actual, expected, count) \
00275   { CU_assertImplementation((strncmp((const char*)(actual), (const char*)(expected), (size_t)(count))), __LINE__, ("CU_ASSERT_NSTRING_NOT_EQUAL(" #actual ","  #expected "," #count ")"), __FILE__, "", FALSE); }
00276 
00281 #define CU_ASSERT_NSTRING_NOT_EQUAL_FATAL(actual, expected, count) \
00282   { CU_assertImplementation((strncmp((const char*)(actual), (const char*)(expected), (size_t)(count))), __LINE__, ("CU_ASSERT_NSTRING_NOT_EQUAL_FATAL(" #actual ","  #expected "," #count ")"), __FILE__, "", TRUE); }
00283 
00288 #define CU_ASSERT_DOUBLE_EQUAL(actual, expected, granularity) \
00289   { CU_assertImplementation(((fabs((double)(actual) - (expected)) <= fabs((double)(granularity)))), __LINE__, ("CU_ASSERT_DOUBLE_EQUAL(" #actual ","  #expected "," #granularity ")"), __FILE__, "", FALSE); }
00290 
00295 #define CU_ASSERT_DOUBLE_EQUAL_FATAL(actual, expected, granularity) \
00296   { CU_assertImplementation(((fabs((double)(actual) - (expected)) <= fabs((double)(granularity)))), __LINE__, ("CU_ASSERT_DOUBLE_EQUAL_FATAL(" #actual ","  #expected "," #granularity ")"), __FILE__, "", TRUE); }
00297 
00302 #define CU_ASSERT_DOUBLE_NOT_EQUAL(actual, expected, granularity) \
00303   { CU_assertImplementation(((fabs((double)(actual) - (expected)) > fabs((double)(granularity)))), __LINE__, ("CU_ASSERT_DOUBLE_NOT_EQUAL(" #actual ","  #expected "," #granularity ")"), __FILE__, "", FALSE); }
00304 
00309 #define CU_ASSERT_DOUBLE_NOT_EQUAL_FATAL(actual, expected, granularity) \
00310   { CU_assertImplementation(((fabs((double)(actual) - (expected)) > fabs((double)(granularity)))), __LINE__, ("CU_ASSERT_DOUBLE_NOT_EQUAL_FATAL(" #actual ","  #expected "," #granularity ")"), __FILE__, "", TRUE); }
00311 
00312 #ifdef __cplusplus
00313 }
00314 #endif
00315 
00316 #ifdef USE_DEPRECATED_CUNIT_NAMES
00317 
00318 #define ASSERT(value) { if (FALSE == (int)(value)) { CU_assertImplementation((BOOL)value, __LINE__, #value, __FILE__, "", FALSE); return; }}
00319 
00320 #define ASSERT_TRUE(value) { if (FALSE == (value)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_TRUE(" #value ")"), __FILE__, "", FALSE); return; }}
00321 
00322 #define ASSERT_FALSE(value) { if (FALSE != (value)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_FALSE(" #value ")"), __FILE__, "", FALSE); return; }}
00323 
00324 #define ASSERT_EQUAL(actual, expected) { if ((actual) != (expected)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_EQUAL(" #actual "," #expected ")"), __FILE__, "", FALSE); return; }}
00325 
00326 #define ASSERT_NOT_EQUAL(actual, expected) { if ((void*)(actual) == (void*)(expected)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_NOT_EQUAL(" #actual "," #expected ")"), __FILE__, "", FALSE); return; }}
00327 
00328 #define ASSERT_PTR_EQUAL(actual, expected) { if ((void*)(actual) != (void*)(expected)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_PTR_EQUAL(" #actual "," #expected ")"), __FILE__, "", FALSE); return; }}
00329 
00330 #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; }}
00331 
00332 #define ASSERT_PTR_NULL(value)  { if (NULL != (void*)(value)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_PTR_NULL(" #value")"), __FILE__, "", FALSE); return; }}
00333 
00334 #define ASSERT_PTR_NOT_NULL(value) { if (NULL == (void*)(value)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_PTR_NOT_NULL(" #value")"), __FILE__, "", FALSE); return; }}
00335 
00336 #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; }}
00337 
00338 #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; }}
00339 
00340 #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; }}
00341 
00342 #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; }}
00343 
00344 #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; }}
00345 
00346 #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; }}
00347 #endif  /* USE_DEPRECATED_CUNIT_NAMES */
00348 
00349 #endif  /*  _CUNIT_CUNIT_H  */
00350 

Generated on Thu Apr 28 12:36:09 2005 for CUnit by  doxygen 1.3.9.1