testautomation_pixels.c 14 KB

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