🛠️🐜 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.

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