testautomation_pixels.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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 SDL_PixelFormatEnum 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_XRGB4444,
  19. SDL_PIXELFORMAT_XBGR4444,
  20. SDL_PIXELFORMAT_XRGB1555,
  21. SDL_PIXELFORMAT_XBGR1555,
  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_XRGB4444",
  65. "SDL_PIXELFORMAT_XBGR4444",
  66. "SDL_PIXELFORMAT_XRGB1555",
  67. "SDL_PIXELFORMAT_XBGR1555",
  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. SDL_PixelFormatEnum format;
  124. Uint32 masks;
  125. SDL_PixelFormat *result;
  126. /* Blank/unknown format */
  127. format = SDL_PIXELFORMAT_UNKNOWN;
  128. SDLTest_Log("Pixel Format: %s (%d)", 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: %d, got %d", format, result->format);
  135. SDLTest_AssertCheck(result->bits_per_pixel == 0,
  136. "Verify value of result.bits_per_pixel; expected: 0, got %u",
  137. result->bits_per_pixel);
  138. SDLTest_AssertCheck(result->bytes_per_pixel == 0,
  139. "Verify value of result.bytes_per_pixel; expected: 0, got %u",
  140. result->bytes_per_pixel);
  141. masks = result->Rmask | result->Gmask | result->Bmask | result->Amask;
  142. SDLTest_AssertCheck(masks == 0, "Verify value of result.[RGBA]mask combined; expected: 0, got %" SDL_PRIu32, masks);
  143. /* Deallocate again */
  144. SDL_DestroyPixelFormat(result);
  145. SDLTest_AssertPass("Call to SDL_DestroyPixelFormat()");
  146. }
  147. /* RGB formats */
  148. for (i = 0; i < g_numAllFormats; i++) {
  149. format = g_AllFormats[i];
  150. SDLTest_Log("Pixel Format: %s (%d)", g_AllFormatsVerbose[i], format);
  151. /* Allocate format */
  152. result = SDL_CreatePixelFormat(format);
  153. SDLTest_AssertPass("Call to SDL_CreatePixelFormat()");
  154. SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");
  155. if (result != NULL) {
  156. SDLTest_AssertCheck(result->format == format, "Verify value of result.format; expected: %d, got %d", format, result->format);
  157. if (!SDL_ISPIXELFORMAT_FOURCC(format)) {
  158. SDLTest_AssertCheck(result->bits_per_pixel > 0,
  159. "Verify value of result.bits_per_pixel; expected: >0, got %u",
  160. result->bits_per_pixel);
  161. SDLTest_AssertCheck(result->bytes_per_pixel > 0,
  162. "Verify value of result.bytes_per_pixel; expected: >0, got %u",
  163. result->bytes_per_pixel);
  164. if (!SDL_ISPIXELFORMAT_INDEXED(format)) {
  165. masks = result->Rmask | result->Gmask | result->Bmask | result->Amask;
  166. SDLTest_AssertCheck(masks > 0, "Verify value of result.[RGBA]mask combined; expected: >0, got %" SDL_PRIu32, masks);
  167. }
  168. }
  169. /* Deallocate again */
  170. SDL_DestroyPixelFormat(result);
  171. SDLTest_AssertPass("Call to SDL_DestroyPixelFormat()");
  172. }
  173. }
  174. /* Negative cases */
  175. /* Invalid Formats */
  176. for (i = 0; i < g_numInvalidPixelFormats; i++) {
  177. SDL_ClearError();
  178. SDLTest_AssertPass("Call to SDL_ClearError()");
  179. format = g_invalidPixelFormats[i];
  180. result = SDL_CreatePixelFormat(format);
  181. SDLTest_AssertPass("Call to SDL_CreatePixelFormat(%d)", format);
  182. SDLTest_AssertCheck(result == NULL, "Verify result is NULL");
  183. error = SDL_GetError();
  184. SDLTest_AssertPass("Call to SDL_GetError()");
  185. SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
  186. if (error != NULL) {
  187. SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0,
  188. "Validate error message, expected: '%s', got: '%s'", expectedError, error);
  189. }
  190. }
  191. /* Invalid free pointer */
  192. SDL_ClearError();
  193. SDLTest_AssertPass("Call to SDL_ClearError()");
  194. SDL_DestroyPixelFormat(NULL);
  195. SDLTest_AssertPass("Call to SDL_DestroyPixelFormat(NULL)");
  196. error = SDL_GetError();
  197. SDLTest_AssertPass("Call to SDL_GetError()");
  198. SDLTest_AssertCheck(error == NULL || error[0] == '\0', "Validate that error message is empty");
  199. return TEST_COMPLETED;
  200. }
  201. /**
  202. * Call to SDL_GetPixelFormatName
  203. *
  204. * \sa SDL_GetPixelFormatName
  205. */
  206. static int pixels_getPixelFormatName(void *arg)
  207. {
  208. const char *unknownFormat = "SDL_PIXELFORMAT_UNKNOWN";
  209. const char *error;
  210. int i;
  211. SDL_PixelFormatEnum format;
  212. const char *result;
  213. /* Blank/undefined format */
  214. format = SDL_PIXELFORMAT_UNKNOWN;
  215. SDLTest_Log("RGB Format: %s (%d)", unknownFormat, format);
  216. /* Get name of format */
  217. result = SDL_GetPixelFormatName(format);
  218. SDLTest_AssertPass("Call to SDL_GetPixelFormatName()");
  219. SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");
  220. if (result != NULL) {
  221. SDLTest_AssertCheck(result[0] != '\0', "Verify result is non-empty");
  222. SDLTest_AssertCheck(SDL_strcmp(result, unknownFormat) == 0,
  223. "Verify result text; expected: %s, got %s", unknownFormat, result);
  224. }
  225. /* RGB formats */
  226. for (i = 0; i < g_numAllFormats; i++) {
  227. format = g_AllFormats[i];
  228. SDLTest_Log("RGB Format: %s (%d)", g_AllFormatsVerbose[i], format);
  229. /* Get name of format */
  230. result = SDL_GetPixelFormatName(format);
  231. SDLTest_AssertPass("Call to SDL_GetPixelFormatName()");
  232. SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");
  233. if (result != NULL) {
  234. SDLTest_AssertCheck(result[0] != '\0', "Verify result is non-empty");
  235. SDLTest_AssertCheck(SDL_strcmp(result, g_AllFormatsVerbose[i]) == 0,
  236. "Verify result text; expected: %s, got %s", g_AllFormatsVerbose[i], result);
  237. }
  238. }
  239. /* Negative cases */
  240. /* Invalid Formats */
  241. SDL_ClearError();
  242. SDLTest_AssertPass("Call to SDL_ClearError()");
  243. for (i = 0; i < g_numInvalidPixelFormats; i++) {
  244. format = g_invalidPixelFormats[i];
  245. result = SDL_GetPixelFormatName(format);
  246. SDLTest_AssertPass("Call to SDL_GetPixelFormatName(%d)", format);
  247. SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");
  248. if (result != NULL) {
  249. SDLTest_AssertCheck(result[0] != '\0',
  250. "Verify result is non-empty; got: %s", result);
  251. SDLTest_AssertCheck(SDL_strcmp(result, g_invalidPixelFormatsVerbose[i]) == 0,
  252. "Validate name is UNKNOWN, expected: '%s', got: '%s'", g_invalidPixelFormatsVerbose[i], result);
  253. }
  254. error = SDL_GetError();
  255. SDLTest_AssertPass("Call to SDL_GetError()");
  256. SDLTest_AssertCheck(error == NULL || error[0] == '\0', "Validate that error message is empty");
  257. }
  258. return TEST_COMPLETED;
  259. }
  260. /**
  261. * Call to SDL_CreatePalette and SDL_DestroyPalette
  262. *
  263. * \sa SDL_CreatePalette
  264. * \sa SDL_DestroyPalette
  265. */
  266. static int pixels_allocFreePalette(void *arg)
  267. {
  268. const char *expectedError = "Parameter 'ncolors' is invalid";
  269. const char *error;
  270. int variation;
  271. int i;
  272. int ncolors;
  273. SDL_Palette *result;
  274. /* Allocate palette */
  275. for (variation = 1; variation <= 3; variation++) {
  276. switch (variation) {
  277. /* Just one color */
  278. default:
  279. case 1:
  280. ncolors = 1;
  281. break;
  282. /* Two colors */
  283. case 2:
  284. ncolors = 2;
  285. break;
  286. /* More than two colors */
  287. case 3:
  288. ncolors = SDLTest_RandomIntegerInRange(8, 16);
  289. break;
  290. }
  291. result = SDL_CreatePalette(ncolors);
  292. SDLTest_AssertPass("Call to SDL_CreatePalette(%d)", ncolors);
  293. SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");
  294. if (result != NULL) {
  295. SDLTest_AssertCheck(result->ncolors == ncolors, "Verify value of result.ncolors; expected: %u, got %u", ncolors, result->ncolors);
  296. if (result->ncolors > 0) {
  297. SDLTest_AssertCheck(result->colors != NULL, "Verify value of result.colors is not NULL");
  298. if (result->colors != NULL) {
  299. for (i = 0; i < result->ncolors; i++) {
  300. SDLTest_AssertCheck(result->colors[i].r == 255, "Verify value of result.colors[%d].r; expected: 255, got %u", i, result->colors[i].r);
  301. SDLTest_AssertCheck(result->colors[i].g == 255, "Verify value of result.colors[%d].g; expected: 255, got %u", i, result->colors[i].g);
  302. SDLTest_AssertCheck(result->colors[i].b == 255, "Verify value of result.colors[%d].b; expected: 255, got %u", i, result->colors[i].b);
  303. }
  304. }
  305. }
  306. /* Deallocate again */
  307. SDL_DestroyPalette(result);
  308. SDLTest_AssertPass("Call to SDL_DestroyPalette()");
  309. }
  310. }
  311. /* Negative cases */
  312. /* Invalid number of colors */
  313. for (ncolors = 0; ncolors > -3; ncolors--) {
  314. SDL_ClearError();
  315. SDLTest_AssertPass("Call to SDL_ClearError()");
  316. result = SDL_CreatePalette(ncolors);
  317. SDLTest_AssertPass("Call to SDL_CreatePalette(%d)", ncolors);
  318. SDLTest_AssertCheck(result == NULL, "Verify result is NULL");
  319. error = SDL_GetError();
  320. SDLTest_AssertPass("Call to SDL_GetError()");
  321. SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
  322. if (error != NULL) {
  323. SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0,
  324. "Validate error message, expected: '%s', got: '%s'", expectedError, error);
  325. }
  326. }
  327. /* Invalid free pointer */
  328. SDL_ClearError();
  329. SDLTest_AssertPass("Call to SDL_ClearError()");
  330. SDL_DestroyPalette(NULL);
  331. SDLTest_AssertPass("Call to SDL_DestroyPalette(NULL)");
  332. error = SDL_GetError();
  333. SDLTest_AssertPass("Call to SDL_GetError()");
  334. SDLTest_AssertCheck(error == NULL || error[0] == '\0', "Validate that error message is empty");
  335. return TEST_COMPLETED;
  336. }
  337. /* ================= Test References ================== */
  338. /* Pixels test cases */
  339. static const SDLTest_TestCaseReference pixelsTest1 = {
  340. (SDLTest_TestCaseFp)pixels_allocFreeFormat, "pixels_allocFreeFormat", "Call to SDL_CreatePixelFormat and SDL_DestroyPixelFormat", TEST_ENABLED
  341. };
  342. static const SDLTest_TestCaseReference pixelsTest2 = {
  343. (SDLTest_TestCaseFp)pixels_allocFreePalette, "pixels_allocFreePalette", "Call to SDL_CreatePalette and SDL_DestroyPalette", TEST_ENABLED
  344. };
  345. static const SDLTest_TestCaseReference pixelsTest3 = {
  346. (SDLTest_TestCaseFp)pixels_getPixelFormatName, "pixels_getPixelFormatName", "Call to SDL_GetPixelFormatName", TEST_ENABLED
  347. };
  348. /* Sequence of Pixels test cases */
  349. static const SDLTest_TestCaseReference *pixelsTests[] = {
  350. &pixelsTest1, &pixelsTest2, &pixelsTest3, NULL
  351. };
  352. /* Pixels test suite (global) */
  353. SDLTest_TestSuiteReference pixelsTestSuite = {
  354. "Pixels",
  355. NULL,
  356. pixelsTests,
  357. NULL
  358. };