src/test/SDL_test_assert.c
author Andreas Schiffler
Thu, 20 Dec 2012 08:50:36 -0800
changeset 6762 c680e085a5f2
parent 6756 398073b195bb
child 6772 5229a117ef97
permissions -rw-r--r--
Add image data definitions (for render tests) to test lib
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 
    22 /*
    23 
    24  Used by the test framework and test cases. 
    25 
    26 */
    27 
    28 #include "SDL_config.h"
    29 
    30 #include "SDL_test.h"
    31 
    32 /* Assert check message format */
    33 const char *SDLTest_AssertCheckFormat = "Assert '%s': %s";
    34 
    35 /* Assert summary message format */
    36 const char *SDLTest_AssertSummaryFormat = "Assert Summary: Total=%d Passed=%d Failed=%d";
    37 
    38 /*! \brief counts the failed asserts */
    39 static Uint32 SDLTest_AssertsFailed = 0;
    40 
    41 /*! \brief counts the passed asserts */
    42 static Uint32 SDLTest_AssertsPassed = 0;
    43 
    44 /*
    45  *  Assert that logs and break execution flow on failures (i.e. for harness errors).
    46  */
    47 void SDLTest_Assert(int assertCondition, char *assertDescription, ...)
    48 {
    49         va_list list;
    50 	char logMessage[SDLTEST_MAX_LOGMESSAGE_LENGTH];
    51                 
    52 	// Print assert description into a buffer
    53 	SDL_memset(logMessage, 0, SDLTEST_MAX_LOGMESSAGE_LENGTH);
    54 	va_start(list, assertDescription);
    55 	SDL_vsnprintf(logMessage, SDLTEST_MAX_LOGMESSAGE_LENGTH - 1, assertDescription, list);
    56 	va_end(list);
    57 	
    58 	// Log, then assert and break on failure
    59 	SDL_assert((SDLTest_AssertCheck(assertCondition, logMessage)));
    60 }
    61 
    62 /*
    63  * Assert that logs but does not break execution flow on failures (i.e. for test cases).
    64  */
    65 int SDLTest_AssertCheck(int assertCondition, char *assertDescription, ...)
    66 {
    67     va_list list;
    68 	char logMessage[SDLTEST_MAX_LOGMESSAGE_LENGTH];
    69 	char *logFormat = (char *)SDLTest_AssertCheckFormat;
    70                 
    71 	// Print assert description into a buffer
    72 	SDL_memset(logMessage, 0, SDLTEST_MAX_LOGMESSAGE_LENGTH);
    73 	va_start(list, assertDescription);
    74 	SDL_vsnprintf(logMessage, SDLTEST_MAX_LOGMESSAGE_LENGTH - 1, assertDescription, list);
    75 	va_end(list);
    76                                                         
    77 	// Log pass or fail message
    78 	if (assertCondition == ASSERT_FAIL)
    79 	{
    80 		SDLTest_AssertsFailed++;
    81 		SDLTest_LogError(logFormat, logMessage, "Failed");
    82 	} 
    83 	else 
    84 	{
    85 		SDLTest_AssertsPassed++;
    86 		SDLTest_Log(logFormat, logMessage, "Passed");
    87 	}
    88 
    89 	return assertCondition;
    90 }
    91 
    92 /*
    93  * Explicitly passing Assert that logs (i.e. for test cases).
    94  */
    95 void SDLTest_AssertPass(char *assertDescription, ...)
    96 {
    97     va_list list;
    98 	char logMessage[SDLTEST_MAX_LOGMESSAGE_LENGTH];
    99 	char *logFormat = (char *)SDLTest_AssertCheckFormat;
   100                 
   101 	// Print assert description into a buffer
   102 	SDL_memset(logMessage, 0, SDLTEST_MAX_LOGMESSAGE_LENGTH);
   103 	va_start(list, assertDescription);
   104 	SDL_vsnprintf(logMessage, SDLTEST_MAX_LOGMESSAGE_LENGTH - 1, assertDescription, list);
   105 	va_end(list);
   106                             
   107     // Log pass message                            
   108 	SDLTest_AssertsPassed++;
   109 	SDLTest_Log(logFormat, logMessage, "Pass");
   110 }
   111 
   112 /*
   113  * Resets the assert summary counters to zero.
   114  */
   115 void SDLTest_ResetAssertSummary()
   116 {
   117 	SDLTest_AssertsPassed = 0;
   118 	SDLTest_AssertsFailed = 0;
   119 }
   120 
   121 /*
   122  * Logs summary of all assertions (total, pass, fail) since last reset 
   123  * as INFO (failed==0) or ERROR (failed > 0).
   124  */
   125 void SDLTest_LogAssertSummary()
   126 {
   127 	char *logFormat = (char *)SDLTest_AssertSummaryFormat;
   128 	Uint32 totalAsserts = SDLTest_AssertsPassed + SDLTest_AssertsFailed;
   129 	if (SDLTest_AssertsFailed == 0)
   130 	{
   131 		SDLTest_Log(logFormat, totalAsserts, SDLTest_AssertsPassed, SDLTest_AssertsFailed);
   132 	} 
   133 	else 
   134 	{
   135 		SDLTest_LogError(logFormat, totalAsserts, SDLTest_AssertsPassed, SDLTest_AssertsFailed);
   136 	}
   137 }
   138 
   139 /*
   140  * Converts the current assert state into a test result
   141  */
   142 int SDLTest_AssertSummaryToTestResult()
   143 {
   144 	if (SDLTest_AssertsFailed > 0) {
   145 		return TEST_RESULT_FAILED;
   146 	} else {
   147 		if (SDLTest_AssertsPassed > 0) {
   148 			return TEST_RESULT_PASSED;
   149 		} else {
   150 			return TEST_RESULT_NO_ASSERT;
   151 		}
   152 	}
   153 }