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.
markus@5664
     1
/*
markus@5664
     2
  Copyright (C) 2011 Markus Kauppila <markus.kauppila@gmail.com>
markus@5664
     3
markus@5664
     4
  This software is provided 'as-is', without any express or implied
markus@5664
     5
  warranty.  In no event will the authors be held liable for any damages
markus@5664
     6
  arising from the use of this software.
markus@5664
     7
markus@5664
     8
  Permission is granted to anyone to use this software for any purpose,
markus@5664
     9
  including commercial applications, and to alter it and redistribute it
markus@5664
    10
  freely, subject to the following restrictions:
markus@5664
    11
markus@5664
    12
  1. The origin of this software must not be misrepresented; you must not
markus@5664
    13
     claim that you wrote the original software. If you use this software
markus@5664
    14
     in a product, an acknowledgment in the product documentation would be
markus@5664
    15
     appreciated but is not required.
markus@5664
    16
  2. Altered source versions must be plainly marked as such, and must not be
markus@5664
    17
     misrepresented as being the original software.
markus@5664
    18
  3. This notice may not be removed or altered from any source distribution.
markus@5664
    19
*/
markus@5664
    20
markus@5667
    21
#ifndef _SDL_TEST_C
markus@5667
    22
#define _SDL_TEST_C
markus@5664
    23
markus@5677
    24
#include <stdio.h> /* printf/fprintf */
markus@5677
    25
#include <stdarg.h> /* va_list */
markus@5677
    26
markus@5667
    27
#include "SDL_test.h"
markus@5664
    28
markus@5671
    29
/*! \brief return value of test case. Non-zero value means that the test failed */
markus@5678
    30
int _testReturnValue;
markus@5667
    31
markus@5678
    32
/*! \brief counts the failed asserts */
markus@5678
    33
int _testAssertsFailed;
markus@5678
    34
markus@5678
    35
/*! \brief counts the passed asserts */
markus@5678
    36
int _testAssertsPassed;
markus@5677
    37
markus@5664
    38
void
markus@5678
    39
_TestCaseInit()
markus@5667
    40
{
markus@5667
    41
	_testReturnValue = 0;
markus@5677
    42
	_testAssertsFailed = 0;
markus@5677
    43
	_testAssertsPassed = 0;
markus@5667
    44
}
markus@5667
    45
markus@5672
    46
int
markus@5678
    47
_TestCaseQuit()
markus@5667
    48
{
markus@5678
    49
	//! \todo make the test fail, if it does not contain any asserts
markus@5679
    50
	printf("Asserts: passed %d, failed %d\n", _testAssertsPassed, _testAssertsFailed);
markus@5680
    51
markus@5679
    52
	if(_testAssertsFailed == 0 && _testAssertsPassed == 0) {
markus@5679
    53
		_testReturnValue = 2;
markus@5679
    54
	}
markus@5679
    55
markus@5672
    56
	return _testReturnValue;
markus@5667
    57
}
markus@5667
    58
markus@5680
    59
markus@5680
    60
/*!
markus@5680
    61
 *  Assert function. Tests if the expected value equals the actual value, then
markus@5680
    62
 *  the test assert succeeds, otherwise it fails and warns about it.
markus@5680
    63
 *
markus@5680
    64
 * \param expected Value user expects to have
markus@5680
    65
 * \param actual The actual value of tested variable
markus@5680
    66
 * \param message Message that will be printed if assert fails
markus@5680
    67
 */
markus@5678
    68
void
markus@5677
    69
AssertEquals(Uint32 expected, Uint32 actual, char* message, ...)
markus@5664
    70
{
markus@5677
    71
   va_list args;
markus@5677
    72
   char buf[256];
markus@5677
    73
markus@5677
    74
   if(expected != actual) {
markus@5677
    75
      va_start( args, message );
markus@5677
    76
      SDL_vsnprintf( buf, sizeof(buf), message, args );
markus@5677
    77
      va_end( args );
markus@5679
    78
      printf("Assert Equals failed: expected %d, got %d; %s\n", expected, actual, buf);
markus@5677
    79
      _testReturnValue = 1;
markus@5677
    80
      _testAssertsFailed++;
markus@5677
    81
   } else {
markus@5677
    82
      _testAssertsPassed++;
markus@5677
    83
   }
markus@5664
    84
}
markus@5664
    85
markus@5680
    86
/*!
markus@5680
    87
 *  Assert function. Tests if the given condition is true. True in
markus@5680
    88
 *  this case means non-zero value. If the condition is true, the
markus@5680
    89
 *  assert passes, otherwise it fails.
markus@5680
    90
 *
markus@5680
    91
 * \param condition Condition which will be evaluated
markus@5680
    92
 * \param message Message that will be printed if assert fails
markus@5680
    93
 */
markus@5678
    94
void
markus@5677
    95
AssertTrue(int condition, char *message, ...)
markus@5677
    96
{
markus@5677
    97
   va_list args;
markus@5677
    98
   char buf[256];
markus@5677
    99
markus@5677
   100
   if (!condition) {
markus@5677
   101
      va_start( args, message );
markus@5677
   102
      SDL_vsnprintf( buf, sizeof(buf), message, args );
markus@5677
   103
      va_end( args );
markus@5677
   104
markus@5677
   105
      printf("Assert IsTrue failed: %s\n", buf);
markus@5677
   106
      _testReturnValue = 1;
markus@5677
   107
      _testAssertsFailed++;
markus@5677
   108
   } else {
markus@5677
   109
      _testAssertsPassed++;
markus@5677
   110
   }
markus@5677
   111
}
markus@5668
   112
markus@5664
   113
#endif