Update audio testautomation: more coverage; added SDL_PauseAudio test /w callback coverage
authorAndreas Schiffler <aschiffler@ferzkopp.net>
Mon, 07 Jul 2014 23:33:57 -0700
changeset 8994b80b0595975c
parent 8993 bc47cf4c679d
child 8995 d095e572e0d5
Update audio testautomation: more coverage; added SDL_PauseAudio test /w callback coverage
test/testautomation_audio.c
     1.1 --- a/test/testautomation_audio.c	Mon Jul 07 23:26:34 2014 -0700
     1.2 +++ b/test/testautomation_audio.c	Mon Jul 07 23:33:57 2014 -0700
     1.3 @@ -3,6 +3,9 @@
     1.4   * New/updated tests: aschiffler at ferzkopp dot net
     1.5   */
     1.6  
     1.7 +/* quiet windows compiler warnings */
     1.8 +#define _CRT_SECURE_NO_WARNINGS
     1.9 +
    1.10  #include <stdio.h>
    1.11  #include <string.h>
    1.12  
    1.13 @@ -25,11 +28,29 @@
    1.14          }
    1.15  }
    1.16  
    1.17 +void
    1.18 +_audioTearDown(void *arg)
    1.19 +{
    1.20 +    /* Remove a possibly created file from SDL disk writer audio driver; ignore errors */
    1.21 +    remove("sdlaudio.raw");
    1.22 +
    1.23 +    SDLTest_AssertPass("Cleanup of test files completed");
    1.24 +}
    1.25 +
    1.26 +
    1.27 +/* Global counter for callback invocation */
    1.28 +int _audio_testCallbackCounter;
    1.29 +
    1.30 +/* Global accumulator for total callback length */
    1.31 +int _audio_testCallbackLength;
    1.32 +
    1.33  
    1.34  /* Test callback function */
    1.35  void _audio_testCallback(void *userdata, Uint8 *stream, int len)
    1.36  {
    1.37 -   /* TODO: add tracking if callback was called */
    1.38 +   /* track that callback was called */
    1.39 +   _audio_testCallbackCounter++;
    1.40 +   _audio_testCallbackLength += len;
    1.41  }
    1.42  
    1.43  
    1.44 @@ -38,8 +59,8 @@
    1.45  /**
    1.46   * \brief Stop and restart audio subsystem
    1.47   *
    1.48 - * \sa http://wiki.libsdl.org/moin.cgi/SDL_QuitSubSystem
    1.49 - * \sa http://wiki.libsdl.org/moin.cgi/SDL_InitSubSystem
    1.50 + * \sa https://wiki.libsdl.org/SDL_QuitSubSystem
    1.51 + * \sa https://wiki.libsdl.org/SDL_InitSubSystem
    1.52   */
    1.53  int audio_quitInitAudioSubSystem()
    1.54  {
    1.55 @@ -56,8 +77,8 @@
    1.56  /**
    1.57   * \brief Start and stop audio directly
    1.58   *
    1.59 - * \sa http://wiki.libsdl.org/moin.cgi/SDL_InitAudio
    1.60 - * \sa http://wiki.libsdl.org/moin.cgi/SDL_QuitAudio
    1.61 + * \sa https://wiki.libsdl.org/SDL_InitAudio
    1.62 + * \sa https://wiki.libsdl.org/SDL_QuitAudio
    1.63   */
    1.64  int audio_initQuitAudio()
    1.65  {
    1.66 @@ -110,15 +131,104 @@
    1.67  /**
    1.68   * \brief Start, open, close and stop audio
    1.69   *
    1.70 - * \sa http://wiki.libsdl.org/moin.cgi/SDL_InitAudio
    1.71 - * \sa http://wiki.libsdl.org/moin.cgi/SDL_OpenAudio
    1.72 - * \sa http://wiki.libsdl.org/moin.cgi/SDL_CloseAudio
    1.73 - * \sa http://wiki.libsdl.org/moin.cgi/SDL_QuitAudio
    1.74 + * \sa https://wiki.libsdl.org/SDL_InitAudio
    1.75 + * \sa https://wiki.libsdl.org/SDL_OpenAudio
    1.76 + * \sa https://wiki.libsdl.org/SDL_CloseAudio
    1.77 + * \sa https://wiki.libsdl.org/SDL_QuitAudio
    1.78   */
    1.79  int audio_initOpenCloseQuitAudio()
    1.80  {
    1.81 +    int result, expectedResult;
    1.82 +    int i, iMax, j, k;
    1.83 +    const char* audioDriver;
    1.84 +    SDL_AudioSpec desired;
    1.85 +
    1.86 +    /* Stop SDL audio subsystem */
    1.87 +    SDL_QuitSubSystem( SDL_INIT_AUDIO );
    1.88 +        SDLTest_AssertPass("Call to SDL_QuitSubSystem(SDL_INIT_AUDIO)");
    1.89 +
    1.90 +        /* Loop over all available audio drivers */
    1.91 +        iMax = SDL_GetNumAudioDrivers();
    1.92 +        SDLTest_AssertPass("Call to SDL_GetNumAudioDrivers");
    1.93 +        SDLTest_AssertCheck(iMax > 0, "Validate number of audio drivers; expected: >0 got: %d", iMax);
    1.94 +        for (i = 0; i < iMax; i++) {
    1.95 +            audioDriver = SDL_GetAudioDriver(i);
    1.96 +            SDLTest_AssertPass("Call to SDL_GetAudioDriver(%d)", i);
    1.97 +            SDLTest_AssertCheck(audioDriver != NULL, "Audio driver name is not NULL");
    1.98 +            SDLTest_AssertCheck(audioDriver[0] != '\0', "Audio driver name is not empty; got: %s", audioDriver);
    1.99 +
   1.100 +            /* Change specs */
   1.101 +            for (j = 0; j < 2; j++) {
   1.102 +
   1.103 +                /* Call Init */
   1.104 +                result = SDL_AudioInit(audioDriver);
   1.105 +                SDLTest_AssertPass("Call to SDL_AudioInit('%s')", audioDriver);
   1.106 +                SDLTest_AssertCheck(result == 0, "Validate result value; expected: 0 got: %d", result);
   1.107 +
   1.108 +                /* Set spec */
   1.109 +                SDL_memset(&desired, 0, sizeof(desired));
   1.110 +                switch (j) {
   1.111 +                    case 0:
   1.112 +                    /* Set standard desired spec */
   1.113 +                    desired.freq = 22050;
   1.114 +                    desired.format = AUDIO_S16SYS;
   1.115 +                    desired.channels = 2;
   1.116 +                    desired.samples = 4096;
   1.117 +                    desired.callback = _audio_testCallback;
   1.118 +                    desired.userdata = NULL;
   1.119 +
   1.120 +                    case 1:
   1.121 +                    /* Set custom desired spec */
   1.122 +                    desired.freq = 48000;
   1.123 +                    desired.format = AUDIO_F32SYS;
   1.124 +                    desired.channels = 2;
   1.125 +                    desired.samples = 2048;
   1.126 +                    desired.callback = _audio_testCallback;
   1.127 +                    desired.userdata = NULL;
   1.128 +                    break;
   1.129 +            }
   1.130 +
   1.131 +            /* Call Open (maybe multiple times) */
   1.132 +            for (k=0; k <= j; k++) {
   1.133 +                result = SDL_OpenAudio(&desired, NULL);
   1.134 +                SDLTest_AssertPass("Call to SDL_OpenAudio(desired_spec_%d, NULL), call %d", j, k+1);
   1.135 +                expectedResult = (k==0) ? 0 : -1;
   1.136 +                SDLTest_AssertCheck(result == expectedResult, "Verify return value; expected: %d, got: %d", expectedResult, result);
   1.137 +            }
   1.138 +
   1.139 +            /* Call Close (maybe multiple times) */
   1.140 +            for (k=0; k <= j; k++) {
   1.141 +                SDL_CloseAudio();
   1.142 +                SDLTest_AssertPass("Call to SDL_CloseAudio(), call %d", k+1);
   1.143 +            }
   1.144 +
   1.145 +            /* Call Quit (maybe multiple times) */
   1.146 +            for (k=0; k <= j; k++) {
   1.147 +                SDL_AudioQuit();
   1.148 +                SDLTest_AssertPass("Call to SDL_AudioQuit(), call %d", k+1);
   1.149 +            }
   1.150 +
   1.151 +        } /* spec loop */
   1.152 +    } /* driver loop */
   1.153 +
   1.154 +        /* Restart audio again */
   1.155 +        _audioSetUp(NULL);
   1.156 +
   1.157 +    return TEST_COMPLETED;
   1.158 +}
   1.159 +
   1.160 +/**
   1.161 + * \brief Pause and unpause audio
   1.162 + *
   1.163 + * \sa https://wiki.libsdl.org/SDL_PauseAudio
   1.164 + */
   1.165 +int audio_pauseUnpauseAudio()
   1.166 +{
   1.167      int result;
   1.168 -    int i, iMax, j;
   1.169 +    int i, iMax, j, k, l;
   1.170 +    int totalDelay;
   1.171 +    int pause_on;
   1.172 +    int originalCounter;
   1.173      const char* audioDriver;
   1.174      SDL_AudioSpec desired;
   1.175  
   1.176 @@ -172,9 +282,47 @@
   1.177              SDLTest_AssertPass("Call to SDL_OpenAudio(desired_spec_%d, NULL)", j);
   1.178              SDLTest_AssertCheck(result == 0, "Verify return value; expected: 0 got: %d", result);
   1.179  
   1.180 +            /* Start and stop audio multiple times */
   1.181 +            for (l=0; l<3; l++) {
   1.182 +                SDLTest_Log("Pause/Unpause iteration: %d", l+1);
   1.183 +            
   1.184 +                /* Reset callback counters */
   1.185 +                _audio_testCallbackCounter = 0;
   1.186 +                _audio_testCallbackLength = 0;
   1.187 +
   1.188 +                /* Un-pause audio to start playing (maybe multiple times) */
   1.189 +                pause_on = 0;
   1.190 +                for (k=0; k <= j; k++) {
   1.191 +                    SDL_PauseAudio(pause_on);
   1.192 +                    SDLTest_AssertPass("Call to SDL_PauseAudio(%d), call %d", pause_on, k+1);
   1.193 +                }
   1.194 +            
   1.195 +                /* Wait for callback */
   1.196 +                totalDelay = 0;
   1.197 +                do {
   1.198 +                    SDL_Delay(10);
   1.199 +                    totalDelay += 10;
   1.200 +                } 
   1.201 +                while (_audio_testCallbackCounter == 0 && totalDelay < 1000);
   1.202 +                SDLTest_AssertCheck(_audio_testCallbackCounter > 0, "Verify callback counter; expected: >0 got: %d", _audio_testCallbackCounter);
   1.203 +                SDLTest_AssertCheck(_audio_testCallbackLength > 0, "Verify callback length; expected: >0 got: %d", _audio_testCallbackLength);
   1.204 +
   1.205 +                /* Pause audio to stop playing (maybe multiple times) */
   1.206 +                for (k=0; k <= j; k++) {
   1.207 +                    pause_on = (k==0) ? 1 : SDLTest_RandomIntegerInRange(99, 9999);
   1.208 +                    SDL_PauseAudio(pause_on);
   1.209 +                    SDLTest_AssertPass("Call to SDL_PauseAudio(%d), call %d", pause_on, k+1);
   1.210 +                }
   1.211 +            
   1.212 +                /* Ensure callback is not called again */
   1.213 +                originalCounter = _audio_testCallbackCounter;
   1.214 +                SDL_Delay(totalDelay + 10);
   1.215 +                SDLTest_AssertCheck(originalCounter == _audio_testCallbackCounter, "Verify callback counter; expected: %d, got: %d", originalCounter, _audio_testCallbackCounter);
   1.216 +            }
   1.217 +
   1.218              /* Call Close */
   1.219 -                    SDL_CloseAudio();
   1.220 -                    SDLTest_AssertPass("Call to SDL_CloseAudio()");
   1.221 +            SDL_CloseAudio();
   1.222 +            SDLTest_AssertPass("Call to SDL_CloseAudio()");
   1.223  
   1.224              /* Call Quit */
   1.225              SDL_AudioQuit();
   1.226 @@ -183,8 +331,8 @@
   1.227          } /* spec loop */
   1.228      } /* driver loop */
   1.229  
   1.230 -        /* Restart audio again */
   1.231 -        _audioSetUp(NULL);
   1.232 +    /* Restart audio again */
   1.233 +    _audioSetUp(NULL);
   1.234  
   1.235      return TEST_COMPLETED;
   1.236  }
   1.237 @@ -192,8 +340,8 @@
   1.238  /**
   1.239   * \brief Enumerate and name available audio devices (output and capture).
   1.240   *
   1.241 - * \sa http://wiki.libsdl.org/moin.cgi/SDL_GetNumAudioDevices
   1.242 - * \sa http://wiki.libsdl.org/moin.cgi/SDL_GetAudioDeviceName
   1.243 + * \sa https://wiki.libsdl.org/SDL_GetNumAudioDevices
   1.244 + * \sa https://wiki.libsdl.org/SDL_GetAudioDeviceName
   1.245   */
   1.246  int audio_enumerateAndNameAudioDevices()
   1.247  {
   1.248 @@ -250,8 +398,8 @@
   1.249  /**
   1.250   * \brief Negative tests around enumeration and naming of audio devices.
   1.251   *
   1.252 - * \sa http://wiki.libsdl.org/moin.cgi/SDL_GetNumAudioDevices
   1.253 - * \sa http://wiki.libsdl.org/moin.cgi/SDL_GetAudioDeviceName
   1.254 + * \sa https://wiki.libsdl.org/SDL_GetNumAudioDevices
   1.255 + * \sa https://wiki.libsdl.org/SDL_GetAudioDeviceName
   1.256   */
   1.257  int audio_enumerateAndNameAudioDevicesNegativeTests()
   1.258  {
   1.259 @@ -297,8 +445,8 @@
   1.260  /**
   1.261   * \brief Checks available audio driver names.
   1.262   *
   1.263 - * \sa http://wiki.libsdl.org/moin.cgi/SDL_GetNumAudioDrivers
   1.264 - * \sa http://wiki.libsdl.org/moin.cgi/SDL_GetAudioDriver
   1.265 + * \sa https://wiki.libsdl.org/SDL_GetNumAudioDrivers
   1.266 + * \sa https://wiki.libsdl.org/SDL_GetAudioDriver
   1.267   */
   1.268  int audio_printAudioDrivers()
   1.269  {
   1.270 @@ -330,7 +478,7 @@
   1.271  /**
   1.272   * \brief Checks current audio driver name with initialized audio.
   1.273   *
   1.274 - * \sa http://wiki.libsdl.org/moin.cgi/SDL_GetCurrentAudioDriver
   1.275 + * \sa https://wiki.libsdl.org/SDL_GetCurrentAudioDriver
   1.276   */
   1.277  int audio_printCurrentAudioDriver()
   1.278  {
   1.279 @@ -362,7 +510,7 @@
   1.280  /**
   1.281   * \brief Builds various audio conversion structures
   1.282   *
   1.283 - * \sa http://wiki.libsdl.org/moin.cgi/SDL_BuildAudioCVT
   1.284 + * \sa https://wiki.libsdl.org/SDL_BuildAudioCVT
   1.285   */
   1.286  int audio_buildAudioCVT()
   1.287  {
   1.288 @@ -426,7 +574,7 @@
   1.289  /**
   1.290   * \brief Checkes calls with invalid input to SDL_BuildAudioCVT
   1.291   *
   1.292 - * \sa http://wiki.libsdl.org/moin.cgi/SDL_BuildAudioCVT
   1.293 + * \sa https://wiki.libsdl.org/SDL_BuildAudioCVT
   1.294   */
   1.295  int audio_buildAudioCVTNegative()
   1.296  {
   1.297 @@ -521,7 +669,7 @@
   1.298  /**
   1.299   * \brief Checks current audio status.
   1.300   *
   1.301 - * \sa http://wiki.libsdl.org/moin.cgi/SDL_GetAudioStatus
   1.302 + * \sa https://wiki.libsdl.org/SDL_GetAudioStatus
   1.303   */
   1.304  int audio_getAudioStatus()
   1.305  {
   1.306 @@ -542,7 +690,7 @@
   1.307  /**
   1.308   * \brief Opens, checks current audio status, and closes a device.
   1.309   *
   1.310 - * \sa http://wiki.libsdl.org/moin.cgi/SDL_GetAudioStatus
   1.311 + * \sa https://wiki.libsdl.org/SDL_GetAudioStatus
   1.312   */
   1.313  int audio_openCloseAndGetAudioStatus()
   1.314  {
   1.315 @@ -600,8 +748,8 @@
   1.316  /**
   1.317   * \brief Locks and unlocks open audio device.
   1.318   *
   1.319 - * \sa http://wiki.libsdl.org/moin.cgi/SDL_LockAudioDevice
   1.320 - * \sa http://wiki.libsdl.org/moin.cgi/SDL_UnlockAudioDevice
   1.321 + * \sa https://wiki.libsdl.org/SDL_LockAudioDevice
   1.322 + * \sa https://wiki.libsdl.org/SDL_UnlockAudioDevice
   1.323   */
   1.324  int audio_lockUnlockOpenAudioDevice()
   1.325  {
   1.326 @@ -663,8 +811,8 @@
   1.327  /**
   1.328   * \brief Convert audio using various conversion structures
   1.329   *
   1.330 - * \sa http://wiki.libsdl.org/moin.cgi/SDL_BuildAudioCVT
   1.331 - * \sa http://wiki.libsdl.org/moin.cgi/SDL_ConvertAudio
   1.332 + * \sa https://wiki.libsdl.org/SDL_BuildAudioCVT
   1.333 + * \sa https://wiki.libsdl.org/SDL_ConvertAudio
   1.334   */
   1.335  int audio_convertAudio()
   1.336  {
   1.337 @@ -762,7 +910,7 @@
   1.338  /**
   1.339   * \brief Opens, checks current connected status, and closes a device.
   1.340   *
   1.341 - * \sa http://wiki.libsdl.org/moin.cgi/SDL_AudioDeviceConnected
   1.342 + * \sa https://wiki.libsdl.org/SDL_AudioDeviceConnected
   1.343   */
   1.344  int audio_openCloseAudioDeviceConnected()
   1.345  {
   1.346 @@ -871,11 +1019,14 @@
   1.347  static const SDLTest_TestCaseReference audioTest14 =
   1.348          { (SDLTest_TestCaseFp)audio_initOpenCloseQuitAudio, "audio_initOpenCloseQuitAudio", "Cycle through init, open, close and quit with various audio specs.", TEST_ENABLED };
   1.349  
   1.350 +static const SDLTest_TestCaseReference audioTest15 =
   1.351 +        { (SDLTest_TestCaseFp)audio_pauseUnpauseAudio, "audio_pauseUnpauseAudio", "Pause and Unpause audio for various audio specs while testing callback.", TEST_ENABLED };
   1.352 +
   1.353  /* Sequence of Audio test cases */
   1.354  static const SDLTest_TestCaseReference *audioTests[] =  {
   1.355      &audioTest1, &audioTest2, &audioTest3, &audioTest4, &audioTest5, &audioTest6,
   1.356      &audioTest7, &audioTest8, &audioTest9, &audioTest10, &audioTest11,
   1.357 -    &audioTest12, &audioTest13, &audioTest14, NULL
   1.358 +    &audioTest12, &audioTest13, &audioTest14, &audioTest15, NULL
   1.359  };
   1.360  
   1.361  /* Audio test suite (global) */
   1.362 @@ -883,5 +1034,5 @@
   1.363      "Audio",
   1.364      _audioSetUp,
   1.365      audioTests,
   1.366 -    NULL
   1.367 +    _audioTearDown
   1.368  };