test/testautomation_sdltest.c
changeset 6875 6c446c82c583
parent 6872 2f4c95464651
child 7144 c700de9c8ed4
     1.1 --- a/test/testautomation_sdltest.c	Wed Feb 13 21:22:29 2013 -0800
     1.2 +++ b/test/testautomation_sdltest.c	Wed Feb 13 23:10:29 2013 -0800
     1.3 @@ -5,6 +5,7 @@
     1.4  #include <stdio.h>
     1.5  #include <limits.h>
     1.6  #include <float.h>
     1.7 +#include <ctype.h>
     1.8  
     1.9  #include "SDL.h"
    1.10  #include "SDL_test.h"
    1.11 @@ -984,6 +985,216 @@
    1.12    return TEST_COMPLETED;
    1.13  }
    1.14  
    1.15 +/**
    1.16 + * @brief Calls to SDLTest_RandomIntegerInRange
    1.17 + */
    1.18 +int
    1.19 +sdltest_randomIntegerInRange(void *arg)
    1.20 +{
    1.21 +  Sint32 min, max;
    1.22 +  Sint32 result;
    1.23 +
    1.24 +  /* Standard range */
    1.25 +  min = (Sint32)SDLTest_RandomSint16();
    1.26 +  max = min + (Sint32)SDLTest_RandomUint8() + 2;
    1.27 +  result = SDLTest_RandomIntegerInRange(min, max);
    1.28 +  SDLTest_AssertPass("Call to SDLTest_RandomIntegerInRange(min,max)");
    1.29 +  SDLTest_AssertCheck(min <= result && result <= max, "Validated returned value; expected: [%d,%d], got: %d", min, max, result);
    1.30 +
    1.31 +  /* One Range */
    1.32 +  min = (Sint32)SDLTest_RandomSint16();
    1.33 +  max = min + 1;
    1.34 +  result = SDLTest_RandomIntegerInRange(min, max);
    1.35 +  SDLTest_AssertPass("Call to SDLTest_RandomIntegerInRange(min,min+1)");
    1.36 +  SDLTest_AssertCheck(min <= result && result <= max, "Validated returned value; expected: [%d,%d], got: %d", min, max, result);
    1.37 +
    1.38 +  /* Zero range */
    1.39 +  min = (Sint32)SDLTest_RandomSint16();
    1.40 +  max = min;
    1.41 +  result = SDLTest_RandomIntegerInRange(min, max);
    1.42 +  SDLTest_AssertPass("Call to SDLTest_RandomIntegerInRange(min,min)");
    1.43 +  SDLTest_AssertCheck(min == result, "Validated returned value; expected: %d, got: %d", min, result);
    1.44 +
    1.45 +  /* Zero range at zero */
    1.46 +  min = 0;
    1.47 +  max = 0;
    1.48 +  result = SDLTest_RandomIntegerInRange(min, max);
    1.49 +  SDLTest_AssertPass("Call to SDLTest_RandomIntegerInRange(0,0)");
    1.50 +  SDLTest_AssertCheck(result == 0, "Validated returned value; expected: 0, got: %d", min, max, result);
    1.51 +
    1.52 +  /* Swapped min-max */
    1.53 +  min = (Sint32)SDLTest_RandomSint16();
    1.54 +  max = min + (Sint32)SDLTest_RandomUint8() + 2;
    1.55 +  result = SDLTest_RandomIntegerInRange(max, min);
    1.56 +  SDLTest_AssertPass("Call to SDLTest_RandomIntegerInRange(max,min)");
    1.57 +  SDLTest_AssertCheck(min <= result && result <= max, "Validated returned value; expected: [%d,%d], got: %d", min, max, result);
    1.58 +
    1.59 +
    1.60 +  /* Range with min at integer limit */
    1.61 +  min = LONG_MIN;
    1.62 +  max = LONG_MIN + (Sint32)SDLTest_RandomSint16();
    1.63 +  result = SDLTest_RandomIntegerInRange(min, max);
    1.64 +  SDLTest_AssertPass("Call to SDLTest_RandomIntegerInRange(SINT32_MIN,...)");
    1.65 +  SDLTest_AssertCheck(min <= result && result <= max, "Validated returned value; expected: [%d,%d], got: %d", min, max, result);
    1.66 +
    1.67 +  /* Range with max at integer limit */
    1.68 +  min = LONG_MAX - (Sint32)SDLTest_RandomSint16();;
    1.69 +  max = LONG_MAX;
    1.70 +  result = SDLTest_RandomIntegerInRange(min, max);
    1.71 +  SDLTest_AssertPass("Call to SDLTest_RandomIntegerInRange(...,SINT32_MAX)");
    1.72 +  SDLTest_AssertCheck(min <= result && result <= max, "Validated returned value; expected: [%d,%d], got: %d", min, max, result);
    1.73 +
    1.74 +  /* Full integer range */
    1.75 +  min = LONG_MIN;
    1.76 +  max = LONG_MAX;
    1.77 +  result = SDLTest_RandomIntegerInRange(min, max);
    1.78 +  SDLTest_AssertPass("Call to SDLTest_RandomIntegerInRange(SINT32_MIN,SINT32_MAX)");
    1.79 +  SDLTest_AssertCheck(min <= result && result <= max, "Validated returned value; expected: [%d,%d], got: %d", min, max, result);
    1.80 +
    1.81 +  return TEST_COMPLETED;
    1.82 +}
    1.83 +
    1.84 +/**
    1.85 + * @brief Calls to SDLTest_RandomAsciiString
    1.86 + */
    1.87 +int
    1.88 +sdltest_randomAsciiString(void *arg)
    1.89 +{
    1.90 +  char* result;
    1.91 +  int len;
    1.92 +  int nonAsciiCharacters;
    1.93 +  int i;
    1.94 +
    1.95 +  result = SDLTest_RandomAsciiString();
    1.96 +  SDLTest_AssertPass("Call to SDLTest_RandomAsciiString()");
    1.97 +  SDLTest_AssertCheck(result != NULL, "Validate that result is not NULL");
    1.98 +  if (result != NULL) {
    1.99 +     len = SDL_strlen(result);
   1.100 +     SDLTest_AssertCheck(len >= 0 && len <= 255, "Validate that result length; expected: len=[1,255], got: %d", len);
   1.101 +     nonAsciiCharacters = 0;
   1.102 +     for (i=0; i<len; i++) {
   1.103 +       if (iscntrl(result[i])) {
   1.104 +         nonAsciiCharacters++;
   1.105 +       }
   1.106 +     }
   1.107 +     SDLTest_AssertCheck(nonAsciiCharacters == 0, "Validate that result does not contain non-Ascii characters, got: %d", nonAsciiCharacters);
   1.108 +     if (nonAsciiCharacters) {
   1.109 +     	SDLTest_LogError("Invalid result from generator: '%s'", result);
   1.110 +     }
   1.111 +     SDL_free(result);  
   1.112 +  }
   1.113 + 
   1.114 +  return TEST_COMPLETED;
   1.115 +}
   1.116 +
   1.117 +
   1.118 +/**
   1.119 + * @brief Calls to SDLTest_RandomAsciiStringWithMaximumLength
   1.120 + */
   1.121 +int
   1.122 +sdltest_randomAsciiStringWithMaximumLength(void *arg)
   1.123 +{
   1.124 +  const char* expectedError = "Parameter 'maxLength' is invalid";
   1.125 +  char* lastError;
   1.126 +  char* result;
   1.127 +  int targetLen;
   1.128 +  int len;
   1.129 +  int nonAsciiCharacters;
   1.130 +  int i;
   1.131 +
   1.132 +  targetLen = 16 + SDLTest_RandomUint8();
   1.133 +  result = SDLTest_RandomAsciiStringWithMaximumLength(targetLen);
   1.134 +  SDLTest_AssertPass("Call to SDLTest_RandomAsciiStringWithMaximumLength(%d)", targetLen);
   1.135 +  SDLTest_AssertCheck(result != NULL, "Validate that result is not NULL");
   1.136 +  if (result != NULL) {
   1.137 +     len = SDL_strlen(result);
   1.138 +     SDLTest_AssertCheck(len >= 0 && len <= targetLen, "Validate that result length; expected: len=[1,%d], got: %d", targetLen, len);
   1.139 +     nonAsciiCharacters = 0;
   1.140 +     for (i=0; i<len; i++) {
   1.141 +       if (iscntrl(result[i])) {
   1.142 +         nonAsciiCharacters++;
   1.143 +       }
   1.144 +     }
   1.145 +     SDLTest_AssertCheck(nonAsciiCharacters == 0, "Validate that result does not contain non-Ascii characters, got: %d", nonAsciiCharacters);
   1.146 +     if (nonAsciiCharacters) {
   1.147 +     	SDLTest_LogError("Invalid result from generator: '%s'", result);
   1.148 +     }
   1.149 +     SDL_free(result);  
   1.150 +  }
   1.151 +
   1.152 +  /* Negative test */
   1.153 +  targetLen = 0;
   1.154 +  result = SDLTest_RandomAsciiStringWithMaximumLength(targetLen);
   1.155 +  SDLTest_AssertPass("Call to SDLTest_RandomAsciiStringWithMaximumLength(%d)", targetLen);
   1.156 +  SDLTest_AssertCheck(result == NULL, "Validate that result is NULL");
   1.157 +  lastError = (char *)SDL_GetError();
   1.158 +  SDLTest_AssertPass("SDL_GetError()");
   1.159 +  SDLTest_AssertCheck(lastError != NULL && SDL_strcmp(lastError, expectedError) == 0,
   1.160 +             "SDL_GetError(): expected message '%s', was message: '%s'",
   1.161 +             expectedError,
   1.162 +             lastError);
   1.163 +
   1.164 +  /* Clear error messages */
   1.165 +  SDL_ClearError();
   1.166 +  SDLTest_AssertPass("SDL_ClearError()");
   1.167 + 
   1.168 +  return TEST_COMPLETED;
   1.169 +}
   1.170 +
   1.171 +/**
   1.172 + * @brief Calls to SDLTest_RandomAsciiStringOfSize
   1.173 + */
   1.174 +int
   1.175 +sdltest_randomAsciiStringOfSize(void *arg)
   1.176 +{
   1.177 +  const char* expectedError = "Parameter 'size' is invalid";
   1.178 +  char* lastError;
   1.179 +  char* result;
   1.180 +  int targetLen;
   1.181 +  int len;
   1.182 +  int nonAsciiCharacters;
   1.183 +  int i;
   1.184 +
   1.185 +  /* Positive test */
   1.186 +  targetLen = 16 + SDLTest_RandomUint8();
   1.187 +  result = SDLTest_RandomAsciiStringOfSize(targetLen);
   1.188 +  SDLTest_AssertPass("Call to SDLTest_RandomAsciiStringOfSize(%d)", targetLen);
   1.189 +  SDLTest_AssertCheck(result != NULL, "Validate that result is not NULL");
   1.190 +  if (result != NULL) {
   1.191 +     len = SDL_strlen(result);
   1.192 +     SDLTest_AssertCheck(len == targetLen, "Validate that result length; expected: len=%d, got: %d", targetLen, len);
   1.193 +     nonAsciiCharacters = 0;
   1.194 +     for (i=0; i<len; i++) {
   1.195 +       if (iscntrl(result[i])) {
   1.196 +         nonAsciiCharacters++;
   1.197 +       }
   1.198 +     }
   1.199 +     SDLTest_AssertCheck(nonAsciiCharacters == 0, "Validate that result does not contain non-Ascii characters, got: %d", nonAsciiCharacters);
   1.200 +     if (nonAsciiCharacters) {
   1.201 +     	SDLTest_LogError("Invalid result from generator: '%s'", result);
   1.202 +     }
   1.203 +     SDL_free(result);  
   1.204 +  }
   1.205 + 
   1.206 +  /* Negative test */
   1.207 +  targetLen = 0;
   1.208 +  result = SDLTest_RandomAsciiStringOfSize(targetLen);
   1.209 +  SDLTest_AssertPass("Call to SDLTest_RandomAsciiStringOfSize(%d)", targetLen);
   1.210 +  SDLTest_AssertCheck(result == NULL, "Validate that result is NULL");
   1.211 +  lastError = (char *)SDL_GetError();
   1.212 +  SDLTest_AssertPass("SDL_GetError()");
   1.213 +  SDLTest_AssertCheck(lastError != NULL && SDL_strcmp(lastError, expectedError) == 0,
   1.214 +             "SDL_GetError(): expected message '%s', was message: '%s'",
   1.215 +             expectedError,
   1.216 +             lastError);
   1.217 +
   1.218 +  /* Clear error messages */
   1.219 +  SDL_ClearError();
   1.220 +  SDLTest_AssertPass("SDL_ClearError()");
   1.221 +   
   1.222 +  return TEST_COMPLETED;
   1.223 +}
   1.224 +
   1.225  
   1.226  /* ================= Test References ================== */
   1.227  
   1.228 @@ -1018,10 +1229,23 @@
   1.229  static const SDLTest_TestCaseReference sdltestTest10 =
   1.230  		{ (SDLTest_TestCaseFp)sdltest_randomBoundaryNumberSint64, "sdltest_randomBoundaryNumberSint64", "Calls to random boundary number generators for Sint64", TEST_ENABLED };
   1.231  
   1.232 +static const SDLTest_TestCaseReference sdltestTest11 =
   1.233 +		{ (SDLTest_TestCaseFp)sdltest_randomIntegerInRange, "sdltest_randomIntegerInRange", "Calls to ranged random number generator", TEST_ENABLED };
   1.234 +
   1.235 +static const SDLTest_TestCaseReference sdltestTest12 =
   1.236 +		{ (SDLTest_TestCaseFp)sdltest_randomAsciiString, "sdltest_randomAsciiString", "Calls to default ASCII string generator", TEST_ENABLED };
   1.237 +
   1.238 +static const SDLTest_TestCaseReference sdltestTest13 =
   1.239 +		{ (SDLTest_TestCaseFp)sdltest_randomAsciiStringWithMaximumLength, "sdltest_randomAsciiStringWithMaximumLength", "Calls to random maximum length ASCII string generator", TEST_ENABLED };
   1.240 +
   1.241 +static const SDLTest_TestCaseReference sdltestTest14 =
   1.242 +		{ (SDLTest_TestCaseFp)sdltest_randomAsciiStringOfSize, "sdltest_randomAsciiStringOfSize", "Calls to fixed size ASCII string generator", TEST_ENABLED };
   1.243 +
   1.244  /* Sequence of SDL_test test cases */
   1.245  static const SDLTest_TestCaseReference *sdltestTests[] =  {
   1.246  	&sdltestTest1, &sdltestTest2, &sdltestTest3, &sdltestTest4, &sdltestTest5, &sdltestTest6,
   1.247 -	&sdltestTest7, &sdltestTest8, &sdltestTest9, &sdltestTest10, NULL
   1.248 +	&sdltestTest7, &sdltestTest8, &sdltestTest9, &sdltestTest10, &sdltestTest11, &sdltestTest12,
   1.249 +	&sdltestTest13, &sdltestTest14, NULL
   1.250  };
   1.251  
   1.252  /* SDL_test test suite (global) */