test/testautomation_timer.c
changeset 7191 75360622e65f
parent 7156 0758e28b08f3
child 9254 6c469ea796e4
equal deleted inserted replaced
7190:11612d544fcd 7191:75360622e65f
    19 /* Fixture */
    19 /* Fixture */
    20 
    20 
    21 void
    21 void
    22 _timerSetUp(void *arg)
    22 _timerSetUp(void *arg)
    23 {
    23 {
    24 	/* Start SDL timer subsystem */
    24     /* Start SDL timer subsystem */
    25 	int ret = SDL_InitSubSystem( SDL_INIT_TIMER );
    25     int ret = SDL_InitSubSystem( SDL_INIT_TIMER );
    26         SDLTest_AssertPass("Call to 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)");
    27     SDLTest_AssertCheck(ret==0, "Check result from SDL_InitSubSystem(SDL_INIT_TIMER)");
    28 	if (ret != 0) {
    28     if (ret != 0) {
    29            SDLTest_LogError("%s", SDL_GetError());
    29            SDLTest_LogError("%s", SDL_GetError());
    30         }
    30         }
    31 }
    31 }
    32 
    32 
    33 /* Test case functions */
    33 /* Test case functions */
    37  */
    37  */
    38 int
    38 int
    39 timer_getPerformanceCounter(void *arg)
    39 timer_getPerformanceCounter(void *arg)
    40 {
    40 {
    41   Uint64 result;
    41   Uint64 result;
    42                                                                                                         		
    42 
    43   result = SDL_GetPerformanceCounter();
    43   result = SDL_GetPerformanceCounter();
    44   SDLTest_AssertPass("Call to SDL_GetPerformanceCounter()");
    44   SDLTest_AssertPass("Call to SDL_GetPerformanceCounter()");
    45   SDLTest_AssertCheck(result > 0, "Check result value, expected: >0, got: %lu", result);
    45   SDLTest_AssertCheck(result > 0, "Check result value, expected: >0, got: %lu", result);
    46   
    46 
    47   return TEST_COMPLETED;
    47   return TEST_COMPLETED;
    48 }
    48 }
    49 
    49 
    50 /**
    50 /**
    51  * @brief Call to SDL_GetPerformanceFrequency
    51  * @brief Call to SDL_GetPerformanceFrequency
    52  */
    52  */
    53 int
    53 int
    54 timer_getPerformanceFrequency(void *arg)
    54 timer_getPerformanceFrequency(void *arg)
    55 {
    55 {
    56   Uint64 result;
    56   Uint64 result;
    57                                                                                                         		
    57 
    58   result = SDL_GetPerformanceFrequency();
    58   result = SDL_GetPerformanceFrequency();
    59   SDLTest_AssertPass("Call to SDL_GetPerformanceFrequency()");
    59   SDLTest_AssertPass("Call to SDL_GetPerformanceFrequency()");
    60   SDLTest_AssertCheck(result > 0, "Check result value, expected: >0, got: %lu", result);
    60   SDLTest_AssertCheck(result > 0, "Check result value, expected: >0, got: %lu", result);
    61   
    61 
    62   return TEST_COMPLETED;
    62   return TEST_COMPLETED;
    63 }
    63 }
    64 
    64 
    65 /**
    65 /**
    66  * @brief Call to SDL_Delay and SDL_GetTicks
    66  * @brief Call to SDL_Delay and SDL_GetTicks
    72   const Uint32 marginOfError = 25;
    72   const Uint32 marginOfError = 25;
    73   Uint32 result;
    73   Uint32 result;
    74   Uint32 result2;
    74   Uint32 result2;
    75   Uint32 difference;
    75   Uint32 difference;
    76 
    76 
    77   /* Zero delay */  
    77   /* Zero delay */
    78   SDL_Delay(0);
    78   SDL_Delay(0);
    79   SDLTest_AssertPass("Call to SDL_Delay(0)");
    79   SDLTest_AssertPass("Call to SDL_Delay(0)");
    80   
    80 
    81   /* Non-zero delay */
    81   /* Non-zero delay */
    82   SDL_Delay(1);
    82   SDL_Delay(1);
    83   SDLTest_AssertPass("Call to SDL_Delay(1)");
    83   SDLTest_AssertPass("Call to SDL_Delay(1)");
    84 
    84 
    85   SDL_Delay(SDLTest_RandomIntegerInRange(5, 15));
    85   SDL_Delay(SDLTest_RandomIntegerInRange(5, 15));
    86   SDLTest_AssertPass("Call to SDL_Delay()");
    86   SDLTest_AssertPass("Call to SDL_Delay()");
    87   
    87 
    88   /* Get ticks count - should be non-zero by now */                                                                                                      		
    88   /* Get ticks count - should be non-zero by now */
    89   result = SDL_GetTicks();
    89   result = SDL_GetTicks();
    90   SDLTest_AssertPass("Call to SDL_GetTicks()");
    90   SDLTest_AssertPass("Call to SDL_GetTicks()");
    91   SDLTest_AssertCheck(result > 0, "Check result value, expected: >0, got: %d", result);
    91   SDLTest_AssertCheck(result > 0, "Check result value, expected: >0, got: %d", result);
    92 
    92 
    93   /* Delay a bit longer and measure ticks and verify difference */
    93   /* Delay a bit longer and measure ticks and verify difference */
    97   SDLTest_AssertPass("Call to SDL_GetTicks()");
    97   SDLTest_AssertPass("Call to SDL_GetTicks()");
    98   SDLTest_AssertCheck(result2 > 0, "Check result value, expected: >0, got: %d", result2);
    98   SDLTest_AssertCheck(result2 > 0, "Check result value, expected: >0, got: %d", result2);
    99   difference = result2 - result;
    99   difference = result2 - result;
   100   SDLTest_AssertCheck(difference > (testDelay - marginOfError), "Check difference, expected: >%d, got: %d", testDelay - marginOfError, difference);
   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);
   101   SDLTest_AssertCheck(difference < (testDelay + marginOfError), "Check difference, expected: <%d, got: %d", testDelay + marginOfError, difference);
   102   
   102 
   103   return TEST_COMPLETED;
   103   return TEST_COMPLETED;
   104 }
   104 }
   105 
   105 
   106 /* Test callback */
   106 /* Test callback */
   107 Uint32 _timerTestCallback(Uint32 interval, void *param)
   107 Uint32 _timerTestCallback(Uint32 interval, void *param)
   108 {
   108 {
   109    _timerCallbackCalled = 1;
   109    _timerCallbackCalled = 1;
   110    
   110 
   111    if (_paramCheck != 0) {
   111    if (_paramCheck != 0) {
   112        SDLTest_AssertCheck(param != NULL, "Check param pointer, expected: non-NULL, got: %s", (param != NULL) ? "non-NULL" : "NULL");
   112        SDLTest_AssertCheck(param != NULL, "Check param pointer, expected: non-NULL, got: %s", (param != NULL) ? "non-NULL" : "NULL");
   113        if (param != NULL) {
   113        if (param != NULL) {
   114           SDLTest_AssertCheck(*(int *)param == _paramValue, "Check param value, expected: %i, got: %i", _paramValue, *(int *)param);
   114           SDLTest_AssertCheck(*(int *)param == _paramValue, "Check param value, expected: %i, got: %i", _paramValue, *(int *)param);
   115        }
   115        }
   116    }
   116    }
   117    
   117 
   118    return 0;
   118    return 0;
   119 }
   119 }
   120 
   120 
   121 /**
   121 /**
   122  * @brief Call to SDL_AddTimer and SDL_RemoveTimer
   122  * @brief Call to SDL_AddTimer and SDL_RemoveTimer
   129   int param;
   129   int param;
   130 
   130 
   131   /* Reset state */
   131   /* Reset state */
   132   _paramCheck = 0;
   132   _paramCheck = 0;
   133   _timerCallbackCalled = 0;
   133   _timerCallbackCalled = 0;
   134   
   134 
   135   /* Set timer with a long delay */  
   135   /* Set timer with a long delay */
   136   id = SDL_AddTimer(10000, _timerTestCallback, NULL);
   136   id = SDL_AddTimer(10000, _timerTestCallback, NULL);
   137   SDLTest_AssertPass("Call to SDL_AddTimer(10000,...)");
   137   SDLTest_AssertPass("Call to SDL_AddTimer(10000,...)");
   138   SDLTest_AssertCheck(id > 0, "Check result value, expected: >0, got: %d", id);
   138   SDLTest_AssertCheck(id > 0, "Check result value, expected: >0, got: %d", id);
   139   
   139 
   140   /* Remove timer again and check that callback was not called */
   140   /* Remove timer again and check that callback was not called */
   141   result = SDL_RemoveTimer(id);
   141   result = SDL_RemoveTimer(id);
   142   SDLTest_AssertPass("Call to SDL_RemoveTimer()");
   142   SDLTest_AssertPass("Call to SDL_RemoveTimer()");
   143   SDLTest_AssertCheck(result == SDL_TRUE, "Check result value, expected: %i, got: %i", SDL_TRUE, result);
   143   SDLTest_AssertCheck(result == SDL_TRUE, "Check result value, expected: %i, got: %i", SDL_TRUE, result);
   144   SDLTest_AssertCheck(_timerCallbackCalled == 0, "Check callback WAS NOT called, expected: 0, got: %i", _timerCallbackCalled);
   144   SDLTest_AssertCheck(_timerCallbackCalled == 0, "Check callback WAS NOT called, expected: 0, got: %i", _timerCallbackCalled);
   152   param = SDLTest_RandomIntegerInRange(-1024, 1024);
   152   param = SDLTest_RandomIntegerInRange(-1024, 1024);
   153   _paramCheck = 1;
   153   _paramCheck = 1;
   154   _paramValue = param;
   154   _paramValue = param;
   155   _timerCallbackCalled = 0;
   155   _timerCallbackCalled = 0;
   156 
   156 
   157   /* Set timer with a short delay */  
   157   /* Set timer with a short delay */
   158   id = SDL_AddTimer(10, _timerTestCallback, (void *)&param);
   158   id = SDL_AddTimer(10, _timerTestCallback, (void *)&param);
   159   SDLTest_AssertPass("Call to SDL_AddTimer(10, param)");
   159   SDLTest_AssertPass("Call to SDL_AddTimer(10, param)");
   160   SDLTest_AssertCheck(id > 0, "Check result value, expected: >0, got: %d", id);
   160   SDLTest_AssertCheck(id > 0, "Check result value, expected: >0, got: %d", id);
   161   
   161 
   162   /* Wait to let timer trigger callback */
   162   /* Wait to let timer trigger callback */
   163   SDL_Delay(100);
   163   SDL_Delay(100);
   164   SDLTest_AssertPass("Call to SDL_Delay(100)");
   164   SDLTest_AssertPass("Call to SDL_Delay(100)");
   165   
   165 
   166   /* Remove timer again and check that callback was called */
   166   /* Remove timer again and check that callback was called */
   167   result = SDL_RemoveTimer(id);
   167   result = SDL_RemoveTimer(id);
   168   SDLTest_AssertPass("Call to SDL_RemoveTimer()");
   168   SDLTest_AssertPass("Call to SDL_RemoveTimer()");
   169   SDLTest_AssertCheck(result == SDL_FALSE, "Check result value, expected: %i, got: %i", SDL_FALSE, result);
   169   SDLTest_AssertCheck(result == SDL_FALSE, "Check result value, expected: %i, got: %i", SDL_FALSE, result);
   170   SDLTest_AssertCheck(_timerCallbackCalled == 1, "Check callback WAS called, expected: 1, got: %i", _timerCallbackCalled);
   170   SDLTest_AssertCheck(_timerCallbackCalled == 1, "Check callback WAS called, expected: 1, got: %i", _timerCallbackCalled);
   171     
   171 
   172   return TEST_COMPLETED;
   172   return TEST_COMPLETED;
   173 }
   173 }
   174 
   174 
   175 /* ================= Test References ================== */
   175 /* ================= Test References ================== */
   176 
   176 
   177 /* Timer test cases */
   177 /* Timer test cases */
   178 static const SDLTest_TestCaseReference timerTest1 =
   178 static const SDLTest_TestCaseReference timerTest1 =
   179 		{ (SDLTest_TestCaseFp)timer_getPerformanceCounter, "timer_getPerformanceCounter", "Call to SDL_GetPerformanceCounter", TEST_ENABLED };
   179         { (SDLTest_TestCaseFp)timer_getPerformanceCounter, "timer_getPerformanceCounter", "Call to SDL_GetPerformanceCounter", TEST_ENABLED };
   180 
   180 
   181 static const SDLTest_TestCaseReference timerTest2 =
   181 static const SDLTest_TestCaseReference timerTest2 =
   182 		{ (SDLTest_TestCaseFp)timer_getPerformanceFrequency, "timer_getPerformanceFrequency", "Call to SDL_GetPerformanceFrequency", TEST_ENABLED };
   182         { (SDLTest_TestCaseFp)timer_getPerformanceFrequency, "timer_getPerformanceFrequency", "Call to SDL_GetPerformanceFrequency", TEST_ENABLED };
   183 
   183 
   184 static const SDLTest_TestCaseReference timerTest3 =
   184 static const SDLTest_TestCaseReference timerTest3 =
   185 		{ (SDLTest_TestCaseFp)timer_delayAndGetTicks, "timer_delayAndGetTicks", "Call to SDL_Delay and SDL_GetTicks", TEST_ENABLED };
   185         { (SDLTest_TestCaseFp)timer_delayAndGetTicks, "timer_delayAndGetTicks", "Call to SDL_Delay and SDL_GetTicks", TEST_ENABLED };
   186 
   186 
   187 static const SDLTest_TestCaseReference timerTest4 =
   187 static const SDLTest_TestCaseReference timerTest4 =
   188 		{ (SDLTest_TestCaseFp)timer_addRemoveTimer, "timer_addRemoveTimer", "Call to SDL_AddTimer and SDL_RemoveTimer", TEST_ENABLED };
   188         { (SDLTest_TestCaseFp)timer_addRemoveTimer, "timer_addRemoveTimer", "Call to SDL_AddTimer and SDL_RemoveTimer", TEST_ENABLED };
   189 
   189 
   190 /* Sequence of Timer test cases */
   190 /* Sequence of Timer test cases */
   191 static const SDLTest_TestCaseReference *timerTests[] =  {
   191 static const SDLTest_TestCaseReference *timerTests[] =  {
   192 	&timerTest1, &timerTest2, &timerTest3, &timerTest4, NULL
   192     &timerTest1, &timerTest2, &timerTest3, &timerTest4, NULL
   193 };
   193 };
   194 
   194 
   195 /* Timer test suite (global) */
   195 /* Timer test suite (global) */
   196 SDLTest_TestSuiteReference timerTestSuite = {
   196 SDLTest_TestSuiteReference timerTestSuite = {
   197 	"Timer",
   197     "Timer",
   198 	_timerSetUp,
   198     _timerSetUp,
   199 	timerTests,
   199     timerTests,
   200 	NULL
   200     NULL
   201 };
   201 };