test/testautomation_surface.c
author Andreas Schiffler <aschiffler@ferzkopp.net>
Sun, 30 Dec 2012 11:12:31 -0800
changeset 6780 111b37940b35
parent 6779 c3b579c78059
child 6811 60d95fcfaf3c
permissions -rw-r--r--
Add define to surface suite to quiet VS compiler warnings
     1 /**
     2  * Original code: automated SDL surface test written by Edgar Simo "bobbens"
     3  * Adapted/rewritten for test lib by Andreas Schiffler
     4  */
     5 
     6 /* Supress C4996 VS compiler warnings for unlink() */
     7 #define _CRT_SECURE_NO_DEPRECATE
     8 #define _CRT_NONSTDC_NO_DEPRECATE
     9 
    10 #include <stdio.h>
    11 #include <sys/stat.h>
    12 
    13 #include "SDL.h"
    14 #include "SDL_test.h"
    15 
    16 /* ================= Test Case Implementation ================== */
    17 
    18 /* Shared test surface */
    19 
    20 static SDL_Surface *referenceSurface = NULL;
    21 static SDL_Surface *testSurface = NULL;
    22 
    23 /* Helper functions for the test cases */
    24 
    25 #define TEST_SURFACE_WIDTH testSurface->w
    26 #define TEST_SURFACE_HEIGHT testSurface->h
    27 
    28 /* Fixture */
    29 
    30 /* Create a 32-bit writable surface for screen tests */
    31 void
    32 _surfaceSetUp(void *arg)
    33 {
    34     Uint32 rmask, gmask, bmask, amask;
    35 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
    36     rmask = 0xff000000;
    37     gmask = 0x00ff0000;
    38     bmask = 0x0000ff00;
    39     amask = 0x000000ff;
    40 #else
    41     rmask = 0x000000ff;
    42     gmask = 0x0000ff00;
    43     bmask = 0x00ff0000;
    44     amask = 0xff000000;
    45 #endif
    46 
    47 	referenceSurface = SDLTest_ImageBlit(); /* For size info */
    48 	testSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, referenceSurface->w, referenceSurface->h, 32, rmask, gmask, bmask, amask);
    49     SDLTest_AssertCheck(testSurface != NULL, "Check that testSurface is not NULL");
    50 }
    51 
    52 void
    53 _surfaceTearDown(void *arg)
    54 {
    55 	if (referenceSurface != NULL) {
    56 		SDL_FreeSurface(referenceSurface);
    57 		referenceSurface = NULL;
    58 	}
    59 	if (testSurface != NULL) {
    60 		SDL_FreeSurface(testSurface);
    61 		testSurface = NULL;
    62 	}
    63 }
    64 
    65 /**
    66  * Helper that clears the test surface
    67  */
    68 void _clearTestSurface()
    69 {
    70 	int ret;
    71 	Uint32 color;
    72 	
    73 	/* Clear surface. */
    74 	color = SDL_MapRGB( testSurface->format, 0, 0, 0);
    75 	SDLTest_AssertPass("Call to SDL_MapRGB()");
    76 	ret = SDL_FillRect( testSurface, NULL, color);
    77 	SDLTest_AssertPass("Call to SDL_FillRect()");
    78 	SDLTest_AssertCheck(ret == 0, "Verify result from SDL_FillRect, expected: 0, got: %i", ret);
    79 }
    80 
    81 /**
    82  * Helper that blits in a specific blend mode, -1 for basic blitting, -2 for color mod, -3 for alpha mod, -4 for mixed blend modes.
    83  */
    84 void _testBlitBlendMode(int mode)
    85 {
    86 	int ret;
    87 	int i, j, ni, nj;
    88 	SDL_Surface *face;
    89 	SDL_Rect rect;
    90 	int nmode;
    91 	SDL_BlendMode bmode;
    92 	int checkFailCount1;
    93 	int checkFailCount2;
    94 	int checkFailCount3;
    95 	int checkFailCount4;
    96 
    97 	/* Check test surface */
    98 	SDLTest_AssertCheck(testSurface != NULL, "Verify testSurface is not NULL");
    99 	if (testSurface == NULL) return;
   100 	
   101 	/* Create sample surface */
   102 	face = SDLTest_ImageFace();
   103 	SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
   104 	if (face == NULL) return;
   105 
   106 	/* Clear the test surface */
   107         _clearTestSurface();
   108         
   109 	/* Target rect size */
   110 	rect.w = face->w;
   111 	rect.h = face->h;
   112 	
   113 	/* Steps to take */
   114 	ni = testSurface->w - face->w;
   115 	nj = testSurface->h - face->h;
   116 
   117 	/* Optionally set blend mode. */
   118 	if (mode >= 0) {
   119 		ret = SDL_SetSurfaceBlendMode( face, (SDL_BlendMode)mode );
   120 		SDLTest_AssertPass("Call to SDL_SetSurfaceBlendMode()");
   121 		SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SetSurfaceBlendMode(..., %i), expected: 0, got: %i", mode, ret);
   122 	}
   123 	
   124 	/* Test blend mode. */
   125 	checkFailCount1 = 0;
   126 	checkFailCount2 = 0;
   127 	checkFailCount3 = 0;
   128 	checkFailCount4 = 0;
   129 	for (j=0; j <= nj; j+=4) {
   130 	  for (i=0; i <= ni; i+=4) {
   131 	  	if (mode == -2) {
   132 			/* Set colour mod. */
   133 			ret = SDL_SetSurfaceColorMod( face, (255/nj)*j, (255/ni)*i, (255/nj)*j );
   134 			if (ret != 0) checkFailCount2++;
   135 		}
   136 	  	else if (mode == -3) {
   137 	  		/* Set alpha mod. */
   138 			ret = SDL_SetSurfaceAlphaMod( face, (255/ni)*i );
   139 			if (ret != 0) checkFailCount3++;
   140 		}
   141 		else if (mode == -4) {
   142 			/* Crazy blending mode magic. */
   143 			nmode = (i/4*j/4) % 4;
   144 			if (nmode==0) bmode = SDL_BLENDMODE_NONE;
   145 			else if (nmode==1) bmode = SDL_BLENDMODE_BLEND;
   146 			else if (nmode==2) bmode = SDL_BLENDMODE_ADD;
   147 			else if (nmode==3) bmode = SDL_BLENDMODE_MOD;
   148 			ret = SDL_SetSurfaceBlendMode( face, bmode );		
   149 			if (ret != 0) checkFailCount4++;
   150 		}
   151 
   152 		 /* Blitting. */
   153 		 rect.x = i;
   154 		 rect.y = j;
   155 		 ret = SDL_BlitSurface( face, NULL, testSurface, &rect );
   156 		 if (ret != 0) checkFailCount1++;
   157 	  }
   158 	}
   159 	SDLTest_AssertCheck(checkFailCount1 == 0, "Validate results from calls to SDL_BlitSurface, expected: 0, got: %i", checkFailCount1);
   160 	SDLTest_AssertCheck(checkFailCount2 == 0, "Validate results from calls to SDL_SetSurfaceColorMod, expected: 0, got: %i", checkFailCount2);
   161 	SDLTest_AssertCheck(checkFailCount3 == 0, "Validate results from calls to SDL_SetSurfaceAlphaMod, expected: 0, got: %i", checkFailCount3);
   162 	SDLTest_AssertCheck(checkFailCount4 == 0, "Validate results from calls to SDL_SetSurfaceBlendMode, expected: 0, got: %i", checkFailCount4);
   163 	
   164 	/* Clean up */
   165 	if (face != NULL) {
   166 		SDL_FreeSurface(face);
   167 		face = NULL;
   168 	}
   169 }
   170 
   171 /* Helper to check that a file exists */
   172 void
   173 _AssertFileExist(const char *filename)
   174 {
   175 	struct stat st;
   176 	int ret = stat(filename, &st);
   177 
   178 	SDLTest_AssertCheck(ret == 0, "Verify file '%s' exists", filename);
   179 }
   180 
   181 
   182 /* Test case functions */
   183 
   184 /**
   185  * @brief Tests sprite saving and loading
   186  */
   187 int
   188 surface_testSaveLoadBitmap(void *arg)
   189 {
   190     int ret;
   191     const char *sampleFilename = "testSaveLoadBitmap.bmp";
   192     SDL_Surface *face;
   193     SDL_Surface *rface;
   194 
   195     /* Create sample surface */
   196     face = SDLTest_ImageFace();
   197     SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
   198     if (face == NULL) return TEST_ABORTED;
   199 
   200     /* Delete test file; ignore errors */
   201     unlink(sampleFilename);
   202 
   203     /* Save a surface */
   204     ret = SDL_SaveBMP(face, sampleFilename);
   205     SDLTest_AssertPass("Call to SDL_SaveBMP()");
   206     SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SaveBMP, expected: 0, got: %i", ret); 
   207     _AssertFileExist(sampleFilename);
   208     	
   209     /* Load a surface */
   210     rface = SDL_LoadBMP(sampleFilename);
   211     SDLTest_AssertPass("Call to SDL_LoadBMP()");
   212     SDLTest_AssertCheck(rface != NULL, "Verify result from SDL_LoadBMP is not NULL");
   213     if (rface != NULL) {
   214 	    SDLTest_AssertCheck(face->w == rface->w, "Verify width of loaded surface, expected: %i, got: %i", face->w, rface->w);
   215 	    SDLTest_AssertCheck(face->h == rface->h, "Verify height of loaded surface, expected: %i, got: %i", face->h, rface->h);
   216     }
   217 
   218     /* Delete test file; ignore errors */
   219     unlink(sampleFilename);
   220 
   221     /* Clean up */
   222     if (face != NULL) {
   223 	SDL_FreeSurface(face);
   224 	face = NULL;
   225     }
   226     if (rface != NULL) {
   227 	SDL_FreeSurface(rface);
   228 	rface = NULL;
   229     }
   230     
   231     return TEST_COMPLETED;
   232 }
   233 
   234 /*!
   235  *  Tests surface conversion.
   236  */
   237 int
   238 surface_testSurfaceConversion(void *arg)
   239 {
   240 	SDL_Surface *rface = NULL, *face = NULL;
   241 	int ret = 0;
   242 
   243 	/* Create sample surface */
   244 	face = SDLTest_ImageFace();
   245 	SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
   246 	if (face == NULL) 
   247 		return TEST_ABORTED;
   248 
   249 	/* Set transparent pixel as the pixel at (0,0) */
   250 	if (face->format->palette) {
   251 	   ret = SDL_SetColorKey(face, SDL_RLEACCEL, *(Uint8 *) face->pixels);
   252 	   SDLTest_AssertPass("Call to SDL_SetColorKey()");
   253 	   SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SetColorKey, expected: 0, got: %i", ret);
   254 	}
   255 
   256 	/* Convert to 32 bit to compare. */
   257 	rface = SDL_ConvertSurface( face, testSurface->format, 0 );
   258 	SDLTest_AssertPass("Call to SDL_ConvertSurface()");
   259 	SDLTest_AssertCheck(rface != NULL, "Verify result from SDL_ConvertSurface is not NULL");
   260 
   261 	/* Compare surface. */
   262 	ret = SDLTest_CompareSurfaces( rface, face, 0 );
   263 	SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
   264 	
   265 	/* Clean up. */
   266 	if (face != NULL) {
   267 		SDL_FreeSurface( face );
   268 		face = NULL;
   269 	}
   270 	if (rface != NULL) {
   271 		SDL_FreeSurface( rface );
   272 		rface = NULL;
   273 	}	
   274 
   275 	return TEST_COMPLETED;
   276 }
   277 
   278 
   279 /**
   280  * @brief Tests sprite loading. A failure case.
   281  */
   282 int
   283 surface_testLoadFailure(void *arg)
   284 {
   285 	SDL_Surface *face = SDL_LoadBMP("nonexistant.bmp");
   286 	SDLTest_AssertCheck(face == NULL, "SDL_CreateLoadBmp");
   287 
   288 	return TEST_COMPLETED;
   289 }
   290 
   291 /**
   292  * @brief Tests some blitting routines.
   293  */
   294 int
   295 surface_testBlit(void *arg)
   296 {
   297    int ret;
   298    SDL_Surface *referenceSurface;
   299 
   300    /* Basic blitting */ 
   301    _testBlitBlendMode(-1);
   302    
   303    /* Verify result by comparing surfaces */
   304    referenceSurface = SDLTest_ImageBlit();
   305    ret = SDLTest_CompareSurfaces( testSurface, referenceSurface, 0 );
   306    SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
   307    
   308    /* Clean up. */
   309    if (referenceSurface != NULL) {   
   310    	SDL_FreeSurface( referenceSurface );
   311    }
   312 
   313    return TEST_COMPLETED;
   314 }
   315 
   316 /**
   317  * @brief Tests some blitting routines with color mod
   318  */
   319 int
   320 surface_testBlitColorMod(void *arg)
   321 {
   322    int ret;
   323    SDL_Surface *referenceSurface;
   324 
   325    /* Basic blitting with color mod */ 
   326    _testBlitBlendMode(-2);
   327    
   328    /* Verify result by comparing surfaces */
   329    referenceSurface = SDLTest_ImageBlitColor();
   330    ret = SDLTest_CompareSurfaces( testSurface, referenceSurface, 0 );
   331    SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
   332    
   333    /* Clean up. */
   334    if (referenceSurface != NULL) {   
   335    	SDL_FreeSurface( referenceSurface );
   336    }
   337 
   338    return TEST_COMPLETED;
   339 }
   340 
   341 /**
   342  * @brief Tests some blitting routines with alpha mod
   343  */
   344 int
   345 surface_testBlitAlphaMod(void *arg)
   346 {
   347    int ret;
   348    SDL_Surface *referenceSurface;
   349 
   350    /* Basic blitting with alpha mod */ 
   351    _testBlitBlendMode(-3);
   352    
   353    /* Verify result by comparing surfaces */
   354    referenceSurface = SDLTest_ImageBlitAlpha();
   355    ret = SDLTest_CompareSurfaces( testSurface, referenceSurface, 0 );
   356    SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
   357    
   358    /* Clean up. */
   359    if (referenceSurface != NULL) {   
   360    	SDL_FreeSurface( referenceSurface );
   361    }
   362 
   363    return TEST_COMPLETED;
   364 }
   365 
   366 
   367 /**
   368  * @brief Tests some more blitting routines.
   369  */
   370 int
   371 surface_testBlitBlendNone(void *arg)
   372 {
   373    int ret;
   374    SDL_Surface *referenceSurface;
   375 
   376    /* Basic blitting */ 
   377    _testBlitBlendMode(SDL_BLENDMODE_NONE);
   378    
   379    /* Verify result by comparing surfaces */
   380    referenceSurface = SDLTest_ImageBlitBlendNone();
   381    ret = SDLTest_CompareSurfaces( testSurface, referenceSurface, 0 );
   382    SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
   383    
   384    /* Clean up. */
   385    if (referenceSurface != NULL) {   
   386    	SDL_FreeSurface( referenceSurface );
   387    }
   388 
   389    return TEST_COMPLETED;
   390 }
   391 
   392 /**
   393  * @brief Tests some more blitting routines.
   394  */
   395 int
   396 surface_testBlitBlendBlend(void *arg)
   397 {
   398    int ret;
   399    SDL_Surface *referenceSurface;
   400 
   401    /* Blend blitting */ 
   402    _testBlitBlendMode(SDL_BLENDMODE_BLEND);
   403    
   404    /* Verify result by comparing surfaces */
   405    referenceSurface = SDLTest_ImageBlitBlend();
   406    ret = SDLTest_CompareSurfaces( testSurface, referenceSurface, 0 );
   407    SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
   408    
   409    /* Clean up. */
   410    if (referenceSurface != NULL) {   
   411    	SDL_FreeSurface( referenceSurface );
   412    }
   413 
   414    return TEST_COMPLETED;
   415 }
   416 
   417 /**
   418  * @brief Tests some more blitting routines.
   419  */
   420 int
   421 surface_testBlitBlendAdd(void *arg)
   422 {
   423    int ret;
   424    SDL_Surface *referenceSurface;
   425 
   426    /* Add blitting */ 
   427    _testBlitBlendMode(SDL_BLENDMODE_ADD);
   428    
   429    /* Verify result by comparing surfaces */
   430    referenceSurface = SDLTest_ImageBlitBlendAdd();
   431    ret = SDLTest_CompareSurfaces( testSurface, referenceSurface, 0 );
   432    SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
   433    
   434    /* Clean up. */
   435    if (referenceSurface != NULL) {   
   436    	SDL_FreeSurface( referenceSurface );
   437    }
   438 
   439    return TEST_COMPLETED;
   440 }
   441 
   442 /**
   443  * @brief Tests some more blitting routines.
   444  */
   445 int
   446 surface_testBlitBlendMod(void *arg)
   447 {
   448    int ret;
   449    SDL_Surface *referenceSurface;
   450 
   451    /* Mod blitting */ 
   452    _testBlitBlendMode(SDL_BLENDMODE_MOD);
   453    
   454    /* Verify result by comparing surfaces */
   455    referenceSurface = SDLTest_ImageBlitBlendMod();
   456    ret = SDLTest_CompareSurfaces( testSurface, referenceSurface, 0 );
   457    SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
   458    
   459    /* Clean up. */
   460    if (referenceSurface != NULL) {   
   461    	SDL_FreeSurface( referenceSurface );
   462    }
   463 
   464    return TEST_COMPLETED;
   465 }
   466 
   467 /**
   468  * @brief Tests some more blitting routines with loop
   469  */
   470 int
   471 surface_testBlitBlendLoop(void *arg) {
   472 
   473    int ret;
   474    SDL_Surface *referenceSurface;
   475 
   476    /* All blitting */ 
   477    _testBlitBlendMode(-4);
   478    
   479    /* Verify result by comparing surfaces */
   480    referenceSurface = SDLTest_ImageBlitBlendAll();
   481    ret = SDLTest_CompareSurfaces( testSurface, referenceSurface, 0 );
   482    SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
   483    
   484    /* Clean up. */
   485    if (referenceSurface != NULL) {   
   486    	SDL_FreeSurface( referenceSurface );
   487    }
   488 
   489    return TEST_COMPLETED;
   490 
   491 }
   492 
   493 /* ================= Test References ================== */
   494 
   495 /* Surface test cases */
   496 static const SDLTest_TestCaseReference surfaceTest1 =
   497 		{ (SDLTest_TestCaseFp)surface_testSaveLoadBitmap, "surface_testSaveLoadBitmap", "Tests sprite saving and loading.", TEST_ENABLED};
   498 
   499 static const SDLTest_TestCaseReference surfaceTest2 =
   500 		{ (SDLTest_TestCaseFp)surface_testBlit, "surface_testBlit", "Tests basic blitting.", TEST_ENABLED};
   501 
   502 static const SDLTest_TestCaseReference surfaceTest3 =
   503 		{ (SDLTest_TestCaseFp)surface_testBlitBlendNone, "surface_testBlitBlendNone", "Tests blitting routines with none blending mode.", TEST_ENABLED};
   504 
   505 static const SDLTest_TestCaseReference surfaceTest4 =
   506 		{ (SDLTest_TestCaseFp)surface_testLoadFailure, "surface_testLoadFailure", "Tests sprite loading. A failure case.", TEST_ENABLED};
   507 
   508 static const SDLTest_TestCaseReference surfaceTest5 =
   509 		{ (SDLTest_TestCaseFp)surface_testSurfaceConversion, "surface_testSurfaceConversion", "Tests surface conversion.", TEST_ENABLED};
   510 
   511 static const SDLTest_TestCaseReference surfaceTest6 =
   512 		{ (SDLTest_TestCaseFp)surface_testBlitColorMod, "surface_testBlitColorMod", "Tests some blitting routines with color mod.", TEST_ENABLED};
   513 
   514 static const SDLTest_TestCaseReference surfaceTest7 =
   515 		{ (SDLTest_TestCaseFp)surface_testBlitAlphaMod, "surface_testBlitAlphaMod", "Tests some blitting routines with alpha mod.", TEST_ENABLED};
   516 
   517 static const SDLTest_TestCaseReference surfaceTest8 =
   518 		{ (SDLTest_TestCaseFp)surface_testBlitBlendLoop, "surface_testBlitBlendLoop", "Test blittin routines with verious blending modes", TEST_ENABLED};
   519 
   520 static const SDLTest_TestCaseReference surfaceTest9 =
   521 		{ (SDLTest_TestCaseFp)surface_testBlitBlendBlend, "surface_testBlitBlendBlend", "Tests blitting routines with blend blending mode.", TEST_ENABLED};
   522 
   523 static const SDLTest_TestCaseReference surfaceTest10 =
   524 		{ (SDLTest_TestCaseFp)surface_testBlitBlendAdd, "surface_testBlitBlendAdd", "Tests blitting routines with add blending mode.", TEST_ENABLED};
   525 
   526 static const SDLTest_TestCaseReference surfaceTest11 =
   527 		{ (SDLTest_TestCaseFp)surface_testBlitBlendMod, "surface_testBlitBlendMod", "Tests blitting routines with mod blending mode.", TEST_ENABLED};
   528 
   529 /* Sequence of Surface test cases */
   530 static const SDLTest_TestCaseReference *surfaceTests[] =  {
   531 	&surfaceTest1, &surfaceTest2, &surfaceTest3, &surfaceTest4, &surfaceTest5,
   532 	&surfaceTest6, &surfaceTest7, &surfaceTest8, &surfaceTest9, &surfaceTest10, &surfaceTest11, NULL
   533 };
   534 
   535 /* Surface test suite (global) */
   536 SDLTest_TestSuiteReference surfaceTestSuite = {
   537 	"Surface",
   538 	_surfaceSetUp,
   539 	surfaceTests,
   540 	_surfaceTearDown
   541 
   542 };