test/testautomation_timer.c
author Andreas Schiffler <aschiffler@ferzkopp.net>
Mon, 14 Jan 2013 08:14:53 -0800
changeset 6815 5fefa916e039
child 7144 c700de9c8ed4
permissions -rw-r--r--
Added timer test suite
     1 /**
     2  * Timer test suite
     3  */
     4 
     5 #include <stdio.h>
     6 
     7 #include "SDL.h"
     8 #include "SDL_test.h"
     9 
    10 /* Flag indicating if the param should be checked */
    11 int _paramCheck = 0;
    12 
    13 /* Userdata value to check */
    14 int _paramValue = 0;
    15 
    16 /* Flag indicating that the callback was called */
    17 int _timerCallbackCalled = 0;
    18 
    19 /* Fixture */
    20 
    21 void
    22 _timerSetUp(void *arg)
    23 {
    24 	/* Start SDL timer subsystem */
    25 	int ret = SDL_InitSubSystem( SDL_INIT_TIMER );
    26         SDLTest_AssertPass("Call to SDL_InitSubSystem(SDL_INIT_TIMER)");
    27 	SDLTest_AssertCheck(ret==0, "Check result from SDL_InitSubSystem(SDL_INIT_TIMER)");
    28 	if (ret != 0) {
    29            SDLTest_LogError("%s", SDL_GetError());
    30         }
    31 }
    32 
    33 /* Test case functions */
    34 
    35 /**
    36  * @brief Call to SDL_GetPerformanceCounter
    37  */
    38 int
    39 timer_getPerformanceCounter(void *arg)
    40 {
    41   Uint64 result;
    42                                                                                                         		
    43   result = SDL_GetPerformanceCounter();
    44   SDLTest_AssertPass("Call to SDL_GetPerformanceCounter()");
    45   SDLTest_AssertCheck(result > 0, "Check result value, expected: >0, got: %lu", result);
    46   
    47   return TEST_COMPLETED;
    48 }
    49 
    50 /**
    51  * @brief Call to SDL_GetPerformanceFrequency
    52  */
    53 int
    54 timer_getPerformanceFrequency(void *arg)
    55 {
    56   Uint64 result;
    57                                                                                                         		
    58   result = SDL_GetPerformanceFrequency();
    59   SDLTest_AssertPass("Call to SDL_GetPerformanceFrequency()");
    60   SDLTest_AssertCheck(result > 0, "Check result value, expected: >0, got: %lu", result);
    61   
    62   return TEST_COMPLETED;
    63 }
    64 
    65 /**
    66  * @brief Call to SDL_Delay and SDL_GetTicks
    67  */
    68 int
    69 timer_delayAndGetTicks(void *arg)
    70 {
    71   const Uint32 testDelay = 100;
    72   const Uint32 marginOfError = 25;
    73   Uint32 result;
    74   Uint32 result2;
    75   Uint32 difference;
    76 
    77   /* Zero delay */  
    78   SDL_Delay(0);
    79   SDLTest_AssertPass("Call to SDL_Delay(0)");
    80   
    81   /* Non-zero delay */
    82   SDL_Delay(1);
    83   SDLTest_AssertPass("Call to SDL_Delay(1)");
    84 
    85   SDL_Delay(SDLTest_RandomIntegerInRange(5, 15));
    86   SDLTest_AssertPass("Call to SDL_Delay()");
    87   
    88   /* Get ticks count - should be non-zero by now */                                                                                                      		
    89   result = SDL_GetTicks();
    90   SDLTest_AssertPass("Call to SDL_GetTicks()");
    91   SDLTest_AssertCheck(result > 0, "Check result value, expected: >0, got: %d", result);
    92 
    93   /* Delay a bit longer and mesure ticks and verify difference */
    94   SDL_Delay(testDelay);
    95   SDLTest_AssertPass("Call to SDL_Delay(%d)", testDelay);
    96   result2 = SDL_GetTicks();
    97   SDLTest_AssertPass("Call to SDL_GetTicks()");
    98   SDLTest_AssertCheck(result2 > 0, "Check result value, expected: >0, got: %d", result2);
    99   difference = result2 - result;
   100   SDLTest_AssertCheck(difference > (testDelay - marginOfError), "Check difference, expected: >%d, got: %d", testDelay - marginOfError, difference);
   101   SDLTest_AssertCheck(difference < (testDelay + marginOfError), "Check difference, expected: <%d, got: %d", testDelay + marginOfError, difference);
   102   
   103   return TEST_COMPLETED;
   104 }
   105 
   106 /* Test callback */
   107 Uint32 _timerTestCallback(Uint32 interval, void *param)
   108 {
   109    _timerCallbackCalled = 1;
   110    
   111    if (_paramCheck != 0) {
   112        SDLTest_AssertCheck(param != NULL, "Check param pointer, expected: non-NULL, got: %s", (param != NULL) ? "non-NULL" : "NULL");
   113        if (param != NULL) {
   114           SDLTest_AssertCheck(*(int *)param == _paramValue, "Check param value, expected: %i, got: %i", _paramValue, *(int *)param);
   115        }
   116    }
   117    
   118    return 0;
   119   return interval;
   120 }
   121 
   122 /**
   123  * @brief Call to SDL_AddTimer and SDL_RemoveTimer
   124  */
   125 int
   126 timer_addRemoveTimer(void *arg)
   127 {
   128   SDL_TimerID id;
   129   SDL_bool result;
   130   int param;
   131 
   132   /* Reset state */
   133   _paramCheck = 0;
   134   _timerCallbackCalled = 0;
   135   
   136   /* Set timer with a long delay */  
   137   id = SDL_AddTimer(10000, _timerTestCallback, NULL);
   138   SDLTest_AssertPass("Call to SDL_AddTimer(10000,...)");
   139   SDLTest_AssertCheck(id > 0, "Check result value, expected: >0, got: %d", id);
   140   
   141   /* Remove timer again and check that callback was not called */
   142   result = SDL_RemoveTimer(id);
   143   SDLTest_AssertPass("Call to SDL_RemoveTimer()");
   144   SDLTest_AssertCheck(result == SDL_TRUE, "Check result value, expected: %i, got: %i", SDL_TRUE, result);
   145   SDLTest_AssertCheck(_timerCallbackCalled == 0, "Check callback WAS NOT called, expected: 0, got: %i", _timerCallbackCalled);
   146 
   147   /* Try to temove timer again (should be a NOOP) */
   148   result = SDL_RemoveTimer(id);
   149   SDLTest_AssertPass("Call to SDL_RemoveTimer()");
   150   SDLTest_AssertCheck(result == SDL_FALSE, "Check result value, expected: %i, got: %i", SDL_FALSE, result);
   151 
   152   /* Reset state */
   153   param = SDLTest_RandomIntegerInRange(-1024, 1024);
   154   _paramCheck = 1;
   155   _paramValue = param;
   156   _timerCallbackCalled = 0;
   157 
   158   /* Set timer with a short delay */  
   159   id = SDL_AddTimer(10, _timerTestCallback, (void *)&param);
   160   SDLTest_AssertPass("Call to SDL_AddTimer(10, param)");
   161   SDLTest_AssertCheck(id > 0, "Check result value, expected: >0, got: %d", id);
   162   
   163   /* Wait to let timer trigger callback */
   164   SDL_Delay(100);
   165   SDLTest_AssertPass("Call to SDL_Delay(100)");
   166   
   167   /* Remove timer again and check that callback was called */
   168   result = SDL_RemoveTimer(id);
   169   SDLTest_AssertPass("Call to SDL_RemoveTimer()");
   170   SDLTest_AssertCheck(result == SDL_FALSE, "Check result value, expected: %i, got: %i", SDL_FALSE, result);
   171   SDLTest_AssertCheck(_timerCallbackCalled == 1, "Check callback WAS called, expected: 1, got: %i", _timerCallbackCalled);
   172     
   173   return TEST_COMPLETED;
   174 }
   175 
   176 /* ================= Test References ================== */
   177 
   178 /* Timer test cases */
   179 static const SDLTest_TestCaseReference timerTest1 =
   180 		{ (SDLTest_TestCaseFp)timer_getPerformanceCounter, "timer_getPerformanceCounter", "Call to SDL_GetPerformanceCounter", TEST_ENABLED };
   181 
   182 static const SDLTest_TestCaseReference timerTest2 =
   183 		{ (SDLTest_TestCaseFp)timer_getPerformanceFrequency, "timer_getPerformanceFrequency", "Call to SDL_GetPerformanceFrequency", TEST_ENABLED };
   184 
   185 static const SDLTest_TestCaseReference timerTest3 =
   186 		{ (SDLTest_TestCaseFp)timer_delayAndGetTicks, "timer_delayAndGetTicks", "Call to SDL_Delay and SDL_GetTicks", TEST_ENABLED };
   187 
   188 static const SDLTest_TestCaseReference timerTest4 =
   189 		{ (SDLTest_TestCaseFp)timer_addRemoveTimer, "timer_addRemoveTimer", "Call to SDL_AddTimer and SDL_RemoveTimer", TEST_ENABLED };
   190 
   191 /* Sequence of Timer test cases */
   192 static const SDLTest_TestCaseReference *timerTests[] =  {
   193 	&timerTest1, &timerTest2, &timerTest3, &timerTest4, NULL
   194 };
   195 
   196 /* Timer test suite (global) */
   197 SDLTest_TestSuiteReference timerTestSuite = {
   198 	"Timer",
   199 	_timerSetUp,
   200 	timerTests,
   201 	NULL
   202 };