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