test/testautomation_timer.c
changeset 6815 5fefa916e039
child 7144 c700de9c8ed4
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testautomation_timer.c	Mon Jan 14 08:14:53 2013 -0800
     1.3 @@ -0,0 +1,202 @@
     1.4 +/**
     1.5 + * Timer test suite
     1.6 + */
     1.7 +
     1.8 +#include <stdio.h>
     1.9 +
    1.10 +#include "SDL.h"
    1.11 +#include "SDL_test.h"
    1.12 +
    1.13 +/* Flag indicating if the param should be checked */
    1.14 +int _paramCheck = 0;
    1.15 +
    1.16 +/* Userdata value to check */
    1.17 +int _paramValue = 0;
    1.18 +
    1.19 +/* Flag indicating that the callback was called */
    1.20 +int _timerCallbackCalled = 0;
    1.21 +
    1.22 +/* Fixture */
    1.23 +
    1.24 +void
    1.25 +_timerSetUp(void *arg)
    1.26 +{
    1.27 +	/* Start SDL timer subsystem */
    1.28 +	int ret = SDL_InitSubSystem( SDL_INIT_TIMER );
    1.29 +        SDLTest_AssertPass("Call to SDL_InitSubSystem(SDL_INIT_TIMER)");
    1.30 +	SDLTest_AssertCheck(ret==0, "Check result from SDL_InitSubSystem(SDL_INIT_TIMER)");
    1.31 +	if (ret != 0) {
    1.32 +           SDLTest_LogError("%s", SDL_GetError());
    1.33 +        }
    1.34 +}
    1.35 +
    1.36 +/* Test case functions */
    1.37 +
    1.38 +/**
    1.39 + * @brief Call to SDL_GetPerformanceCounter
    1.40 + */
    1.41 +int
    1.42 +timer_getPerformanceCounter(void *arg)
    1.43 +{
    1.44 +  Uint64 result;
    1.45 +                                                                                                        		
    1.46 +  result = SDL_GetPerformanceCounter();
    1.47 +  SDLTest_AssertPass("Call to SDL_GetPerformanceCounter()");
    1.48 +  SDLTest_AssertCheck(result > 0, "Check result value, expected: >0, got: %lu", result);
    1.49 +  
    1.50 +  return TEST_COMPLETED;
    1.51 +}
    1.52 +
    1.53 +/**
    1.54 + * @brief Call to SDL_GetPerformanceFrequency
    1.55 + */
    1.56 +int
    1.57 +timer_getPerformanceFrequency(void *arg)
    1.58 +{
    1.59 +  Uint64 result;
    1.60 +                                                                                                        		
    1.61 +  result = SDL_GetPerformanceFrequency();
    1.62 +  SDLTest_AssertPass("Call to SDL_GetPerformanceFrequency()");
    1.63 +  SDLTest_AssertCheck(result > 0, "Check result value, expected: >0, got: %lu", result);
    1.64 +  
    1.65 +  return TEST_COMPLETED;
    1.66 +}
    1.67 +
    1.68 +/**
    1.69 + * @brief Call to SDL_Delay and SDL_GetTicks
    1.70 + */
    1.71 +int
    1.72 +timer_delayAndGetTicks(void *arg)
    1.73 +{
    1.74 +  const Uint32 testDelay = 100;
    1.75 +  const Uint32 marginOfError = 25;
    1.76 +  Uint32 result;
    1.77 +  Uint32 result2;
    1.78 +  Uint32 difference;
    1.79 +
    1.80 +  /* Zero delay */  
    1.81 +  SDL_Delay(0);
    1.82 +  SDLTest_AssertPass("Call to SDL_Delay(0)");
    1.83 +  
    1.84 +  /* Non-zero delay */
    1.85 +  SDL_Delay(1);
    1.86 +  SDLTest_AssertPass("Call to SDL_Delay(1)");
    1.87 +
    1.88 +  SDL_Delay(SDLTest_RandomIntegerInRange(5, 15));
    1.89 +  SDLTest_AssertPass("Call to SDL_Delay()");
    1.90 +  
    1.91 +  /* Get ticks count - should be non-zero by now */                                                                                                      		
    1.92 +  result = SDL_GetTicks();
    1.93 +  SDLTest_AssertPass("Call to SDL_GetTicks()");
    1.94 +  SDLTest_AssertCheck(result > 0, "Check result value, expected: >0, got: %d", result);
    1.95 +
    1.96 +  /* Delay a bit longer and mesure ticks and verify difference */
    1.97 +  SDL_Delay(testDelay);
    1.98 +  SDLTest_AssertPass("Call to SDL_Delay(%d)", testDelay);
    1.99 +  result2 = SDL_GetTicks();
   1.100 +  SDLTest_AssertPass("Call to SDL_GetTicks()");
   1.101 +  SDLTest_AssertCheck(result2 > 0, "Check result value, expected: >0, got: %d", result2);
   1.102 +  difference = result2 - result;
   1.103 +  SDLTest_AssertCheck(difference > (testDelay - marginOfError), "Check difference, expected: >%d, got: %d", testDelay - marginOfError, difference);
   1.104 +  SDLTest_AssertCheck(difference < (testDelay + marginOfError), "Check difference, expected: <%d, got: %d", testDelay + marginOfError, difference);
   1.105 +  
   1.106 +  return TEST_COMPLETED;
   1.107 +}
   1.108 +
   1.109 +/* Test callback */
   1.110 +Uint32 _timerTestCallback(Uint32 interval, void *param)
   1.111 +{
   1.112 +   _timerCallbackCalled = 1;
   1.113 +   
   1.114 +   if (_paramCheck != 0) {
   1.115 +       SDLTest_AssertCheck(param != NULL, "Check param pointer, expected: non-NULL, got: %s", (param != NULL) ? "non-NULL" : "NULL");
   1.116 +       if (param != NULL) {
   1.117 +          SDLTest_AssertCheck(*(int *)param == _paramValue, "Check param value, expected: %i, got: %i", _paramValue, *(int *)param);
   1.118 +       }
   1.119 +   }
   1.120 +   
   1.121 +   return 0;
   1.122 +  return interval;
   1.123 +}
   1.124 +
   1.125 +/**
   1.126 + * @brief Call to SDL_AddTimer and SDL_RemoveTimer
   1.127 + */
   1.128 +int
   1.129 +timer_addRemoveTimer(void *arg)
   1.130 +{
   1.131 +  SDL_TimerID id;
   1.132 +  SDL_bool result;
   1.133 +  int param;
   1.134 +
   1.135 +  /* Reset state */
   1.136 +  _paramCheck = 0;
   1.137 +  _timerCallbackCalled = 0;
   1.138 +  
   1.139 +  /* Set timer with a long delay */  
   1.140 +  id = SDL_AddTimer(10000, _timerTestCallback, NULL);
   1.141 +  SDLTest_AssertPass("Call to SDL_AddTimer(10000,...)");
   1.142 +  SDLTest_AssertCheck(id > 0, "Check result value, expected: >0, got: %d", id);
   1.143 +  
   1.144 +  /* Remove timer again and check that callback was not called */
   1.145 +  result = SDL_RemoveTimer(id);
   1.146 +  SDLTest_AssertPass("Call to SDL_RemoveTimer()");
   1.147 +  SDLTest_AssertCheck(result == SDL_TRUE, "Check result value, expected: %i, got: %i", SDL_TRUE, result);
   1.148 +  SDLTest_AssertCheck(_timerCallbackCalled == 0, "Check callback WAS NOT called, expected: 0, got: %i", _timerCallbackCalled);
   1.149 +
   1.150 +  /* Try to temove timer again (should be a NOOP) */
   1.151 +  result = SDL_RemoveTimer(id);
   1.152 +  SDLTest_AssertPass("Call to SDL_RemoveTimer()");
   1.153 +  SDLTest_AssertCheck(result == SDL_FALSE, "Check result value, expected: %i, got: %i", SDL_FALSE, result);
   1.154 +
   1.155 +  /* Reset state */
   1.156 +  param = SDLTest_RandomIntegerInRange(-1024, 1024);
   1.157 +  _paramCheck = 1;
   1.158 +  _paramValue = param;
   1.159 +  _timerCallbackCalled = 0;
   1.160 +
   1.161 +  /* Set timer with a short delay */  
   1.162 +  id = SDL_AddTimer(10, _timerTestCallback, (void *)&param);
   1.163 +  SDLTest_AssertPass("Call to SDL_AddTimer(10, param)");
   1.164 +  SDLTest_AssertCheck(id > 0, "Check result value, expected: >0, got: %d", id);
   1.165 +  
   1.166 +  /* Wait to let timer trigger callback */
   1.167 +  SDL_Delay(100);
   1.168 +  SDLTest_AssertPass("Call to SDL_Delay(100)");
   1.169 +  
   1.170 +  /* Remove timer again and check that callback was called */
   1.171 +  result = SDL_RemoveTimer(id);
   1.172 +  SDLTest_AssertPass("Call to SDL_RemoveTimer()");
   1.173 +  SDLTest_AssertCheck(result == SDL_FALSE, "Check result value, expected: %i, got: %i", SDL_FALSE, result);
   1.174 +  SDLTest_AssertCheck(_timerCallbackCalled == 1, "Check callback WAS called, expected: 1, got: %i", _timerCallbackCalled);
   1.175 +    
   1.176 +  return TEST_COMPLETED;
   1.177 +}
   1.178 +
   1.179 +/* ================= Test References ================== */
   1.180 +
   1.181 +/* Timer test cases */
   1.182 +static const SDLTest_TestCaseReference timerTest1 =
   1.183 +		{ (SDLTest_TestCaseFp)timer_getPerformanceCounter, "timer_getPerformanceCounter", "Call to SDL_GetPerformanceCounter", TEST_ENABLED };
   1.184 +
   1.185 +static const SDLTest_TestCaseReference timerTest2 =
   1.186 +		{ (SDLTest_TestCaseFp)timer_getPerformanceFrequency, "timer_getPerformanceFrequency", "Call to SDL_GetPerformanceFrequency", TEST_ENABLED };
   1.187 +
   1.188 +static const SDLTest_TestCaseReference timerTest3 =
   1.189 +		{ (SDLTest_TestCaseFp)timer_delayAndGetTicks, "timer_delayAndGetTicks", "Call to SDL_Delay and SDL_GetTicks", TEST_ENABLED };
   1.190 +
   1.191 +static const SDLTest_TestCaseReference timerTest4 =
   1.192 +		{ (SDLTest_TestCaseFp)timer_addRemoveTimer, "timer_addRemoveTimer", "Call to SDL_AddTimer and SDL_RemoveTimer", TEST_ENABLED };
   1.193 +
   1.194 +/* Sequence of Timer test cases */
   1.195 +static const SDLTest_TestCaseReference *timerTests[] =  {
   1.196 +	&timerTest1, &timerTest2, &timerTest3, &timerTest4, NULL
   1.197 +};
   1.198 +
   1.199 +/* Timer test suite (global) */
   1.200 +SDLTest_TestSuiteReference timerTestSuite = {
   1.201 +	"Timer",
   1.202 +	_timerSetUp,
   1.203 +	timerTests,
   1.204 +	NULL
   1.205 +};