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