Test.h
author Jens Alfke <jens@mooseyard.com>
Sat May 31 11:26:17 2008 -0700 (2008-05-31)
changeset 12 66b870428f85
parent 11 e5976864dfe9
child 13 c59dec088990
permissions -rw-r--r--
* Worked around compiler warnings in Test.h when building for iPhone.
* Made Mercurial ignore the documentation files.
     1 //
     2 //  Test.h
     3 //  MYUtilities
     4 //
     5 //  Created by Jens Alfke on 1/5/08.
     6 //  Copyright 2008 Jens Alfke. All rights reserved.
     7 //
     8 
     9 #import <Foundation/Foundation.h>
    10 #import "CollectionUtils.h"
    11 #import "Logging.h"
    12 
    13 
    14 /** Call this first thing in main() to run tests.
    15     This function is a no-op if the DEBUG macro is not defined (i.e. in a release build).
    16     At runtime, to cause a particular test "X" to run, add a command-line argument "Test_X".
    17     To run all tests, add the argument "Test_All".
    18     To run only tests without starting the main program, add the argument "Test_Only". */
    19 #if DEBUG
    20 void RunTestCases( int argc, const char **argv );
    21 extern BOOL gRunningTestCase;
    22 #else
    23 #define RunTestCases(ARGC,ARGV)
    24 #define gRunningTestCase NO
    25 #endif
    26 
    27 /** The TestCase() macro declares a test case.
    28     Its argument is a name for the test case (without quotes), and it's followed with a block
    29     of code implementing the test.
    30     The code should raise an exception if anything fails.
    31     The CAssert, CAssertEqual and CAssertEq macros, below, are the most useful way to do this.
    32     A test case can register a dependency on another test case by calling RequireTestCase().
    33     Example:
    34         TestCase(MyLib) {
    35             RequireTestCase("LibIDependOn");
    36             CAssertEq( myFunction(), 12345 );
    37         }
    38     Test cases are disabled if the DEBUG macro is not defined (i.e. in a release build). */
    39 #if DEBUG
    40 #define TestCase(NAME)      __attribute__ ((section ("__TEXT, Tests"))) void Test_##NAME(void); \
    41                             struct TestCaseLink linkToTest##NAME = {&Test_##NAME,#NAME}; \
    42                             __attribute__((constructor)) static void registerTestCase##NAME() \
    43                                 {linkToTest##NAME.next = gAllTestCases; gAllTestCases=&linkToTest##NAME; } \
    44                             void Test_##NAME(void)
    45 #else
    46 #define TestCase(NAME)      __attribute__((unused)) static void Test_##NAME(void)
    47 #endif
    48 
    49 /** Can call this in a test case to indicate a prerequisite.
    50     The prerequisite test will be run first, and if it fails, the current test case will be skipped. */
    51 #if DEBUG
    52 #define RequireTestCase(NAME)   _RequireTestCase(#NAME)
    53 void _RequireTestCase( const char *name );
    54 #else
    55 #define RequireTestCase(NAME)
    56 #endif
    57 
    58 
    59 /** General-purpose assertions, replacing NSAssert etc.. You can use these outside test cases. */
    60 
    61 #define Assert(COND,MSG...)    do{ if( __builtin_expect(!(COND),NO) ) { \
    62                                     IN_SEGMENT_NORETURN(Logging) {_AssertFailed(self,_cmd, __FILE__, __LINE__,\
    63                                                         #COND,##MSG,NULL);} } }while(0)
    64 #define CAssert(COND,MSG...)    do{ if( __builtin_expect(!(COND),NO) ) { \
    65                                     static const char *_name = __PRETTY_FUNCTION__;\
    66                                     IN_SEGMENT_NORETURN(Logging) {_AssertFailed(nil, _name, __FILE__, __LINE__,\
    67                                                         #COND,##MSG,NULL);} } }while(0)
    68 
    69 #define AssertEqual(VAL,EXPECTED)   do{ id _val = VAL, _expected = EXPECTED;\
    70                                         Assert(_val==_expected || [_val isEqual: _expected], @"Unexpected value for %s: %@ (expected %@)", #VAL,_val,_expected); \
    71                                     }while(0)
    72 #define CAssertEqual(VAL,EXPECTED)  do{ id _val = (VAL), _expected = (EXPECTED);\
    73                                         CAssert(_val==_expected || [_val isEqual: _expected], @"Unexpected value for %s: %@ (expected %@)", #VAL,_val,_expected); \
    74                                     }while(0)
    75 
    76 // AssertEq is for scalars (int, float...)
    77 // Note: "typeof()" builtin function requires settingn C language dialect to GNU99.
    78 #define AssertEq(VAL,EXPECTED)  do{ typeof(VAL) _val = VAL; typeof(EXPECTED) _expected = EXPECTED;\
    79                                     Assert(_val==_expected, @"Unexpected value for %s: %@ (expected %@)", #VAL,$object(_val),$object(_expected)); \
    80                                 }while(0)
    81 #define CAssertEq(VAL,EXPECTED) do{ typeof(VAL) _val = VAL; typeof(EXPECTED) _expected = EXPECTED;\
    82                                     CAssert(_val==_expected, @"Unexpected value for %s: %@ (expected %@)", #VAL,$object(_val),$object(_expected)); \
    83                                 }while(0)
    84 
    85 
    86 // Nasty internals ...
    87 #if DEBUG
    88 void _RunTestCase( void (*testptr)(), const char *name );
    89 
    90 struct TestCaseLink {void (*testptr)(); const char *name; BOOL passed; struct TestCaseLink *next;};
    91 extern struct TestCaseLink *gAllTestCases;
    92 #endif DEBUG
    93 void _AssertFailed( id rcvr, const void *selOrFn, const char *sourceFile, int sourceLine,
    94                    const char *condString, NSString *message, ... ) __attribute__((noreturn));