testautomation_pixels.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /**
  2. * Pixels test suite
  3. */
  4. #include <SDL3/SDL.h>
  5. #include <SDL3/SDL_test.h>
  6. #include "testautomation_suites.h"
  7. /* Test case functions */
  8. /* Definition of all RGB formats used to test pixel conversions */
  9. static const Uint32 g_AllFormats[] = {
  10. SDL_PIXELFORMAT_INDEX1LSB,
  11. SDL_PIXELFORMAT_INDEX1MSB,
  12. SDL_PIXELFORMAT_INDEX2LSB,
  13. SDL_PIXELFORMAT_INDEX2MSB,
  14. SDL_PIXELFORMAT_INDEX4LSB,
  15. SDL_PIXELFORMAT_INDEX4MSB,
  16. SDL_PIXELFORMAT_INDEX8,
  17. SDL_PIXELFORMAT_RGB332,
  18. SDL_PIXELFORMAT_RGB444,
  19. SDL_PIXELFORMAT_BGR444,
  20. SDL_PIXELFORMAT_RGB555,
  21. SDL_PIXELFORMAT_BGR555,
  22. SDL_PIXELFORMAT_ARGB4444,
  23. SDL_PIXELFORMAT_RGBA4444,
  24. SDL_PIXELFORMAT_ABGR4444,
  25. SDL_PIXELFORMAT_BGRA4444,
  26. SDL_PIXELFORMAT_ARGB1555,
  27. SDL_PIXELFORMAT_RGBA5551,
  28. SDL_PIXELFORMAT_ABGR1555,
  29. SDL_PIXELFORMAT_BGRA5551,
  30. SDL_PIXELFORMAT_RGB565,
  31. SDL_PIXELFORMAT_BGR565,
  32. SDL_PIXELFORMAT_RGB24,
  33. SDL_PIXELFORMAT_BGR24,
  34. SDL_PIXELFORMAT_XRGB8888,
  35. SDL_PIXELFORMAT_RGBX8888,
  36. SDL_PIXELFORMAT_XBGR8888,
  37. SDL_PIXELFORMAT_BGRX8888,
  38. SDL_PIXELFORMAT_ARGB8888,
  39. SDL_PIXELFORMAT_RGBA8888,
  40. SDL_PIXELFORMAT_ABGR8888,
  41. SDL_PIXELFORMAT_BGRA8888,
  42. SDL_PIXELFORMAT_XRGB2101010,
  43. SDL_PIXELFORMAT_XBGR2101010,
  44. SDL_PIXELFORMAT_ARGB2101010,
  45. SDL_PIXELFORMAT_ABGR2101010,
  46. SDL_PIXELFORMAT_YV12,
  47. SDL_PIXELFORMAT_IYUV,
  48. SDL_PIXELFORMAT_YUY2,
  49. SDL_PIXELFORMAT_UYVY,
  50. SDL_PIXELFORMAT_YVYU,
  51. SDL_PIXELFORMAT_NV12,
  52. SDL_PIXELFORMAT_NV21
  53. };
  54. static const int g_numAllFormats = SDL_arraysize(g_AllFormats);
  55. static const char *g_AllFormatsVerbose[] = {
  56. "SDL_PIXELFORMAT_INDEX1LSB",
  57. "SDL_PIXELFORMAT_INDEX1MSB",
  58. "SDL_PIXELFORMAT_INDEX2LSB",
  59. "SDL_PIXELFORMAT_INDEX2MSB",
  60. "SDL_PIXELFORMAT_INDEX4LSB",
  61. "SDL_PIXELFORMAT_INDEX4MSB",
  62. "SDL_PIXELFORMAT_INDEX8",
  63. "SDL_PIXELFORMAT_RGB332",
  64. "SDL_PIXELFORMAT_RGB444",
  65. "SDL_PIXELFORMAT_BGR444",
  66. "SDL_PIXELFORMAT_RGB555",
  67. "SDL_PIXELFORMAT_BGR555",
  68. "SDL_PIXELFORMAT_ARGB4444",
  69. "SDL_PIXELFORMAT_RGBA4444",
  70. "SDL_PIXELFORMAT_ABGR4444",
  71. "SDL_PIXELFORMAT_BGRA4444",
  72. "SDL_PIXELFORMAT_ARGB1555",
  73. "SDL_PIXELFORMAT_RGBA5551",
  74. "SDL_PIXELFORMAT_ABGR1555",
  75. "SDL_PIXELFORMAT_BGRA5551",
  76. "SDL_PIXELFORMAT_RGB565",
  77. "SDL_PIXELFORMAT_BGR565",
  78. "SDL_PIXELFORMAT_RGB24",
  79. "SDL_PIXELFORMAT_BGR24",
  80. "SDL_PIXELFORMAT_XRGB8888",
  81. "SDL_PIXELFORMAT_RGBX8888",
  82. "SDL_PIXELFORMAT_XBGR8888",
  83. "SDL_PIXELFORMAT_BGRX8888",
  84. "SDL_PIXELFORMAT_ARGB8888",
  85. "SDL_PIXELFORMAT_RGBA8888",
  86. "SDL_PIXELFORMAT_ABGR8888",
  87. "SDL_PIXELFORMAT_BGRA8888",
  88. "SDL_PIXELFORMAT_XRGB2101010",
  89. "SDL_PIXELFORMAT_XBGR2101010",
  90. "SDL_PIXELFORMAT_ARGB2101010",
  91. "SDL_PIXELFORMAT_ABGR2101010",
  92. "SDL_PIXELFORMAT_YV12",
  93. "SDL_PIXELFORMAT_IYUV",
  94. "SDL_PIXELFORMAT_YUY2",
  95. "SDL_PIXELFORMAT_UYVY",
  96. "SDL_PIXELFORMAT_YVYU",
  97. "SDL_PIXELFORMAT_NV12",
  98. "SDL_PIXELFORMAT_NV21"
  99. };
  100. /* Definition of some invalid formats for negative tests */
  101. static Uint32 g_invalidPixelFormats[] = {
  102. SDL_DEFINE_PIXELFORMAT(SDL_PIXELTYPE_PACKED32, SDL_PACKEDORDER_ABGR, SDL_PACKEDLAYOUT_1010102 + 1, 32, 4),
  103. SDL_DEFINE_PIXELFORMAT(SDL_PIXELTYPE_PACKED32, SDL_PACKEDORDER_ABGR, SDL_PACKEDLAYOUT_1010102 + 2, 32, 4)
  104. };
  105. static const int g_numInvalidPixelFormats = SDL_arraysize(g_invalidPixelFormats);
  106. static const char *g_invalidPixelFormatsVerbose[] = {
  107. "SDL_PIXELFORMAT_UNKNOWN",
  108. "SDL_PIXELFORMAT_UNKNOWN"
  109. };
  110. /* Test case functions */
  111. /**
  112. * Call to SDL_CreatePixelFormat and SDL_DestroyPixelFormat
  113. *
  114. * \sa SDL_CreatePixelFormat
  115. * \sa SDL_DestroyPixelFormat
  116. */
  117. static int pixels_allocFreeFormat(void *arg)
  118. {
  119. const char *unknownFormat = "SDL_PIXELFORMAT_UNKNOWN";
  120. const char *expectedError = "Unknown pixel format";
  121. const char *error;
  122. int i;
  123. Uint32 format;
  124. Uint32 masks;
  125. SDL_PixelFormat *result;
  126. /* Blank/unknown format */
  127. format = 0;
  128. SDLTest_Log("Pixel Format: %s (%" SDL_PRIu32 ")", unknownFormat, format);
  129. /* Allocate format */
  130. result = SDL_CreatePixelFormat(format);
  131. SDLTest_AssertPass("Call to SDL_CreatePixelFormat()");
  132. SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");
  133. if (result != NULL) {
  134. SDLTest_AssertCheck(result->format == format, "Verify value of result.format; expected: %" SDL_PRIu32 ", got %" SDL_PRIu32, format, result->format);
  135. SDLTest_AssertCheck(result->BitsPerPixel == 0, "Verify value of result.BitsPerPixel; expected: 0, got %u", result->BitsPerPixel);
  136. SDLTest_AssertCheck(result->BytesPerPixel == 0, "Verify value of result.BytesPerPixel; expected: 0, got %u", result->BytesPerPixel);
  137. masks = result->Rmask | result->Gmask | result->Bmask | result->Amask;
  138. SDLTest_AssertCheck(masks == 0, "Verify value of result.[RGBA]mask combined; expected: 0, got %" SDL_PRIu32, masks);
  139. /* Deallocate again */
  140. SDL_DestroyPixelFormat(result);
  141. SDLTest_AssertPass("Call to SDL_DestroyPixelFormat()");
  142. }
  143. /* RGB formats */
  144. for (i = 0; i < g_numAllFormats; i++) {
  145. format = g_AllFormats[i];
  146. SDLTest_Log("Pixel Format: %s (%" SDL_PRIu32 ")", g_AllFormatsVerbose[i], format);
  147. /* Allocate format */
  148. result = SDL_CreatePixelFormat(format);
  149. SDLTest_AssertPass("Call to SDL_CreatePixelFormat()");
  150. SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");
  151. if (result != NULL) {
  152. SDLTest_AssertCheck(result->format == format, "Verify value of result.format; expected: %" SDL_PRIu32 ", got %" SDL_PRIu32, format, result->format);
  153. if (!SDL_ISPIXELFORMAT_FOURCC(format)) {
  154. SDLTest_AssertCheck(result->BitsPerPixel > 0, "Verify value of result.BitsPerPixel; expected: >0, got %u", result->BitsPerPixel);
  155. SDLTest_AssertCheck(result->BytesPerPixel > 0, "Verify value of result.BytesPerPixel; expected: >0, got %u", result->BytesPerPixel);
  156. if (!SDL_ISPIXELFORMAT_INDEXED(format)) {
  157. masks = result->Rmask | result->Gmask | result->Bmask | result->Amask;
  158. SDLTest_AssertCheck(masks > 0, "Verify value of result.[RGBA]mask combined; expected: >0, got %" SDL_PRIu32, masks);
  159. }
  160. }
  161. /* Deallocate again */
  162. SDL_DestroyPixelFormat(result);
  163. SDLTest_AssertPass("Call to SDL_DestroyPixelFormat()");
  164. }
  165. }
  166. /* Negative cases */
  167. /* Invalid Formats */
  168. for (i = 0; i < g_numInvalidPixelFormats; i++) {
  169. SDL_ClearError();
  170. SDLTest_AssertPass("Call to SDL_ClearError()");
  171. format = g_invalidPixelFormats[i];
  172. result = SDL_CreatePixelFormat(format);
  173. SDLTest_AssertPass("Call to SDL_CreatePixelFormat(%" SDL_PRIu32 ")", format);
  174. SDLTest_AssertCheck(result == NULL, "Verify result is NULL");
  175. error = SDL_GetError();
  176. SDLTest_AssertPass("Call to SDL_GetError()");
  177. SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
  178. if (error != NULL) {
  179. SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0,
  180. "Validate error message, expected: '%s', got: '%s'", expectedError, error);
  181. }
  182. }
  183. /* Invalid free pointer */
  184. SDL_ClearError();
  185. SDLTest_AssertPass("Call to SDL_ClearError()");
  186. SDL_DestroyPixelFormat(NULL);
  187. SDLTest_AssertPass("Call to SDL_DestroyPixelFormat(NULL)");
  188. error = SDL_GetError();
  189. SDLTest_AssertPass("Call to SDL_GetError()");
  190. SDLTest_AssertCheck(error == NULL || error[0] == '\0', "Validate that error message is empty");
  191. return TEST_COMPLETED;
  192. }
  193. /**
  194. * Call to SDL_GetPixelFormatName
  195. *
  196. * \sa SDL_GetPixelFormatName
  197. */
  198. static int pixels_getPixelFormatName(void *arg)
  199. {
  200. const char *unknownFormat = "SDL_PIXELFORMAT_UNKNOWN";
  201. const char *error;
  202. int i;
  203. Uint32 format;
  204. const char *result;
  205. /* Blank/undefined format */
  206. format = 0;
  207. SDLTest_Log("RGB Format: %s (%" SDL_PRIu32 ")", unknownFormat, format);
  208. /* Get name of format */
  209. result = SDL_GetPixelFormatName(format);
  210. SDLTest_AssertPass("Call to SDL_GetPixelFormatName()");
  211. SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");
  212. if (result != NULL) {
  213. SDLTest_AssertCheck(result[0] != '\0', "Verify result is non-empty");
  214. SDLTest_AssertCheck(SDL_strcmp(result, unknownFormat) == 0,
  215. "Verify result text; expected: %s, got %s", unknownFormat, result);
  216. }
  217. /* RGB formats */
  218. for (i = 0; i < g_numAllFormats; i++) {
  219. format = g_AllFormats[i];
  220. SDLTest_Log("RGB Format: %s (%" SDL_PRIu32 ")", g_AllFormatsVerbose[i], format);
  221. /* Get name of format */
  222. result = SDL_GetPixelFormatName(format);
  223. SDLTest_AssertPass("Call to SDL_GetPixelFormatName()");
  224. SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");
  225. if (result != NULL) {
  226. SDLTest_AssertCheck(result[0] != '\0', "Verify result is non-empty");
  227. SDLTest_AssertCheck(SDL_strcmp(result, g_AllFormatsVerbose[i]) == 0,
  228. "Verify result text; expected: %s, got %s", g_AllFormatsVerbose[i], result);
  229. }
  230. }
  231. /* Negative cases */
  232. /* Invalid Formats */
  233. SDL_ClearError();
  234. SDLTest_AssertPass("Call to SDL_ClearError()");
  235. for (i = 0; i < g_numInvalidPixelFormats; i++) {
  236. format = g_invalidPixelFormats[i];
  237. result = SDL_GetPixelFormatName(format);
  238. SDLTest_AssertPass("Call to SDL_GetPixelFormatName(%" SDL_PRIu32 ")", format);
  239. SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");
  240. if (result != NULL) {
  241. SDLTest_AssertCheck(result[0] != '\0',
  242. "Verify result is non-empty; got: %s", result);
  243. SDLTest_AssertCheck(SDL_strcmp(result, g_invalidPixelFormatsVerbose[i]) == 0,
  244. "Validate name is UNKNOWN, expected: '%s', got: '%s'", g_invalidPixelFormatsVerbose[i], result);
  245. }
  246. error = SDL_GetError();
  247. SDLTest_AssertPass("Call to SDL_GetError()");
  248. SDLTest_AssertCheck(error == NULL || error[0] == '\0', "Validate that error message is empty");
  249. }
  250. return TEST_COMPLETED;
  251. }
  252. /**
  253. * Call to SDL_CreatePalette and SDL_DestroyPalette
  254. *
  255. * \sa SDL_CreatePalette
  256. * \sa SDL_DestroyPalette
  257. */
  258. static int pixels_allocFreePalette(void *arg)
  259. {
  260. const char *expectedError = "Parameter 'ncolors' is invalid";
  261. const char *error;
  262. int variation;
  263. int i;
  264. int ncolors;
  265. SDL_Palette *result;
  266. /* Allocate palette */
  267. for (variation = 1; variation <= 3; variation++) {
  268. switch (variation) {
  269. /* Just one color */
  270. default:
  271. case 1:
  272. ncolors = 1;
  273. break;
  274. /* Two colors */
  275. case 2:
  276. ncolors = 2;
  277. break;
  278. /* More than two colors */
  279. case 3:
  280. ncolors = SDLTest_RandomIntegerInRange(8, 16);
  281. break;
  282. }
  283. result = SDL_CreatePalette(ncolors);
  284. SDLTest_AssertPass("Call to SDL_CreatePalette(%d)", ncolors);
  285. SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");
  286. if (result != NULL) {
  287. SDLTest_AssertCheck(result->ncolors == ncolors, "Verify value of result.ncolors; expected: %u, got %u", ncolors, result->ncolors);
  288. if (result->ncolors > 0) {
  289. SDLTest_AssertCheck(result->colors != NULL, "Verify value of result.colors is not NULL");
  290. if (result->colors != NULL) {
  291. for (i = 0; i < result->ncolors; i++) {
  292. SDLTest_AssertCheck(result->colors[i].r == 255, "Verify value of result.colors[%d].r; expected: 255, got %u", i, result->colors[i].r);
  293. SDLTest_AssertCheck(result->colors[i].g == 255, "Verify value of result.colors[%d].g; expected: 255, got %u", i, result->colors[i].g);
  294. SDLTest_AssertCheck(result->colors[i].b == 255, "Verify value of result.colors[%d].b; expected: 255, got %u", i, result->colors[i].b);
  295. }
  296. }
  297. }
  298. /* Deallocate again */
  299. SDL_DestroyPalette(result);
  300. SDLTest_AssertPass("Call to SDL_DestroyPalette()");
  301. }
  302. }
  303. /* Negative cases */
  304. /* Invalid number of colors */
  305. for (ncolors = 0; ncolors > -3; ncolors--) {
  306. SDL_ClearError();
  307. SDLTest_AssertPass("Call to SDL_ClearError()");
  308. result = SDL_CreatePalette(ncolors);
  309. SDLTest_AssertPass("Call to SDL_CreatePalette(%d)", ncolors);
  310. SDLTest_AssertCheck(result == NULL, "Verify result is NULL");
  311. error = SDL_GetError();
  312. SDLTest_AssertPass("Call to SDL_GetError()");
  313. SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
  314. if (error != NULL) {
  315. SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0,
  316. "Validate error message, expected: '%s', got: '%s'", expectedError, error);
  317. }
  318. }
  319. /* Invalid free pointer */
  320. SDL_ClearError();
  321. SDLTest_AssertPass("Call to SDL_ClearError()");
  322. SDL_DestroyPalette(NULL);
  323. SDLTest_AssertPass("Call to SDL_DestroyPalette(NULL)");
  324. error = SDL_GetError();
  325. SDLTest_AssertPass("Call to SDL_GetError()");
  326. SDLTest_AssertCheck(error == NULL || error[0] == '\0', "Validate that error message is empty");
  327. return TEST_COMPLETED;
  328. }
  329. /* ================= Test References ================== */
  330. /* Pixels test cases */
  331. static const SDLTest_TestCaseReference pixelsTest1 = {
  332. (SDLTest_TestCaseFp)pixels_allocFreeFormat, "pixels_allocFreeFormat", "Call to SDL_CreatePixelFormat and SDL_DestroyPixelFormat", TEST_ENABLED
  333. };
  334. static const SDLTest_TestCaseReference pixelsTest2 = {
  335. (SDLTest_TestCaseFp)pixels_allocFreePalette, "pixels_allocFreePalette", "Call to SDL_CreatePalette and SDL_DestroyPalette", TEST_ENABLED
  336. };
  337. static const SDLTest_TestCaseReference pixelsTest3 = {
  338. (SDLTest_TestCaseFp)pixels_getPixelFormatName, "pixels_getPixelFormatName", "Call to SDL_GetPixelFormatName", TEST_ENABLED
  339. };
  340. /* Sequence of Pixels test cases */
  341. static const SDLTest_TestCaseReference *pixelsTests[] = {
  342. &pixelsTest1, &pixelsTest2, &pixelsTest3, NULL
  343. };
  344. /* Pixels test suite (global) */
  345. SDLTest_TestSuiteReference pixelsTestSuite = {
  346. "Pixels",
  347. NULL,
  348. pixelsTests,
  349. NULL
  350. };