test/test-automation/SDL_test.c
author Markus Kauppila
Sat, 04 Jun 2011 17:58:46 +0300
changeset 5679 10a5199c4169
parent 5678 50401a6c3c95
child 5680 e61bdd43a2c3
permissions -rw-r--r--
Test case fails if it doesn't contain any asserts.
     1 /*
     2   Copyright (C) 2011 Markus Kauppila <markus.kauppila@gmail.com>
     3 
     4   This software is provided 'as-is', without any express or implied
     5   warranty.  In no event will the authors be held liable for any damages
     6   arising from the use of this software.
     7 
     8   Permission is granted to anyone to use this software for any purpose,
     9   including commercial applications, and to alter it and redistribute it
    10   freely, subject to the following restrictions:
    11 
    12   1. The origin of this software must not be misrepresented; you must not
    13      claim that you wrote the original software. If you use this software
    14      in a product, an acknowledgment in the product documentation would be
    15      appreciated but is not required.
    16   2. Altered source versions must be plainly marked as such, and must not be
    17      misrepresented as being the original software.
    18   3. This notice may not be removed or altered from any source distribution.
    19 */
    20 
    21 #ifndef _SDL_TEST_C
    22 #define _SDL_TEST_C
    23 
    24 #include <stdio.h> /* printf/fprintf */
    25 #include <stdarg.h> /* va_list */
    26 
    27 #include "SDL_test.h"
    28 
    29 /*! \brief return value of test case. Non-zero value means that the test failed */
    30 int _testReturnValue;
    31 
    32 /*! \brief counts the failed asserts */
    33 int _testAssertsFailed;
    34 
    35 /*! \brief counts the passed asserts */
    36 int _testAssertsPassed;
    37 
    38 void
    39 _TestCaseInit()
    40 {
    41 	_testReturnValue = 0;
    42 	_testAssertsFailed = 0;
    43 	_testAssertsPassed = 0;
    44 }
    45 
    46 int
    47 _TestCaseQuit()
    48 {
    49 	//! \todo make the test fail, if it does not contain any asserts
    50 	printf("Asserts: passed %d, failed %d\n", _testAssertsPassed, _testAssertsFailed);
    51 	if(_testAssertsFailed == 0 && _testAssertsPassed == 0) {
    52 		_testReturnValue = 2;
    53 	}
    54 
    55 	return _testReturnValue;
    56 }
    57 
    58 void
    59 AssertEquals(Uint32 expected, Uint32 actual, char* message, ...)
    60 {
    61    va_list args;
    62    char buf[256];
    63 
    64    if(expected != actual) {
    65       va_start( args, message );
    66       SDL_vsnprintf( buf, sizeof(buf), message, args );
    67       va_end( args );
    68       printf("Assert Equals failed: expected %d, got %d; %s\n", expected, actual, buf);
    69       _testReturnValue = 1;
    70       _testAssertsFailed++;
    71    } else {
    72       _testAssertsPassed++;
    73    }
    74 }
    75 
    76 void
    77 AssertTrue(int condition, char *message, ...)
    78 {
    79    va_list args;
    80    char buf[256];
    81 
    82    if (!condition) {
    83       va_start( args, message );
    84       SDL_vsnprintf( buf, sizeof(buf), message, args );
    85       va_end( args );
    86 
    87       printf("Assert IsTrue failed: %s\n", buf);
    88       _testReturnValue = 1;
    89       _testAssertsFailed++;
    90    } else {
    91       _testAssertsPassed++;
    92    }
    93 }
    94 
    95 #endif