testautomation_surface.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. /**
  2. * Original code: automated SDL surface test written by Edgar Simo "bobbens"
  3. * Adapted/rewritten for test lib by Andreas Schiffler
  4. */
  5. /* Suppress 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 <SDL3/SDL.h>
  18. #include <SDL3/SDL_test.h>
  19. #include "testautomation_suites.h"
  20. #include "testautomation_images.h"
  21. #define CHECK_FUNC(FUNC, PARAMS) \
  22. { \
  23. int result = FUNC PARAMS; \
  24. if (result != 0) { \
  25. SDLTest_AssertCheck(result == 0, "Validate result from %s, expected: 0, got: %i, %s", #FUNC, result, SDL_GetError()); \
  26. } \
  27. }
  28. /* ================= Test Case Implementation ================== */
  29. /* Shared test surface */
  30. static SDL_Surface *referenceSurface = NULL;
  31. static SDL_Surface *testSurface = NULL;
  32. /* Fixture */
  33. /* Create a 32-bit writable surface for blitting tests */
  34. static void surfaceSetUp(void *arg)
  35. {
  36. int result;
  37. SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
  38. SDL_BlendMode currentBlendMode;
  39. referenceSurface = SDLTest_ImageBlit(); /* For size info */
  40. testSurface = SDL_CreateSurface(referenceSurface->w, referenceSurface->h, SDL_PIXELFORMAT_RGBA32);
  41. SDLTest_AssertCheck(testSurface != NULL, "Check that testSurface is not NULL");
  42. if (testSurface != NULL) {
  43. /* Disable blend mode for target surface */
  44. result = SDL_SetSurfaceBlendMode(testSurface, blendMode);
  45. SDLTest_AssertCheck(result == 0, "Validate result from SDL_SetSurfaceBlendMode, expected: 0, got: %i", result);
  46. result = SDL_GetSurfaceBlendMode(testSurface, &currentBlendMode);
  47. SDLTest_AssertCheck(result == 0, "Validate result from SDL_GetSurfaceBlendMode, expected: 0, got: %i", result);
  48. SDLTest_AssertCheck(currentBlendMode == blendMode, "Validate blendMode, expected: %" SDL_PRIu32 ", got: %" SDL_PRIu32, blendMode, currentBlendMode);
  49. }
  50. }
  51. static void surfaceTearDown(void *arg)
  52. {
  53. SDL_DestroySurface(referenceSurface);
  54. referenceSurface = NULL;
  55. SDL_DestroySurface(testSurface);
  56. testSurface = NULL;
  57. }
  58. /**
  59. * Helper that clears the test surface
  60. */
  61. static void clearTestSurface(void)
  62. {
  63. int ret;
  64. Uint32 color;
  65. /* Clear surface. */
  66. color = SDL_MapSurfaceRGBA(testSurface, 0, 0, 0, 0);
  67. SDLTest_AssertPass("Call to SDL_MapSurfaceRGBA()");
  68. ret = SDL_FillSurfaceRect(testSurface, NULL, color);
  69. SDLTest_AssertPass("Call to SDL_FillSurfaceRect()");
  70. SDLTest_AssertCheck(ret == 0, "Verify result from SDL_FillSurfaceRect, expected: 0, got: %i", ret);
  71. }
  72. /**
  73. * 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.
  74. */
  75. static void testBlitBlendMode(int mode)
  76. {
  77. int ret;
  78. int i, j, ni, nj;
  79. SDL_Surface *face;
  80. SDL_Rect rect;
  81. int nmode;
  82. SDL_BlendMode bmode;
  83. int checkFailCount1;
  84. int checkFailCount2;
  85. int checkFailCount3;
  86. int checkFailCount4;
  87. /* Check test surface */
  88. SDLTest_AssertCheck(testSurface != NULL, "Verify testSurface is not NULL");
  89. if (testSurface == NULL) {
  90. return;
  91. }
  92. /* Create sample surface */
  93. face = SDLTest_ImageFace();
  94. SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
  95. if (face == NULL) {
  96. return;
  97. }
  98. /* Reset alpha modulation */
  99. ret = SDL_SetSurfaceAlphaMod(face, 255);
  100. SDLTest_AssertPass("Call to SDL_SetSurfaceAlphaMod()");
  101. SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SetSurfaceAlphaMod(), expected: 0, got: %i", ret);
  102. /* Reset color modulation */
  103. ret = SDL_SetSurfaceColorMod(face, 255, 255, 255);
  104. SDLTest_AssertPass("Call to SDL_SetSurfaceColorMod()");
  105. SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SetSurfaceColorMod(), expected: 0, got: %i", ret);
  106. /* Reset color key */
  107. ret = SDL_SetSurfaceColorKey(face, SDL_FALSE, 0);
  108. SDLTest_AssertPass("Call to SDL_SetSurfaceColorKey()");
  109. SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SetSurfaceColorKey(), expected: 0, got: %i", ret);
  110. /* Clear the test surface */
  111. clearTestSurface();
  112. /* Target rect size */
  113. rect.w = face->w;
  114. rect.h = face->h;
  115. /* Steps to take */
  116. ni = testSurface->w - face->w;
  117. nj = testSurface->h - face->h;
  118. /* Optionally set blend mode. */
  119. if (mode >= 0) {
  120. ret = SDL_SetSurfaceBlendMode(face, (SDL_BlendMode)mode);
  121. SDLTest_AssertPass("Call to SDL_SetSurfaceBlendMode()");
  122. SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SetSurfaceBlendMode(..., %i), expected: 0, got: %i", mode, ret);
  123. }
  124. /* Test blend mode. */
  125. checkFailCount1 = 0;
  126. checkFailCount2 = 0;
  127. checkFailCount3 = 0;
  128. checkFailCount4 = 0;
  129. for (j = 0; j <= nj; j += 4) {
  130. for (i = 0; i <= ni; i += 4) {
  131. if (mode == -2) {
  132. /* Set color mod. */
  133. ret = SDL_SetSurfaceColorMod(face, (Uint8)((255 / nj) * j), (Uint8)((255 / ni) * i), (Uint8)((255 / nj) * j));
  134. if (ret != 0) {
  135. checkFailCount2++;
  136. }
  137. } else if (mode == -3) {
  138. /* Set alpha mod. */
  139. ret = SDL_SetSurfaceAlphaMod(face, (Uint8)((255 / ni) * i));
  140. if (ret != 0) {
  141. checkFailCount3++;
  142. }
  143. } else if (mode == -4) {
  144. /* Crazy blending mode magic. */
  145. nmode = (i / 4 * j / 4) % 4;
  146. if (nmode == 0) {
  147. bmode = SDL_BLENDMODE_NONE;
  148. } else if (nmode == 1) {
  149. bmode = SDL_BLENDMODE_BLEND;
  150. } else if (nmode == 2) {
  151. bmode = SDL_BLENDMODE_ADD;
  152. } else if (nmode == 3) {
  153. bmode = SDL_BLENDMODE_MOD;
  154. } else {
  155. /* Should be impossible, but some static checkers are too imprecise and will complain */
  156. SDLTest_LogError("Invalid: nmode=%d", nmode);
  157. return;
  158. }
  159. ret = SDL_SetSurfaceBlendMode(face, bmode);
  160. if (ret != 0) {
  161. checkFailCount4++;
  162. }
  163. }
  164. /* Blitting. */
  165. rect.x = i;
  166. rect.y = j;
  167. ret = SDL_BlitSurface(face, NULL, testSurface, &rect);
  168. if (ret != 0) {
  169. checkFailCount1++;
  170. }
  171. }
  172. }
  173. SDLTest_AssertCheck(checkFailCount1 == 0, "Validate results from calls to SDL_BlitSurface, expected: 0, got: %i", checkFailCount1);
  174. SDLTest_AssertCheck(checkFailCount2 == 0, "Validate results from calls to SDL_SetSurfaceColorMod, expected: 0, got: %i", checkFailCount2);
  175. SDLTest_AssertCheck(checkFailCount3 == 0, "Validate results from calls to SDL_SetSurfaceAlphaMod, expected: 0, got: %i", checkFailCount3);
  176. SDLTest_AssertCheck(checkFailCount4 == 0, "Validate results from calls to SDL_SetSurfaceBlendMode, expected: 0, got: %i", checkFailCount4);
  177. /* Clean up */
  178. SDL_DestroySurface(face);
  179. face = NULL;
  180. }
  181. /* Helper to check that a file exists */
  182. static void AssertFileExist(const char *filename)
  183. {
  184. struct stat st;
  185. int ret = stat(filename, &st);
  186. SDLTest_AssertCheck(ret == 0, "Verify file '%s' exists", filename);
  187. }
  188. /* Test case functions */
  189. /**
  190. * Tests sprite saving and loading
  191. */
  192. static int surface_testSaveLoadBitmap(void *arg)
  193. {
  194. int ret;
  195. const char *sampleFilename = "testSaveLoadBitmap.bmp";
  196. SDL_Surface *face;
  197. SDL_Surface *rface;
  198. /* Create sample surface */
  199. face = SDLTest_ImageFace();
  200. SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
  201. if (face == NULL) {
  202. return TEST_ABORTED;
  203. }
  204. /* Delete test file; ignore errors */
  205. unlink(sampleFilename);
  206. /* Save a surface */
  207. ret = SDL_SaveBMP(face, sampleFilename);
  208. SDLTest_AssertPass("Call to SDL_SaveBMP()");
  209. SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SaveBMP, expected: 0, got: %i", ret);
  210. AssertFileExist(sampleFilename);
  211. /* Load a surface */
  212. rface = SDL_LoadBMP(sampleFilename);
  213. SDLTest_AssertPass("Call to SDL_LoadBMP()");
  214. SDLTest_AssertCheck(rface != NULL, "Verify result from SDL_LoadBMP is not NULL");
  215. if (rface != NULL) {
  216. SDLTest_AssertCheck(face->w == rface->w, "Verify width of loaded surface, expected: %i, got: %i", face->w, rface->w);
  217. SDLTest_AssertCheck(face->h == rface->h, "Verify height of loaded surface, expected: %i, got: %i", face->h, rface->h);
  218. }
  219. /* Delete test file; ignore errors */
  220. unlink(sampleFilename);
  221. /* Clean up */
  222. SDL_DestroySurface(face);
  223. face = NULL;
  224. SDL_DestroySurface(rface);
  225. rface = NULL;
  226. return TEST_COMPLETED;
  227. }
  228. /**
  229. * Tests surface conversion.
  230. */
  231. static int surface_testSurfaceConversion(void *arg)
  232. {
  233. SDL_Surface *rface = NULL, *face = NULL;
  234. int ret = 0;
  235. /* Create sample surface */
  236. face = SDLTest_ImageFace();
  237. SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
  238. if (face == NULL) {
  239. return TEST_ABORTED;
  240. }
  241. /* Set transparent pixel as the pixel at (0,0) */
  242. if (SDL_GetSurfacePalette(face)) {
  243. ret = SDL_SetSurfaceColorKey(face, SDL_TRUE, *(Uint8 *)face->pixels);
  244. SDLTest_AssertPass("Call to SDL_SetSurfaceColorKey()");
  245. SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SetSurfaceColorKey, expected: 0, got: %i", ret);
  246. }
  247. /* Convert to 32 bit to compare. */
  248. rface = SDL_ConvertSurface(face, testSurface->format);
  249. SDLTest_AssertPass("Call to SDL_ConvertSurface()");
  250. SDLTest_AssertCheck(rface != NULL, "Verify result from SDL_ConvertSurface is not NULL");
  251. /* Compare surface. */
  252. ret = SDLTest_CompareSurfaces(rface, face, 0);
  253. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  254. /* Clean up. */
  255. SDL_DestroySurface(face);
  256. face = NULL;
  257. SDL_DestroySurface(rface);
  258. rface = NULL;
  259. return TEST_COMPLETED;
  260. }
  261. /**
  262. * Tests surface conversion across all pixel formats.
  263. */
  264. static int surface_testCompleteSurfaceConversion(void *arg)
  265. {
  266. Uint32 pixel_formats[] = {
  267. SDL_PIXELFORMAT_INDEX8,
  268. SDL_PIXELFORMAT_RGB332,
  269. SDL_PIXELFORMAT_XRGB4444,
  270. SDL_PIXELFORMAT_XBGR4444,
  271. SDL_PIXELFORMAT_XRGB1555,
  272. SDL_PIXELFORMAT_XBGR1555,
  273. SDL_PIXELFORMAT_ARGB4444,
  274. SDL_PIXELFORMAT_RGBA4444,
  275. SDL_PIXELFORMAT_ABGR4444,
  276. SDL_PIXELFORMAT_BGRA4444,
  277. SDL_PIXELFORMAT_ARGB1555,
  278. SDL_PIXELFORMAT_RGBA5551,
  279. SDL_PIXELFORMAT_ABGR1555,
  280. SDL_PIXELFORMAT_BGRA5551,
  281. SDL_PIXELFORMAT_RGB565,
  282. SDL_PIXELFORMAT_BGR565,
  283. SDL_PIXELFORMAT_RGB24,
  284. SDL_PIXELFORMAT_BGR24,
  285. SDL_PIXELFORMAT_XRGB8888,
  286. SDL_PIXELFORMAT_RGBX8888,
  287. SDL_PIXELFORMAT_XBGR8888,
  288. SDL_PIXELFORMAT_BGRX8888,
  289. SDL_PIXELFORMAT_ARGB8888,
  290. SDL_PIXELFORMAT_RGBA8888,
  291. SDL_PIXELFORMAT_ABGR8888,
  292. SDL_PIXELFORMAT_BGRA8888,
  293. #if 0 /* We aren't testing HDR10 colorspace conversion */
  294. SDL_PIXELFORMAT_XRGB2101010,
  295. SDL_PIXELFORMAT_XBGR2101010,
  296. SDL_PIXELFORMAT_ARGB2101010,
  297. SDL_PIXELFORMAT_ABGR2101010,
  298. #endif
  299. };
  300. SDL_Surface *face = NULL, *cvt1, *cvt2, *final;
  301. const SDL_PixelFormatDetails *fmt1, *fmt2;
  302. int i, j, ret = 0;
  303. /* Create sample surface */
  304. face = SDLTest_ImageFace();
  305. SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
  306. if (face == NULL) {
  307. return TEST_ABORTED;
  308. }
  309. /* Set transparent pixel as the pixel at (0,0) */
  310. if (SDL_GetSurfacePalette(face)) {
  311. ret = SDL_SetSurfaceColorKey(face, SDL_TRUE, *(Uint8 *)face->pixels);
  312. SDLTest_AssertPass("Call to SDL_SetSurfaceColorKey()");
  313. SDLTest_AssertCheck(ret == 0, "Verify result from SDL_SetSurfaceColorKey, expected: 0, got: %i", ret);
  314. }
  315. for (i = 0; i < SDL_arraysize(pixel_formats); ++i) {
  316. for (j = 0; j < SDL_arraysize(pixel_formats); ++j) {
  317. fmt1 = SDL_GetPixelFormatDetails(pixel_formats[i]);
  318. SDLTest_AssertCheck(fmt1 != NULL, "SDL_GetPixelFormatDetails(%s[0x%08" SDL_PRIx32 "]) should return a non-null pixel format",
  319. SDL_GetPixelFormatName(pixel_formats[i]), pixel_formats[i]);
  320. cvt1 = SDL_ConvertSurface(face, fmt1->format);
  321. SDLTest_AssertCheck(cvt1 != NULL, "SDL_ConvertSurface(..., %s[0x%08" SDL_PRIx32 "]) should return a non-null surface",
  322. SDL_GetPixelFormatName(pixel_formats[i]), pixel_formats[i]);
  323. fmt2 = SDL_GetPixelFormatDetails(pixel_formats[j]);
  324. SDLTest_AssertCheck(fmt2 != NULL, "SDL_GetPixelFormatDetails(%s[0x%08" SDL_PRIx32 "]) should return a non-null pixel format",
  325. SDL_GetPixelFormatName(pixel_formats[i]), pixel_formats[i]);
  326. cvt2 = SDL_ConvertSurface(cvt1, fmt2->format);
  327. SDLTest_AssertCheck(cvt2 != NULL, "SDL_ConvertSurface(..., %s[0x%08" SDL_PRIx32 "]) should return a non-null surface",
  328. SDL_GetPixelFormatName(pixel_formats[i]), pixel_formats[i]);
  329. if (fmt1 && fmt2 &&
  330. fmt1->bytes_per_pixel == SDL_BYTESPERPIXEL(face->format) &&
  331. fmt2->bytes_per_pixel == SDL_BYTESPERPIXEL(face->format) &&
  332. SDL_ISPIXELFORMAT_ALPHA(fmt1->format) == SDL_ISPIXELFORMAT_ALPHA(face->format) &&
  333. SDL_ISPIXELFORMAT_ALPHA(fmt2->format) == SDL_ISPIXELFORMAT_ALPHA(face->format)) {
  334. final = SDL_ConvertSurface(cvt2, face->format);
  335. SDL_assert(final != NULL);
  336. /* Compare surface. */
  337. ret = SDLTest_CompareSurfaces(face, final, 0);
  338. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  339. SDL_DestroySurface(final);
  340. }
  341. SDL_DestroySurface(cvt1);
  342. SDL_DestroySurface(cvt2);
  343. }
  344. }
  345. /* Clean up. */
  346. SDL_DestroySurface(face);
  347. return TEST_COMPLETED;
  348. }
  349. /**
  350. * Tests sprite loading. A failure case.
  351. */
  352. static int surface_testLoadFailure(void *arg)
  353. {
  354. SDL_Surface *face = SDL_LoadBMP("nonexistant.bmp");
  355. SDLTest_AssertCheck(face == NULL, "SDL_CreateLoadBmp");
  356. return TEST_COMPLETED;
  357. }
  358. /**
  359. * Tests some blitting routines.
  360. */
  361. static int surface_testBlit(void *arg)
  362. {
  363. int ret;
  364. SDL_Surface *compareSurface;
  365. /* Basic blitting */
  366. testBlitBlendMode(-1);
  367. /* Verify result by comparing surfaces */
  368. compareSurface = SDLTest_ImageBlit();
  369. ret = SDLTest_CompareSurfaces(testSurface, compareSurface, 0);
  370. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  371. /* Clean up. */
  372. SDL_DestroySurface(compareSurface);
  373. return TEST_COMPLETED;
  374. }
  375. /**
  376. * Tests some blitting routines with color mod
  377. */
  378. static int surface_testBlitColorMod(void *arg)
  379. {
  380. int ret;
  381. SDL_Surface *compareSurface;
  382. /* Basic blitting with color mod */
  383. testBlitBlendMode(-2);
  384. /* Verify result by comparing surfaces */
  385. compareSurface = SDLTest_ImageBlitColor();
  386. ret = SDLTest_CompareSurfaces(testSurface, compareSurface, 0);
  387. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  388. /* Clean up. */
  389. SDL_DestroySurface(compareSurface);
  390. return TEST_COMPLETED;
  391. }
  392. /**
  393. * Tests some blitting routines with alpha mod
  394. */
  395. static int surface_testBlitAlphaMod(void *arg)
  396. {
  397. int ret;
  398. SDL_Surface *compareSurface;
  399. /* Basic blitting with alpha mod */
  400. testBlitBlendMode(-3);
  401. /* Verify result by comparing surfaces */
  402. compareSurface = SDLTest_ImageBlitAlpha();
  403. ret = SDLTest_CompareSurfaces(testSurface, compareSurface, 0);
  404. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  405. /* Clean up. */
  406. SDL_DestroySurface(compareSurface);
  407. return TEST_COMPLETED;
  408. }
  409. /**
  410. * Tests some more blitting routines.
  411. */
  412. static int surface_testBlitBlendNone(void *arg)
  413. {
  414. int ret;
  415. SDL_Surface *compareSurface;
  416. /* Basic blitting */
  417. testBlitBlendMode(SDL_BLENDMODE_NONE);
  418. /* Verify result by comparing surfaces */
  419. compareSurface = SDLTest_ImageBlitBlendNone();
  420. ret = SDLTest_CompareSurfaces(testSurface, compareSurface, 0);
  421. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  422. /* Clean up. */
  423. SDL_DestroySurface(compareSurface);
  424. return TEST_COMPLETED;
  425. }
  426. /**
  427. * Tests some more blitting routines.
  428. */
  429. static int surface_testBlitBlendBlend(void *arg)
  430. {
  431. int ret;
  432. SDL_Surface *compareSurface;
  433. /* Blend blitting */
  434. testBlitBlendMode(SDL_BLENDMODE_BLEND);
  435. /* Verify result by comparing surfaces */
  436. compareSurface = SDLTest_ImageBlitBlend();
  437. ret = SDLTest_CompareSurfaces(testSurface, compareSurface, 0);
  438. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  439. /* Clean up. */
  440. SDL_DestroySurface(compareSurface);
  441. return TEST_COMPLETED;
  442. }
  443. /**
  444. * Tests some more blitting routines.
  445. */
  446. static int surface_testBlitBlendAdd(void *arg)
  447. {
  448. int ret;
  449. SDL_Surface *compareSurface;
  450. /* Add blitting */
  451. testBlitBlendMode(SDL_BLENDMODE_ADD);
  452. /* Verify result by comparing surfaces */
  453. compareSurface = SDLTest_ImageBlitBlendAdd();
  454. ret = SDLTest_CompareSurfaces(testSurface, compareSurface, 0);
  455. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  456. /* Clean up. */
  457. SDL_DestroySurface(compareSurface);
  458. return TEST_COMPLETED;
  459. }
  460. /**
  461. * Tests some more blitting routines.
  462. */
  463. static int surface_testBlitBlendMod(void *arg)
  464. {
  465. int ret;
  466. SDL_Surface *compareSurface;
  467. /* Mod blitting */
  468. testBlitBlendMode(SDL_BLENDMODE_MOD);
  469. /* Verify result by comparing surfaces */
  470. compareSurface = SDLTest_ImageBlitBlendMod();
  471. ret = SDLTest_CompareSurfaces(testSurface, compareSurface, 0);
  472. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  473. /* Clean up. */
  474. SDL_DestroySurface(compareSurface);
  475. return TEST_COMPLETED;
  476. }
  477. /**
  478. * Tests some more blitting routines with loop
  479. */
  480. static int surface_testBlitBlendLoop(void *arg)
  481. {
  482. int ret;
  483. SDL_Surface *compareSurface;
  484. /* All blitting modes */
  485. testBlitBlendMode(-4);
  486. /* Verify result by comparing surfaces */
  487. compareSurface = SDLTest_ImageBlitBlendAll();
  488. ret = SDLTest_CompareSurfaces(testSurface, compareSurface, 0);
  489. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  490. /* Clean up. */
  491. SDL_DestroySurface(compareSurface);
  492. return TEST_COMPLETED;
  493. }
  494. static int surface_testOverflow(void *arg)
  495. {
  496. char buf[1024];
  497. const char *expectedError;
  498. SDL_Surface *surface;
  499. SDL_memset(buf, '\0', sizeof(buf));
  500. expectedError = "Parameter 'width' is invalid";
  501. surface = SDL_CreateSurface(-3, 100, SDL_PIXELFORMAT_INDEX8);
  502. SDLTest_AssertCheck(surface == NULL, "Should detect negative width");
  503. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  504. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  505. surface = SDL_CreateSurfaceFrom(-1, 1, SDL_PIXELFORMAT_INDEX8, buf, 4);
  506. SDLTest_AssertCheck(surface == NULL, "Should detect negative width");
  507. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  508. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  509. surface = SDL_CreateSurfaceFrom(-1, 1, SDL_PIXELFORMAT_RGBA8888, buf, 4);
  510. SDLTest_AssertCheck(surface == NULL, "Should detect negative width");
  511. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  512. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  513. expectedError = "Parameter 'height' is invalid";
  514. surface = SDL_CreateSurface(100, -3, SDL_PIXELFORMAT_INDEX8);
  515. SDLTest_AssertCheck(surface == NULL, "Should detect negative height");
  516. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  517. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  518. surface = SDL_CreateSurfaceFrom(1, -1, SDL_PIXELFORMAT_INDEX8, buf, 4);
  519. SDLTest_AssertCheck(surface == NULL, "Should detect negative height");
  520. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  521. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  522. surface = SDL_CreateSurfaceFrom(1, -1, SDL_PIXELFORMAT_RGBA8888, buf, 4);
  523. SDLTest_AssertCheck(surface == NULL, "Should detect negative height");
  524. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  525. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  526. expectedError = "Parameter 'pitch' is invalid";
  527. surface = SDL_CreateSurfaceFrom(4, 1, SDL_PIXELFORMAT_INDEX8, buf, -1);
  528. SDLTest_AssertCheck(surface == NULL, "Should detect negative pitch");
  529. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  530. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  531. surface = SDL_CreateSurfaceFrom(1, 1, SDL_PIXELFORMAT_RGBA8888, buf, -1);
  532. SDLTest_AssertCheck(surface == NULL, "Should detect negative pitch");
  533. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  534. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  535. surface = SDL_CreateSurfaceFrom(1, 1, SDL_PIXELFORMAT_RGBA8888, buf, 0);
  536. SDLTest_AssertCheck(surface == NULL, "Should detect zero pitch");
  537. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  538. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  539. surface = SDL_CreateSurfaceFrom(1, 1, SDL_PIXELFORMAT_RGBA8888, NULL, 0);
  540. SDLTest_AssertCheck(surface != NULL, "Allow zero pitch for partially set up surfaces: %s",
  541. surface != NULL ? "(success)" : SDL_GetError());
  542. SDL_DestroySurface(surface);
  543. /* Less than 1 byte per pixel: the pitch can legitimately be less than
  544. * the width, but it must be enough to hold the appropriate number of
  545. * bits per pixel. SDL_PIXELFORMAT_INDEX4* needs 1 byte per 2 pixels. */
  546. surface = SDL_CreateSurfaceFrom(6, 1, SDL_PIXELFORMAT_INDEX4LSB, buf, 3);
  547. SDLTest_AssertCheck(surface != NULL, "6px * 4 bits per px fits in 3 bytes: %s",
  548. surface != NULL ? "(success)" : SDL_GetError());
  549. SDL_DestroySurface(surface);
  550. surface = SDL_CreateSurfaceFrom(6, 1, SDL_PIXELFORMAT_INDEX4MSB, buf, 3);
  551. SDLTest_AssertCheck(surface != NULL, "6px * 4 bits per px fits in 3 bytes: %s",
  552. surface != NULL ? "(success)" : SDL_GetError());
  553. SDL_DestroySurface(surface);
  554. surface = SDL_CreateSurfaceFrom(7, 1, SDL_PIXELFORMAT_INDEX4LSB, buf, 3);
  555. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  556. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  557. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  558. surface = SDL_CreateSurfaceFrom(7, 1, SDL_PIXELFORMAT_INDEX4MSB, buf, 3);
  559. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  560. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  561. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  562. surface = SDL_CreateSurfaceFrom(7, 1, SDL_PIXELFORMAT_INDEX4LSB, buf, 4);
  563. SDLTest_AssertCheck(surface != NULL, "7px * 4 bits per px fits in 4 bytes: %s",
  564. surface != NULL ? "(success)" : SDL_GetError());
  565. SDL_DestroySurface(surface);
  566. surface = SDL_CreateSurfaceFrom(7, 1, SDL_PIXELFORMAT_INDEX4MSB, buf, 4);
  567. SDLTest_AssertCheck(surface != NULL, "7px * 4 bits per px fits in 4 bytes: %s",
  568. surface != NULL ? "(success)" : SDL_GetError());
  569. SDL_DestroySurface(surface);
  570. /* SDL_PIXELFORMAT_INDEX2* needs 1 byte per 4 pixels. */
  571. surface = SDL_CreateSurfaceFrom(12, 1, SDL_PIXELFORMAT_INDEX2LSB, buf, 3);
  572. SDLTest_AssertCheck(surface != NULL, "12px * 2 bits per px fits in 3 bytes: %s",
  573. surface != NULL ? "(success)" : SDL_GetError());
  574. SDL_DestroySurface(surface);
  575. surface = SDL_CreateSurfaceFrom(12, 1, SDL_PIXELFORMAT_INDEX2MSB, buf, 3);
  576. SDLTest_AssertCheck(surface != NULL, "12px * 2 bits per px fits in 3 bytes: %s",
  577. surface != NULL ? "(success)" : SDL_GetError());
  578. SDL_DestroySurface(surface);
  579. surface = SDL_CreateSurfaceFrom(13, 1, SDL_PIXELFORMAT_INDEX2LSB, buf, 3);
  580. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp (%d)", surface ? surface->pitch : 0);
  581. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  582. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  583. surface = SDL_CreateSurfaceFrom(13, 1, SDL_PIXELFORMAT_INDEX2MSB, buf, 3);
  584. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  585. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  586. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  587. surface = SDL_CreateSurfaceFrom(13, 1, SDL_PIXELFORMAT_INDEX2LSB, buf, 4);
  588. SDLTest_AssertCheck(surface != NULL, "13px * 2 bits per px fits in 4 bytes: %s",
  589. surface != NULL ? "(success)" : SDL_GetError());
  590. SDL_DestroySurface(surface);
  591. surface = SDL_CreateSurfaceFrom(13, 1, SDL_PIXELFORMAT_INDEX2MSB, buf, 4);
  592. SDLTest_AssertCheck(surface != NULL, "13px * 2 bits per px fits in 4 bytes: %s",
  593. surface != NULL ? "(success)" : SDL_GetError());
  594. SDL_DestroySurface(surface);
  595. /* SDL_PIXELFORMAT_INDEX1* needs 1 byte per 8 pixels. */
  596. surface = SDL_CreateSurfaceFrom(16, 1, SDL_PIXELFORMAT_INDEX1LSB, buf, 2);
  597. SDLTest_AssertCheck(surface != NULL, "16px * 1 bit per px fits in 2 bytes: %s",
  598. surface != NULL ? "(success)" : SDL_GetError());
  599. SDL_DestroySurface(surface);
  600. surface = SDL_CreateSurfaceFrom(16, 1, SDL_PIXELFORMAT_INDEX1MSB, buf, 2);
  601. SDLTest_AssertCheck(surface != NULL, "16px * 1 bit per px fits in 2 bytes: %s",
  602. surface != NULL ? "(success)" : SDL_GetError());
  603. SDL_DestroySurface(surface);
  604. surface = SDL_CreateSurfaceFrom(17, 1, SDL_PIXELFORMAT_INDEX1LSB, buf, 2);
  605. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  606. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  607. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  608. surface = SDL_CreateSurfaceFrom(17, 1, SDL_PIXELFORMAT_INDEX1MSB, buf, 2);
  609. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  610. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  611. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  612. surface = SDL_CreateSurfaceFrom(17, 1, SDL_PIXELFORMAT_INDEX1LSB, buf, 3);
  613. SDLTest_AssertCheck(surface != NULL, "17px * 1 bit per px fits in 3 bytes: %s",
  614. surface != NULL ? "(success)" : SDL_GetError());
  615. SDL_DestroySurface(surface);
  616. surface = SDL_CreateSurfaceFrom(17, 1, SDL_PIXELFORMAT_INDEX1MSB, buf, 3);
  617. SDLTest_AssertCheck(surface != NULL, "17px * 1 bit per px fits in 3 bytes: %s",
  618. surface != NULL ? "(success)" : SDL_GetError());
  619. SDL_DestroySurface(surface);
  620. /* SDL_PIXELFORMAT_INDEX8 and SDL_PIXELFORMAT_RGB332 require 1 byte per pixel. */
  621. surface = SDL_CreateSurfaceFrom(5, 1, SDL_PIXELFORMAT_RGB332, buf, 5);
  622. SDLTest_AssertCheck(surface != NULL, "5px * 8 bits per px fits in 5 bytes: %s",
  623. surface != NULL ? "(success)" : SDL_GetError());
  624. SDL_DestroySurface(surface);
  625. surface = SDL_CreateSurfaceFrom(5, 1, SDL_PIXELFORMAT_INDEX8, buf, 5);
  626. SDLTest_AssertCheck(surface != NULL, "5px * 8 bits per px fits in 5 bytes: %s",
  627. surface != NULL ? "(success)" : SDL_GetError());
  628. SDL_DestroySurface(surface);
  629. surface = SDL_CreateSurfaceFrom(6, 1, SDL_PIXELFORMAT_RGB332, buf, 5);
  630. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  631. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  632. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  633. surface = SDL_CreateSurfaceFrom(6, 1, SDL_PIXELFORMAT_INDEX8, buf, 5);
  634. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  635. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  636. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  637. /* Everything else requires more than 1 byte per pixel, and rounds up
  638. * each pixel to an integer number of bytes (e.g. RGB555 is really
  639. * XRGB1555, with 1 bit per pixel wasted). */
  640. surface = SDL_CreateSurfaceFrom(3, 1, SDL_PIXELFORMAT_XRGB1555, buf, 6);
  641. SDLTest_AssertCheck(surface != NULL, "3px * 15 (really 16) bits per px fits in 6 bytes: %s",
  642. surface != NULL ? "(success)" : SDL_GetError());
  643. SDL_DestroySurface(surface);
  644. surface = SDL_CreateSurfaceFrom(3, 1, SDL_PIXELFORMAT_XRGB1555, buf, 6);
  645. SDLTest_AssertCheck(surface != NULL, "5px * 15 (really 16) bits per px fits in 6 bytes: %s",
  646. surface != NULL ? "(success)" : SDL_GetError());
  647. SDL_DestroySurface(surface);
  648. surface = SDL_CreateSurfaceFrom(4, 1, SDL_PIXELFORMAT_XRGB1555, buf, 6);
  649. SDLTest_AssertCheck(surface == NULL, "4px * 15 (really 16) bits per px doesn't fit in 6 bytes");
  650. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  651. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  652. surface = SDL_CreateSurfaceFrom(4, 1, SDL_PIXELFORMAT_XRGB1555, buf, 6);
  653. SDLTest_AssertCheck(surface == NULL, "4px * 15 (really 16) bits per px doesn't fit in 6 bytes");
  654. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  655. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  656. if (sizeof(size_t) == 4 && sizeof(int) >= 4) {
  657. SDL_ClearError();
  658. expectedError = "aligning pitch would overflow";
  659. /* 0x5555'5555 * 3bpp = 0xffff'ffff which fits in size_t, but adding
  660. * alignment padding makes it overflow */
  661. surface = SDL_CreateSurface(0x55555555, 1, SDL_PIXELFORMAT_RGB24);
  662. SDLTest_AssertCheck(surface == NULL, "Should detect overflow in pitch + alignment");
  663. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  664. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  665. SDL_ClearError();
  666. expectedError = "width * bpp would overflow";
  667. /* 0x4000'0000 * 4bpp = 0x1'0000'0000 which (just) overflows */
  668. surface = SDL_CreateSurface(0x40000000, 1, SDL_PIXELFORMAT_ARGB8888);
  669. SDLTest_AssertCheck(surface == NULL, "Should detect overflow in width * bytes per pixel");
  670. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  671. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  672. SDL_ClearError();
  673. expectedError = "height * pitch would overflow";
  674. surface = SDL_CreateSurface((1 << 29) - 1, (1 << 29) - 1, SDL_PIXELFORMAT_INDEX8);
  675. SDLTest_AssertCheck(surface == NULL, "Should detect overflow in width * height");
  676. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  677. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  678. SDL_ClearError();
  679. expectedError = "height * pitch would overflow";
  680. surface = SDL_CreateSurface((1 << 15) + 1, (1 << 15) + 1, SDL_PIXELFORMAT_ARGB8888);
  681. SDLTest_AssertCheck(surface == NULL, "Should detect overflow in width * height * bytes per pixel");
  682. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  683. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  684. } else {
  685. SDLTest_Log("Can't easily overflow size_t on this platform");
  686. }
  687. return TEST_COMPLETED;
  688. }
  689. static int surface_testFlip(void *arg)
  690. {
  691. SDL_Surface *surface;
  692. Uint8 *pixels;
  693. int offset;
  694. const char *expectedError;
  695. surface = SDL_CreateSurface(3, 3, SDL_PIXELFORMAT_RGB24);
  696. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  697. SDL_ClearError();
  698. expectedError = "Parameter 'surface' is invalid";
  699. SDL_FlipSurface(NULL, SDL_FLIP_HORIZONTAL);
  700. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  701. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  702. SDL_ClearError();
  703. expectedError = "Parameter 'flip' is invalid";
  704. SDL_FlipSurface(surface, SDL_FLIP_NONE);
  705. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  706. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  707. pixels = (Uint8 *)surface->pixels;
  708. *pixels = 0xFF;
  709. offset = 0;
  710. SDLTest_AssertPass("Call to SDL_FlipSurface(surface, SDL_FLIP_VERTICAL)");
  711. CHECK_FUNC(SDL_FlipSurface, (surface, SDL_FLIP_VERTICAL));
  712. SDLTest_AssertCheck(pixels[offset] == 0x00,
  713. "Expected pixels[%d] == 0x00 got 0x%.2X", offset, pixels[offset]);
  714. offset = 2 * surface->pitch;
  715. SDLTest_AssertCheck(pixels[offset] == 0xFF,
  716. "Expected pixels[%d] == 0xFF got 0x%.2X", offset, pixels[offset]);
  717. SDLTest_AssertPass("Call to SDL_FlipSurface(surface, SDL_FLIP_HORIZONTAL)");
  718. CHECK_FUNC(SDL_FlipSurface, (surface, SDL_FLIP_HORIZONTAL));
  719. SDLTest_AssertCheck(pixels[offset] == 0x00,
  720. "Expected pixels[%d] == 0x00 got 0x%.2X", offset, pixels[offset]);
  721. offset += (surface->w - 1) * SDL_BYTESPERPIXEL(surface->format);
  722. SDLTest_AssertCheck(pixels[offset] == 0xFF,
  723. "Expected pixels[%d] == 0xFF got 0x%.2X", offset, pixels[offset]);
  724. SDL_DestroySurface(surface);
  725. return TEST_COMPLETED;
  726. }
  727. static int surface_testPalette(void *arg)
  728. {
  729. SDL_Surface *source, *surface, *output;
  730. SDL_Palette *palette;
  731. Uint8 *pixels;
  732. palette = SDL_CreatePalette(2);
  733. SDLTest_AssertCheck(palette != NULL, "SDL_CreatePalette()");
  734. source = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_INDEX8);
  735. SDLTest_AssertCheck(source != NULL, "SDL_CreateSurface()");
  736. SDLTest_AssertCheck(SDL_GetSurfacePalette(source) == NULL, "SDL_GetSurfacePalette(source)");
  737. surface = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_INDEX8);
  738. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  739. SDLTest_AssertCheck(SDL_GetSurfacePalette(surface) == NULL, "SDL_GetSurfacePalette(surface)");
  740. pixels = (Uint8 *)surface->pixels;
  741. SDLTest_AssertCheck(*pixels == 0, "Expected *pixels == 0 got %u", *pixels);
  742. /* Identity copy between indexed surfaces without a palette */
  743. *(Uint8 *)source->pixels = 1;
  744. SDL_BlitSurface(source, NULL, surface, NULL);
  745. SDLTest_AssertCheck(*pixels == 1, "Expected *pixels == 1 got %u", *pixels);
  746. /* Identity copy between indexed surfaces where the destination has a palette */
  747. palette->colors[0].r = 0;
  748. palette->colors[0].g = 0;
  749. palette->colors[0].b = 0;
  750. palette->colors[1].r = 0xFF;
  751. palette->colors[1].g = 0;
  752. palette->colors[1].b = 0;
  753. SDL_SetSurfacePalette(surface, palette);
  754. *pixels = 0;
  755. SDL_BlitSurface(source, NULL, surface, NULL);
  756. SDLTest_AssertCheck(*pixels == 1, "Expected *pixels == 1 got %u", *pixels);
  757. output = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA32);
  758. SDLTest_AssertCheck(output != NULL, "SDL_CreateSurface()");
  759. pixels = (Uint8 *)output->pixels;
  760. SDL_BlitSurface(surface, NULL, output, NULL);
  761. SDLTest_AssertCheck(*pixels == 0xFF, "Expected *pixels == 0xFF got 0x%.2X", *pixels);
  762. /* Set the palette color and blit again */
  763. palette->colors[1].r = 0xAA;
  764. SDL_SetSurfacePalette(surface, palette);
  765. SDL_BlitSurface(surface, NULL, output, NULL);
  766. SDLTest_AssertCheck(*pixels == 0xAA, "Expected *pixels == 0xAA got 0x%.2X", *pixels);
  767. SDL_DestroyPalette(palette);
  768. SDL_DestroySurface(source);
  769. SDL_DestroySurface(surface);
  770. SDL_DestroySurface(output);
  771. return TEST_COMPLETED;
  772. }
  773. /* ================= Test References ================== */
  774. /* Surface test cases */
  775. static const SDLTest_TestCaseReference surfaceTest1 = {
  776. (SDLTest_TestCaseFp)surface_testSaveLoadBitmap, "surface_testSaveLoadBitmap", "Tests sprite saving and loading.", TEST_ENABLED
  777. };
  778. static const SDLTest_TestCaseReference surfaceTest2 = {
  779. (SDLTest_TestCaseFp)surface_testBlit, "surface_testBlit", "Tests basic blitting.", TEST_ENABLED
  780. };
  781. static const SDLTest_TestCaseReference surfaceTest3 = {
  782. (SDLTest_TestCaseFp)surface_testBlitBlendNone, "surface_testBlitBlendNone", "Tests blitting routines with none blending mode.", TEST_ENABLED
  783. };
  784. static const SDLTest_TestCaseReference surfaceTest4 = {
  785. (SDLTest_TestCaseFp)surface_testLoadFailure, "surface_testLoadFailure", "Tests sprite loading. A failure case.", TEST_ENABLED
  786. };
  787. static const SDLTest_TestCaseReference surfaceTest5 = {
  788. (SDLTest_TestCaseFp)surface_testSurfaceConversion, "surface_testSurfaceConversion", "Tests surface conversion.", TEST_ENABLED
  789. };
  790. static const SDLTest_TestCaseReference surfaceTest6 = {
  791. (SDLTest_TestCaseFp)surface_testCompleteSurfaceConversion, "surface_testCompleteSurfaceConversion", "Tests surface conversion across all pixel formats", TEST_ENABLED
  792. };
  793. static const SDLTest_TestCaseReference surfaceTest7 = {
  794. (SDLTest_TestCaseFp)surface_testBlitColorMod, "surface_testBlitColorMod", "Tests some blitting routines with color mod.", TEST_ENABLED
  795. };
  796. static const SDLTest_TestCaseReference surfaceTest8 = {
  797. (SDLTest_TestCaseFp)surface_testBlitAlphaMod, "surface_testBlitAlphaMod", "Tests some blitting routines with alpha mod.", TEST_ENABLED
  798. };
  799. /* TODO: rewrite test case, define new test data and re-enable; current implementation fails */
  800. static const SDLTest_TestCaseReference surfaceTest9 = {
  801. (SDLTest_TestCaseFp)surface_testBlitBlendLoop, "surface_testBlitBlendLoop", "Test blitting routines with various blending modes", TEST_DISABLED
  802. };
  803. /* TODO: rewrite test case, define new test data and re-enable; current implementation fails */
  804. static const SDLTest_TestCaseReference surfaceTest10 = {
  805. (SDLTest_TestCaseFp)surface_testBlitBlendBlend, "surface_testBlitBlendBlend", "Tests blitting routines with blend blending mode.", TEST_DISABLED
  806. };
  807. /* TODO: rewrite test case, define new test data and re-enable; current implementation fails */
  808. static const SDLTest_TestCaseReference surfaceTest11 = {
  809. (SDLTest_TestCaseFp)surface_testBlitBlendAdd, "surface_testBlitBlendAdd", "Tests blitting routines with add blending mode.", TEST_DISABLED
  810. };
  811. static const SDLTest_TestCaseReference surfaceTest12 = {
  812. (SDLTest_TestCaseFp)surface_testBlitBlendMod, "surface_testBlitBlendMod", "Tests blitting routines with mod blending mode.", TEST_ENABLED
  813. };
  814. static const SDLTest_TestCaseReference surfaceTestOverflow = {
  815. surface_testOverflow, "surface_testOverflow", "Test overflow detection.", TEST_ENABLED
  816. };
  817. static const SDLTest_TestCaseReference surfaceTestFlip = {
  818. surface_testFlip, "surface_testFlip", "Test surface flipping.", TEST_ENABLED
  819. };
  820. static const SDLTest_TestCaseReference surfaceTestPalette = {
  821. surface_testPalette, "surface_testPalette", "Test surface palette operations.", TEST_ENABLED
  822. };
  823. /* Sequence of Surface test cases */
  824. static const SDLTest_TestCaseReference *surfaceTests[] = {
  825. &surfaceTest1, &surfaceTest2, &surfaceTest3, &surfaceTest4, &surfaceTest5,
  826. &surfaceTest6, &surfaceTest7, &surfaceTest8, &surfaceTest9, &surfaceTest10,
  827. &surfaceTest11, &surfaceTest12, &surfaceTestOverflow, &surfaceTestFlip,
  828. &surfaceTestPalette, NULL
  829. };
  830. /* Surface test suite (global) */
  831. SDLTest_TestSuiteReference surfaceTestSuite = {
  832. "Surface",
  833. surfaceSetUp,
  834. surfaceTests,
  835. surfaceTearDown
  836. };