Add tests to SDLtest suite
authorAndreas Schiffler <aschiffler@ferzkopp.net>
Wed, 13 Feb 2013 23:10:29 -0800
changeset 68756c446c82c583
parent 6874 8a715e36551d
child 6876 3a4f7453da33
Add tests to SDLtest suite
include/SDL_test_fuzzer.h
test/testautomation_sdltest.c
     1.1 --- a/include/SDL_test_fuzzer.h	Wed Feb 13 21:22:29 2013 -0800
     1.2 +++ b/include/SDL_test_fuzzer.h	Wed Feb 13 23:10:29 2013 -0800
     1.3 @@ -323,7 +323,10 @@
     1.4   * If Max in smaller tham min, then the values are swapped.
     1.5   * Min and max are the same value, that value will be returned.
     1.6   *
     1.7 - * \returns Generated integer
     1.8 + * \param min Minimum inclusive value of returned random number
     1.9 + * \param max Maximum inclusive value of returned random number
    1.10 + *
    1.11 + * \returns Generated random integer in range
    1.12   */
    1.13  Sint32 SDLTest_RandomIntegerInRange(Sint32 min, Sint32 max);
    1.14  
     2.1 --- a/test/testautomation_sdltest.c	Wed Feb 13 21:22:29 2013 -0800
     2.2 +++ b/test/testautomation_sdltest.c	Wed Feb 13 23:10:29 2013 -0800
     2.3 @@ -5,6 +5,7 @@
     2.4  #include <stdio.h>
     2.5  #include <limits.h>
     2.6  #include <float.h>
     2.7 +#include <ctype.h>
     2.8  
     2.9  #include "SDL.h"
    2.10  #include "SDL_test.h"
    2.11 @@ -984,6 +985,216 @@
    2.12    return TEST_COMPLETED;
    2.13  }
    2.14  
    2.15 +/**
    2.16 + * @brief Calls to SDLTest_RandomIntegerInRange
    2.17 + */
    2.18 +int
    2.19 +sdltest_randomIntegerInRange(void *arg)
    2.20 +{
    2.21 +  Sint32 min, max;
    2.22 +  Sint32 result;
    2.23 +
    2.24 +  /* Standard range */
    2.25 +  min = (Sint32)SDLTest_RandomSint16();
    2.26 +  max = min + (Sint32)SDLTest_RandomUint8() + 2;
    2.27 +  result = SDLTest_RandomIntegerInRange(min, max);
    2.28 +  SDLTest_AssertPass("Call to SDLTest_RandomIntegerInRange(min,max)");
    2.29 +  SDLTest_AssertCheck(min <= result && result <= max, "Validated returned value; expected: [%d,%d], got: %d", min, max, result);
    2.30 +
    2.31 +  /* One Range */
    2.32 +  min = (Sint32)SDLTest_RandomSint16();
    2.33 +  max = min + 1;
    2.34 +  result = SDLTest_RandomIntegerInRange(min, max);
    2.35 +  SDLTest_AssertPass("Call to SDLTest_RandomIntegerInRange(min,min+1)");
    2.36 +  SDLTest_AssertCheck(min <= result && result <= max, "Validated returned value; expected: [%d,%d], got: %d", min, max, result);
    2.37 +
    2.38 +  /* Zero range */
    2.39 +  min = (Sint32)SDLTest_RandomSint16();
    2.40 +  max = min;
    2.41 +  result = SDLTest_RandomIntegerInRange(min, max);
    2.42 +  SDLTest_AssertPass("Call to SDLTest_RandomIntegerInRange(min,min)");
    2.43 +  SDLTest_AssertCheck(min == result, "Validated returned value; expected: %d, got: %d", min, result);
    2.44 +
    2.45 +  /* Zero range at zero */
    2.46 +  min = 0;
    2.47 +  max = 0;
    2.48 +  result = SDLTest_RandomIntegerInRange(min, max);
    2.49 +  SDLTest_AssertPass("Call to SDLTest_RandomIntegerInRange(0,0)");
    2.50 +  SDLTest_AssertCheck(result == 0, "Validated returned value; expected: 0, got: %d", min, max, result);
    2.51 +
    2.52 +  /* Swapped min-max */
    2.53 +  min = (Sint32)SDLTest_RandomSint16();
    2.54 +  max = min + (Sint32)SDLTest_RandomUint8() + 2;
    2.55 +  result = SDLTest_RandomIntegerInRange(max, min);
    2.56 +  SDLTest_AssertPass("Call to SDLTest_RandomIntegerInRange(max,min)");
    2.57 +  SDLTest_AssertCheck(min <= result && result <= max, "Validated returned value; expected: [%d,%d], got: %d", min, max, result);
    2.58 +
    2.59 +
    2.60 +  /* Range with min at integer limit */
    2.61 +  min = LONG_MIN;
    2.62 +  max = LONG_MIN + (Sint32)SDLTest_RandomSint16();
    2.63 +  result = SDLTest_RandomIntegerInRange(min, max);
    2.64 +  SDLTest_AssertPass("Call to SDLTest_RandomIntegerInRange(SINT32_MIN,...)");
    2.65 +  SDLTest_AssertCheck(min <= result && result <= max, "Validated returned value; expected: [%d,%d], got: %d", min, max, result);
    2.66 +
    2.67 +  /* Range with max at integer limit */
    2.68 +  min = LONG_MAX - (Sint32)SDLTest_RandomSint16();;
    2.69 +  max = LONG_MAX;
    2.70 +  result = SDLTest_RandomIntegerInRange(min, max);
    2.71 +  SDLTest_AssertPass("Call to SDLTest_RandomIntegerInRange(...,SINT32_MAX)");
    2.72 +  SDLTest_AssertCheck(min <= result && result <= max, "Validated returned value; expected: [%d,%d], got: %d", min, max, result);
    2.73 +
    2.74 +  /* Full integer range */
    2.75 +  min = LONG_MIN;
    2.76 +  max = LONG_MAX;
    2.77 +  result = SDLTest_RandomIntegerInRange(min, max);
    2.78 +  SDLTest_AssertPass("Call to SDLTest_RandomIntegerInRange(SINT32_MIN,SINT32_MAX)");
    2.79 +  SDLTest_AssertCheck(min <= result && result <= max, "Validated returned value; expected: [%d,%d], got: %d", min, max, result);
    2.80 +
    2.81 +  return TEST_COMPLETED;
    2.82 +}
    2.83 +
    2.84 +/**
    2.85 + * @brief Calls to SDLTest_RandomAsciiString
    2.86 + */
    2.87 +int
    2.88 +sdltest_randomAsciiString(void *arg)
    2.89 +{
    2.90 +  char* result;
    2.91 +  int len;
    2.92 +  int nonAsciiCharacters;
    2.93 +  int i;
    2.94 +
    2.95 +  result = SDLTest_RandomAsciiString();
    2.96 +  SDLTest_AssertPass("Call to SDLTest_RandomAsciiString()");
    2.97 +  SDLTest_AssertCheck(result != NULL, "Validate that result is not NULL");
    2.98 +  if (result != NULL) {
    2.99 +     len = SDL_strlen(result);
   2.100 +     SDLTest_AssertCheck(len >= 0 && len <= 255, "Validate that result length; expected: len=[1,255], got: %d", len);
   2.101 +     nonAsciiCharacters = 0;
   2.102 +     for (i=0; i<len; i++) {
   2.103 +       if (iscntrl(result[i])) {
   2.104 +         nonAsciiCharacters++;
   2.105 +       }
   2.106 +     }
   2.107 +     SDLTest_AssertCheck(nonAsciiCharacters == 0, "Validate that result does not contain non-Ascii characters, got: %d", nonAsciiCharacters);
   2.108 +     if (nonAsciiCharacters) {
   2.109 +     	SDLTest_LogError("Invalid result from generator: '%s'", result);
   2.110 +     }
   2.111 +     SDL_free(result);  
   2.112 +  }
   2.113 + 
   2.114 +  return TEST_COMPLETED;
   2.115 +}
   2.116 +
   2.117 +
   2.118 +/**
   2.119 + * @brief Calls to SDLTest_RandomAsciiStringWithMaximumLength
   2.120 + */
   2.121 +int
   2.122 +sdltest_randomAsciiStringWithMaximumLength(void *arg)
   2.123 +{
   2.124 +  const char* expectedError = "Parameter 'maxLength' is invalid";
   2.125 +  char* lastError;
   2.126 +  char* result;
   2.127 +  int targetLen;
   2.128 +  int len;
   2.129 +  int nonAsciiCharacters;
   2.130 +  int i;
   2.131 +
   2.132 +  targetLen = 16 + SDLTest_RandomUint8();
   2.133 +  result = SDLTest_RandomAsciiStringWithMaximumLength(targetLen);
   2.134 +  SDLTest_AssertPass("Call to SDLTest_RandomAsciiStringWithMaximumLength(%d)", targetLen);
   2.135 +  SDLTest_AssertCheck(result != NULL, "Validate that result is not NULL");
   2.136 +  if (result != NULL) {
   2.137 +     len = SDL_strlen(result);
   2.138 +     SDLTest_AssertCheck(len >= 0 && len <= targetLen, "Validate that result length; expected: len=[1,%d], got: %d", targetLen, len);
   2.139 +     nonAsciiCharacters = 0;
   2.140 +     for (i=0; i<len; i++) {
   2.141 +       if (iscntrl(result[i])) {
   2.142 +         nonAsciiCharacters++;
   2.143 +       }
   2.144 +     }
   2.145 +     SDLTest_AssertCheck(nonAsciiCharacters == 0, "Validate that result does not contain non-Ascii characters, got: %d", nonAsciiCharacters);
   2.146 +     if (nonAsciiCharacters) {
   2.147 +     	SDLTest_LogError("Invalid result from generator: '%s'", result);
   2.148 +     }
   2.149 +     SDL_free(result);  
   2.150 +  }
   2.151 +
   2.152 +  /* Negative test */
   2.153 +  targetLen = 0;
   2.154 +  result = SDLTest_RandomAsciiStringWithMaximumLength(targetLen);
   2.155 +  SDLTest_AssertPass("Call to SDLTest_RandomAsciiStringWithMaximumLength(%d)", targetLen);
   2.156 +  SDLTest_AssertCheck(result == NULL, "Validate that result is NULL");
   2.157 +  lastError = (char *)SDL_GetError();
   2.158 +  SDLTest_AssertPass("SDL_GetError()");
   2.159 +  SDLTest_AssertCheck(lastError != NULL && SDL_strcmp(lastError, expectedError) == 0,
   2.160 +             "SDL_GetError(): expected message '%s', was message: '%s'",
   2.161 +             expectedError,
   2.162 +             lastError);
   2.163 +
   2.164 +  /* Clear error messages */
   2.165 +  SDL_ClearError();
   2.166 +  SDLTest_AssertPass("SDL_ClearError()");
   2.167 + 
   2.168 +  return TEST_COMPLETED;
   2.169 +}
   2.170 +
   2.171 +/**
   2.172 + * @brief Calls to SDLTest_RandomAsciiStringOfSize
   2.173 + */
   2.174 +int
   2.175 +sdltest_randomAsciiStringOfSize(void *arg)
   2.176 +{
   2.177 +  const char* expectedError = "Parameter 'size' is invalid";
   2.178 +  char* lastError;
   2.179 +  char* result;
   2.180 +  int targetLen;
   2.181 +  int len;
   2.182 +  int nonAsciiCharacters;
   2.183 +  int i;
   2.184 +
   2.185 +  /* Positive test */
   2.186 +  targetLen = 16 + SDLTest_RandomUint8();
   2.187 +  result = SDLTest_RandomAsciiStringOfSize(targetLen);
   2.188 +  SDLTest_AssertPass("Call to SDLTest_RandomAsciiStringOfSize(%d)", targetLen);
   2.189 +  SDLTest_AssertCheck(result != NULL, "Validate that result is not NULL");
   2.190 +  if (result != NULL) {
   2.191 +     len = SDL_strlen(result);
   2.192 +     SDLTest_AssertCheck(len == targetLen, "Validate that result length; expected: len=%d, got: %d", targetLen, len);
   2.193 +     nonAsciiCharacters = 0;
   2.194 +     for (i=0; i<len; i++) {
   2.195 +       if (iscntrl(result[i])) {
   2.196 +         nonAsciiCharacters++;
   2.197 +       }
   2.198 +     }
   2.199 +     SDLTest_AssertCheck(nonAsciiCharacters == 0, "Validate that result does not contain non-Ascii characters, got: %d", nonAsciiCharacters);
   2.200 +     if (nonAsciiCharacters) {
   2.201 +     	SDLTest_LogError("Invalid result from generator: '%s'", result);
   2.202 +     }
   2.203 +     SDL_free(result);  
   2.204 +  }
   2.205 + 
   2.206 +  /* Negative test */
   2.207 +  targetLen = 0;
   2.208 +  result = SDLTest_RandomAsciiStringOfSize(targetLen);
   2.209 +  SDLTest_AssertPass("Call to SDLTest_RandomAsciiStringOfSize(%d)", targetLen);
   2.210 +  SDLTest_AssertCheck(result == NULL, "Validate that result is NULL");
   2.211 +  lastError = (char *)SDL_GetError();
   2.212 +  SDLTest_AssertPass("SDL_GetError()");
   2.213 +  SDLTest_AssertCheck(lastError != NULL && SDL_strcmp(lastError, expectedError) == 0,
   2.214 +             "SDL_GetError(): expected message '%s', was message: '%s'",
   2.215 +             expectedError,
   2.216 +             lastError);
   2.217 +
   2.218 +  /* Clear error messages */
   2.219 +  SDL_ClearError();
   2.220 +  SDLTest_AssertPass("SDL_ClearError()");
   2.221 +   
   2.222 +  return TEST_COMPLETED;
   2.223 +}
   2.224 +
   2.225  
   2.226  /* ================= Test References ================== */
   2.227  
   2.228 @@ -1018,10 +1229,23 @@
   2.229  static const SDLTest_TestCaseReference sdltestTest10 =
   2.230  		{ (SDLTest_TestCaseFp)sdltest_randomBoundaryNumberSint64, "sdltest_randomBoundaryNumberSint64", "Calls to random boundary number generators for Sint64", TEST_ENABLED };
   2.231  
   2.232 +static const SDLTest_TestCaseReference sdltestTest11 =
   2.233 +		{ (SDLTest_TestCaseFp)sdltest_randomIntegerInRange, "sdltest_randomIntegerInRange", "Calls to ranged random number generator", TEST_ENABLED };
   2.234 +
   2.235 +static const SDLTest_TestCaseReference sdltestTest12 =
   2.236 +		{ (SDLTest_TestCaseFp)sdltest_randomAsciiString, "sdltest_randomAsciiString", "Calls to default ASCII string generator", TEST_ENABLED };
   2.237 +
   2.238 +static const SDLTest_TestCaseReference sdltestTest13 =
   2.239 +		{ (SDLTest_TestCaseFp)sdltest_randomAsciiStringWithMaximumLength, "sdltest_randomAsciiStringWithMaximumLength", "Calls to random maximum length ASCII string generator", TEST_ENABLED };
   2.240 +
   2.241 +static const SDLTest_TestCaseReference sdltestTest14 =
   2.242 +		{ (SDLTest_TestCaseFp)sdltest_randomAsciiStringOfSize, "sdltest_randomAsciiStringOfSize", "Calls to fixed size ASCII string generator", TEST_ENABLED };
   2.243 +
   2.244  /* Sequence of SDL_test test cases */
   2.245  static const SDLTest_TestCaseReference *sdltestTests[] =  {
   2.246  	&sdltestTest1, &sdltestTest2, &sdltestTest3, &sdltestTest4, &sdltestTest5, &sdltestTest6,
   2.247 -	&sdltestTest7, &sdltestTest8, &sdltestTest9, &sdltestTest10, NULL
   2.248 +	&sdltestTest7, &sdltestTest8, &sdltestTest9, &sdltestTest10, &sdltestTest11, &sdltestTest12,
   2.249 +	&sdltestTest13, &sdltestTest14, NULL
   2.250  };
   2.251  
   2.252  /* SDL_test test suite (global) */