src/test/SDL_test_assert.c
changeset 6756 398073b195bb
parent 6727 1b5280cd5885
child 6762 c680e085a5f2
     1.1 --- a/src/test/SDL_test_assert.c	Sat Dec 15 00:30:17 2012 +0000
     1.2 +++ b/src/test/SDL_test_assert.c	Fri Dec 14 23:05:34 2012 -0800
     1.3 @@ -30,10 +30,10 @@
     1.4  #include "SDL_test.h"
     1.5  
     1.6  /* Assert check message format */
     1.7 -const char *SDLTest_AssertCheckFmt = "Assert '%s': %s";
     1.8 +const char *SDLTest_AssertCheckFormat = "Assert '%s': %s";
     1.9  
    1.10  /* Assert summary message format */
    1.11 -const char *SDLTest_AssertSummaryFmt = "Assert Summary: Total=%d Passed=%d Failed=%d";
    1.12 +const char *SDLTest_AssertSummaryFormat = "Assert Summary: Total=%d Passed=%d Failed=%d";
    1.13  
    1.14  /*! \brief counts the failed asserts */
    1.15  static Uint32 SDLTest_AssertsFailed = 0;
    1.16 @@ -44,32 +44,72 @@
    1.17  /*
    1.18   *  Assert that logs and break execution flow on failures (i.e. for harness errors).
    1.19   */
    1.20 -void SDLTest_Assert(int assertCondition, char *assertDescription)
    1.21 +void SDLTest_Assert(int assertCondition, char *assertDescription, ...)
    1.22  {
    1.23 -	SDL_assert((SDLTest_AssertCheck(assertCondition, assertDescription)));
    1.24 +        va_list list;
    1.25 +	char logMessage[SDLTEST_MAX_LOGMESSAGE_LENGTH];
    1.26 +                
    1.27 +	// Print assert description into a buffer
    1.28 +	memset(logMessage, 0, SDLTEST_MAX_LOGMESSAGE_LENGTH);
    1.29 +	va_start(list, assertDescription);
    1.30 +	SDL_vsnprintf(logMessage, SDLTEST_MAX_LOGMESSAGE_LENGTH - 1, assertDescription, list);
    1.31 +	va_end(list);
    1.32 +	
    1.33 +	// Log, then assert and break on failure
    1.34 +	SDL_assert((SDLTest_AssertCheck(assertCondition, logMessage)));
    1.35  }
    1.36  
    1.37  /*
    1.38   * Assert that logs but does not break execution flow on failures (i.e. for test cases).
    1.39   */
    1.40 -int SDLTest_AssertCheck(int assertCondition, char *assertDescription)
    1.41 +int SDLTest_AssertCheck(int assertCondition, char *assertDescription, ...)
    1.42  {
    1.43 -	char *fmt = (char *)SDLTest_AssertCheckFmt;
    1.44 +    va_list list;
    1.45 +	char logMessage[SDLTEST_MAX_LOGMESSAGE_LENGTH];
    1.46 +	char *logFormat = (char *)SDLTest_AssertCheckFormat;
    1.47 +                
    1.48 +	// Print assert description into a buffer
    1.49 +	memset(logMessage, 0, SDLTEST_MAX_LOGMESSAGE_LENGTH);
    1.50 +	va_start(list, assertDescription);
    1.51 +	SDL_vsnprintf(logMessage, SDLTEST_MAX_LOGMESSAGE_LENGTH - 1, assertDescription, list);
    1.52 +	va_end(list);
    1.53 +                                                        
    1.54 +	// Log pass or fail message
    1.55  	if (assertCondition == ASSERT_FAIL)
    1.56  	{
    1.57  		SDLTest_AssertsFailed++;
    1.58 -		SDLTest_LogError(fmt, assertDescription, "Failed");
    1.59 +		SDLTest_LogError(logFormat, logMessage, "Failed");
    1.60  	} 
    1.61  	else 
    1.62  	{
    1.63  		SDLTest_AssertsPassed++;
    1.64 -		SDLTest_Log(fmt, assertDescription, "Passed");
    1.65 +		SDLTest_Log(logFormat, logMessage, "Passed");
    1.66  	}
    1.67  
    1.68  	return assertCondition;
    1.69  }
    1.70  
    1.71  /*
    1.72 + * Explicitly passing Assert that logs (i.e. for test cases).
    1.73 + */
    1.74 +void SDLTest_AssertPass(char *assertDescription, ...)
    1.75 +{
    1.76 +    va_list list;
    1.77 +	char logMessage[SDLTEST_MAX_LOGMESSAGE_LENGTH];
    1.78 +	char *logFormat = (char *)SDLTest_AssertCheckFormat;
    1.79 +                
    1.80 +	// Print assert description into a buffer
    1.81 +	memset(logMessage, 0, SDLTEST_MAX_LOGMESSAGE_LENGTH);
    1.82 +	va_start(list, assertDescription);
    1.83 +	SDL_vsnprintf(logMessage, SDLTEST_MAX_LOGMESSAGE_LENGTH - 1, assertDescription, list);
    1.84 +	va_end(list);
    1.85 +                            
    1.86 +    // Log pass message                            
    1.87 +	SDLTest_AssertsPassed++;
    1.88 +	SDLTest_Log(logFormat, logMessage, "Pass");
    1.89 +}
    1.90 +
    1.91 +/*
    1.92   * Resets the assert summary counters to zero.
    1.93   */
    1.94  void SDLTest_ResetAssertSummary()
    1.95 @@ -84,15 +124,15 @@
    1.96   */
    1.97  void SDLTest_LogAssertSummary()
    1.98  {
    1.99 -	char *fmt = (char *)SDLTest_AssertSummaryFmt;
   1.100 +	char *logFormat = (char *)SDLTest_AssertSummaryFormat;
   1.101  	Uint32 totalAsserts = SDLTest_AssertsPassed + SDLTest_AssertsFailed;
   1.102  	if (SDLTest_AssertsFailed == 0)
   1.103  	{
   1.104 -		SDLTest_Log(fmt, totalAsserts, SDLTest_AssertsPassed, SDLTest_AssertsFailed);
   1.105 +		SDLTest_Log(logFormat, totalAsserts, SDLTest_AssertsPassed, SDLTest_AssertsFailed);
   1.106  	} 
   1.107  	else 
   1.108  	{
   1.109 -		SDLTest_LogError(fmt, totalAsserts, SDLTest_AssertsPassed, SDLTest_AssertsFailed);
   1.110 +		SDLTest_LogError(logFormat, totalAsserts, SDLTest_AssertsPassed, SDLTest_AssertsFailed);
   1.111  	}
   1.112  }
   1.113