testautomation_pixels.c 14 KB

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