test/testautomation_pixels.c
author Andreas Schiffler <aschiffler@ferzkopp.net>
Sun, 05 May 2013 11:17:40 -0700
changeset 7151 99a4964db4c3
parent 7142 f4a670e51cde
child 7153 51cf5475ac56
permissions -rw-r--r--
Add input validation to SDL_CalculateGammaRamp; add test coverage to Pixels suite; update test cases in Pixels suite
     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 /* Test case functions */
   101 
   102 /**
   103  * @brief Call to SDL_AllocFormat and SDL_FreeFormat
   104  *
   105  * @sa http://wiki.libsdl.org/moin.fcg/SDL_AllocFormat
   106  * @sa http://wiki.libsdl.org/moin.fcg/SDL_FreeFormat 
   107  */
   108 int
   109 pixels_allocFreeFormat(void *arg)
   110 {
   111   const char *expectedError = "Parameter 'format' is invalid";
   112   const char *error;   
   113   char message[256];
   114   int i;
   115   Uint32 format;
   116   Uint32 masks;
   117   SDL_PixelFormat* result;
   118 
   119   /* RGB formats */
   120   for (i = 0; i < _numRGBPixelFormats; i++) {
   121     format = _RGBPixelFormats[i];
   122     SDLTest_Log("RGB Format: %s (%u)", _RGBPixelFormatsVerbose[i], format);
   123   
   124     /* Allocate format */
   125     result = SDL_AllocFormat(format);
   126     SDLTest_AssertPass("Call to SDL_AllocFormat()");
   127     SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");  
   128     if (result != NULL) {
   129       SDLTest_AssertCheck(result->format == format, "Verify value of result.format; expected: %u, got %u", format, result->format);  
   130       SDLTest_AssertCheck(result->BitsPerPixel > 0, "Verify value of result.BitsPerPixel; expected: >0, got %u", result->BitsPerPixel);  
   131       SDLTest_AssertCheck(result->BytesPerPixel > 0, "Verify value of result.BytesPerPixel; expected: >0, got %u", result->BytesPerPixel);  
   132       if (result->palette != NULL) {      
   133          masks = result->Rmask | result->Gmask | result->Bmask | result->Amask;
   134          SDLTest_AssertCheck(masks > 0, "Verify value of result.[RGBA]mask combined; expected: >0, got %u", masks);
   135       }
   136       
   137       /* Deallocate again */
   138       SDL_FreeFormat(result);
   139       SDLTest_AssertPass("Call to SDL_FreeFormat()");          
   140     }
   141   }
   142 
   143   /* Non-RGB formats */
   144   for (i = 0; i < _numNonRGBPixelFormats; i++) {
   145     format = _nonRGBPixelFormats[i];
   146     SDLTest_Log("non-RGB Format: %s (%u)", _nonRGBPixelFormatsVerbose[i], format);
   147   
   148     /* Try to allocate format */
   149     result = SDL_AllocFormat(format);
   150     SDLTest_AssertPass("Call to SDL_AllocFormat()");
   151     SDLTest_AssertCheck(result == NULL, "Verify result is NULL");  
   152   }
   153   
   154   /* Negative cases */
   155   
   156   /* Invalid Format */  
   157   SDL_ClearError();
   158   SDLTest_AssertPass("Call to SDL_ClearError()");
   159   format = 0xffffffff;
   160   result = SDL_AllocFormat(format);
   161   SDLTest_AssertPass("Call to SDL_AllocFormat(0xffffffff)");
   162   SDLTest_AssertCheck(result == NULL, "Verify result is NULL");  
   163   error = SDL_GetError();
   164   SDLTest_AssertPass("Call to SDL_GetError()");
   165   SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
   166   if (error != NULL) {
   167       SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0, 
   168           "Validate error message, expected: '%s', got: '%s'", expectedError, error);
   169   }
   170 
   171   /* Invalid free pointer */
   172   SDL_ClearError();
   173   SDLTest_AssertPass("Call to SDL_ClearError()");
   174   SDL_FreeFormat(NULL);
   175   SDLTest_AssertPass("Call to SDL_FreeFormat(NULL)");
   176   error = SDL_GetError();
   177   SDLTest_AssertPass("Call to SDL_GetError()");
   178   SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
   179   if (error != NULL) {
   180       SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0, 
   181           "Validate error message, expected: '%s', got: '%s'", expectedError, error);
   182   }
   183   
   184   return TEST_COMPLETED;
   185 }
   186 
   187 /**
   188  * @brief Call to SDL_AllocPalette and SDL_FreePalette
   189  *
   190  * @sa http://wiki.libsdl.org/moin.fcg/SDL_AllocPalette
   191  * @sa http://wiki.libsdl.org/moin.fcg/SDL_FreePalette
   192  */
   193 int
   194 pixels_allocFreePalette(void *arg)
   195 {
   196   const char *expectedError1 = "Parameter 'ncolors' is invalid";
   197   const char *expectedError2 = "Parameter 'palette' is invalid";
   198   const char *error;   
   199   char message[256];
   200   int variation;
   201   int i;
   202   int ncolors;
   203   SDL_Palette* result;
   204 
   205   /* Allocate palette */
   206   for (variation = 1; variation <= 3; variation++) {  
   207     switch (variation) {
   208       /* Just one color */
   209       case 1:
   210         ncolors = 1;
   211         break;
   212       /* Two colors */
   213       case 2:
   214         ncolors = 2;
   215         break;
   216       /* More than two colors */
   217       case 3:
   218         ncolors = SDLTest_RandomIntegerInRange(8, 16);
   219         break;
   220     }
   221 
   222     result = SDL_AllocPalette(ncolors);
   223     SDLTest_AssertPass("Call to SDL_AllocPalette(%d)", ncolors);
   224     SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");  
   225     if (result != NULL) {
   226       SDLTest_AssertCheck(result->ncolors == ncolors, "Verify value of result.ncolors; expected: %u, got %u", ncolors, result->ncolors);  
   227       if (result->ncolors > 0) {
   228         SDLTest_AssertCheck(result->colors != NULL, "Verify value of result.colors is not NULL");
   229         if (result->colors != NULL) {
   230           for(i = 0; i < result->ncolors; i++) {
   231             SDLTest_AssertCheck(result->colors[i].r == 255, "Verify value of result.colors[%d].r; expected: 255, got %u", i, result->colors[i].r);  
   232             SDLTest_AssertCheck(result->colors[i].g == 255, "Verify value of result.colors[%d].g; expected: 255, got %u", i, result->colors[i].g);  
   233             SDLTest_AssertCheck(result->colors[i].b == 255, "Verify value of result.colors[%d].b; expected: 255, got %u", i, result->colors[i].b);  
   234            }
   235          } 
   236       }
   237       
   238       /* Deallocate again */
   239       SDL_FreePalette(result);
   240       SDLTest_AssertPass("Call to SDL_FreePalette()");          
   241     }
   242   }
   243 
   244   /* Negative cases */
   245     
   246   /* Invalid number of colors */
   247   for (ncolors = 0; ncolors > -3; ncolors--) {
   248     SDL_ClearError();
   249     SDLTest_AssertPass("Call to SDL_ClearError()");
   250     result = SDL_AllocPalette(ncolors);
   251     SDLTest_AssertPass("Call to SDL_AllocPalette(%d)", ncolors);
   252     SDLTest_AssertCheck(result == NULL, "Verify result is NULL");
   253     error = SDL_GetError();
   254     SDLTest_AssertPass("Call to SDL_GetError()");
   255     SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
   256     if (error != NULL) {
   257       SDLTest_AssertCheck(SDL_strcmp(error, expectedError1) == 0, 
   258           "Validate error message, expected: '%s', got: '%s'", expectedError1, error);
   259     }
   260   }
   261 
   262   /* Invalid free pointer */
   263   SDL_ClearError();
   264   SDLTest_AssertPass("Call to SDL_ClearError()");
   265   SDL_FreePalette(NULL);
   266   SDLTest_AssertPass("Call to SDL_FreePalette(NULL)");
   267   error = SDL_GetError();
   268   SDLTest_AssertPass("Call to SDL_GetError()");
   269   SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
   270   if (error != NULL) {
   271       SDLTest_AssertCheck(SDL_strcmp(error, expectedError2) == 0, 
   272           "Validate error message, expected: '%s', got: '%s'", expectedError2, error);
   273   }
   274     
   275   return TEST_COMPLETED;
   276 }
   277 
   278 /**
   279  * @brief Call to SDL_CalculateGammaRamp
   280  *
   281  * @sa http://wiki.libsdl.org/moin.fcg/SDL_CalculateGammaRamp
   282  */
   283 int
   284 pixels_calcGammaRamp(void *arg)
   285 {
   286   const char *expectedError1 = "Parameter 'gamma' is invalid";
   287   const char *expectedError2 = "Parameter 'ramp' is invalid";
   288   const char *error;   
   289   char message[256];
   290   float gamma;
   291   Uint16 *ramp;
   292   int variation;
   293   int i;
   294   int changed;
   295   Uint16 magic = 0xbeef;
   296 
   297   /* Allocate temp ramp array and fill with some value*/
   298   ramp = (Uint16 *)SDL_malloc(256 * sizeof(Uint16));
   299   SDLTest_AssertCheck(ramp != NULL, "Validate temp ramp array could be allocated");
   300   if (ramp == NULL) return TEST_ABORTED;
   301   
   302   /* Make call with different gamma values */
   303   for (variation = 0; variation < 4; variation++) {
   304     switch (variation) {
   305       /* gamma = 0 all black */
   306       case 0:      
   307         gamma = 0.0f;
   308         break;
   309       /* gamma = 1 identity */
   310       case 1:
   311         gamma = 1.0f;
   312         break;
   313       /* gamma = ]0,1[ normal range */  
   314       case 2:
   315         gamma = 0.01f + 0.98f * SDLTest_RandomUnitFloat();
   316         break;
   317       /* gamma = >1.0 non-standard range */  
   318       case 3:
   319         gamma = 0.01f + 0.98f * SDLTest_RandomUnitFloat();
   320         break;
   321     }
   322 
   323     /* Make call and check that values were updated */  
   324     for (i = 0; i < 256; i++) ramp[i] = magic;
   325     SDL_CalculateGammaRamp(gamma, ramp);
   326     SDLTest_AssertPass("Call to SDL_CalculateGammaRamp(%f)", gamma);
   327     changed = 0;
   328     for (i = 0; i < 256; i++) if (ramp[i] != magic) changed++;
   329     SDLTest_AssertCheck(changed > 250, "Validate that ramp was calculated; expected: >250 values changed, got: %d values changed", changed);
   330 
   331     /* Additional value checks for some cases */
   332     i = SDLTest_RandomIntegerInRange(64,192);
   333     switch (variation) {
   334       case 0:
   335         SDLTest_AssertCheck(ramp[i] == 0, "Validate value at position %d; expected: 0, got: %d", i, ramp[i]);
   336         break;
   337       case 1:
   338         SDLTest_AssertCheck(ramp[i] == (i << 8) | i, "Validate value at position %d; expected: %d, got: %d", i, (i << 8) | i, ramp[i]);
   339         break;
   340       case 2:
   341       case 3:
   342         SDLTest_AssertCheck(ramp[i] > 0, "Validate value at position %d; expected: >0, got: %d", i, ramp[i]);
   343         break;
   344     }
   345   }
   346 
   347   
   348   /* Negative cases */
   349   SDL_ClearError();
   350   SDLTest_AssertPass("Call to SDL_ClearError()");
   351   gamma = -1;
   352   for (i=0; i<256; i++) ramp[i] = magic;
   353   SDL_CalculateGammaRamp(gamma, ramp);
   354   SDLTest_AssertPass("Call to SDL_CalculateGammaRamp(%f)", gamma);
   355   error = SDL_GetError();
   356   SDLTest_AssertPass("Call to SDL_GetError()");
   357   SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
   358   if (error != NULL) {
   359       SDLTest_AssertCheck(SDL_strcmp(error, expectedError1) == 0, 
   360           "Validate error message, expected: '%s', got: '%s'", expectedError1, error);
   361   }
   362   changed = 0;
   363   for (i = 0; i < 256; i++) if (ramp[i] != magic) changed++;
   364   SDLTest_AssertCheck(changed ==0, "Validate that ramp unchanged; expected: 0 values changed got: %d values changed", changed);
   365 
   366   SDL_CalculateGammaRamp(0.5f, NULL);
   367   SDLTest_AssertPass("Call to SDL_CalculateGammaRamp(0.5,NULL)");
   368   error = SDL_GetError();
   369   SDLTest_AssertPass("Call to SDL_GetError()");
   370   SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
   371   if (error != NULL) {
   372       SDLTest_AssertCheck(SDL_strcmp(error, expectedError2) == 0, 
   373           "Validate error message, expected: '%s', got: '%s'", expectedError2, error);
   374   }
   375   
   376   /* Cleanup */
   377   SDL_free(ramp);
   378     
   379     
   380   return TEST_COMPLETED;
   381 }
   382 
   383 /* ================= Test References ================== */
   384 
   385 /* Pixels test cases */
   386 static const SDLTest_TestCaseReference pixelsTest1 =
   387 		{ (SDLTest_TestCaseFp)pixels_allocFreeFormat, "pixels_allocFreeFormat", "Call to SDL_AllocFormat and SDL_FreeFormat", TEST_ENABLED };
   388 
   389 static const SDLTest_TestCaseReference pixelsTest2 =
   390 		{ (SDLTest_TestCaseFp)pixels_allocFreePalette, "pixels_allocFreePalette", "Call to SDL_AllocPalette and SDL_FreePalette", TEST_ENABLED };
   391 
   392 static const SDLTest_TestCaseReference pixelsTest3 =
   393 		{ (SDLTest_TestCaseFp)pixels_calcGammaRamp, "pixels_calcGammaRamp", "Call to SDL_CalculateGammaRamp", TEST_ENABLED };
   394 
   395 /* Sequence of Pixels test cases */
   396 static const SDLTest_TestCaseReference *pixelsTests[] =  {
   397 	&pixelsTest1, &pixelsTest2, &pixelsTest3, NULL
   398 };
   399 
   400 /* Pixels test suite (global) */
   401 SDLTest_TestSuiteReference pixelsTestSuite = {
   402 	"Pixels",
   403 	NULL,
   404 	pixelsTests,
   405 	NULL
   406 };