Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Add surface test suite; minor improvements to render suite; refactor …
Browse files Browse the repository at this point in the history
…image saving into test lib compare function; fix for Haiku build
  • Loading branch information
ferzkopp committed Dec 27, 2012
1 parent 4064148 commit ea16a2e
Show file tree
Hide file tree
Showing 6 changed files with 636 additions and 61 deletions.
2 changes: 1 addition & 1 deletion include/SDL_test_images.h
Expand Up @@ -53,7 +53,7 @@ typedef struct SDLTest_SurfaceImage_s {
int width;
int height;
unsigned int bytes_per_pixel; /* 3:RGB, 4:RGBA */
const unsigned char pixel_data[];
unsigned char pixel_data[];
} SDLTest_SurfaceImage_t;

/* Test images */
Expand Down
22 changes: 19 additions & 3 deletions src/test/SDL_test_compare.c
Expand Up @@ -32,7 +32,11 @@
#include "SDL_test.h"


int SDLTest_CompareSurfaces( SDL_Surface *surface, SDL_Surface *referenceSurface, int allowable_error )
/* Counter for _CompareSurface calls; used for filename creation when comparisons fail */
static int _CompareSurfaceCount = 0;

/* Compare surfaces */
int SDLTest_CompareSurfaces(SDL_Surface *surface, SDL_Surface *referenceSurface, int allowable_error)
{
int ret;
int i,j;
Expand All @@ -41,18 +45,20 @@ int SDLTest_CompareSurfaces( SDL_Surface *surface, SDL_Surface *referenceSurface
int dist;
Uint8 R, G, B, A;
Uint8 Rd, Gd, Bd, Ad;
char imageFilename[128];
char referenceFilename[128];

/* Validate input surfaces */
if (surface == NULL || referenceSurface == NULL) {
return -1;
}

/* Make surface size is the same. */
/* Make sure surface size is the same. */
if ((surface->w != referenceSurface->w) || (surface->h != referenceSurface->h)) {
return -2;
}

/* Sanitize input */
/* Sanitize input value */
if (allowable_error<0) {
allowable_error = 0;
}
Expand Down Expand Up @@ -87,5 +93,15 @@ int SDLTest_CompareSurfaces( SDL_Surface *surface, SDL_Surface *referenceSurface
SDL_UnlockSurface( surface );
SDL_UnlockSurface( referenceSurface );

/* Save test image and reference for analysis on failures */
_CompareSurfaceCount++;
if (ret != 0) {
SDL_snprintf(imageFilename, 127, "CompareSurfaces%04d_TestOutput.bmp", _CompareSurfaceCount);
SDL_SaveBMP(surface, imageFilename);
SDL_snprintf(referenceFilename, 127, "CompareSurfaces%04d_Reference.bmp", _CompareSurfaceCount);
SDL_SaveBMP(referenceSurface, referenceFilename);
SDLTest_LogError("Surfaces from failed comparison saved as '%s' and '%s'", imageFilename, referenceFilename);
}

return ret;
}
3 changes: 2 additions & 1 deletion test/Makefile.in
Expand Up @@ -74,7 +74,8 @@ testautomation$(EXE): $(srcdir)/testautomation.c \
$(srcdir)/testautomation_rect.c \
$(srcdir)/testautomation_render.c \
$(srcdir)/testautomation_rwops.c \
$(srcdir)/testautomation_audio.c
$(srcdir)/testautomation_audio.c \
$(srcdir)/testautomation_surface.c
$(CC) -o $@ $^ $(CFLAGS) -lSDL2_test $(LIBS)

testmultiaudio$(EXE): $(srcdir)/testmultiaudio.c
Expand Down
147 changes: 93 additions & 54 deletions test/testautomation_render.c
Expand Up @@ -22,13 +22,14 @@
#define ALLOWABLE_ERROR_OPAQUE 0
#define ALLOWABLE_ERROR_BLENDED 64

/* Test window and renderer */
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;

/* Prototypes for helper functions */

static int _clearScreen (void);
static void _compare(const char *msg, SDL_Surface *s, int allowable_error);
static void _compare(SDL_Surface *reference, int allowable_error);
static int _hasTexAlpha(void);
static int _hasTexColor(void);
static SDL_Texture *_loadTestFace(void);
Expand All @@ -53,7 +54,7 @@ void InitCreateRenderer(void *arg)
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDLTest_AssertPass("SDL_CreateRenderer()");
SDLTest_AssertCheck(renderer != 0, "Check SDL_CreateRenderer result");
if (renderer == 0) {
if (renderer == NULL) {
SDL_DestroyWindow(window);
return;
}
Expand All @@ -66,11 +67,13 @@ void CleanupDestroyRenderer(void *arg)
{
if (renderer != NULL) {
SDL_DestroyRenderer(renderer);
renderer = NULL;
SDLTest_AssertPass("SDL_DestroyRenderer()");
}

if (window != NULL) {
SDL_DestroyWindow(window);
window = NULL;
SDLTest_AssertPass("SDL_DestroyWindow");
}
}
Expand Down Expand Up @@ -106,6 +109,7 @@ int render_testPrimitives (void *arg)
int ret;
int x, y;
SDL_Rect rect;
SDL_Surface *referenceSurface = NULL;
int checkFailCount1;
int checkFailCount2;

Expand Down Expand Up @@ -182,7 +186,14 @@ int render_testPrimitives (void *arg)
SDLTest_AssertCheck(ret == 0, "Validate result from SDL_RenderDrawLine, expected: 0, got: %i", ret);

/* See if it's the same. */
_compare( "Primitives output not the same.", SDLTest_ImagePrimitives(), ALLOWABLE_ERROR_OPAQUE );
referenceSurface = SDLTest_ImagePrimitives();
_compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE );

/* Clean up. */
if (referenceSurface != NULL) {
SDL_FreeSurface(referenceSurface);
referenceSurface = NULL;
}

return TEST_COMPLETED;
}
Expand All @@ -200,6 +211,7 @@ int render_testPrimitivesBlend (void *arg)
int ret;
int i, j;
SDL_Rect rect;
SDL_Surface *referenceSurface = NULL;
int checkFailCount1;
int checkFailCount2;
int checkFailCount3;
Expand Down Expand Up @@ -317,9 +329,16 @@ int render_testPrimitivesBlend (void *arg)
SDLTest_AssertCheck(checkFailCount3 == 0, "Validate results from calls to SDL_RenderDrawPoint, expected: 0, got: %i", checkFailCount3);

/* See if it's the same. */
_compare( "Blended primitives output not the same.", SDLTest_ImagePrimitivesBlend(), ALLOWABLE_ERROR_BLENDED );
referenceSurface = SDLTest_ImagePrimitivesBlend();
_compare(referenceSurface, ALLOWABLE_ERROR_BLENDED );

/* Clean up. */
if (referenceSurface != NULL) {
SDL_FreeSurface(referenceSurface);
referenceSurface = NULL;
}

return TEST_COMPLETED;
return TEST_COMPLETED;
}


Expand All @@ -337,6 +356,7 @@ render_testBlit(void *arg)
int ret;
SDL_Rect rect;
SDL_Texture *tface;
SDL_Surface *referenceSurface = NULL;
Uint32 tformat;
int taccess, tw, th;
int i, j, ni, nj;
Expand Down Expand Up @@ -374,11 +394,16 @@ render_testBlit(void *arg)
}
SDLTest_AssertCheck(checkFailCount1 == 0, "Validate results from calls to SDL_RenderCopy, expected: 0, got: %i", checkFailCount1);

/* See if it's the same */
referenceSurface = SDLTest_ImageBlit();
_compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE );

/* Clean up. */
SDL_DestroyTexture( tface );

/* See if it's the same */
_compare( "Blit output not the same.", SDLTest_ImageBlit(), ALLOWABLE_ERROR_OPAQUE );
if (referenceSurface != NULL) {
SDL_FreeSurface(referenceSurface);
referenceSurface = NULL;
}

return TEST_COMPLETED;
}
Expand All @@ -398,6 +423,7 @@ render_testBlitColor (void *arg)
int ret;
SDL_Rect rect;
SDL_Texture *tface;
SDL_Surface *referenceSurface = NULL;
Uint32 tformat;
int taccess, tw, th;
int i, j, ni, nj;
Expand Down Expand Up @@ -438,12 +464,16 @@ render_testBlitColor (void *arg)
SDLTest_AssertCheck(checkFailCount1 == 0, "Validate results from calls to SDL_SetTextureColorMod, expected: 0, got: %i", checkFailCount1);
SDLTest_AssertCheck(checkFailCount2 == 0, "Validate results from calls to SDL_RenderCopy, expected: 0, got: %i", checkFailCount2);

/* See if it's the same. */
referenceSurface = SDLTest_ImageBlitColor();
_compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE );

/* Clean up. */
SDL_DestroyTexture( tface );

/* See if it's the same. */
_compare( "Blit output not the same (using SDL_SetTextureColorMod).",
SDLTest_ImageBlitColor(), ALLOWABLE_ERROR_OPAQUE );
if (referenceSurface != NULL) {
SDL_FreeSurface(referenceSurface);
referenceSurface = NULL;
}

return TEST_COMPLETED;
}
Expand All @@ -463,13 +493,13 @@ render_testBlitAlpha (void *arg)
int ret;
SDL_Rect rect;
SDL_Texture *tface;
SDL_Surface *referenceSurface = NULL;
Uint32 tformat;
int taccess, tw, th;
int i, j, ni, nj;
int checkFailCount1;
int checkFailCount2;


/* Need alpha or just skip test. */
SDLTest_AssertCheck(_hasTexAlpha(), "_hasTexAlpha");

Expand Down Expand Up @@ -507,12 +537,16 @@ render_testBlitAlpha (void *arg)
SDLTest_AssertCheck(checkFailCount1 == 0, "Validate results from calls to SDL_SetTextureAlphaMod, expected: 0, got: %i", checkFailCount1);
SDLTest_AssertCheck(checkFailCount2 == 0, "Validate results from calls to SDL_RenderCopy, expected: 0, got: %i", checkFailCount2);

/* See if it's the same. */
referenceSurface = SDLTest_ImageBlitAlpha();
_compare(referenceSurface, ALLOWABLE_ERROR_BLENDED );

/* Clean up. */
SDL_DestroyTexture( tface );

/* See if it's the same. */
_compare( "Blit output not the same (using SDL_SetSurfaceAlphaMod).",
SDLTest_ImageBlitAlpha(), ALLOWABLE_ERROR_BLENDED );
if (referenceSurface != NULL) {
SDL_FreeSurface(referenceSurface);
referenceSurface = NULL;
}

return TEST_COMPLETED;
}
Expand Down Expand Up @@ -584,6 +618,7 @@ render_testBlitBlend (void *arg)
int ret;
SDL_Rect rect;
SDL_Texture *tface;
SDL_Surface *referenceSurface = NULL;
Uint32 tformat;
int taccess, tw, th;
int i, j, ni, nj;
Expand Down Expand Up @@ -618,26 +653,39 @@ render_testBlitBlend (void *arg)

/* Test None. */
_testBlitBlendMode( tface, SDL_BLENDMODE_NONE );
/* See if it's the same. */
_compare( "Blit blending output not the same (using SDL_BLENDMODE_NONE).",
SDLTest_ImageBlitBlendNone(), ALLOWABLE_ERROR_OPAQUE );


referenceSurface = SDLTest_ImageBlitBlendNone();
_compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE );
if (referenceSurface != NULL) {
SDL_FreeSurface(referenceSurface);
referenceSurface = NULL;
}

/* Test Blend. */
_testBlitBlendMode( tface, SDL_BLENDMODE_BLEND );
_compare( "Blit blending output not the same (using SDL_BLENDMODE_BLEND).",
SDLTest_ImageBlitBlend(), ALLOWABLE_ERROR_BLENDED );

referenceSurface = SDLTest_ImageBlitBlend();
_compare(referenceSurface, ALLOWABLE_ERROR_BLENDED );
if (referenceSurface != NULL) {
SDL_FreeSurface(referenceSurface);
referenceSurface = NULL;
}

/* Test Add. */
_testBlitBlendMode( tface, SDL_BLENDMODE_ADD );
_compare( "Blit blending output not the same (using SDL_BLENDMODE_ADD).",
SDLTest_ImageBlitBlendAdd(), ALLOWABLE_ERROR_BLENDED );
referenceSurface = SDLTest_ImageBlitBlendAdd();
_compare(referenceSurface, ALLOWABLE_ERROR_BLENDED );
if (referenceSurface != NULL) {
SDL_FreeSurface(referenceSurface);
referenceSurface = NULL;
}

/* Test Mod. */
_testBlitBlendMode( tface, SDL_BLENDMODE_MOD);
_compare( "Blit blending output not the same (using SDL_BLENDMODE_MOD).",
SDLTest_ImageBlitBlendMod(), ALLOWABLE_ERROR_BLENDED );
referenceSurface = SDLTest_ImageBlitBlendMod();
_compare(referenceSurface, ALLOWABLE_ERROR_BLENDED );
if (referenceSurface != NULL) {
SDL_FreeSurface(referenceSurface);
referenceSurface = NULL;
}

/* Clear surface. */
_clearScreen();
Expand Down Expand Up @@ -682,9 +730,13 @@ render_testBlitBlend (void *arg)
/* Clean up. */
SDL_DestroyTexture( tface );

/* Check to see if matches. */
_compare( "Blit blending output not the same (using SDL_BLENDMODE_*).",
SDLTest_ImageBlitBlendAll(), ALLOWABLE_ERROR_BLENDED);
/* Check to see if final image matches. */
referenceSurface = SDLTest_ImageBlitBlendAll();
_compare(referenceSurface, ALLOWABLE_ERROR_BLENDED);
if (referenceSurface != NULL) {
SDL_FreeSurface(referenceSurface);
referenceSurface = NULL;
}

return TEST_COMPLETED;
}
Expand Down Expand Up @@ -902,9 +954,6 @@ _hasTexAlpha(void)
return 1;
}

/* Counter for _compare calls use for filename creation when comparisons fail */
static int _renderCompareCount = 0;

/**
* @brief Compares screen pixels with image pixels. Helper function.
*
Expand All @@ -918,17 +967,15 @@ static int _renderCompareCount = 0;
* http://wiki.libsdl.org/moin.cgi/SDL_FreeSurface
*/
static void
_compare(const char *msg, SDL_Surface *s, int allowable_error)
_compare(SDL_Surface *referenceSurface, int allowable_error)
{
int ret;
SDL_Rect rect;
Uint8 pix[4*TESTRENDER_SCREEN_W*TESTRENDER_SCREEN_H];
SDL_Surface *testsur;
char imageFilename[128];
char referenceFilename[128];
SDL_Surface *testSurface;

/* Read pixels. */
/* Explicitly specify the rect in case the window isn't expected size... */
/* Explicitly specify the rect in case the window isn't the expected size... */
rect.x = 0;
rect.y = 0;
rect.w = TESTRENDER_SCREEN_W;
Expand All @@ -937,26 +984,18 @@ _compare(const char *msg, SDL_Surface *s, int allowable_error)
SDLTest_AssertCheck(ret == 0, "Validate result from SDL_RenderReadPixels, expected: 0, got: %i", ret);

/* Create surface. */
testsur = SDL_CreateRGBSurfaceFrom( pix, TESTRENDER_SCREEN_W, TESTRENDER_SCREEN_H, 32, TESTRENDER_SCREEN_W*4,
testSurface = SDL_CreateRGBSurfaceFrom( pix, TESTRENDER_SCREEN_W, TESTRENDER_SCREEN_H, 32, TESTRENDER_SCREEN_W*4,
RENDER_COMPARE_RMASK, RENDER_COMPARE_GMASK, RENDER_COMPARE_BMASK, RENDER_COMPARE_AMASK);
SDLTest_AssertCheck(testsur != NULL, "Verify result from SDL_CreateRGBSurfaceFrom");
SDLTest_AssertCheck(testSurface != NULL, "Verify result from SDL_CreateRGBSurfaceFrom");

/* Compare surface. */
ret = SDLTest_CompareSurfaces( testsur, s, allowable_error );
ret = SDLTest_CompareSurfaces( testSurface, referenceSurface, allowable_error );
SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);

/* Save source image and reference image for analysis */
_renderCompareCount++;
if (ret != 0) {
SDL_snprintf(imageFilename, 127, "compare%04d_SourceImage.bmp", _renderCompareCount);
SDL_SaveBMP(testsur, imageFilename);
SDL_snprintf(referenceFilename, 127, "compare%04d_ReferenceImage.bmp", _renderCompareCount);
SDL_SaveBMP(s, referenceFilename);
SDLTest_LogError("Surfaces from failed comparison saved as '%s' and '%s'", imageFilename, referenceFilename);
}

/* Clean up. */
SDL_FreeSurface(testsur);
if (testSurface != NULL) {
SDL_FreeSurface(testSurface);
}
}

/**
Expand Down

0 comments on commit ea16a2e

Please sign in to comment.