Minor improvements.
5 // Created by Jens Alfke on 1/5/08.
6 // Copyright 2008 Jens Alfke. All rights reserved.
9 #import <Foundation/Foundation.h>
10 #import "CollectionUtils.h"
13 /** Call this first thing in main() to run tests.
14 This function is a no-op if the DEBUG macro is not defined (i.e. in a release build).
15 At runtime, to cause a particular test "X" to run, add a command-line argument "Test_X".
16 To run all tests, add the argument "Test_All".
17 To run only tests without starting the main program, add the argument "Test_Only". */
19 void RunTestCases( int argc, const char **argv );
20 extern BOOL gRunningTestCase;
22 #define RunTestCases(ARGC,ARGV)
23 #define gRunningTestCase NO
26 /** The TestCase() macro declares a test case.
27 Its argument is a name for the test case (without quotes), and it's followed with a block
28 of code implementing the test.
29 The code should raise an exception if anything fails.
30 The CAssert, CAssertEqual and CAssertEq macros, below, are the most useful way to do this.
31 A test case can register a dependency on another test case by calling RequireTestCase().
34 RequireTestCase("LibIDependOn");
35 CAssertEq( myFunction(), 12345 );
37 Test cases are disabled if the DEBUG macro is not defined (i.e. in a release build). */
39 #define TestCase(NAME) void Test_##NAME(void); \
40 struct TestCaseLink linkToTest##NAME = {&Test_##NAME,#NAME}; \
41 __attribute__((constructor)) static void registerTestCase##NAME() \
42 {linkToTest##NAME.next = gAllTestCases; gAllTestCases=&linkToTest##NAME; } \
43 void Test_##NAME(void)
45 #define TestCase(NAME) __attribute__((unused)) static void Test_##NAME(void)
48 /** Can call this in a test case to indicate a prerequisite.
49 The prerequisite test will be run first, and if it fails, the current test case will be skipped. */
50 #define RequireTestCase(NAME) _RequireTestCase(#NAME)
51 void _RequireTestCase( const char *name );
55 /** General-purpose assertions, replacing NSAssert etc.. You can use these outside test cases. */
57 #define Assert(COND,MSG...) do{ if( __builtin_expect(!(COND),NO) ) \
58 _AssertFailed(self,(const char*)_cmd, __FILE__, __LINE__,\
59 #COND,##MSG,NULL); }while(0)
60 #define CAssert(COND,MSG...) do{ if( __builtin_expect(!(COND),NO) ) \
61 _AssertFailed(nil, __PRETTY_FUNCTION__, __FILE__, __LINE__,\
62 #COND,##MSG,NULL); }while(0)
64 #define AssertEqual(VAL,EXPECTED) do{ id _val = VAL, _expected = EXPECTED;\
65 Assert(_val==_expected || [_val isEqual: _expected], @"Unexpected value for %s: %@ (expected %@)", #VAL,_val,_expected); \
67 #define CAssertEqual(VAL,EXPECTED) do{ id _val = (VAL), _expected = (EXPECTED);\
68 CAssert(_val==_expected || [_val isEqual: _expected], @"Unexpected value for %s: %@ (expected %@)", #VAL,_val,_expected); \
71 // AssertEq is for scalars (int, float...)
72 #define AssertEq(VAL,EXPECTED) do{ typeof(VAL) _val = VAL; typeof(EXPECTED) _expected = EXPECTED;\
73 Assert(_val==_expected, @"Unexpected value for %s: %@ (expected %@)", #VAL,$object(_val),$object(_expected)); \
75 #define CAssertEq(VAL,EXPECTED) do{ typeof(VAL) _val = VAL; typeof(EXPECTED) _expected = EXPECTED;\
76 CAssert(_val==_expected, @"Unexpected value for %s: %@ (expected %@)", #VAL,$object(_val),$object(_expected)); \
81 #pragma mark EXCEPTION UTILITIES:
83 @interface NSException (MooseyardUtil)
84 /** Returns a textual, human-readable backtrace of the point where the exception was thrown. */
85 - (NSString*) my_callStack;
90 // Nasty internals ...
92 void _RunTestCase( void (*testptr)(), const char *name );
94 struct TestCaseLink {void (*testptr)(); const char *name; BOOL passed; struct TestCaseLink *next;};
95 extern struct TestCaseLink *gAllTestCases;
97 void _AssertFailed( id rcvr, const char *selOrFn, const char *sourceFile, int sourceLine,
98 const char *condString, NSString *message, ... ) __attribute__((noreturn));