testautomation_pixels.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /**
  2. * Pixels test suite
  3. */
  4. #include <SDL3/SDL.h>
  5. #include <SDL3/SDL_test.h>
  6. /* Test case functions */
  7. /* Definition of all RGB formats used to test pixel conversions */
  8. const int g_numAllFormats = 38;
  9. 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. const char *g_AllFormatsVerbose[] = {
  50. "SDL_PIXELFORMAT_INDEX1LSB",
  51. "SDL_PIXELFORMAT_INDEX1MSB",
  52. "SDL_PIXELFORMAT_INDEX4LSB",
  53. "SDL_PIXELFORMAT_INDEX4MSB",
  54. "SDL_PIXELFORMAT_INDEX8",
  55. "SDL_PIXELFORMAT_RGB332",
  56. "SDL_PIXELFORMAT_RGB444",
  57. "SDL_PIXELFORMAT_BGR444",
  58. "SDL_PIXELFORMAT_RGB555",
  59. "SDL_PIXELFORMAT_BGR555",
  60. "SDL_PIXELFORMAT_ARGB4444",
  61. "SDL_PIXELFORMAT_RGBA4444",
  62. "SDL_PIXELFORMAT_ABGR4444",
  63. "SDL_PIXELFORMAT_BGRA4444",
  64. "SDL_PIXELFORMAT_ARGB1555",
  65. "SDL_PIXELFORMAT_RGBA5551",
  66. "SDL_PIXELFORMAT_ABGR1555",
  67. "SDL_PIXELFORMAT_BGRA5551",
  68. "SDL_PIXELFORMAT_RGB565",
  69. "SDL_PIXELFORMAT_BGR565",
  70. "SDL_PIXELFORMAT_RGB24",
  71. "SDL_PIXELFORMAT_BGR24",
  72. "SDL_PIXELFORMAT_RGB888",
  73. "SDL_PIXELFORMAT_RGBX8888",
  74. "SDL_PIXELFORMAT_BGR888",
  75. "SDL_PIXELFORMAT_BGRX8888",
  76. "SDL_PIXELFORMAT_ARGB8888",
  77. "SDL_PIXELFORMAT_RGBA8888",
  78. "SDL_PIXELFORMAT_ABGR8888",
  79. "SDL_PIXELFORMAT_BGRA8888",
  80. "SDL_PIXELFORMAT_ARGB2101010",
  81. "SDL_PIXELFORMAT_YV12",
  82. "SDL_PIXELFORMAT_IYUV",
  83. "SDL_PIXELFORMAT_YUY2",
  84. "SDL_PIXELFORMAT_UYVY",
  85. "SDL_PIXELFORMAT_YVYU",
  86. "SDL_PIXELFORMAT_NV12",
  87. "SDL_PIXELFORMAT_NV21"
  88. };
  89. /* Definition of some invalid formats for negative tests */
  90. const int g_numInvalidPixelFormats = 2;
  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. const char *g_invalidPixelFormatsVerbose[] = {
  96. "SDL_PIXELFORMAT_UNKNOWN",
  97. "SDL_PIXELFORMAT_UNKNOWN"
  98. };
  99. /* Test case functions */
  100. /**
  101. * @brief Call to SDL_CreatePixelFormat and SDL_DestroyPixelFormat
  102. *
  103. * @sa http://wiki.libsdl.org/SDL_CreatePixelFormat
  104. * @sa http://wiki.libsdl.org/SDL_DestroyPixelFormat
  105. */
  106. int pixels_allocFreeFormat(void *arg)
  107. {
  108. const char *unknownFormat = "SDL_PIXELFORMAT_UNKNOWN";
  109. const char *expectedError = "Parameter 'format' is invalid";
  110. const char *expectedError2 = "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, expectedError2) == 0,
  168. "Validate error message, expected: '%s', got: '%s'", expectedError2, 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, "Validate that error message was not NULL");
  179. if (error != NULL) {
  180. SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0,
  181. "Validate error message, expected: '%s', got: '%s'", expectedError, error);
  182. }
  183. return TEST_COMPLETED;
  184. }
  185. /**
  186. * @brief Call to SDL_GetPixelFormatName
  187. *
  188. * @sa http://wiki.libsdl.org/SDL_GetPixelFormatName
  189. */
  190. int pixels_getPixelFormatName(void *arg)
  191. {
  192. const char *unknownFormat = "SDL_PIXELFORMAT_UNKNOWN";
  193. const char *error;
  194. int i;
  195. Uint32 format;
  196. const char *result;
  197. /* Blank/undefined format */
  198. format = 0;
  199. SDLTest_Log("RGB Format: %s (%" SDL_PRIu32 ")", unknownFormat, format);
  200. /* Get name of format */
  201. result = SDL_GetPixelFormatName(format);
  202. SDLTest_AssertPass("Call to SDL_GetPixelFormatName()");
  203. SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");
  204. if (result != NULL) {
  205. SDLTest_AssertCheck(result[0] != '\0', "Verify result is non-empty");
  206. SDLTest_AssertCheck(SDL_strcmp(result, unknownFormat) == 0,
  207. "Verify result text; expected: %s, got %s", unknownFormat, result);
  208. }
  209. /* RGB formats */
  210. for (i = 0; i < g_numAllFormats; i++) {
  211. format = g_AllFormats[i];
  212. SDLTest_Log("RGB Format: %s (%" SDL_PRIu32 ")", g_AllFormatsVerbose[i], format);
  213. /* Get name of format */
  214. result = SDL_GetPixelFormatName(format);
  215. SDLTest_AssertPass("Call to SDL_GetPixelFormatName()");
  216. SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");
  217. if (result != NULL) {
  218. SDLTest_AssertCheck(result[0] != '\0', "Verify result is non-empty");
  219. SDLTest_AssertCheck(SDL_strcmp(result, g_AllFormatsVerbose[i]) == 0,
  220. "Verify result text; expected: %s, got %s", g_AllFormatsVerbose[i], result);
  221. }
  222. }
  223. /* Negative cases */
  224. /* Invalid Formats */
  225. SDL_ClearError();
  226. SDLTest_AssertPass("Call to SDL_ClearError()");
  227. for (i = 0; i < g_numInvalidPixelFormats; i++) {
  228. format = g_invalidPixelFormats[i];
  229. result = SDL_GetPixelFormatName(format);
  230. SDLTest_AssertPass("Call to SDL_GetPixelFormatName(%" SDL_PRIu32 ")", format);
  231. SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");
  232. if (result != NULL) {
  233. SDLTest_AssertCheck(result[0] != '\0',
  234. "Verify result is non-empty; got: %s", result);
  235. SDLTest_AssertCheck(SDL_strcmp(result, g_invalidPixelFormatsVerbose[i]) == 0,
  236. "Validate name is UNKNOWN, expected: '%s', got: '%s'", g_invalidPixelFormatsVerbose[i], result);
  237. }
  238. error = SDL_GetError();
  239. SDLTest_AssertPass("Call to SDL_GetError()");
  240. SDLTest_AssertCheck(error == NULL || error[0] == '\0', "Validate that error message is empty");
  241. }
  242. return TEST_COMPLETED;
  243. }
  244. /**
  245. * @brief Call to SDL_CreatePalette and SDL_DestroyPalette
  246. *
  247. * @sa http://wiki.libsdl.org/SDL_CreatePalette
  248. * @sa http://wiki.libsdl.org/SDL_DestroyPalette
  249. */
  250. int pixels_allocFreePalette(void *arg)
  251. {
  252. const char *expectedError1 = "Parameter 'ncolors' is invalid";
  253. const char *expectedError2 = "Parameter 'palette' is invalid";
  254. const char *error;
  255. int variation;
  256. int i;
  257. int ncolors;
  258. SDL_Palette *result;
  259. /* Allocate palette */
  260. for (variation = 1; variation <= 3; variation++) {
  261. switch (variation) {
  262. /* Just one color */
  263. default:
  264. case 1:
  265. ncolors = 1;
  266. break;
  267. /* Two colors */
  268. case 2:
  269. ncolors = 2;
  270. break;
  271. /* More than two colors */
  272. case 3:
  273. ncolors = SDLTest_RandomIntegerInRange(8, 16);
  274. break;
  275. }
  276. result = SDL_CreatePalette(ncolors);
  277. SDLTest_AssertPass("Call to SDL_CreatePalette(%d)", ncolors);
  278. SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");
  279. if (result != NULL) {
  280. SDLTest_AssertCheck(result->ncolors == ncolors, "Verify value of result.ncolors; expected: %u, got %u", ncolors, result->ncolors);
  281. if (result->ncolors > 0) {
  282. SDLTest_AssertCheck(result->colors != NULL, "Verify value of result.colors is not NULL");
  283. if (result->colors != NULL) {
  284. for (i = 0; i < result->ncolors; i++) {
  285. SDLTest_AssertCheck(result->colors[i].r == 255, "Verify value of result.colors[%d].r; expected: 255, got %u", i, result->colors[i].r);
  286. SDLTest_AssertCheck(result->colors[i].g == 255, "Verify value of result.colors[%d].g; expected: 255, got %u", i, result->colors[i].g);
  287. SDLTest_AssertCheck(result->colors[i].b == 255, "Verify value of result.colors[%d].b; expected: 255, got %u", i, result->colors[i].b);
  288. }
  289. }
  290. }
  291. /* Deallocate again */
  292. SDL_DestroyPalette(result);
  293. SDLTest_AssertPass("Call to SDL_DestroyPalette()");
  294. }
  295. }
  296. /* Negative cases */
  297. /* Invalid number of colors */
  298. for (ncolors = 0; ncolors > -3; ncolors--) {
  299. SDL_ClearError();
  300. SDLTest_AssertPass("Call to SDL_ClearError()");
  301. result = SDL_CreatePalette(ncolors);
  302. SDLTest_AssertPass("Call to SDL_CreatePalette(%d)", ncolors);
  303. SDLTest_AssertCheck(result == NULL, "Verify result is NULL");
  304. error = SDL_GetError();
  305. SDLTest_AssertPass("Call to SDL_GetError()");
  306. SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
  307. if (error != NULL) {
  308. SDLTest_AssertCheck(SDL_strcmp(error, expectedError1) == 0,
  309. "Validate error message, expected: '%s', got: '%s'", expectedError1, error);
  310. }
  311. }
  312. /* Invalid free pointer */
  313. SDL_ClearError();
  314. SDLTest_AssertPass("Call to SDL_ClearError()");
  315. SDL_DestroyPalette(NULL);
  316. SDLTest_AssertPass("Call to SDL_DestroyPalette(NULL)");
  317. error = SDL_GetError();
  318. SDLTest_AssertPass("Call to SDL_GetError()");
  319. SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
  320. if (error != NULL) {
  321. SDLTest_AssertCheck(SDL_strcmp(error, expectedError2) == 0,
  322. "Validate error message, expected: '%s', got: '%s'", expectedError2, error);
  323. }
  324. return TEST_COMPLETED;
  325. }
  326. /* ================= Test References ================== */
  327. /* Pixels test cases */
  328. static const SDLTest_TestCaseReference pixelsTest1 = {
  329. (SDLTest_TestCaseFp)pixels_allocFreeFormat, "pixels_allocFreeFormat", "Call to SDL_CreatePixelFormat and SDL_DestroyPixelFormat", TEST_ENABLED
  330. };
  331. static const SDLTest_TestCaseReference pixelsTest2 = {
  332. (SDLTest_TestCaseFp)pixels_allocFreePalette, "pixels_allocFreePalette", "Call to SDL_CreatePalette and SDL_DestroyPalette", TEST_ENABLED
  333. };
  334. static const SDLTest_TestCaseReference pixelsTest3 = {
  335. (SDLTest_TestCaseFp)pixels_getPixelFormatName, "pixels_getPixelFormatName", "Call to SDL_GetPixelFormatName", TEST_ENABLED
  336. };
  337. /* Sequence of Pixels test cases */
  338. static const SDLTest_TestCaseReference *pixelsTests[] = {
  339. &pixelsTest1, &pixelsTest2, &pixelsTest3, NULL
  340. };
  341. /* Pixels test suite (global) */
  342. SDLTest_TestSuiteReference pixelsTestSuite = {
  343. "Pixels",
  344. NULL,
  345. pixelsTests,
  346. NULL
  347. };