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