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