testautomation_pixels.c 14 KB

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