2 * Original code: automated SDL surface test written by Edgar Simo "bobbens"
3 * Adapted/rewritten for test lib by Andreas Schiffler
6 /* Supress C4996 VS compiler warnings for unlink() */
7 #define _CRT_SECURE_NO_DEPRECATE
8 #define _CRT_NONSTDC_NO_DEPRECATE
20 #include <unistd.h> /* For unlink() */
23 /* ================= Test Case Implementation ================== */
25 /* Shared test surface */
27 static SDL_Surface *referenceSurface = NULL;
28 static SDL_Surface *testSurface = NULL;
30 /* Helper functions for the test cases */
32 #define TEST_SURFACE_WIDTH testSurface->w
33 #define TEST_SURFACE_HEIGHT testSurface->h
37 /* Create a 32-bit writable surface for blitting tests */
39 _surfaceSetUp(void *arg)
42 SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
43 SDL_BlendMode currentBlendMode;
44 Uint32 rmask, gmask, bmask, amask;
45 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
57 referenceSurface = SDLTest_ImageBlit(); /* For size info */
58 testSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, referenceSurface->w, referenceSurface->h, 32, rmask, gmask, bmask, amask);
59 SDLTest_AssertCheck(testSurface != NULL, "Check that testSurface is not NULL");
60 if (testSurface != NULL) {
61 /* Disable blend mode for target surface */
62 result = SDL_SetSurfaceBlendMode(testSurface, blendMode);
63 SDLTest_AssertCheck(result == 0, "Validate result from SDL_SetSurfaceBlendMode, expected: 0, got: %i", result);
64 result = SDL_GetSurfaceBlendMode(testSurface, ¤tBlendMode);
65 SDLTest_AssertCheck(result == 0, "Validate result from SDL_GetSurfaceBlendMode, expected: 0, got: %i", result);
66 SDLTest_AssertCheck(currentBlendMode == blendMode, "Validate blendMode, expected: %i, got: %i", blendMode, currentBlendMode);
71 _surfaceTearDown(void *arg)
73 SDL_FreeSurface(referenceSurface);
74 referenceSurface = NULL;
75 SDL_FreeSurface(testSurface);
80 * Helper that clears the test surface
82 void _clearTestSurface()
88 color = SDL_MapRGBA( testSurface->format, 0, 0, 0, 0);
89 SDLTest_AssertPass("Call to SDL_MapRGBA()");
90 ret = SDL_FillRect( testSurface, NULL, color);
91 SDLTest_AssertPass("Call to SDL_FillRect()");
92 SDLTest_AssertCheck(ret == 0, "Verify result from SDL_FillRect, expected: 0, got: %i", ret);
96 * 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.
98 void _testBlitBlendMode(int mode)
111 /* Check test surface */
112 SDLTest_AssertCheck(testSurface != NULL, "Verify testSurface is not NULL");
113 if (testSurface == NULL) return;
115 /* Create sample surface */
116 face = SDLTest_ImageFace();
117 SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
118 if (face == NULL) return;
120 /* Reset alpha modulation */
121 ret = SDL_SetSurfaceAlphaMod(face, 255);
122 SDLTest_AssertPass("Call to SDL_SetSurfaceAlphaMod()");
123 SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SetSurfaceAlphaMod(), expected: 0, got: %i", ret);
125 /* Reset color modulation */
126 ret = SDL_SetSurfaceColorMod(face, 255, 255, 255);
127 SDLTest_AssertPass("Call to SDL_SetSurfaceColorMod()");
128 SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SetSurfaceColorMod(), expected: 0, got: %i", ret);
130 /* Reset color key */
131 ret = SDL_SetColorKey(face, SDL_FALSE, 0);
132 SDLTest_AssertPass("Call to SDL_SetColorKey()");
133 SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SetColorKey(), expected: 0, got: %i", ret);
135 /* Clear the test surface */
138 /* Target rect size */
143 ni = testSurface->w - face->w;
144 nj = testSurface->h - face->h;
146 /* Optionally set blend mode. */
148 ret = SDL_SetSurfaceBlendMode( face, (SDL_BlendMode)mode );
149 SDLTest_AssertPass("Call to SDL_SetSurfaceBlendMode()");
150 SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SetSurfaceBlendMode(..., %i), expected: 0, got: %i", mode, ret);
153 /* Test blend mode. */
158 for (j=0; j <= nj; j+=4) {
159 for (i=0; i <= ni; i+=4) {
162 ret = SDL_SetSurfaceColorMod( face, (255/nj)*j, (255/ni)*i, (255/nj)*j );
163 if (ret != 0) checkFailCount2++;
165 else if (mode == -3) {
167 ret = SDL_SetSurfaceAlphaMod( face, (255/ni)*i );
168 if (ret != 0) checkFailCount3++;
170 else if (mode == -4) {
171 /* Crazy blending mode magic. */
172 nmode = (i/4*j/4) % 4;
174 bmode = SDL_BLENDMODE_NONE;
175 } else if (nmode==1) {
176 bmode = SDL_BLENDMODE_BLEND;
177 } else if (nmode==2) {
178 bmode = SDL_BLENDMODE_ADD;
179 } else if (nmode==3) {
180 bmode = SDL_BLENDMODE_MOD;
182 ret = SDL_SetSurfaceBlendMode( face, bmode );
183 if (ret != 0) checkFailCount4++;
189 ret = SDL_BlitSurface( face, NULL, testSurface, &rect );
190 if (ret != 0) checkFailCount1++;
193 SDLTest_AssertCheck(checkFailCount1 == 0, "Validate results from calls to SDL_BlitSurface, expected: 0, got: %i", checkFailCount1);
194 SDLTest_AssertCheck(checkFailCount2 == 0, "Validate results from calls to SDL_SetSurfaceColorMod, expected: 0, got: %i", checkFailCount2);
195 SDLTest_AssertCheck(checkFailCount3 == 0, "Validate results from calls to SDL_SetSurfaceAlphaMod, expected: 0, got: %i", checkFailCount3);
196 SDLTest_AssertCheck(checkFailCount4 == 0, "Validate results from calls to SDL_SetSurfaceBlendMode, expected: 0, got: %i", checkFailCount4);
199 SDL_FreeSurface(face);
203 /* Helper to check that a file exists */
205 _AssertFileExist(const char *filename)
208 int ret = stat(filename, &st);
210 SDLTest_AssertCheck(ret == 0, "Verify file '%s' exists", filename);
214 /* Test case functions */
217 * @brief Tests sprite saving and loading
220 surface_testSaveLoadBitmap(void *arg)
223 const char *sampleFilename = "testSaveLoadBitmap.bmp";
227 /* Create sample surface */
228 face = SDLTest_ImageFace();
229 SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
230 if (face == NULL) return TEST_ABORTED;
232 /* Delete test file; ignore errors */
233 unlink(sampleFilename);
236 ret = SDL_SaveBMP(face, sampleFilename);
237 SDLTest_AssertPass("Call to SDL_SaveBMP()");
238 SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SaveBMP, expected: 0, got: %i", ret);
239 _AssertFileExist(sampleFilename);
242 rface = SDL_LoadBMP(sampleFilename);
243 SDLTest_AssertPass("Call to SDL_LoadBMP()");
244 SDLTest_AssertCheck(rface != NULL, "Verify result from SDL_LoadBMP is not NULL");
246 SDLTest_AssertCheck(face->w == rface->w, "Verify width of loaded surface, expected: %i, got: %i", face->w, rface->w);
247 SDLTest_AssertCheck(face->h == rface->h, "Verify height of loaded surface, expected: %i, got: %i", face->h, rface->h);
250 /* Delete test file; ignore errors */
251 unlink(sampleFilename);
254 SDL_FreeSurface(face);
256 SDL_FreeSurface(rface);
259 return TEST_COMPLETED;
263 * Tests surface conversion.
266 surface_testSurfaceConversion(void *arg)
268 SDL_Surface *rface = NULL, *face = NULL;
271 /* Create sample surface */
272 face = SDLTest_ImageFace();
273 SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
277 /* Set transparent pixel as the pixel at (0,0) */
278 if (face->format->palette) {
279 ret = SDL_SetColorKey(face, SDL_RLEACCEL, *(Uint8 *) face->pixels);
280 SDLTest_AssertPass("Call to SDL_SetColorKey()");
281 SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SetColorKey, expected: 0, got: %i", ret);
284 /* Convert to 32 bit to compare. */
285 rface = SDL_ConvertSurface( face, testSurface->format, 0 );
286 SDLTest_AssertPass("Call to SDL_ConvertSurface()");
287 SDLTest_AssertCheck(rface != NULL, "Verify result from SDL_ConvertSurface is not NULL");
289 /* Compare surface. */
290 ret = SDLTest_CompareSurfaces( rface, face, 0 );
291 SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
294 SDL_FreeSurface(face);
296 SDL_FreeSurface(rface);
299 return TEST_COMPLETED;
304 * Tests surface conversion across all pixel formats.
307 surface_testCompleteSurfaceConversion(void *arg)
309 Uint32 pixel_formats[] = {
310 SDL_PIXELFORMAT_INDEX8,
311 SDL_PIXELFORMAT_RGB332,
312 SDL_PIXELFORMAT_RGB444,
313 SDL_PIXELFORMAT_RGB555,
314 SDL_PIXELFORMAT_BGR555,
315 SDL_PIXELFORMAT_ARGB4444,
316 SDL_PIXELFORMAT_RGBA4444,
317 SDL_PIXELFORMAT_ABGR4444,
318 SDL_PIXELFORMAT_BGRA4444,
319 SDL_PIXELFORMAT_ARGB1555,
320 SDL_PIXELFORMAT_RGBA5551,
321 SDL_PIXELFORMAT_ABGR1555,
322 SDL_PIXELFORMAT_BGRA5551,
323 SDL_PIXELFORMAT_RGB565,
324 SDL_PIXELFORMAT_BGR565,
325 SDL_PIXELFORMAT_RGB24,
326 SDL_PIXELFORMAT_BGR24,
327 SDL_PIXELFORMAT_RGB888,
328 SDL_PIXELFORMAT_RGBX8888,
329 SDL_PIXELFORMAT_BGR888,
330 SDL_PIXELFORMAT_BGRX8888,
331 SDL_PIXELFORMAT_ARGB8888,
332 SDL_PIXELFORMAT_RGBA8888,
333 SDL_PIXELFORMAT_ABGR8888,
334 SDL_PIXELFORMAT_BGRA8888,
335 SDL_PIXELFORMAT_ARGB2101010,
337 SDL_Surface *face = NULL, *cvt1, *cvt2, *final;
338 SDL_PixelFormat *fmt1, *fmt2;
341 /* Create sample surface */
342 face = SDLTest_ImageFace();
343 SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
347 /* Set transparent pixel as the pixel at (0,0) */
348 if (face->format->palette) {
349 ret = SDL_SetColorKey(face, SDL_RLEACCEL, *(Uint8 *) face->pixels);
350 SDLTest_AssertPass("Call to SDL_SetColorKey()");
351 SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SetColorKey, expected: 0, got: %i", ret);
354 for ( i = 0; i < SDL_arraysize(pixel_formats); ++i ) {
355 for ( j = 0; j < SDL_arraysize(pixel_formats); ++j ) {
356 fmt1 = SDL_AllocFormat(pixel_formats[i]);
357 SDL_assert(fmt1 != NULL);
358 cvt1 = SDL_ConvertSurface(face, fmt1, 0);
359 SDL_assert(cvt1 != NULL);
361 fmt2 = SDL_AllocFormat(pixel_formats[j]);
362 SDL_assert(fmt1 != NULL);
363 cvt2 = SDL_ConvertSurface(cvt1, fmt2, 0);
364 SDL_assert(cvt2 != NULL);
366 if ( fmt1->BytesPerPixel == face->format->BytesPerPixel &&
367 fmt2->BytesPerPixel == face->format->BytesPerPixel &&
368 (fmt1->Amask != 0) == (face->format->Amask != 0) &&
369 (fmt2->Amask != 0) == (face->format->Amask != 0) ) {
370 final = SDL_ConvertSurface( cvt2, face->format, 0 );
371 SDL_assert(final != NULL);
373 /* Compare surface. */
374 ret = SDLTest_CompareSurfaces( face, final, 0 );
375 SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
376 SDL_FreeSurface(final);
379 SDL_FreeSurface(cvt1);
380 SDL_FreeFormat(fmt1);
381 SDL_FreeSurface(cvt2);
382 SDL_FreeFormat(fmt2);
387 SDL_FreeSurface( face );
389 return TEST_COMPLETED;
394 * @brief Tests sprite loading. A failure case.
397 surface_testLoadFailure(void *arg)
399 SDL_Surface *face = SDL_LoadBMP("nonexistant.bmp");
400 SDLTest_AssertCheck(face == NULL, "SDL_CreateLoadBmp");
402 return TEST_COMPLETED;
406 * @brief Tests some blitting routines.
409 surface_testBlit(void *arg)
412 SDL_Surface *compareSurface;
415 _testBlitBlendMode(-1);
417 /* Verify result by comparing surfaces */
418 compareSurface = SDLTest_ImageBlit();
419 ret = SDLTest_CompareSurfaces( testSurface, compareSurface, 0 );
420 SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
423 SDL_FreeSurface(compareSurface);
425 return TEST_COMPLETED;
429 * @brief Tests some blitting routines with color mod
432 surface_testBlitColorMod(void *arg)
435 SDL_Surface *compareSurface;
437 /* Basic blitting with color mod */
438 _testBlitBlendMode(-2);
440 /* Verify result by comparing surfaces */
441 compareSurface = SDLTest_ImageBlitColor();
442 ret = SDLTest_CompareSurfaces( testSurface, compareSurface, 0 );
443 SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
446 SDL_FreeSurface(compareSurface);
448 return TEST_COMPLETED;
452 * @brief Tests some blitting routines with alpha mod
455 surface_testBlitAlphaMod(void *arg)
458 SDL_Surface *compareSurface;
460 /* Basic blitting with alpha mod */
461 _testBlitBlendMode(-3);
463 /* Verify result by comparing surfaces */
464 compareSurface = SDLTest_ImageBlitAlpha();
465 ret = SDLTest_CompareSurfaces( testSurface, compareSurface, 0 );
466 SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
469 SDL_FreeSurface(compareSurface);
471 return TEST_COMPLETED;
476 * @brief Tests some more blitting routines.
479 surface_testBlitBlendNone(void *arg)
482 SDL_Surface *compareSurface;
485 _testBlitBlendMode(SDL_BLENDMODE_NONE);
487 /* Verify result by comparing surfaces */
488 compareSurface = SDLTest_ImageBlitBlendNone();
489 ret = SDLTest_CompareSurfaces( testSurface, compareSurface, 0 );
490 SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
493 SDL_FreeSurface(compareSurface);
495 return TEST_COMPLETED;
499 * @brief Tests some more blitting routines.
502 surface_testBlitBlendBlend(void *arg)
505 SDL_Surface *compareSurface;
508 _testBlitBlendMode(SDL_BLENDMODE_BLEND);
510 /* Verify result by comparing surfaces */
511 compareSurface = SDLTest_ImageBlitBlend();
512 ret = SDLTest_CompareSurfaces( testSurface, compareSurface, 0 );
513 SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
516 SDL_FreeSurface(compareSurface);
518 return TEST_COMPLETED;
522 * @brief Tests some more blitting routines.
525 surface_testBlitBlendAdd(void *arg)
528 SDL_Surface *compareSurface;
531 _testBlitBlendMode(SDL_BLENDMODE_ADD);
533 /* Verify result by comparing surfaces */
534 compareSurface = SDLTest_ImageBlitBlendAdd();
535 ret = SDLTest_CompareSurfaces( testSurface, compareSurface, 0 );
536 SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
539 SDL_FreeSurface(compareSurface);
541 return TEST_COMPLETED;
545 * @brief Tests some more blitting routines.
548 surface_testBlitBlendMod(void *arg)
551 SDL_Surface *compareSurface;
554 _testBlitBlendMode(SDL_BLENDMODE_MOD);
556 /* Verify result by comparing surfaces */
557 compareSurface = SDLTest_ImageBlitBlendMod();
558 ret = SDLTest_CompareSurfaces( testSurface, compareSurface, 0 );
559 SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
562 SDL_FreeSurface(compareSurface);
564 return TEST_COMPLETED;
568 * @brief Tests some more blitting routines with loop
571 surface_testBlitBlendLoop(void *arg) {
574 SDL_Surface *compareSurface;
576 /* All blitting modes */
577 _testBlitBlendMode(-4);
579 /* Verify result by comparing surfaces */
580 compareSurface = SDLTest_ImageBlitBlendAll();
581 ret = SDLTest_CompareSurfaces( testSurface, compareSurface, 0 );
582 SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
585 SDL_FreeSurface(compareSurface);
587 return TEST_COMPLETED;
591 /* ================= Test References ================== */
593 /* Surface test cases */
594 static const SDLTest_TestCaseReference surfaceTest1 =
595 { (SDLTest_TestCaseFp)surface_testSaveLoadBitmap, "surface_testSaveLoadBitmap", "Tests sprite saving and loading.", TEST_ENABLED};
597 static const SDLTest_TestCaseReference surfaceTest2 =
598 { (SDLTest_TestCaseFp)surface_testBlit, "surface_testBlit", "Tests basic blitting.", TEST_ENABLED};
600 static const SDLTest_TestCaseReference surfaceTest3 =
601 { (SDLTest_TestCaseFp)surface_testBlitBlendNone, "surface_testBlitBlendNone", "Tests blitting routines with none blending mode.", TEST_ENABLED};
603 static const SDLTest_TestCaseReference surfaceTest4 =
604 { (SDLTest_TestCaseFp)surface_testLoadFailure, "surface_testLoadFailure", "Tests sprite loading. A failure case.", TEST_ENABLED};
606 static const SDLTest_TestCaseReference surfaceTest5 =
607 { (SDLTest_TestCaseFp)surface_testSurfaceConversion, "surface_testSurfaceConversion", "Tests surface conversion.", TEST_ENABLED};
609 static const SDLTest_TestCaseReference surfaceTest6 =
610 { (SDLTest_TestCaseFp)surface_testCompleteSurfaceConversion, "surface_testCompleteSurfaceConversion", "Tests surface conversion across all pixel formats", TEST_ENABLED};
612 static const SDLTest_TestCaseReference surfaceTest7 =
613 { (SDLTest_TestCaseFp)surface_testBlitColorMod, "surface_testBlitColorMod", "Tests some blitting routines with color mod.", TEST_ENABLED};
615 static const SDLTest_TestCaseReference surfaceTest8 =
616 { (SDLTest_TestCaseFp)surface_testBlitAlphaMod, "surface_testBlitAlphaMod", "Tests some blitting routines with alpha mod.", TEST_ENABLED};
618 /* TODO: rewrite test case, define new test data and re-enable; current implementation fails */
619 static const SDLTest_TestCaseReference surfaceTest9 =
620 { (SDLTest_TestCaseFp)surface_testBlitBlendLoop, "surface_testBlitBlendLoop", "Test blitting routines with various blending modes", TEST_DISABLED};
622 /* TODO: rewrite test case, define new test data and re-enable; current implementation fails */
623 static const SDLTest_TestCaseReference surfaceTest10 =
624 { (SDLTest_TestCaseFp)surface_testBlitBlendBlend, "surface_testBlitBlendBlend", "Tests blitting routines with blend blending mode.", TEST_DISABLED};
626 /* TODO: rewrite test case, define new test data and re-enable; current implementation fails */
627 static const SDLTest_TestCaseReference surfaceTest11 =
628 { (SDLTest_TestCaseFp)surface_testBlitBlendAdd, "surface_testBlitBlendAdd", "Tests blitting routines with add blending mode.", TEST_DISABLED};
630 static const SDLTest_TestCaseReference surfaceTest12 =
631 { (SDLTest_TestCaseFp)surface_testBlitBlendMod, "surface_testBlitBlendMod", "Tests blitting routines with mod blending mode.", TEST_ENABLED};
634 /* Sequence of Surface test cases */
635 static const SDLTest_TestCaseReference *surfaceTests[] = {
636 &surfaceTest1, &surfaceTest2, &surfaceTest3, &surfaceTest4, &surfaceTest5,
637 &surfaceTest6, &surfaceTest7, &surfaceTest8, &surfaceTest9, &surfaceTest10,
638 &surfaceTest11, &surfaceTest12, NULL
641 /* Surface test suite (global) */
642 SDLTest_TestSuiteReference surfaceTestSuite = {