test/test-automation/SDL_test.c
author Markus Kauppila
Sat, 04 Jun 2011 18:08:54 +0300
changeset 5680 e61bdd43a2c3
parent 5679 10a5199c4169
child 5681 7abfcc71aa7d
permissions -rw-r--r--
Fixed old comments and added new ones.
     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 
    52 	if(_testAssertsFailed == 0 && _testAssertsPassed == 0) {
    53 		_testReturnValue = 2;
    54 	}
    55 
    56 	return _testReturnValue;
    57 }
    58 
    59 
    60 /*!
    61  *  Assert function. Tests if the expected value equals the actual value, then
    62  *  the test assert succeeds, otherwise it fails and warns about it.
    63  *
    64  * \param expected Value user expects to have
    65  * \param actual The actual value of tested variable
    66  * \param message Message that will be printed if assert fails
    67  */
    68 void
    69 AssertEquals(Uint32 expected, Uint32 actual, char* message, ...)
    70 {
    71    va_list args;
    72    char buf[256];
    73 
    74    if(expected != actual) {
    75       va_start( args, message );
    76       SDL_vsnprintf( buf, sizeof(buf), message, args );
    77       va_end( args );
    78       printf("Assert Equals failed: expected %d, got %d; %s\n", expected, actual, buf);
    79       _testReturnValue = 1;
    80       _testAssertsFailed++;
    81    } else {
    82       _testAssertsPassed++;
    83    }
    84 }
    85 
    86 /*!
    87  *  Assert function. Tests if the given condition is true. True in
    88  *  this case means non-zero value. If the condition is true, the
    89  *  assert passes, otherwise it fails.
    90  *
    91  * \param condition Condition which will be evaluated
    92  * \param message Message that will be printed if assert fails
    93  */
    94 void
    95 AssertTrue(int condition, char *message, ...)
    96 {
    97    va_list args;
    98    char buf[256];
    99 
   100    if (!condition) {
   101       va_start( args, message );
   102       SDL_vsnprintf( buf, sizeof(buf), message, args );
   103       va_end( args );
   104 
   105       printf("Assert IsTrue failed: %s\n", buf);
   106       _testReturnValue = 1;
   107       _testAssertsFailed++;
   108    } else {
   109       _testAssertsPassed++;
   110    }
   111 }
   112 
   113 #endif