Test.h
author Jens Alfke <jens@mooseyard.com>
Sat May 17 13:14:48 2008 -0700 (2008-05-17)
changeset 9 823e7e74088e
parent 1 e55a17cdabd2
child 10 82a37ccf6b8c
permissions -rw-r--r--
* Assert macros now put the failure code in a separate segment.
* Added $string utility.
     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 #define RequireTestCase(NAME)   _RequireTestCase(#NAME)
    52 void _RequireTestCase( const char *name );
    53 
    54 
    55 
    56 /** General-purpose assertions, replacing NSAssert etc.. You can use these outside test cases. */
    57 
    58 #ifdef __cplusplus
    59     #define IN_SEGMENT_NORETURN(SEG)
    60 #else
    61     #define IN_SEGMENT_NORETURN(SEG) auto __attribute__ ((section ("__TEXT, "#SEG))) __attribute__ ((noinline)) __attribute__((noreturn)) void _assertfailure_(void);\
    62                             _assertfailure_();\
    63                             void _assertfailure_(void)
    64 #endif
    65 
    66 
    67 #define Assert(COND,MSG...)    do{ if( __builtin_expect(!(COND),NO) ) { \
    68                                     IN_SEGMENT_NORETURN(Logging) {_AssertFailed(self,(const char*)_cmd, __FILE__, __LINE__,\
    69                                                         #COND,##MSG,NULL);} } }while(0)
    70 #define CAssert(COND,MSG...)    do{ if( __builtin_expect(!(COND),NO) ) { \
    71                                     static const char *_name = __PRETTY_FUNCTION__;\
    72                                     IN_SEGMENT_NORETURN(Logging) {_AssertFailed(nil, _name, __FILE__, __LINE__,\
    73                                                         #COND,##MSG,NULL);} } }while(0)
    74 
    75 #define AssertEqual(VAL,EXPECTED)   do{ id _val = VAL, _expected = EXPECTED;\
    76                                         Assert(_val==_expected || [_val isEqual: _expected], @"Unexpected value for %s: %@ (expected %@)", #VAL,_val,_expected); \
    77                                     }while(0)
    78 #define CAssertEqual(VAL,EXPECTED)  do{ id _val = (VAL), _expected = (EXPECTED);\
    79                                         CAssert(_val==_expected || [_val isEqual: _expected], @"Unexpected value for %s: %@ (expected %@)", #VAL,_val,_expected); \
    80                                     }while(0)
    81 
    82 // AssertEq is for scalars (int, float...)
    83 #define AssertEq(VAL,EXPECTED)  do{ typeof(VAL) _val = VAL; typeof(EXPECTED) _expected = EXPECTED;\
    84                                     Assert(_val==_expected, @"Unexpected value for %s: %@ (expected %@)", #VAL,$object(_val),$object(_expected)); \
    85                                 }while(0)
    86 #define CAssertEq(VAL,EXPECTED) do{ typeof(VAL) _val = VAL; typeof(EXPECTED) _expected = EXPECTED;\
    87                                     CAssert(_val==_expected, @"Unexpected value for %s: %@ (expected %@)", #VAL,$object(_val),$object(_expected)); \
    88                                 }while(0)
    89 
    90 
    91 #pragma mark -
    92 #pragma mark EXCEPTION UTILITIES:
    93 
    94 @interface NSException (MooseyardUtil)
    95 /** Returns a textual, human-readable backtrace of the point where the exception was thrown. */
    96 - (NSString*) my_callStack;
    97 @end
    98 
    99 
   100 
   101 // Nasty internals ...
   102 #if DEBUG
   103 void _RunTestCase( void (*testptr)(), const char *name );
   104 
   105 struct TestCaseLink {void (*testptr)(); const char *name; BOOL passed; struct TestCaseLink *next;};
   106 extern struct TestCaseLink *gAllTestCases;
   107 #endif DEBUG
   108 void _AssertFailed( id rcvr, const char *selOrFn, const char *sourceFile, int sourceLine,
   109                    const char *condString, NSString *message, ... ) __attribute__((noreturn));