src/test/SDL_test_assert.c
author Andreas Schiffler
Sat, 01 Dec 2012 14:48:30 -0800
changeset 6718 918ba414168b
parent 6717 2acd95060548
child 6721 53b71f45a53a
permissions -rw-r--r--
Update assert API in test lib; add to and harness; add test lib to VS2010 and VS2012 solution; fix VS2012 solution; fix compiler warning
     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 /*! \brief counts the failed asserts */
    33 static Uint32 SDLTest_AssertsFailed = 0;
    34 
    35 /*! \brief counts the passed asserts */
    36 static Uint32 SDLTest_AssertsPassed = 0;
    37 
    38 /* Assert check message format */
    39 const char *SDLTest_AssertCheckFmt = "Assert %s: %s";
    40 
    41 /* Assert summary message format */
    42 const char *SDLTest_AssertSummaryFmt = "Assert Summary: Total=%d Passed=%d Failed=%d";
    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 	SDL_assert((SDLTest_AssertCheck(assertCondition, assertDescription)));
    50 }
    51 
    52 /*
    53  * Assert that logs but does not break execution flow on failures (i.e. for test cases).
    54  */
    55 int SDLTest_AssertCheck(int assertCondition, char *assertDescription)
    56 {
    57 	char *fmt = (char *)SDLTest_AssertCheckFmt;
    58 	if (assertCondition == ASSERT_FAIL)
    59 	{
    60 		SDLTest_AssertsFailed++;
    61 		SDLTest_LogError(fmt, "Failed", assertDescription);
    62 	} 
    63 	else 
    64 	{
    65 		SDLTest_AssertsPassed++;
    66 		SDLTest_Log(fmt, "Passed", assertDescription);
    67 	}
    68 
    69 	return assertCondition;
    70 }
    71 
    72 /*
    73  * Resets the assert summary counters to zero.
    74  */
    75 void SDLTest_ResetAssertSummary()
    76 {
    77 	SDLTest_AssertsPassed = 0;
    78 	SDLTest_AssertsFailed = 0;
    79 }
    80 
    81 /*
    82  * Logs summary of all assertions (total, pass, fail) since last reset 
    83  * as INFO (failed==0) or ERROR (failed > 0).
    84  */
    85 void SDLTest_LogAssertSummary()
    86 {
    87 	char *fmt = (char *)SDLTest_AssertSummaryFmt;
    88 	Uint32 totalAsserts = SDLTest_AssertsPassed + SDLTest_AssertsFailed;
    89 	if (SDLTest_AssertsFailed == 0)
    90 	{
    91 		SDLTest_Log(fmt, totalAsserts, SDLTest_AssertsPassed, SDLTest_AssertsFailed);
    92 	} 
    93 	else 
    94 	{
    95 		SDLTest_LogError(fmt, totalAsserts, SDLTest_AssertsPassed, SDLTest_AssertsFailed);
    96 	}
    97 }