Added timer test suite
authorAndreas Schiffler <aschiffler@ferzkopp.net>
Mon, 14 Jan 2013 08:14:53 -0800
changeset 68155fefa916e039
parent 6814 fccdef204fcf
child 6816 b3d3ef1e15b5
Added timer test suite
VisualC/tests/testautomation/testautomation_vs2010.vcxproj
VisualC/tests/testautomation/testautomation_vs2012.vcxproj
test/Makefile.in
test/testautomation_suites.h
test/testautomation_timer.c
     1.1 --- a/VisualC/tests/testautomation/testautomation_vs2010.vcxproj	Mon Jan 14 07:20:55 2013 -0800
     1.2 +++ b/VisualC/tests/testautomation/testautomation_vs2010.vcxproj	Mon Jan 14 08:14:53 2013 -0800
     1.3 @@ -189,6 +189,7 @@
     1.4      <ClCompile Include="..\..\..\test\testautomation_video.c" />
     1.5      <ClCompile Include="..\..\..\test\testautomation_syswm.c" />
     1.6      <ClCompile Include="..\..\..\test\testautomation_mouse.c" />
     1.7 +    <ClCompile Include="..\..\..\test\testautomation_timer.c" />
     1.8    </ItemGroup>
     1.9    <ItemGroup>
    1.10      <ClInclude Include="..\..\..\test\testautomation_suites.h" />
     2.1 --- a/VisualC/tests/testautomation/testautomation_vs2012.vcxproj	Mon Jan 14 07:20:55 2013 -0800
     2.2 +++ b/VisualC/tests/testautomation/testautomation_vs2012.vcxproj	Mon Jan 14 08:14:53 2013 -0800
     2.3 @@ -193,6 +193,7 @@
     2.4      <ClCompile Include="..\..\..\test\testautomation_video.c" />
     2.5      <ClCompile Include="..\..\..\test\testautomation_syswm.c" />
     2.6      <ClCompile Include="..\..\..\test\testautomation_mouse.c" />
     2.7 +    <ClCompile Include="..\..\..\test\testautomation_timer.c" />
     2.8    </ItemGroup>
     2.9    <ItemGroup>
    2.10      <ClInclude Include="..\..\..\test\testautomation_suites.h" />
     3.1 --- a/test/Makefile.in	Mon Jan 14 07:20:55 2013 -0800
     3.2 +++ b/test/Makefile.in	Mon Jan 14 08:14:53 2013 -0800
     3.3 @@ -80,7 +80,8 @@
     3.4  		      $(srcdir)/testautomation_keyboard.c \
     3.5  		      $(srcdir)/testautomation_video.c \
     3.6  		      $(srcdir)/testautomation_syswm.c \
     3.7 -		      $(srcdir)/testautomation_mouse.c
     3.8 +		      $(srcdir)/testautomation_mouse.c \
     3.9 +		      $(srcdir)/testautomation_timer.c
    3.10  	$(CC) -o $@ $^ $(CFLAGS) -lSDL2_test $(LIBS) 
    3.11  
    3.12  testmultiaudio$(EXE): $(srcdir)/testmultiaudio.c
     4.1 --- a/test/testautomation_suites.h	Mon Jan 14 07:20:55 2013 -0800
     4.2 +++ b/test/testautomation_suites.h	Mon Jan 14 08:14:53 2013 -0800
     4.3 @@ -21,6 +21,7 @@
     4.4  extern SDLTest_TestSuiteReference syswmTestSuite;
     4.5  extern SDLTest_TestSuiteReference videoTestSuite;
     4.6  extern SDLTest_TestSuiteReference mouseTestSuite;
     4.7 +extern SDLTest_TestSuiteReference timerTestSuite;
     4.8  
     4.9  // All test suites
    4.10  SDLTest_TestSuiteReference *testSuites[] =  {
    4.11 @@ -36,6 +37,7 @@
    4.12  	&syswmTestSuite,
    4.13  	&videoTestSuite,
    4.14  	&mouseTestSuite,
    4.15 +	&timerTestSuite,
    4.16  	NULL
    4.17  };
    4.18  
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/test/testautomation_timer.c	Mon Jan 14 08:14:53 2013 -0800
     5.3 @@ -0,0 +1,202 @@
     5.4 +/**
     5.5 + * Timer test suite
     5.6 + */
     5.7 +
     5.8 +#include <stdio.h>
     5.9 +
    5.10 +#include "SDL.h"
    5.11 +#include "SDL_test.h"
    5.12 +
    5.13 +/* Flag indicating if the param should be checked */
    5.14 +int _paramCheck = 0;
    5.15 +
    5.16 +/* Userdata value to check */
    5.17 +int _paramValue = 0;
    5.18 +
    5.19 +/* Flag indicating that the callback was called */
    5.20 +int _timerCallbackCalled = 0;
    5.21 +
    5.22 +/* Fixture */
    5.23 +
    5.24 +void
    5.25 +_timerSetUp(void *arg)
    5.26 +{
    5.27 +	/* Start SDL timer subsystem */
    5.28 +	int ret = SDL_InitSubSystem( SDL_INIT_TIMER );
    5.29 +        SDLTest_AssertPass("Call to SDL_InitSubSystem(SDL_INIT_TIMER)");
    5.30 +	SDLTest_AssertCheck(ret==0, "Check result from SDL_InitSubSystem(SDL_INIT_TIMER)");
    5.31 +	if (ret != 0) {
    5.32 +           SDLTest_LogError("%s", SDL_GetError());
    5.33 +        }
    5.34 +}
    5.35 +
    5.36 +/* Test case functions */
    5.37 +
    5.38 +/**
    5.39 + * @brief Call to SDL_GetPerformanceCounter
    5.40 + */
    5.41 +int
    5.42 +timer_getPerformanceCounter(void *arg)
    5.43 +{
    5.44 +  Uint64 result;
    5.45 +                                                                                                        		
    5.46 +  result = SDL_GetPerformanceCounter();
    5.47 +  SDLTest_AssertPass("Call to SDL_GetPerformanceCounter()");
    5.48 +  SDLTest_AssertCheck(result > 0, "Check result value, expected: >0, got: %lu", result);
    5.49 +  
    5.50 +  return TEST_COMPLETED;
    5.51 +}
    5.52 +
    5.53 +/**
    5.54 + * @brief Call to SDL_GetPerformanceFrequency
    5.55 + */
    5.56 +int
    5.57 +timer_getPerformanceFrequency(void *arg)
    5.58 +{
    5.59 +  Uint64 result;
    5.60 +                                                                                                        		
    5.61 +  result = SDL_GetPerformanceFrequency();
    5.62 +  SDLTest_AssertPass("Call to SDL_GetPerformanceFrequency()");
    5.63 +  SDLTest_AssertCheck(result > 0, "Check result value, expected: >0, got: %lu", result);
    5.64 +  
    5.65 +  return TEST_COMPLETED;
    5.66 +}
    5.67 +
    5.68 +/**
    5.69 + * @brief Call to SDL_Delay and SDL_GetTicks
    5.70 + */
    5.71 +int
    5.72 +timer_delayAndGetTicks(void *arg)
    5.73 +{
    5.74 +  const Uint32 testDelay = 100;
    5.75 +  const Uint32 marginOfError = 25;
    5.76 +  Uint32 result;
    5.77 +  Uint32 result2;
    5.78 +  Uint32 difference;
    5.79 +
    5.80 +  /* Zero delay */  
    5.81 +  SDL_Delay(0);
    5.82 +  SDLTest_AssertPass("Call to SDL_Delay(0)");
    5.83 +  
    5.84 +  /* Non-zero delay */
    5.85 +  SDL_Delay(1);
    5.86 +  SDLTest_AssertPass("Call to SDL_Delay(1)");
    5.87 +
    5.88 +  SDL_Delay(SDLTest_RandomIntegerInRange(5, 15));
    5.89 +  SDLTest_AssertPass("Call to SDL_Delay()");
    5.90 +  
    5.91 +  /* Get ticks count - should be non-zero by now */                                                                                                      		
    5.92 +  result = SDL_GetTicks();
    5.93 +  SDLTest_AssertPass("Call to SDL_GetTicks()");
    5.94 +  SDLTest_AssertCheck(result > 0, "Check result value, expected: >0, got: %d", result);
    5.95 +
    5.96 +  /* Delay a bit longer and mesure ticks and verify difference */
    5.97 +  SDL_Delay(testDelay);
    5.98 +  SDLTest_AssertPass("Call to SDL_Delay(%d)", testDelay);
    5.99 +  result2 = SDL_GetTicks();
   5.100 +  SDLTest_AssertPass("Call to SDL_GetTicks()");
   5.101 +  SDLTest_AssertCheck(result2 > 0, "Check result value, expected: >0, got: %d", result2);
   5.102 +  difference = result2 - result;
   5.103 +  SDLTest_AssertCheck(difference > (testDelay - marginOfError), "Check difference, expected: >%d, got: %d", testDelay - marginOfError, difference);
   5.104 +  SDLTest_AssertCheck(difference < (testDelay + marginOfError), "Check difference, expected: <%d, got: %d", testDelay + marginOfError, difference);
   5.105 +  
   5.106 +  return TEST_COMPLETED;
   5.107 +}
   5.108 +
   5.109 +/* Test callback */
   5.110 +Uint32 _timerTestCallback(Uint32 interval, void *param)
   5.111 +{
   5.112 +   _timerCallbackCalled = 1;
   5.113 +   
   5.114 +   if (_paramCheck != 0) {
   5.115 +       SDLTest_AssertCheck(param != NULL, "Check param pointer, expected: non-NULL, got: %s", (param != NULL) ? "non-NULL" : "NULL");
   5.116 +       if (param != NULL) {
   5.117 +          SDLTest_AssertCheck(*(int *)param == _paramValue, "Check param value, expected: %i, got: %i", _paramValue, *(int *)param);
   5.118 +       }
   5.119 +   }
   5.120 +   
   5.121 +   return 0;
   5.122 +  return interval;
   5.123 +}
   5.124 +
   5.125 +/**
   5.126 + * @brief Call to SDL_AddTimer and SDL_RemoveTimer
   5.127 + */
   5.128 +int
   5.129 +timer_addRemoveTimer(void *arg)
   5.130 +{
   5.131 +  SDL_TimerID id;
   5.132 +  SDL_bool result;
   5.133 +  int param;
   5.134 +
   5.135 +  /* Reset state */
   5.136 +  _paramCheck = 0;
   5.137 +  _timerCallbackCalled = 0;
   5.138 +  
   5.139 +  /* Set timer with a long delay */  
   5.140 +  id = SDL_AddTimer(10000, _timerTestCallback, NULL);
   5.141 +  SDLTest_AssertPass("Call to SDL_AddTimer(10000,...)");
   5.142 +  SDLTest_AssertCheck(id > 0, "Check result value, expected: >0, got: %d", id);
   5.143 +  
   5.144 +  /* Remove timer again and check that callback was not called */
   5.145 +  result = SDL_RemoveTimer(id);
   5.146 +  SDLTest_AssertPass("Call to SDL_RemoveTimer()");
   5.147 +  SDLTest_AssertCheck(result == SDL_TRUE, "Check result value, expected: %i, got: %i", SDL_TRUE, result);
   5.148 +  SDLTest_AssertCheck(_timerCallbackCalled == 0, "Check callback WAS NOT called, expected: 0, got: %i", _timerCallbackCalled);
   5.149 +
   5.150 +  /* Try to temove timer again (should be a NOOP) */
   5.151 +  result = SDL_RemoveTimer(id);
   5.152 +  SDLTest_AssertPass("Call to SDL_RemoveTimer()");
   5.153 +  SDLTest_AssertCheck(result == SDL_FALSE, "Check result value, expected: %i, got: %i", SDL_FALSE, result);
   5.154 +
   5.155 +  /* Reset state */
   5.156 +  param = SDLTest_RandomIntegerInRange(-1024, 1024);
   5.157 +  _paramCheck = 1;
   5.158 +  _paramValue = param;
   5.159 +  _timerCallbackCalled = 0;
   5.160 +
   5.161 +  /* Set timer with a short delay */  
   5.162 +  id = SDL_AddTimer(10, _timerTestCallback, (void *)&param);
   5.163 +  SDLTest_AssertPass("Call to SDL_AddTimer(10, param)");
   5.164 +  SDLTest_AssertCheck(id > 0, "Check result value, expected: >0, got: %d", id);
   5.165 +  
   5.166 +  /* Wait to let timer trigger callback */
   5.167 +  SDL_Delay(100);
   5.168 +  SDLTest_AssertPass("Call to SDL_Delay(100)");
   5.169 +  
   5.170 +  /* Remove timer again and check that callback was called */
   5.171 +  result = SDL_RemoveTimer(id);
   5.172 +  SDLTest_AssertPass("Call to SDL_RemoveTimer()");
   5.173 +  SDLTest_AssertCheck(result == SDL_FALSE, "Check result value, expected: %i, got: %i", SDL_FALSE, result);
   5.174 +  SDLTest_AssertCheck(_timerCallbackCalled == 1, "Check callback WAS called, expected: 1, got: %i", _timerCallbackCalled);
   5.175 +    
   5.176 +  return TEST_COMPLETED;
   5.177 +}
   5.178 +
   5.179 +/* ================= Test References ================== */
   5.180 +
   5.181 +/* Timer test cases */
   5.182 +static const SDLTest_TestCaseReference timerTest1 =
   5.183 +		{ (SDLTest_TestCaseFp)timer_getPerformanceCounter, "timer_getPerformanceCounter", "Call to SDL_GetPerformanceCounter", TEST_ENABLED };
   5.184 +
   5.185 +static const SDLTest_TestCaseReference timerTest2 =
   5.186 +		{ (SDLTest_TestCaseFp)timer_getPerformanceFrequency, "timer_getPerformanceFrequency", "Call to SDL_GetPerformanceFrequency", TEST_ENABLED };
   5.187 +
   5.188 +static const SDLTest_TestCaseReference timerTest3 =
   5.189 +		{ (SDLTest_TestCaseFp)timer_delayAndGetTicks, "timer_delayAndGetTicks", "Call to SDL_Delay and SDL_GetTicks", TEST_ENABLED };
   5.190 +
   5.191 +static const SDLTest_TestCaseReference timerTest4 =
   5.192 +		{ (SDLTest_TestCaseFp)timer_addRemoveTimer, "timer_addRemoveTimer", "Call to SDL_AddTimer and SDL_RemoveTimer", TEST_ENABLED };
   5.193 +
   5.194 +/* Sequence of Timer test cases */
   5.195 +static const SDLTest_TestCaseReference *timerTests[] =  {
   5.196 +	&timerTest1, &timerTest2, &timerTest3, &timerTest4, NULL
   5.197 +};
   5.198 +
   5.199 +/* Timer test suite (global) */
   5.200 +SDLTest_TestSuiteReference timerTestSuite = {
   5.201 +	"Timer",
   5.202 +	_timerSetUp,
   5.203 +	timerTests,
   5.204 +	NULL
   5.205 +};