🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

826 lines
31 KiB

  1. /**
  2. * Original code: automated SDL surface test written by Edgar Simo "bobbens"
  3. * Adapted/rewritten for test lib by Andreas Schiffler
  4. */
  5. /* Supress C4996 VS compiler warnings for unlink() */
  6. #define _CRT_SECURE_NO_DEPRECATE
  7. #define _CRT_NONSTDC_NO_DEPRECATE
  8. #include <stdio.h>
  9. #ifndef _MSC_VER
  10. #include <unistd.h>
  11. #endif
  12. #include <sys/stat.h>
  13. #include "SDL.h"
  14. #include "SDL_test.h"
  15. #ifdef __MACOSX__
  16. #include <unistd.h> /* For unlink() */
  17. #endif
  18. /* ================= Test Case Implementation ================== */
  19. /* Shared test surface */
  20. static SDL_Surface *referenceSurface = NULL;
  21. static SDL_Surface *testSurface = NULL;
  22. /* Helper functions for the test cases */
  23. #define TEST_SURFACE_WIDTH testSurface->w
  24. #define TEST_SURFACE_HEIGHT testSurface->h
  25. /* Fixture */
  26. /* Create a 32-bit writable surface for blitting tests */
  27. void
  28. _surfaceSetUp(void *arg)
  29. {
  30. int result;
  31. SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
  32. SDL_BlendMode currentBlendMode;
  33. Uint32 rmask, gmask, bmask, amask;
  34. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  35. rmask = 0xff000000;
  36. gmask = 0x00ff0000;
  37. bmask = 0x0000ff00;
  38. amask = 0x000000ff;
  39. #else
  40. rmask = 0x000000ff;
  41. gmask = 0x0000ff00;
  42. bmask = 0x00ff0000;
  43. amask = 0xff000000;
  44. #endif
  45. referenceSurface = SDLTest_ImageBlit(); /* For size info */
  46. testSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, referenceSurface->w, referenceSurface->h, 32, rmask, gmask, bmask, amask);
  47. SDLTest_AssertCheck(testSurface != NULL, "Check that testSurface is not NULL");
  48. if (testSurface != NULL) {
  49. /* Disable blend mode for target surface */
  50. result = SDL_SetSurfaceBlendMode(testSurface, blendMode);
  51. SDLTest_AssertCheck(result == 0, "Validate result from SDL_SetSurfaceBlendMode, expected: 0, got: %i", result);
  52. result = SDL_GetSurfaceBlendMode(testSurface, &currentBlendMode);
  53. SDLTest_AssertCheck(result == 0, "Validate result from SDL_GetSurfaceBlendMode, expected: 0, got: %i", result);
  54. SDLTest_AssertCheck(currentBlendMode == blendMode, "Validate blendMode, expected: %i, got: %i", blendMode, currentBlendMode);
  55. }
  56. }
  57. void
  58. _surfaceTearDown(void *arg)
  59. {
  60. SDL_FreeSurface(referenceSurface);
  61. referenceSurface = NULL;
  62. SDL_FreeSurface(testSurface);
  63. testSurface = NULL;
  64. }
  65. /**
  66. * Helper that clears the test surface
  67. */
  68. void _clearTestSurface()
  69. {
  70. int ret;
  71. Uint32 color;
  72. /* Clear surface. */
  73. color = SDL_MapRGBA( testSurface->format, 0, 0, 0, 0);
  74. SDLTest_AssertPass("Call to SDL_MapRGBA()");
  75. ret = SDL_FillRect( testSurface, NULL, color);
  76. SDLTest_AssertPass("Call to SDL_FillRect()");
  77. SDLTest_AssertCheck(ret == 0, "Verify result from SDL_FillRect, expected: 0, got: %i", ret);
  78. }
  79. /**
  80. * 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.
  81. */
  82. void _testBlitBlendMode(int mode)
  83. {
  84. int ret;
  85. int i, j, ni, nj;
  86. SDL_Surface *face;
  87. SDL_Rect rect;
  88. int nmode;
  89. SDL_BlendMode bmode;
  90. int checkFailCount1;
  91. int checkFailCount2;
  92. int checkFailCount3;
  93. int checkFailCount4;
  94. /* Check test surface */
  95. SDLTest_AssertCheck(testSurface != NULL, "Verify testSurface is not NULL");
  96. if (testSurface == NULL) return;
  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. /* Reset alpha modulation */
  102. ret = SDL_SetSurfaceAlphaMod(face, 255);
  103. SDLTest_AssertPass("Call to SDL_SetSurfaceAlphaMod()");
  104. SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SetSurfaceAlphaMod(), expected: 0, got: %i", ret);
  105. /* Reset color modulation */
  106. ret = SDL_SetSurfaceColorMod(face, 255, 255, 255);
  107. SDLTest_AssertPass("Call to SDL_SetSurfaceColorMod()");
  108. SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SetSurfaceColorMod(), expected: 0, got: %i", ret);
  109. /* Reset color key */
  110. ret = SDL_SetColorKey(face, SDL_FALSE, 0);
  111. SDLTest_AssertPass("Call to SDL_SetColorKey()");
  112. SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SetColorKey(), expected: 0, got: %i", ret);
  113. /* Clear the test surface */
  114. _clearTestSurface();
  115. /* Target rect size */
  116. rect.w = face->w;
  117. rect.h = face->h;
  118. /* Steps to take */
  119. ni = testSurface->w - face->w;
  120. nj = testSurface->h - face->h;
  121. /* Optionally set blend mode. */
  122. if (mode >= 0) {
  123. ret = SDL_SetSurfaceBlendMode( face, (SDL_BlendMode)mode );
  124. SDLTest_AssertPass("Call to SDL_SetSurfaceBlendMode()");
  125. SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SetSurfaceBlendMode(..., %i), expected: 0, got: %i", mode, ret);
  126. }
  127. /* Test blend mode. */
  128. checkFailCount1 = 0;
  129. checkFailCount2 = 0;
  130. checkFailCount3 = 0;
  131. checkFailCount4 = 0;
  132. for (j=0; j <= nj; j+=4) {
  133. for (i=0; i <= ni; i+=4) {
  134. if (mode == -2) {
  135. /* Set color mod. */
  136. ret = SDL_SetSurfaceColorMod( face, (255/nj)*j, (255/ni)*i, (255/nj)*j );
  137. if (ret != 0) checkFailCount2++;
  138. }
  139. else if (mode == -3) {
  140. /* Set alpha mod. */
  141. ret = SDL_SetSurfaceAlphaMod( face, (255/ni)*i );
  142. if (ret != 0) checkFailCount3++;
  143. }
  144. else if (mode == -4) {
  145. /* Crazy blending mode magic. */
  146. nmode = (i/4*j/4) % 4;
  147. if (nmode==0) {
  148. bmode = SDL_BLENDMODE_NONE;
  149. } else if (nmode==1) {
  150. bmode = SDL_BLENDMODE_BLEND;
  151. } else if (nmode==2) {
  152. bmode = SDL_BLENDMODE_ADD;
  153. } else if (nmode==3) {
  154. bmode = SDL_BLENDMODE_MOD;
  155. } else {
  156. /* Should be impossible, but some static checkers are too imprecise and will complain */
  157. SDLTest_LogError("Invalid: nmode=%d", nmode);
  158. return;
  159. }
  160. ret = SDL_SetSurfaceBlendMode( face, bmode );
  161. if (ret != 0) checkFailCount4++;
  162. }
  163. /* Blitting. */
  164. rect.x = i;
  165. rect.y = j;
  166. ret = SDL_BlitSurface( face, NULL, testSurface, &rect );
  167. if (ret != 0) checkFailCount1++;
  168. }
  169. }
  170. SDLTest_AssertCheck(checkFailCount1 == 0, "Validate results from calls to SDL_BlitSurface, expected: 0, got: %i", checkFailCount1);
  171. SDLTest_AssertCheck(checkFailCount2 == 0, "Validate results from calls to SDL_SetSurfaceColorMod, expected: 0, got: %i", checkFailCount2);
  172. SDLTest_AssertCheck(checkFailCount3 == 0, "Validate results from calls to SDL_SetSurfaceAlphaMod, expected: 0, got: %i", checkFailCount3);
  173. SDLTest_AssertCheck(checkFailCount4 == 0, "Validate results from calls to SDL_SetSurfaceBlendMode, expected: 0, got: %i", checkFailCount4);
  174. /* Clean up */
  175. SDL_FreeSurface(face);
  176. face = NULL;
  177. }
  178. /* Helper to check that a file exists */
  179. void
  180. _AssertFileExist(const char *filename)
  181. {
  182. struct stat st;
  183. int ret = stat(filename, &st);
  184. SDLTest_AssertCheck(ret == 0, "Verify file '%s' exists", filename);
  185. }
  186. /* Test case functions */
  187. /**
  188. * @brief Tests sprite saving and loading
  189. */
  190. int
  191. surface_testSaveLoadBitmap(void *arg)
  192. {
  193. int ret;
  194. const char *sampleFilename = "testSaveLoadBitmap.bmp";
  195. SDL_Surface *face;
  196. SDL_Surface *rface;
  197. /* Create sample surface */
  198. face = SDLTest_ImageFace();
  199. SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
  200. if (face == NULL) return TEST_ABORTED;
  201. /* Delete test file; ignore errors */
  202. unlink(sampleFilename);
  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. /* Load a surface */
  209. rface = SDL_LoadBMP(sampleFilename);
  210. SDLTest_AssertPass("Call to SDL_LoadBMP()");
  211. SDLTest_AssertCheck(rface != NULL, "Verify result from SDL_LoadBMP is not NULL");
  212. if (rface != NULL) {
  213. SDLTest_AssertCheck(face->w == rface->w, "Verify width of loaded surface, expected: %i, got: %i", face->w, rface->w);
  214. SDLTest_AssertCheck(face->h == rface->h, "Verify height of loaded surface, expected: %i, got: %i", face->h, rface->h);
  215. }
  216. /* Delete test file; ignore errors */
  217. unlink(sampleFilename);
  218. /* Clean up */
  219. SDL_FreeSurface(face);
  220. face = NULL;
  221. SDL_FreeSurface(rface);
  222. rface = NULL;
  223. return TEST_COMPLETED;
  224. }
  225. /* !
  226. * Tests surface conversion.
  227. */
  228. int
  229. surface_testSurfaceConversion(void *arg)
  230. {
  231. SDL_Surface *rface = NULL, *face = NULL;
  232. int ret = 0;
  233. /* Create sample surface */
  234. face = SDLTest_ImageFace();
  235. SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
  236. if (face == NULL)
  237. return TEST_ABORTED;
  238. /* Set transparent pixel as the pixel at (0,0) */
  239. if (face->format->palette) {
  240. ret = SDL_SetColorKey(face, SDL_RLEACCEL, *(Uint8 *) face->pixels);
  241. SDLTest_AssertPass("Call to SDL_SetColorKey()");
  242. SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SetColorKey, expected: 0, got: %i", ret);
  243. }
  244. /* Convert to 32 bit to compare. */
  245. rface = SDL_ConvertSurface( face, testSurface->format, 0 );
  246. SDLTest_AssertPass("Call to SDL_ConvertSurface()");
  247. SDLTest_AssertCheck(rface != NULL, "Verify result from SDL_ConvertSurface is not NULL");
  248. /* Compare surface. */
  249. ret = SDLTest_CompareSurfaces( rface, face, 0 );
  250. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  251. /* Clean up. */
  252. SDL_FreeSurface(face);
  253. face = NULL;
  254. SDL_FreeSurface(rface);
  255. rface = NULL;
  256. return TEST_COMPLETED;
  257. }
  258. /* !
  259. * Tests surface conversion across all pixel formats.
  260. */
  261. int
  262. surface_testCompleteSurfaceConversion(void *arg)
  263. {
  264. Uint32 pixel_formats[] = {
  265. SDL_PIXELFORMAT_INDEX8,
  266. SDL_PIXELFORMAT_RGB332,
  267. SDL_PIXELFORMAT_RGB444,
  268. SDL_PIXELFORMAT_BGR444,
  269. SDL_PIXELFORMAT_RGB555,
  270. SDL_PIXELFORMAT_BGR555,
  271. SDL_PIXELFORMAT_ARGB4444,
  272. SDL_PIXELFORMAT_RGBA4444,
  273. SDL_PIXELFORMAT_ABGR4444,
  274. SDL_PIXELFORMAT_BGRA4444,
  275. SDL_PIXELFORMAT_ARGB1555,
  276. SDL_PIXELFORMAT_RGBA5551,
  277. SDL_PIXELFORMAT_ABGR1555,
  278. SDL_PIXELFORMAT_BGRA5551,
  279. SDL_PIXELFORMAT_RGB565,
  280. SDL_PIXELFORMAT_BGR565,
  281. SDL_PIXELFORMAT_RGB24,
  282. SDL_PIXELFORMAT_BGR24,
  283. SDL_PIXELFORMAT_RGB888,
  284. SDL_PIXELFORMAT_RGBX8888,
  285. SDL_PIXELFORMAT_BGR888,
  286. SDL_PIXELFORMAT_BGRX8888,
  287. SDL_PIXELFORMAT_ARGB8888,
  288. SDL_PIXELFORMAT_RGBA8888,
  289. SDL_PIXELFORMAT_ABGR8888,
  290. SDL_PIXELFORMAT_BGRA8888,
  291. SDL_PIXELFORMAT_ARGB2101010,
  292. };
  293. SDL_Surface *face = NULL, *cvt1, *cvt2, *final;
  294. SDL_PixelFormat *fmt1, *fmt2;
  295. int i, j, ret = 0;
  296. /* Create sample surface */
  297. face = SDLTest_ImageFace();
  298. SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
  299. if (face == NULL)
  300. return TEST_ABORTED;
  301. /* Set transparent pixel as the pixel at (0,0) */
  302. if (face->format->palette) {
  303. ret = SDL_SetColorKey(face, SDL_RLEACCEL, *(Uint8 *) face->pixels);
  304. SDLTest_AssertPass("Call to SDL_SetColorKey()");
  305. SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SetColorKey, expected: 0, got: %i", ret);
  306. }
  307. for ( i = 0; i < SDL_arraysize(pixel_formats); ++i ) {
  308. for ( j = 0; j < SDL_arraysize(pixel_formats); ++j ) {
  309. fmt1 = SDL_AllocFormat(pixel_formats[i]);
  310. SDL_assert(fmt1 != NULL);
  311. cvt1 = SDL_ConvertSurface(face, fmt1, 0);
  312. SDL_assert(cvt1 != NULL);
  313. fmt2 = SDL_AllocFormat(pixel_formats[j]);
  314. SDL_assert(fmt1 != NULL);
  315. cvt2 = SDL_ConvertSurface(cvt1, fmt2, 0);
  316. SDL_assert(cvt2 != NULL);
  317. if ( fmt1->BytesPerPixel == face->format->BytesPerPixel &&
  318. fmt2->BytesPerPixel == face->format->BytesPerPixel &&
  319. (fmt1->Amask != 0) == (face->format->Amask != 0) &&
  320. (fmt2->Amask != 0) == (face->format->Amask != 0) ) {
  321. final = SDL_ConvertSurface( cvt2, face->format, 0 );
  322. SDL_assert(final != NULL);
  323. /* Compare surface. */
  324. ret = SDLTest_CompareSurfaces( face, final, 0 );
  325. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  326. SDL_FreeSurface(final);
  327. }
  328. SDL_FreeSurface(cvt1);
  329. SDL_FreeFormat(fmt1);
  330. SDL_FreeSurface(cvt2);
  331. SDL_FreeFormat(fmt2);
  332. }
  333. }
  334. /* Clean up. */
  335. SDL_FreeSurface( face );
  336. return TEST_COMPLETED;
  337. }
  338. /**
  339. * @brief Tests sprite loading. A failure case.
  340. */
  341. int
  342. surface_testLoadFailure(void *arg)
  343. {
  344. SDL_Surface *face = SDL_LoadBMP("nonexistant.bmp");
  345. SDLTest_AssertCheck(face == NULL, "SDL_CreateLoadBmp");
  346. return TEST_COMPLETED;
  347. }
  348. /**
  349. * @brief Tests some blitting routines.
  350. */
  351. int
  352. surface_testBlit(void *arg)
  353. {
  354. int ret;
  355. SDL_Surface *compareSurface;
  356. /* Basic blitting */
  357. _testBlitBlendMode(-1);
  358. /* Verify result by comparing surfaces */
  359. compareSurface = SDLTest_ImageBlit();
  360. ret = SDLTest_CompareSurfaces( testSurface, compareSurface, 0 );
  361. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  362. /* Clean up. */
  363. SDL_FreeSurface(compareSurface);
  364. return TEST_COMPLETED;
  365. }
  366. /**
  367. * @brief Tests some blitting routines with color mod
  368. */
  369. int
  370. surface_testBlitColorMod(void *arg)
  371. {
  372. int ret;
  373. SDL_Surface *compareSurface;
  374. /* Basic blitting with color mod */
  375. _testBlitBlendMode(-2);
  376. /* Verify result by comparing surfaces */
  377. compareSurface = SDLTest_ImageBlitColor();
  378. ret = SDLTest_CompareSurfaces( testSurface, compareSurface, 0 );
  379. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  380. /* Clean up. */
  381. SDL_FreeSurface(compareSurface);
  382. return TEST_COMPLETED;
  383. }
  384. /**
  385. * @brief Tests some blitting routines with alpha mod
  386. */
  387. int
  388. surface_testBlitAlphaMod(void *arg)
  389. {
  390. int ret;
  391. SDL_Surface *compareSurface;
  392. /* Basic blitting with alpha mod */
  393. _testBlitBlendMode(-3);
  394. /* Verify result by comparing surfaces */
  395. compareSurface = SDLTest_ImageBlitAlpha();
  396. ret = SDLTest_CompareSurfaces( testSurface, compareSurface, 0 );
  397. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  398. /* Clean up. */
  399. SDL_FreeSurface(compareSurface);
  400. return TEST_COMPLETED;
  401. }
  402. /**
  403. * @brief Tests some more blitting routines.
  404. */
  405. int
  406. surface_testBlitBlendNone(void *arg)
  407. {
  408. int ret;
  409. SDL_Surface *compareSurface;
  410. /* Basic blitting */
  411. _testBlitBlendMode(SDL_BLENDMODE_NONE);
  412. /* Verify result by comparing surfaces */
  413. compareSurface = SDLTest_ImageBlitBlendNone();
  414. ret = SDLTest_CompareSurfaces( testSurface, compareSurface, 0 );
  415. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  416. /* Clean up. */
  417. SDL_FreeSurface(compareSurface);
  418. return TEST_COMPLETED;
  419. }
  420. /**
  421. * @brief Tests some more blitting routines.
  422. */
  423. int
  424. surface_testBlitBlendBlend(void *arg)
  425. {
  426. int ret;
  427. SDL_Surface *compareSurface;
  428. /* Blend blitting */
  429. _testBlitBlendMode(SDL_BLENDMODE_BLEND);
  430. /* Verify result by comparing surfaces */
  431. compareSurface = SDLTest_ImageBlitBlend();
  432. ret = SDLTest_CompareSurfaces( testSurface, compareSurface, 0 );
  433. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  434. /* Clean up. */
  435. SDL_FreeSurface(compareSurface);
  436. return TEST_COMPLETED;
  437. }
  438. /**
  439. * @brief Tests some more blitting routines.
  440. */
  441. int
  442. surface_testBlitBlendAdd(void *arg)
  443. {
  444. int ret;
  445. SDL_Surface *compareSurface;
  446. /* Add blitting */
  447. _testBlitBlendMode(SDL_BLENDMODE_ADD);
  448. /* Verify result by comparing surfaces */
  449. compareSurface = SDLTest_ImageBlitBlendAdd();
  450. ret = SDLTest_CompareSurfaces( testSurface, compareSurface, 0 );
  451. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  452. /* Clean up. */
  453. SDL_FreeSurface(compareSurface);
  454. return TEST_COMPLETED;
  455. }
  456. /**
  457. * @brief Tests some more blitting routines.
  458. */
  459. int
  460. surface_testBlitBlendMod(void *arg)
  461. {
  462. int ret;
  463. SDL_Surface *compareSurface;
  464. /* Mod blitting */
  465. _testBlitBlendMode(SDL_BLENDMODE_MOD);
  466. /* Verify result by comparing surfaces */
  467. compareSurface = SDLTest_ImageBlitBlendMod();
  468. ret = SDLTest_CompareSurfaces( testSurface, compareSurface, 0 );
  469. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  470. /* Clean up. */
  471. SDL_FreeSurface(compareSurface);
  472. return TEST_COMPLETED;
  473. }
  474. /**
  475. * @brief Tests some more blitting routines with loop
  476. */
  477. int
  478. surface_testBlitBlendLoop(void *arg) {
  479. int ret;
  480. SDL_Surface *compareSurface;
  481. /* All blitting modes */
  482. _testBlitBlendMode(-4);
  483. /* Verify result by comparing surfaces */
  484. compareSurface = SDLTest_ImageBlitBlendAll();
  485. ret = SDLTest_CompareSurfaces( testSurface, compareSurface, 0 );
  486. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  487. /* Clean up. */
  488. SDL_FreeSurface(compareSurface);
  489. return TEST_COMPLETED;
  490. }
  491. int
  492. surface_testOverflow(void *arg)
  493. {
  494. char buf[1024];
  495. const char *expectedError;
  496. SDL_Surface *surface;
  497. SDL_memset(buf, '\0', sizeof(buf));
  498. expectedError = "Parameter 'width' is invalid";
  499. surface = SDL_CreateRGBSurfaceWithFormat(0, -3, 100, 8, SDL_PIXELFORMAT_INDEX8);
  500. SDLTest_AssertCheck(surface == NULL, "Should detect negative width");
  501. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  502. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  503. surface = SDL_CreateRGBSurfaceWithFormatFrom(buf, -1, 1, 8, 4, SDL_PIXELFORMAT_INDEX8);
  504. SDLTest_AssertCheck(surface == NULL, "Should detect negative width");
  505. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  506. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  507. surface = SDL_CreateRGBSurfaceFrom(buf, -1, 1, 32, 4, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
  508. SDLTest_AssertCheck(surface == NULL, "Should detect negative width");
  509. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  510. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  511. expectedError = "Parameter 'height' is invalid";
  512. surface = SDL_CreateRGBSurfaceWithFormat(0, 100, -3, 8, SDL_PIXELFORMAT_INDEX8);
  513. SDLTest_AssertCheck(surface == NULL, "Should detect negative height");
  514. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  515. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  516. surface = SDL_CreateRGBSurfaceWithFormatFrom(buf, 1, -1, 8, 4, SDL_PIXELFORMAT_INDEX8);
  517. SDLTest_AssertCheck(surface == NULL, "Should detect negative height");
  518. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  519. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  520. surface = SDL_CreateRGBSurfaceFrom(buf, 1, -1, 32, 4, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
  521. SDLTest_AssertCheck(surface == NULL, "Should detect negative height");
  522. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  523. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  524. expectedError = "Parameter 'pitch' is invalid";
  525. surface = SDL_CreateRGBSurfaceWithFormatFrom(buf, 4, 1, 8, -1, SDL_PIXELFORMAT_INDEX8);
  526. SDLTest_AssertCheck(surface == NULL, "Should detect negative pitch");
  527. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  528. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  529. surface = SDL_CreateRGBSurfaceFrom(buf, 1, 1, 32, -1, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
  530. SDLTest_AssertCheck(surface == NULL, "Should detect negative pitch");
  531. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  532. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  533. /* Less than 1 byte per pixel: the pitch can legitimately be less than
  534. * the width, but it must be enough to hold the appropriate number of
  535. * bits per pixel. SDL_PIXELFORMAT_INDEX4* needs 1 byte per 2 pixels. */
  536. surface = SDL_CreateRGBSurfaceWithFormatFrom(buf, 6, 1, 4, 3, SDL_PIXELFORMAT_INDEX4LSB);
  537. SDLTest_AssertCheck(surface != NULL, "6px * 4 bits per px fits in 3 bytes: %s",
  538. surface != NULL ? "(success)" : SDL_GetError());
  539. SDL_FreeSurface(surface);
  540. surface = SDL_CreateRGBSurfaceFrom(buf, 6, 1, 4, 3, 0, 0, 0, 0);
  541. SDLTest_AssertCheck(surface != NULL, "6px * 4 bits per px fits in 3 bytes: %s",
  542. surface != NULL ? "(success)" : SDL_GetError());
  543. SDL_FreeSurface(surface);
  544. surface = SDL_CreateRGBSurfaceWithFormatFrom(buf, 7, 1, 4, 3, SDL_PIXELFORMAT_INDEX4LSB);
  545. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  546. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  547. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  548. surface = SDL_CreateRGBSurfaceFrom(buf, 7, 1, 4, 3, 0, 0, 0, 0);
  549. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  550. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  551. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  552. surface = SDL_CreateRGBSurfaceWithFormatFrom(buf, 7, 1, 4, 4, SDL_PIXELFORMAT_INDEX4LSB);
  553. SDLTest_AssertCheck(surface != NULL, "7px * 4 bits per px fits in 4 bytes: %s",
  554. surface != NULL ? "(success)" : SDL_GetError());
  555. SDL_FreeSurface(surface);
  556. surface = SDL_CreateRGBSurfaceFrom(buf, 7, 1, 4, 4, 0, 0, 0, 0);
  557. SDLTest_AssertCheck(surface != NULL, "7px * 4 bits per px fits in 4 bytes: %s",
  558. surface != NULL ? "(success)" : SDL_GetError());
  559. SDL_FreeSurface(surface);
  560. /* SDL_PIXELFORMAT_INDEX1* needs 1 byte per 8 pixels. */
  561. surface = SDL_CreateRGBSurfaceWithFormatFrom(buf, 16, 1, 1, 2, SDL_PIXELFORMAT_INDEX1LSB);
  562. SDLTest_AssertCheck(surface != NULL, "16px * 1 bit per px fits in 2 bytes: %s",
  563. surface != NULL ? "(success)" : SDL_GetError());
  564. SDL_FreeSurface(surface);
  565. surface = SDL_CreateRGBSurfaceFrom(buf, 16, 1, 1, 2, 0, 0, 0, 0);
  566. SDLTest_AssertCheck(surface != NULL, "16px * 1 bit per px fits in 2 bytes: %s",
  567. surface != NULL ? "(success)" : SDL_GetError());
  568. SDL_FreeSurface(surface);
  569. surface = SDL_CreateRGBSurfaceWithFormatFrom(buf, 17, 1, 1, 2, SDL_PIXELFORMAT_INDEX1LSB);
  570. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  571. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  572. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  573. surface = SDL_CreateRGBSurfaceFrom(buf, 17, 1, 1, 2, 0, 0, 0, 0);
  574. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  575. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  576. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  577. surface = SDL_CreateRGBSurfaceWithFormatFrom(buf, 17, 1, 1, 3, SDL_PIXELFORMAT_INDEX1LSB);
  578. SDLTest_AssertCheck(surface != NULL, "17px * 1 bit per px fits in 3 bytes: %s",
  579. surface != NULL ? "(success)" : SDL_GetError());
  580. SDL_FreeSurface(surface);
  581. surface = SDL_CreateRGBSurfaceFrom(buf, 17, 1, 1, 3, 0, 0, 0, 0);
  582. SDLTest_AssertCheck(surface != NULL, "17px * 1 bit per px fits in 3 bytes: %s",
  583. surface != NULL ? "(success)" : SDL_GetError());
  584. SDL_FreeSurface(surface);
  585. /* SDL_PIXELFORMAT_INDEX8 and SDL_PIXELFORMAT_RGB332 require 1 byte per pixel. */
  586. surface = SDL_CreateRGBSurfaceWithFormatFrom(buf, 5, 1, 8, 5, SDL_PIXELFORMAT_RGB332);
  587. SDLTest_AssertCheck(surface != NULL, "5px * 8 bits per px fits in 5 bytes: %s",
  588. surface != NULL ? "(success)" : SDL_GetError());
  589. SDL_FreeSurface(surface);
  590. surface = SDL_CreateRGBSurfaceFrom(buf, 5, 1, 8, 5, 0, 0, 0, 0);
  591. SDLTest_AssertCheck(surface != NULL, "5px * 8 bits per px fits in 5 bytes: %s",
  592. surface != NULL ? "(success)" : SDL_GetError());
  593. SDL_FreeSurface(surface);
  594. surface = SDL_CreateRGBSurfaceWithFormatFrom(buf, 6, 1, 8, 5, SDL_PIXELFORMAT_RGB332);
  595. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  596. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  597. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  598. surface = SDL_CreateRGBSurfaceFrom(buf, 6, 1, 8, 5, 0, 0, 0, 0);
  599. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  600. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  601. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  602. /* Everything else requires more than 1 byte per pixel, and rounds up
  603. * each pixel to an integer number of bytes (e.g. RGB555 is really
  604. * XRGB1555, with 1 bit per pixel wasted). */
  605. surface = SDL_CreateRGBSurfaceWithFormatFrom(buf, 3, 1, 15, 6, SDL_PIXELFORMAT_RGB555);
  606. SDLTest_AssertCheck(surface != NULL, "3px * 15 (really 16) bits per px fits in 6 bytes: %s",
  607. surface != NULL ? "(success)" : SDL_GetError());
  608. SDL_FreeSurface(surface);
  609. surface = SDL_CreateRGBSurfaceFrom(buf, 3, 1, 15, 6, 0, 0, 0, 0);
  610. SDLTest_AssertCheck(surface != NULL, "5px * 15 (really 16) bits per px fits in 6 bytes: %s",
  611. surface != NULL ? "(success)" : SDL_GetError());
  612. SDL_FreeSurface(surface);
  613. surface = SDL_CreateRGBSurfaceWithFormatFrom(buf, 4, 1, 15, 6, SDL_PIXELFORMAT_RGB555);
  614. SDLTest_AssertCheck(surface == NULL, "4px * 15 (really 16) bits per px doesn't fit in 6 bytes");
  615. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  616. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  617. surface = SDL_CreateRGBSurfaceFrom(buf, 4, 1, 15, 6, 0, 0, 0, 0);
  618. SDLTest_AssertCheck(surface == NULL, "4px * 15 (really 16) bits per px doesn't fit in 6 bytes");
  619. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  620. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  621. if (sizeof (size_t) == 4 && sizeof (int) >= 4) {
  622. expectedError = "Out of memory";
  623. surface = SDL_CreateRGBSurfaceWithFormat(0, SDL_MAX_SINT32, 1, 8, SDL_PIXELFORMAT_INDEX8);
  624. SDLTest_AssertCheck(surface == NULL, "Should detect overflow in width + alignment");
  625. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  626. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  627. surface = SDL_CreateRGBSurfaceWithFormat(0, SDL_MAX_SINT32 / 2, 1, 32, SDL_PIXELFORMAT_ARGB8888);
  628. SDLTest_AssertCheck(surface == NULL, "Should detect overflow in width * bytes per pixel");
  629. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  630. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  631. surface = SDL_CreateRGBSurfaceWithFormat(0, (1 << 29) - 1, (1 << 29) - 1, 8, SDL_PIXELFORMAT_INDEX8);
  632. SDLTest_AssertCheck(surface == NULL, "Should detect overflow in width * height");
  633. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  634. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  635. surface = SDL_CreateRGBSurfaceWithFormat(0, (1 << 15) + 1, (1 << 15) + 1, 32, SDL_PIXELFORMAT_ARGB8888);
  636. SDLTest_AssertCheck(surface == NULL, "Should detect overflow in width * height * bytes per pixel");
  637. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  638. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  639. }
  640. else {
  641. SDLTest_Log("Can't easily overflow size_t on this platform");
  642. }
  643. return TEST_COMPLETED;
  644. }
  645. /* ================= Test References ================== */
  646. /* Surface test cases */
  647. static const SDLTest_TestCaseReference surfaceTest1 =
  648. { (SDLTest_TestCaseFp)surface_testSaveLoadBitmap, "surface_testSaveLoadBitmap", "Tests sprite saving and loading.", TEST_ENABLED};
  649. static const SDLTest_TestCaseReference surfaceTest2 =
  650. { (SDLTest_TestCaseFp)surface_testBlit, "surface_testBlit", "Tests basic blitting.", TEST_ENABLED};
  651. static const SDLTest_TestCaseReference surfaceTest3 =
  652. { (SDLTest_TestCaseFp)surface_testBlitBlendNone, "surface_testBlitBlendNone", "Tests blitting routines with none blending mode.", TEST_ENABLED};
  653. static const SDLTest_TestCaseReference surfaceTest4 =
  654. { (SDLTest_TestCaseFp)surface_testLoadFailure, "surface_testLoadFailure", "Tests sprite loading. A failure case.", TEST_ENABLED};
  655. static const SDLTest_TestCaseReference surfaceTest5 =
  656. { (SDLTest_TestCaseFp)surface_testSurfaceConversion, "surface_testSurfaceConversion", "Tests surface conversion.", TEST_ENABLED};
  657. static const SDLTest_TestCaseReference surfaceTest6 =
  658. { (SDLTest_TestCaseFp)surface_testCompleteSurfaceConversion, "surface_testCompleteSurfaceConversion", "Tests surface conversion across all pixel formats", TEST_ENABLED};
  659. static const SDLTest_TestCaseReference surfaceTest7 =
  660. { (SDLTest_TestCaseFp)surface_testBlitColorMod, "surface_testBlitColorMod", "Tests some blitting routines with color mod.", TEST_ENABLED};
  661. static const SDLTest_TestCaseReference surfaceTest8 =
  662. { (SDLTest_TestCaseFp)surface_testBlitAlphaMod, "surface_testBlitAlphaMod", "Tests some blitting routines with alpha mod.", TEST_ENABLED};
  663. /* TODO: rewrite test case, define new test data and re-enable; current implementation fails */
  664. static const SDLTest_TestCaseReference surfaceTest9 =
  665. { (SDLTest_TestCaseFp)surface_testBlitBlendLoop, "surface_testBlitBlendLoop", "Test blitting routines with various blending modes", TEST_DISABLED};
  666. /* TODO: rewrite test case, define new test data and re-enable; current implementation fails */
  667. static const SDLTest_TestCaseReference surfaceTest10 =
  668. { (SDLTest_TestCaseFp)surface_testBlitBlendBlend, "surface_testBlitBlendBlend", "Tests blitting routines with blend blending mode.", TEST_DISABLED};
  669. /* TODO: rewrite test case, define new test data and re-enable; current implementation fails */
  670. static const SDLTest_TestCaseReference surfaceTest11 =
  671. { (SDLTest_TestCaseFp)surface_testBlitBlendAdd, "surface_testBlitBlendAdd", "Tests blitting routines with add blending mode.", TEST_DISABLED};
  672. static const SDLTest_TestCaseReference surfaceTest12 =
  673. { (SDLTest_TestCaseFp)surface_testBlitBlendMod, "surface_testBlitBlendMod", "Tests blitting routines with mod blending mode.", TEST_ENABLED};
  674. static const SDLTest_TestCaseReference surfaceTestOverflow =
  675. { surface_testOverflow, "surface_testOverflow", "Test overflow detection.", TEST_ENABLED};
  676. /* Sequence of Surface test cases */
  677. static const SDLTest_TestCaseReference *surfaceTests[] = {
  678. &surfaceTest1, &surfaceTest2, &surfaceTest3, &surfaceTest4, &surfaceTest5,
  679. &surfaceTest6, &surfaceTest7, &surfaceTest8, &surfaceTest9, &surfaceTest10,
  680. &surfaceTest11, &surfaceTest12, &surfaceTestOverflow, NULL
  681. };
  682. /* Surface test suite (global) */
  683. SDLTest_TestSuiteReference surfaceTestSuite = {
  684. "Surface",
  685. _surfaceSetUp,
  686. surfaceTests,
  687. _surfaceTearDown
  688. };