src/test/SDL_test_assert.c
changeset 6717 2acd95060548
child 6718 918ba414168b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/test/SDL_test_assert.c	Fri Nov 30 23:25:34 2012 -0800
     1.3 @@ -0,0 +1,96 @@
     1.4 +/*
     1.5 +  Simple DirectMedia Layer
     1.6 +  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     1.7 +
     1.8 +  This software is provided 'as-is', without any express or implied
     1.9 +  warranty.  In no event will the authors be held liable for any damages
    1.10 +  arising from the use of this software.
    1.11 +
    1.12 +  Permission is granted to anyone to use this software for any purpose,
    1.13 +  including commercial applications, and to alter it and redistribute it
    1.14 +  freely, subject to the following restrictions:
    1.15 +
    1.16 +  1. The origin of this software must not be misrepresented; you must not
    1.17 +     claim that you wrote the original software. If you use this software
    1.18 +     in a product, an acknowledgment in the product documentation would be
    1.19 +     appreciated but is not required.
    1.20 +  2. Altered source versions must be plainly marked as such, and must not be
    1.21 +     misrepresented as being the original software.
    1.22 +  3. This notice may not be removed or altered from any source distribution.
    1.23 +*/
    1.24 +
    1.25 +/*
    1.26 +
    1.27 + Used by the test framework and test cases. 
    1.28 +
    1.29 +*/
    1.30 +
    1.31 +#include "SDL_config.h"
    1.32 +
    1.33 +#include "SDL_test.h"
    1.34 +
    1.35 +/*! \brief counts the failed asserts */
    1.36 +static Uint32 SDLTest_testAssertsFailed = 0;
    1.37 +
    1.38 +/*! \brief counts the passed asserts */
    1.39 +static Uint32 SDLTest_testAssertsPassed = 0;
    1.40 +
    1.41 +/* Assert check message format */
    1.42 +const char *SDLTest_AssertCheckFmt = "Assert %s: %s";
    1.43 +
    1.44 +/* Assert summary message format */
    1.45 +const char *SDLTest_AssertSummaryFmt = "Assert Summary: Total=%d Passed=%d Failed=%d";
    1.46 +
    1.47 +/*
    1.48 + *  Assert that logs and break execution flow on failures (i.e. for harness errors).
    1.49 + */
    1.50 +void SDLTest_Assert(int assertCondition, char *assertDescription)
    1.51 +{
    1.52 +	SDLTest_AssertCheck(assertCondition, assertDescription);
    1.53 +	SDL_assert((assertCondition));
    1.54 +}
    1.55 +
    1.56 +/*
    1.57 + * Assert that logs but does not break execution flow on failures (i.e. for test cases).
    1.58 + */
    1.59 +void SDLTest_AssertCheck(int assertCondition, char *assertDescription)
    1.60 +{
    1.61 +	char *fmt = (char *)SDLTest_AssertCheckFmt;
    1.62 +	if (assertCondition)
    1.63 +	{
    1.64 +		SDLTest_testAssertsPassed++;
    1.65 +		SDLTest_Log(fmt, "Passed", assertDescription);
    1.66 +	} 
    1.67 +	else 
    1.68 +	{
    1.69 +		SDLTest_testAssertsFailed++;
    1.70 +		SDLTest_LogError(fmt, "Failed", assertDescription);
    1.71 +	}
    1.72 +}
    1.73 +
    1.74 +/*
    1.75 + * Resets the assert summary counters to zero.
    1.76 + */
    1.77 +void SDLTest_ResetAssertSummary()
    1.78 +{
    1.79 +	SDLTest_testAssertsPassed = 0;
    1.80 +	SDLTest_testAssertsFailed = 0;
    1.81 +}
    1.82 +
    1.83 +/*
    1.84 + * Logs summary of all assertions (total, pass, fail) since last reset 
    1.85 + * as INFO (failed==0) or ERROR (failed > 0).
    1.86 + */
    1.87 +void SDLTest_LogAssertSummary()
    1.88 +{
    1.89 +	char *fmt = (char *)SDLTest_AssertSummaryFmt;
    1.90 +	Uint32 totalAsserts = SDLTest_testAssertsPassed + SDLTest_testAssertsFailed;
    1.91 +	if (SDLTest_testAssertsFailed == 0)
    1.92 +	{
    1.93 +		SDLTest_Log(fmt, totalAsserts, SDLTest_testAssertsPassed, SDLTest_testAssertsFailed);
    1.94 +	} 
    1.95 +	else 
    1.96 +	{
    1.97 +		SDLTest_LogError(fmt, totalAsserts, SDLTest_testAssertsPassed, SDLTest_testAssertsFailed);
    1.98 +	}
    1.99 +}