1
0

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