testautomation_pixels.c 14 KB

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