test/testautomation_pixels.c
author Andreas Schiffler <aschiffler@ferzkopp.net>
Sat, 04 May 2013 09:11:18 -0700
changeset 7142 f4a670e51cde
parent 7134 09ee216e6a25
child 7151 99a4964db4c3
permissions -rw-r--r--
Added additional input validation to SDL_AllocPalette; added error codes; added test case to Pixels suite for coverage
     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   int i;
   112   Uint32 format;
   113   Uint32 masks;
   114   SDL_PixelFormat* result;
   115 
   116   /* RGB formats */
   117   for (i = 0; i < _numRGBPixelFormats; i++) {
   118     format = _RGBPixelFormats[i];
   119     SDLTest_Log("RGB Format: %s (%u)", _RGBPixelFormatsVerbose[i], format);
   120   
   121     /* Allocate format */
   122     result = SDL_AllocFormat(format);
   123     SDLTest_AssertPass("Call to SDL_AllocFormat()");
   124     SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");  
   125     if (result != NULL) {
   126       SDLTest_AssertCheck(result->format == format, "Verify value of result.format; expected: %u, got %u", format, result->format);  
   127       SDLTest_AssertCheck(result->BitsPerPixel > 0, "Verify value of result.BitsPerPixel; expected: >0, got %u", result->BitsPerPixel);  
   128       SDLTest_AssertCheck(result->BytesPerPixel > 0, "Verify value of result.BytesPerPixel; expected: >0, got %u", result->BytesPerPixel);  
   129       if (result->palette != NULL) {      
   130          masks = result->Rmask | result->Gmask | result->Bmask | result->Amask;
   131          SDLTest_AssertCheck(masks > 0, "Verify value of result.[RGBA]mask combined; expected: >0, got %u", masks);
   132       }
   133       
   134       /* Deallocate again */
   135       SDL_FreeFormat(result);
   136       SDLTest_AssertPass("Call to SDL_FreeFormat()");          
   137     }
   138   }
   139 
   140   /* Non-RGB formats */
   141   for (i = 0; i < _numNonRGBPixelFormats; i++) {
   142     format = _nonRGBPixelFormats[i];
   143     SDLTest_Log("non-RGB Format: %s (%u)", _nonRGBPixelFormatsVerbose[i], format);
   144   
   145     /* Try to allocate format */
   146     result = SDL_AllocFormat(format);
   147     SDLTest_AssertPass("Call to SDL_AllocFormat()");
   148     SDLTest_AssertCheck(result == NULL, "Verify result is NULL");  
   149   }
   150   
   151   /* Negative cases */
   152   format = 0xffffffff;
   153   result = SDL_AllocFormat(format);
   154   SDLTest_AssertPass("Call to SDL_AllocFormat(0xffffffff)");
   155   SDLTest_AssertCheck(result == NULL, "Verify result is NULL");  
   156   /* TODO: check error code */
   157 
   158   SDL_FreeFormat(NULL);
   159   SDLTest_AssertPass("Call to SDL_FreeFormat(NULL)");
   160   /* TODO: check error code */
   161   
   162   return TEST_COMPLETED;
   163 }
   164 
   165 /**
   166  * @brief Call to SDL_AllocPalette and SDL_FreePalette
   167  *
   168  * @sa http://wiki.libsdl.org/moin.fcg/SDL_AllocPalette
   169  * @sa http://wiki.libsdl.org/moin.fcg/SDL_FreePalette
   170  */
   171 int
   172 pixels_allocFreePalette(void *arg)
   173 {
   174   int variation;
   175   int i;
   176   int ncolors;
   177   SDL_Palette* result;
   178 
   179   /* Allocate palette */
   180   for (variation = 1; variation <= 3; variation++) {  
   181     switch (variation) {
   182       /* Just one color */
   183       case 1:
   184         ncolors = 1;
   185         break;
   186       /* Two colors */
   187       case 2:
   188         ncolors = 2;
   189         break;
   190       /* More than two colors */
   191       case 3:
   192         ncolors = SDLTest_RandomIntegerInRange(8, 16);
   193         break;
   194     }
   195 
   196     result = SDL_AllocPalette(ncolors);
   197     SDLTest_AssertPass("Call to SDL_AllocPalette(%d)", ncolors);
   198     SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");  
   199     if (result != NULL) {
   200       SDLTest_AssertCheck(result->ncolors == ncolors, "Verify value of result.ncolors; expected: %u, got %u", ncolors, result->ncolors);  
   201       if (result->ncolors > 0) {
   202         SDLTest_AssertCheck(result->colors != NULL, "Verify value of result.colors is not NULL");
   203         if (result->colors != NULL) {
   204           for(i = 0; i < result->ncolors; i++) {
   205             SDLTest_AssertCheck(result->colors[i].r == 255, "Verify value of result.colors[%d].r; expected: 255, got %u", i, result->colors[i].r);  
   206             SDLTest_AssertCheck(result->colors[i].g == 255, "Verify value of result.colors[%d].g; expected: 255, got %u", i, result->colors[i].g);  
   207             SDLTest_AssertCheck(result->colors[i].b == 255, "Verify value of result.colors[%d].b; expected: 255, got %u", i, result->colors[i].b);  
   208            }
   209          } 
   210       }
   211       
   212       /* Deallocate again */
   213       SDL_FreePalette(result);
   214       SDLTest_AssertPass("Call to SDL_FreePalette()");          
   215     }
   216   }
   217 
   218   /* Negative cases */
   219   for (ncolors = 0; ncolors > -3; ncolors--) {
   220     result = SDL_AllocPalette(ncolors);
   221     SDLTest_AssertPass("Call to SDL_AllocPalette(%d)", ncolors);
   222     SDLTest_AssertCheck(result == NULL, "Verify result is NULL");
   223     /* TODO: check error code */
   224   }
   225 
   226   SDL_FreePalette(NULL);
   227   SDLTest_AssertPass("Call to SDL_FreePalette(NULL)");
   228   /* TODO: check error code */
   229     
   230   return TEST_COMPLETED;
   231 }
   232 
   233 /* ================= Test References ================== */
   234 
   235 /* Pixels test cases */
   236 static const SDLTest_TestCaseReference pixelsTest1 =
   237 		{ (SDLTest_TestCaseFp)pixels_allocFreeFormat, "pixels_allocFreeFormat", "Call to SDL_AllocFormat and SDL_FreeFormat", TEST_ENABLED };
   238 
   239 static const SDLTest_TestCaseReference pixelsTest2 =
   240 		{ (SDLTest_TestCaseFp)pixels_allocFreePalette, "pixels_allocFreePalette", "Call to SDL_AllocPalette and SDL_FreePalette", TEST_ENABLED };
   241 
   242 /* Sequence of Pixels test cases */
   243 static const SDLTest_TestCaseReference *pixelsTests[] =  {
   244 	&pixelsTest1, &pixelsTest2, NULL
   245 };
   246 
   247 /* Pixels test suite (global) */
   248 SDLTest_TestSuiteReference pixelsTestSuite = {
   249 	"Pixels",
   250 	NULL,
   251 	pixelsTests,
   252 	NULL
   253 };