1 /** 2 * Pixels test suite 3 */ 4 5 #include <stdio.h> 6 7 #include "SDL.h" 8 #include "SDL_test.h" 9 10 /* Test case functions */ 11 12 /* Definition of all RGB formats used to test pixel conversions */ 13 const int _numRGBPixelFormats = 30; 14 Uint32 _RGBPixelFormats[] = 15 { 16 SDL_PIXELFORMAT_INDEX1LSB, 17 SDL_PIXELFORMAT_INDEX1MSB, 18 SDL_PIXELFORMAT_INDEX4LSB, 19 SDL_PIXELFORMAT_INDEX4MSB, 20 SDL_PIXELFORMAT_INDEX8, 21 SDL_PIXELFORMAT_RGB332, 22 SDL_PIXELFORMAT_RGB444, 23 SDL_PIXELFORMAT_RGB555, 24 SDL_PIXELFORMAT_BGR555, 25 SDL_PIXELFORMAT_ARGB4444, 26 SDL_PIXELFORMAT_RGBA4444, 27 SDL_PIXELFORMAT_ABGR4444, 28 SDL_PIXELFORMAT_BGRA4444, 29 SDL_PIXELFORMAT_ARGB1555, 30 SDL_PIXELFORMAT_RGBA5551, 31 SDL_PIXELFORMAT_ABGR1555, 32 SDL_PIXELFORMAT_BGRA5551, 33 SDL_PIXELFORMAT_RGB565, 34 SDL_PIXELFORMAT_BGR565, 35 SDL_PIXELFORMAT_RGB24, 36 SDL_PIXELFORMAT_BGR24, 37 SDL_PIXELFORMAT_RGB888, 38 SDL_PIXELFORMAT_RGBX8888, 39 SDL_PIXELFORMAT_BGR888, 40 SDL_PIXELFORMAT_BGRX8888, 41 SDL_PIXELFORMAT_ARGB8888, 42 SDL_PIXELFORMAT_RGBA8888, 43 SDL_PIXELFORMAT_ABGR8888, 44 SDL_PIXELFORMAT_BGRA8888, 45 SDL_PIXELFORMAT_ARGB2101010 46 }; 47 char* _RGBPixelFormatsVerbose[] = 48 { 49 "SDL_PIXELFORMAT_INDEX1LSB", 50 "SDL_PIXELFORMAT_INDEX1MSB", 51 "SDL_PIXELFORMAT_INDEX4LSB", 52 "SDL_PIXELFORMAT_INDEX4MSB", 53 "SDL_PIXELFORMAT_INDEX8", 54 "SDL_PIXELFORMAT_RGB332", 55 "SDL_PIXELFORMAT_RGB444", 56 "SDL_PIXELFORMAT_RGB555", 57 "SDL_PIXELFORMAT_BGR555", 58 "SDL_PIXELFORMAT_ARGB4444", 59 "SDL_PIXELFORMAT_RGBA4444", 60 "SDL_PIXELFORMAT_ABGR4444", 61 "SDL_PIXELFORMAT_BGRA4444", 62 "SDL_PIXELFORMAT_ARGB1555", 63 "SDL_PIXELFORMAT_RGBA5551", 64 "SDL_PIXELFORMAT_ABGR1555", 65 "SDL_PIXELFORMAT_BGRA5551", 66 "SDL_PIXELFORMAT_RGB565", 67 "SDL_PIXELFORMAT_BGR565", 68 "SDL_PIXELFORMAT_RGB24", 69 "SDL_PIXELFORMAT_BGR24", 70 "SDL_PIXELFORMAT_RGB888", 71 "SDL_PIXELFORMAT_RGBX8888", 72 "SDL_PIXELFORMAT_BGR888", 73 "SDL_PIXELFORMAT_BGRX8888", 74 "SDL_PIXELFORMAT_ARGB8888", 75 "SDL_PIXELFORMAT_RGBA8888", 76 "SDL_PIXELFORMAT_ABGR8888", 77 "SDL_PIXELFORMAT_BGRA8888", 78 "SDL_PIXELFORMAT_ARGB2101010" 79 }; 80 81 /* Definition of all Non-RGB formats used to test pixel conversions */ 82 const int _numNonRGBPixelFormats = 5; 83 Uint32 _nonRGBPixelFormats[] = 84 { 85 SDL_PIXELFORMAT_YV12, 86 SDL_PIXELFORMAT_IYUV, 87 SDL_PIXELFORMAT_YUY2, 88 SDL_PIXELFORMAT_UYVY, 89 SDL_PIXELFORMAT_YVYU 90 }; 91 char* _nonRGBPixelFormatsVerbose[] = 92 { 93 "SDL_PIXELFORMAT_YV12", 94 "SDL_PIXELFORMAT_IYUV", 95 "SDL_PIXELFORMAT_YUY2", 96 "SDL_PIXELFORMAT_UYVY", 97 "SDL_PIXELFORMAT_YVYU" 98 }; 99 100 /* Definition of some invalid formats for negative tests */ 101 const int _numInvalidPixelFormats = 2; 102 Uint32 _invalidPixelFormats[] = 103 { 104 0xfffffffe, 105 0xffffffff 106 }; 107 char* _invalidPixelFormatsVerbose[] = 108 { 109 "SDL_PIXELFORMAT_UNKNOWN", 110 "SDL_PIXELFORMAT_UNKNOWN" 111 }; 112 113 /* Test case functions */ 114 115 /** 116 * @brief Call to SDL_AllocFormat and SDL_FreeFormat 117 * 118 * @sa http://wiki.libsdl.org/moin.fcg/SDL_AllocFormat 119 * @sa http://wiki.libsdl.org/moin.fcg/SDL_FreeFormat 120 */ 121 int 122 pixels_allocFreeFormat(void *arg) 123 { 124 const char *unknownFormat = "SDL_PIXELFORMAT_UNKNOWN"; 125 const char *expectedError = "Parameter 'format' is invalid"; 126 const char *error; 127 int i; 128 Uint32 format; 129 Uint32 masks; 130 SDL_PixelFormat* result; 131 132 /* Blank/unknown format */ 133 format = 0; 134 SDLTest_Log("RGB Format: %s (%u)", unknownFormat, format); 135 136 /* Allocate format */ 137 result = SDL_AllocFormat(format); 138 SDLTest_AssertPass("Call to SDL_AllocFormat()"); 139 SDLTest_AssertCheck(result != NULL, "Verify result is not NULL"); 140 if (result != NULL) { 141 SDLTest_AssertCheck(result->format == format, "Verify value of result.format; expected: %u, got %u", format, result->format); 142 SDLTest_AssertCheck(result->BitsPerPixel == 0, "Verify value of result.BitsPerPixel; expected: 0, got %u", result->BitsPerPixel); 143 SDLTest_AssertCheck(result->BytesPerPixel == 0, "Verify value of result.BytesPerPixel; expected: 0, got %u", result->BytesPerPixel); 144 masks = result->Rmask | result->Gmask | result->Bmask | result->Amask; 145 SDLTest_AssertCheck(masks == 0, "Verify value of result.[RGBA]mask combined; expected: 0, got %u", masks); 146 147 /* Deallocate again */ 148 SDL_FreeFormat(result); 149 SDLTest_AssertPass("Call to SDL_FreeFormat()"); 150 } 151 152 /* RGB formats */ 153 for (i = 0; i < _numRGBPixelFormats; i++) { 154 format = _RGBPixelFormats[i]; 155 SDLTest_Log("RGB Format: %s (%u)", _RGBPixelFormatsVerbose[i], format); 156 157 /* Allocate format */ 158 result = SDL_AllocFormat(format); 159 SDLTest_AssertPass("Call to SDL_AllocFormat()"); 160 SDLTest_AssertCheck(result != NULL, "Verify result is not NULL"); 161 if (result != NULL) { 162 SDLTest_AssertCheck(result->format == format, "Verify value of result.format; expected: %u, got %u", format, result->format); 163 SDLTest_AssertCheck(result->BitsPerPixel > 0, "Verify value of result.BitsPerPixel; expected: >0, got %u", result->BitsPerPixel); 164 SDLTest_AssertCheck(result->BytesPerPixel > 0, "Verify value of result.BytesPerPixel; expected: >0, got %u", result->BytesPerPixel); 165 if (result->palette != NULL) { 166 masks = result->Rmask | result->Gmask | result->Bmask | result->Amask; 167 SDLTest_AssertCheck(masks > 0, "Verify value of result.[RGBA]mask combined; expected: >0, got %u", masks); 168 } 169 170 /* Deallocate again */ 171 SDL_FreeFormat(result); 172 SDLTest_AssertPass("Call to SDL_FreeFormat()"); 173 } 174 } 175 176 /* Non-RGB formats */ 177 for (i = 0; i < _numNonRGBPixelFormats; i++) { 178 format = _nonRGBPixelFormats[i]; 179 SDLTest_Log("non-RGB Format: %s (%u)", _nonRGBPixelFormatsVerbose[i], format); 180 181 /* Try to allocate format */ 182 result = SDL_AllocFormat(format); 183 SDLTest_AssertPass("Call to SDL_AllocFormat()"); 184 SDLTest_AssertCheck(result == NULL, "Verify result is NULL"); 185 } 186 187 /* Negative cases */ 188 189 /* Invalid Formats */ 190 for (i = 0; i < _numInvalidPixelFormats; i++) { 191 SDL_ClearError(); 192 SDLTest_AssertPass("Call to SDL_ClearError()"); 193 format = _invalidPixelFormats[i]; 194 result = SDL_AllocFormat(format); 195 SDLTest_AssertPass("Call to SDL_AllocFormat(%u)", format); 196 SDLTest_AssertCheck(result == NULL, "Verify result is NULL"); 197 error = SDL_GetError(); 198 SDLTest_AssertPass("Call to SDL_GetError()"); 199 SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL"); 200 if (error != NULL) { 201 SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0, 202 "Validate error message, expected: '%s', got: '%s'", expectedError, error); 203 } 204 } 205 206 /* Invalid free pointer */ 207 SDL_ClearError(); 208 SDLTest_AssertPass("Call to SDL_ClearError()"); 209 SDL_FreeFormat(NULL); 210 SDLTest_AssertPass("Call to SDL_FreeFormat(NULL)"); 211 error = SDL_GetError(); 212 SDLTest_AssertPass("Call to SDL_GetError()"); 213 SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL"); 214 if (error != NULL) { 215 SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0, 216 "Validate error message, expected: '%s', got: '%s'", expectedError, error); 217 } 218 219 return TEST_COMPLETED; 220 } 221 222 /** 223 * @brief Call to SDL_GetPixelFormatName 224 * 225 * @sa http://wiki.libsdl.org/moin.fcg/SDL_GetPixelFormatName 226 */ 227 int 228 pixels_getPixelFormatName(void *arg) 229 { 230 const char *unknownFormat = "SDL_PIXELFORMAT_UNKNOWN"; 231 const char *error; 232 int i; 233 Uint32 format; 234 char* result; 235 236 /* Blank/undefined format */ 237 format = 0; 238 SDLTest_Log("RGB Format: %s (%u)", unknownFormat, format); 239 240 /* Get name of format */ 241 result = (char *)SDL_GetPixelFormatName(format); 242 SDLTest_AssertPass("Call to SDL_GetPixelFormatName()"); 243 SDLTest_AssertCheck(result != NULL, "Verify result is not NULL"); 244 if (result != NULL) { 245 SDLTest_AssertCheck(result[0] != '\0', "Verify result is non-empty"); 246 SDLTest_AssertCheck(SDL_strcmp(result, unknownFormat) == 0, 247 "Verify result text; expected: %s, got %s", unknownFormat, result); 248 } 249 250 /* RGB formats */ 251 for (i = 0; i < _numRGBPixelFormats; i++) { 252 format = _RGBPixelFormats[i]; 253 SDLTest_Log("RGB Format: %s (%u)", _RGBPixelFormatsVerbose[i], format); 254 255 /* Get name of format */ 256 result = (char *)SDL_GetPixelFormatName(format); 257 SDLTest_AssertPass("Call to SDL_GetPixelFormatName()"); 258 SDLTest_AssertCheck(result != NULL, "Verify result is not NULL"); 259 if (result != NULL) { 260 SDLTest_AssertCheck(result[0] != '\0', "Verify result is non-empty"); 261 SDLTest_AssertCheck(SDL_strcmp(result, _RGBPixelFormatsVerbose[i]) == 0, 262 "Verify result text; expected: %s, got %s", _RGBPixelFormatsVerbose[i], result); 263 } 264 } 265 266 /* Non-RGB formats */ 267 for (i = 0; i < _numNonRGBPixelFormats; i++) { 268 format = _nonRGBPixelFormats[i]; 269 SDLTest_Log("non-RGB Format: %s (%u)", _nonRGBPixelFormatsVerbose[i], format); 270 271 /* Get name of format */ 272 result = (char *)SDL_GetPixelFormatName(format); 273 SDLTest_AssertPass("Call to SDL_GetPixelFormatName()"); 274 SDLTest_AssertCheck(result != NULL, "Verify result is not NULL"); 275 if (result != NULL) { 276 SDLTest_AssertCheck(result[0] != '\0', "Verify result is non-empty"); 277 SDLTest_AssertCheck(SDL_strcmp(result, _nonRGBPixelFormatsVerbose[i]) == 0, 278 "Verify result text; expected: %s, got %s", _nonRGBPixelFormatsVerbose[i], result); 279 } 280 } 281 282 /* Negative cases */ 283 284 /* Invalid Formats */ 285 SDL_ClearError(); 286 SDLTest_AssertPass("Call to SDL_ClearError()"); 287 for (i = 0; i < _numInvalidPixelFormats; i++) { 288 format = _invalidPixelFormats[i]; 289 result = (char *)SDL_GetPixelFormatName(format); 290 SDLTest_AssertPass("Call to SDL_GetPixelFormatName(%u)", format); 291 SDLTest_AssertCheck(result != NULL, "Verify result is not NULL"); 292 if (result != NULL) { 293 SDLTest_AssertCheck(result[0] != '\0', 294 "Verify result is non-empty; got: %s", result); 295 SDLTest_AssertCheck(SDL_strcmp(result, _invalidPixelFormatsVerbose[i]) == 0, 296 "Validate name is UNKNOWN, expected: '%s', got: '%s'", _invalidPixelFormatsVerbose[i], result); 297 } 298 error = SDL_GetError(); 299 SDLTest_AssertPass("Call to SDL_GetError()"); 300 SDLTest_AssertCheck(error == NULL || error[0] == '\0', "Validate that error message is empty"); 301 } 302 303 return TEST_COMPLETED; 304 } 305 306 /** 307 * @brief Call to SDL_AllocPalette and SDL_FreePalette 308 * 309 * @sa http://wiki.libsdl.org/moin.fcg/SDL_AllocPalette 310 * @sa http://wiki.libsdl.org/moin.fcg/SDL_FreePalette 311 */ 312 int 313 pixels_allocFreePalette(void *arg) 314 { 315 const char *expectedError1 = "Parameter 'ncolors' is invalid"; 316 const char *expectedError2 = "Parameter 'palette' is invalid"; 317 const char *error; 318 int variation; 319 int i; 320 int ncolors; 321 SDL_Palette* result; 322 323 /* Allocate palette */ 324 for (variation = 1; variation <= 3; variation++) { 325 switch (variation) { 326 /* Just one color */ 327 case 1: 328 ncolors = 1; 329 break; 330 /* Two colors */ 331 case 2: 332 ncolors = 2; 333 break; 334 /* More than two colors */ 335 case 3: 336 ncolors = SDLTest_RandomIntegerInRange(8, 16); 337 break; 338 } 339 340 result = SDL_AllocPalette(ncolors); 341 SDLTest_AssertPass("Call to SDL_AllocPalette(%d)", ncolors); 342 SDLTest_AssertCheck(result != NULL, "Verify result is not NULL"); 343 if (result != NULL) { 344 SDLTest_AssertCheck(result->ncolors == ncolors, "Verify value of result.ncolors; expected: %u, got %u", ncolors, result->ncolors); 345 if (result->ncolors > 0) { 346 SDLTest_AssertCheck(result->colors != NULL, "Verify value of result.colors is not NULL"); 347 if (result->colors != NULL) { 348 for(i = 0; i < result->ncolors; i++) { 349 SDLTest_AssertCheck(result->colors[i].r == 255, "Verify value of result.colors[%d].r; expected: 255, got %u", i, result->colors[i].r); 350 SDLTest_AssertCheck(result->colors[i].g == 255, "Verify value of result.colors[%d].g; expected: 255, got %u", i, result->colors[i].g); 351 SDLTest_AssertCheck(result->colors[i].b == 255, "Verify value of result.colors[%d].b; expected: 255, got %u", i, result->colors[i].b); 352 } 353 } 354 } 355 356 /* Deallocate again */ 357 SDL_FreePalette(result); 358 SDLTest_AssertPass("Call to SDL_FreePalette()"); 359 } 360 } 361 362 /* Negative cases */ 363 364 /* Invalid number of colors */ 365 for (ncolors = 0; ncolors > -3; ncolors--) { 366 SDL_ClearError(); 367 SDLTest_AssertPass("Call to SDL_ClearError()"); 368 result = SDL_AllocPalette(ncolors); 369 SDLTest_AssertPass("Call to SDL_AllocPalette(%d)", ncolors); 370 SDLTest_AssertCheck(result == NULL, "Verify result is NULL"); 371 error = SDL_GetError(); 372 SDLTest_AssertPass("Call to SDL_GetError()"); 373 SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL"); 374 if (error != NULL) { 375 SDLTest_AssertCheck(SDL_strcmp(error, expectedError1) == 0, 376 "Validate error message, expected: '%s', got: '%s'", expectedError1, error); 377 } 378 } 379 380 /* Invalid free pointer */ 381 SDL_ClearError(); 382 SDLTest_AssertPass("Call to SDL_ClearError()"); 383 SDL_FreePalette(NULL); 384 SDLTest_AssertPass("Call to SDL_FreePalette(NULL)"); 385 error = SDL_GetError(); 386 SDLTest_AssertPass("Call to SDL_GetError()"); 387 SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL"); 388 if (error != NULL) { 389 SDLTest_AssertCheck(SDL_strcmp(error, expectedError2) == 0, 390 "Validate error message, expected: '%s', got: '%s'", expectedError2, error); 391 } 392 393 return TEST_COMPLETED; 394 } 395 396 /** 397 * @brief Call to SDL_CalculateGammaRamp 398 * 399 * @sa http://wiki.libsdl.org/moin.fcg/SDL_CalculateGammaRamp 400 */ 401 int 402 pixels_calcGammaRamp(void *arg) 403 { 404 const char *expectedError1 = "Parameter 'gamma' is invalid"; 405 const char *expectedError2 = "Parameter 'ramp' is invalid"; 406 const char *error; 407 float gamma; 408 Uint16 *ramp; 409 int variation; 410 int i; 411 int changed; 412 Uint16 magic = 0xbeef; 413 414 /* Allocate temp ramp array and fill with some value */ 415 ramp = (Uint16 *)SDL_malloc(256 * sizeof(Uint16)); 416 SDLTest_AssertCheck(ramp != NULL, "Validate temp ramp array could be allocated"); 417 if (ramp == NULL) return TEST_ABORTED; 418 419 /* Make call with different gamma values */ 420 for (variation = 0; variation < 4; variation++) { 421 switch (variation) { 422 /* gamma = 0 all black */ 423 case 0: 424 gamma = 0.0f; 425 break; 426 /* gamma = 1 identity */ 427 case 1: 428 gamma = 1.0f; 429 break; 430 /* gamma = [0.2,0.8] normal range */ 431 case 2: 432 gamma = 0.2f + 0.8f * SDLTest_RandomUnitFloat(); 433 break; 434 /* gamma = >1.1 non-standard range */ 435 case 3: 436 gamma = 1.1f + SDLTest_RandomUnitFloat(); 437 break; 438 } 439 440 /* Make call and check that values were updated */ 441 for (i = 0; i < 256; i++) ramp[i] = magic; 442 SDL_CalculateGammaRamp(gamma, ramp); 443 SDLTest_AssertPass("Call to SDL_CalculateGammaRamp(%f)", gamma); 444 changed = 0; 445 for (i = 0; i < 256; i++) if (ramp[i] != magic) changed++; 446 SDLTest_AssertCheck(changed > 250, "Validate that ramp was calculated; expected: >250 values changed, got: %d values changed", changed); 447 448 /* Additional value checks for some cases */ 449 i = SDLTest_RandomIntegerInRange(64,192); 450 switch (variation) { 451 case 0: 452 SDLTest_AssertCheck(ramp[i] == 0, "Validate value at position %d; expected: 0, got: %d", i, ramp[i]); 453 break; 454 case 1: 455 SDLTest_AssertCheck(ramp[i] == ((i << 8) | i), "Validate value at position %d; expected: %d, got: %d", i, (i << 8) | i, ramp[i]); 456 break; 457 case 2: 458 case 3: 459 SDLTest_AssertCheck(ramp[i] > 0, "Validate value at position %d; expected: >0, got: %d", i, ramp[i]); 460 break; 461 } 462 } 463 464 /* Negative cases */ 465 SDL_ClearError(); 466 SDLTest_AssertPass("Call to SDL_ClearError()"); 467 gamma = -1; 468 for (i=0; i<256; i++) ramp[i] = magic; 469 SDL_CalculateGammaRamp(gamma, ramp); 470 SDLTest_AssertPass("Call to SDL_CalculateGammaRamp(%f)", gamma); 471 error = SDL_GetError(); 472 SDLTest_AssertPass("Call to SDL_GetError()"); 473 SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL"); 474 if (error != NULL) { 475 SDLTest_AssertCheck(SDL_strcmp(error, expectedError1) == 0, 476 "Validate error message, expected: '%s', got: '%s'", expectedError1, error); 477 } 478 changed = 0; 479 for (i = 0; i < 256; i++) if (ramp[i] != magic) changed++; 480 SDLTest_AssertCheck(changed ==0, "Validate that ramp unchanged; expected: 0 values changed got: %d values changed", changed); 481 482 SDL_CalculateGammaRamp(0.5f, NULL); 483 SDLTest_AssertPass("Call to SDL_CalculateGammaRamp(0.5,NULL)"); 484 error = SDL_GetError(); 485 SDLTest_AssertPass("Call to SDL_GetError()"); 486 SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL"); 487 if (error != NULL) { 488 SDLTest_AssertCheck(SDL_strcmp(error, expectedError2) == 0, 489 "Validate error message, expected: '%s', got: '%s'", expectedError2, error); 490 } 491 492 /* Cleanup */ 493 SDL_free(ramp); 494 495 496 return TEST_COMPLETED; 497 } 498 499 /* ================= Test References ================== */ 500 501 /* Pixels test cases */ 502 static const SDLTest_TestCaseReference pixelsTest1 = 503 { (SDLTest_TestCaseFp)pixels_allocFreeFormat, "pixels_allocFreeFormat", "Call to SDL_AllocFormat and SDL_FreeFormat", TEST_ENABLED }; 504 505 static const SDLTest_TestCaseReference pixelsTest2 = 506 { (SDLTest_TestCaseFp)pixels_allocFreePalette, "pixels_allocFreePalette", "Call to SDL_AllocPalette and SDL_FreePalette", TEST_ENABLED }; 507 508 static const SDLTest_TestCaseReference pixelsTest3 = 509 { (SDLTest_TestCaseFp)pixels_calcGammaRamp, "pixels_calcGammaRamp", "Call to SDL_CalculateGammaRamp", TEST_ENABLED }; 510 511 static const SDLTest_TestCaseReference pixelsTest4 = 512 { (SDLTest_TestCaseFp)pixels_getPixelFormatName, "pixels_getPixelFormatName", "Call to SDL_GetPixelFormatName", TEST_ENABLED }; 513 514 /* Sequence of Pixels test cases */ 515 static const SDLTest_TestCaseReference *pixelsTests[] = { 516 &pixelsTest1, &pixelsTest2, &pixelsTest3, &pixelsTest4, NULL 517 }; 518 519 /* Pixels test suite (global) */ 520 SDLTest_TestSuiteReference pixelsTestSuite = { 521 "Pixels", 522 NULL, 523 pixelsTests, 524 NULL 525 }; 526