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.
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@5679
    51
	if(_testAssertsFailed == 0 && _testAssertsPassed == 0) {
markus@5679
    52
		_testReturnValue = 2;
markus@5679
    53
	}
markus@5679
    54
markus@5672
    55
	return _testReturnValue;
markus@5667
    56
}
markus@5667
    57
markus@5678
    58
void
markus@5677
    59
AssertEquals(Uint32 expected, Uint32 actual, char* message, ...)
markus@5664
    60
{
markus@5677
    61
   va_list args;
markus@5677
    62
   char buf[256];
markus@5677
    63
markus@5677
    64
   if(expected != actual) {
markus@5677
    65
      va_start( args, message );
markus@5677
    66
      SDL_vsnprintf( buf, sizeof(buf), message, args );
markus@5677
    67
      va_end( args );
markus@5679
    68
      printf("Assert Equals failed: expected %d, got %d; %s\n", expected, actual, buf);
markus@5677
    69
      _testReturnValue = 1;
markus@5677
    70
      _testAssertsFailed++;
markus@5677
    71
   } else {
markus@5677
    72
      _testAssertsPassed++;
markus@5677
    73
   }
markus@5664
    74
}
markus@5664
    75
markus@5678
    76
void
markus@5677
    77
AssertTrue(int condition, char *message, ...)
markus@5677
    78
{
markus@5677
    79
   va_list args;
markus@5677
    80
   char buf[256];
markus@5677
    81
markus@5677
    82
   if (!condition) {
markus@5677
    83
      va_start( args, message );
markus@5677
    84
      SDL_vsnprintf( buf, sizeof(buf), message, args );
markus@5677
    85
      va_end( args );
markus@5677
    86
markus@5677
    87
      printf("Assert IsTrue failed: %s\n", buf);
markus@5677
    88
      _testReturnValue = 1;
markus@5677
    89
      _testAssertsFailed++;
markus@5677
    90
   } else {
markus@5677
    91
      _testAssertsPassed++;
markus@5677
    92
   }
markus@5677
    93
}
markus@5668
    94
markus@5664
    95
#endif