testautomation_pixels.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 _numRGBPixelFormats = 31;
  9. Uint32 _RGBPixelFormats[] =
  10. {
  11. SDL_PIXELFORMAT_INDEX1LSB,
  12. SDL_PIXELFORMAT_INDEX1MSB,
  13. SDL_PIXELFORMAT_INDEX4LSB,
  14. SDL_PIXELFORMAT_INDEX4MSB,
  15. SDL_PIXELFORMAT_INDEX8,
  16. SDL_PIXELFORMAT_RGB332,
  17. SDL_PIXELFORMAT_RGB444,
  18. SDL_PIXELFORMAT_BGR444,
  19. SDL_PIXELFORMAT_RGB555,
  20. SDL_PIXELFORMAT_BGR555,
  21. SDL_PIXELFORMAT_ARGB4444,
  22. SDL_PIXELFORMAT_RGBA4444,
  23. SDL_PIXELFORMAT_ABGR4444,
  24. SDL_PIXELFORMAT_BGRA4444,
  25. SDL_PIXELFORMAT_ARGB1555,
  26. SDL_PIXELFORMAT_RGBA5551,
  27. SDL_PIXELFORMAT_ABGR1555,
  28. SDL_PIXELFORMAT_BGRA5551,
  29. SDL_PIXELFORMAT_RGB565,
  30. SDL_PIXELFORMAT_BGR565,
  31. SDL_PIXELFORMAT_RGB24,
  32. SDL_PIXELFORMAT_BGR24,
  33. SDL_PIXELFORMAT_RGB888,
  34. SDL_PIXELFORMAT_RGBX8888,
  35. SDL_PIXELFORMAT_BGR888,
  36. SDL_PIXELFORMAT_BGRX8888,
  37. SDL_PIXELFORMAT_ARGB8888,
  38. SDL_PIXELFORMAT_RGBA8888,
  39. SDL_PIXELFORMAT_ABGR8888,
  40. SDL_PIXELFORMAT_BGRA8888,
  41. SDL_PIXELFORMAT_ARGB2101010
  42. };
  43. const char* _RGBPixelFormatsVerbose[] =
  44. {
  45. "SDL_PIXELFORMAT_INDEX1LSB",
  46. "SDL_PIXELFORMAT_INDEX1MSB",
  47. "SDL_PIXELFORMAT_INDEX4LSB",
  48. "SDL_PIXELFORMAT_INDEX4MSB",
  49. "SDL_PIXELFORMAT_INDEX8",
  50. "SDL_PIXELFORMAT_RGB332",
  51. "SDL_PIXELFORMAT_RGB444",
  52. "SDL_PIXELFORMAT_BGR444",
  53. "SDL_PIXELFORMAT_RGB555",
  54. "SDL_PIXELFORMAT_BGR555",
  55. "SDL_PIXELFORMAT_ARGB4444",
  56. "SDL_PIXELFORMAT_RGBA4444",
  57. "SDL_PIXELFORMAT_ABGR4444",
  58. "SDL_PIXELFORMAT_BGRA4444",
  59. "SDL_PIXELFORMAT_ARGB1555",
  60. "SDL_PIXELFORMAT_RGBA5551",
  61. "SDL_PIXELFORMAT_ABGR1555",
  62. "SDL_PIXELFORMAT_BGRA5551",
  63. "SDL_PIXELFORMAT_RGB565",
  64. "SDL_PIXELFORMAT_BGR565",
  65. "SDL_PIXELFORMAT_RGB24",
  66. "SDL_PIXELFORMAT_BGR24",
  67. "SDL_PIXELFORMAT_RGB888",
  68. "SDL_PIXELFORMAT_RGBX8888",
  69. "SDL_PIXELFORMAT_BGR888",
  70. "SDL_PIXELFORMAT_BGRX8888",
  71. "SDL_PIXELFORMAT_ARGB8888",
  72. "SDL_PIXELFORMAT_RGBA8888",
  73. "SDL_PIXELFORMAT_ABGR8888",
  74. "SDL_PIXELFORMAT_BGRA8888",
  75. "SDL_PIXELFORMAT_ARGB2101010"
  76. };
  77. /* Definition of all Non-RGB formats used to test pixel conversions */
  78. const int _numNonRGBPixelFormats = 7;
  79. Uint32 _nonRGBPixelFormats[] =
  80. {
  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. const char* _nonRGBPixelFormatsVerbose[] =
  90. {
  91. "SDL_PIXELFORMAT_YV12",
  92. "SDL_PIXELFORMAT_IYUV",
  93. "SDL_PIXELFORMAT_YUY2",
  94. "SDL_PIXELFORMAT_UYVY",
  95. "SDL_PIXELFORMAT_YVYU",
  96. "SDL_PIXELFORMAT_NV12",
  97. "SDL_PIXELFORMAT_NV21"
  98. };
  99. /* Definition of some invalid formats for negative tests */
  100. const int _numInvalidPixelFormats = 2;
  101. Uint32 _invalidPixelFormats[] =
  102. {
  103. 0xfffffffe,
  104. 0xffffffff
  105. };
  106. const char* _invalidPixelFormatsVerbose[] =
  107. {
  108. "SDL_PIXELFORMAT_UNKNOWN",
  109. "SDL_PIXELFORMAT_UNKNOWN"
  110. };
  111. /* Test case functions */
  112. /**
  113. * @brief Call to SDL_AllocFormat and SDL_FreeFormat
  114. *
  115. * @sa http://wiki.libsdl.org/SDL_AllocFormat
  116. * @sa http://wiki.libsdl.org/SDL_FreeFormat
  117. */
  118. int
  119. pixels_allocFreeFormat(void *arg)
  120. {
  121. const char *unknownFormat = "SDL_PIXELFORMAT_UNKNOWN";
  122. const char *expectedError = "Parameter 'format' is invalid";
  123. const char *error;
  124. int i;
  125. Uint32 format;
  126. Uint32 masks;
  127. SDL_PixelFormat* result;
  128. /* Blank/unknown format */
  129. format = 0;
  130. SDLTest_Log("RGB Format: %s (%" SDL_PRIu32 ")", unknownFormat, format);
  131. /* Allocate format */
  132. result = SDL_AllocFormat(format);
  133. SDLTest_AssertPass("Call to SDL_AllocFormat()");
  134. SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");
  135. if (result != NULL) {
  136. SDLTest_AssertCheck(result->format == format, "Verify value of result.format; expected: %" SDL_PRIu32 ", got %" SDL_PRIu32, format, result->format);
  137. SDLTest_AssertCheck(result->BitsPerPixel == 0, "Verify value of result.BitsPerPixel; expected: 0, got %u", result->BitsPerPixel);
  138. SDLTest_AssertCheck(result->BytesPerPixel == 0, "Verify value of result.BytesPerPixel; expected: 0, got %u", result->BytesPerPixel);
  139. masks = result->Rmask | result->Gmask | result->Bmask | result->Amask;
  140. SDLTest_AssertCheck(masks == 0, "Verify value of result.[RGBA]mask combined; expected: 0, got %" SDL_PRIu32, masks);
  141. /* Deallocate again */
  142. SDL_FreeFormat(result);
  143. SDLTest_AssertPass("Call to SDL_FreeFormat()");
  144. }
  145. /* RGB formats */
  146. for (i = 0; i < _numRGBPixelFormats; i++) {
  147. format = _RGBPixelFormats[i];
  148. SDLTest_Log("RGB Format: %s (%" SDL_PRIu32 ")", _RGBPixelFormatsVerbose[i], format);
  149. /* Allocate format */
  150. result = SDL_AllocFormat(format);
  151. SDLTest_AssertPass("Call to SDL_AllocFormat()");
  152. SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");
  153. if (result != NULL) {
  154. SDLTest_AssertCheck(result->format == format, "Verify value of result.format; expected: %" SDL_PRIu32 ", got %" SDL_PRIu32, format, result->format);
  155. SDLTest_AssertCheck(result->BitsPerPixel > 0, "Verify value of result.BitsPerPixel; expected: >0, got %u", result->BitsPerPixel);
  156. SDLTest_AssertCheck(result->BytesPerPixel > 0, "Verify value of result.BytesPerPixel; expected: >0, got %u", result->BytesPerPixel);
  157. if (result->palette != NULL) {
  158. masks = result->Rmask | result->Gmask | result->Bmask | result->Amask;
  159. SDLTest_AssertCheck(masks > 0, "Verify value of result.[RGBA]mask combined; expected: >0, got %" SDL_PRIu32, masks);
  160. }
  161. /* Deallocate again */
  162. SDL_FreeFormat(result);
  163. SDLTest_AssertPass("Call to SDL_FreeFormat()");
  164. }
  165. }
  166. /* Non-RGB formats */
  167. for (i = 0; i < _numNonRGBPixelFormats; i++) {
  168. format = _nonRGBPixelFormats[i];
  169. SDLTest_Log("non-RGB Format: %s (%" SDL_PRIu32 ")", _nonRGBPixelFormatsVerbose[i], format);
  170. /* Try to allocate format */
  171. result = SDL_AllocFormat(format);
  172. SDLTest_AssertPass("Call to SDL_AllocFormat()");
  173. SDLTest_AssertCheck(result == NULL, "Verify result is NULL");
  174. }
  175. /* Negative cases */
  176. /* Invalid Formats */
  177. for (i = 0; i < _numInvalidPixelFormats; i++) {
  178. SDL_ClearError();
  179. SDLTest_AssertPass("Call to SDL_ClearError()");
  180. format = _invalidPixelFormats[i];
  181. result = SDL_AllocFormat(format);
  182. SDLTest_AssertPass("Call to SDL_AllocFormat(%" SDL_PRIu32 ")", format);
  183. SDLTest_AssertCheck(result == NULL, "Verify result is NULL");
  184. error = SDL_GetError();
  185. SDLTest_AssertPass("Call to SDL_GetError()");
  186. SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
  187. if (error != NULL) {
  188. SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0,
  189. "Validate error message, expected: '%s', got: '%s'", expectedError, error);
  190. }
  191. }
  192. /* Invalid free pointer */
  193. SDL_ClearError();
  194. SDLTest_AssertPass("Call to SDL_ClearError()");
  195. SDL_FreeFormat(NULL);
  196. SDLTest_AssertPass("Call to SDL_FreeFormat(NULL)");
  197. error = SDL_GetError();
  198. SDLTest_AssertPass("Call to SDL_GetError()");
  199. SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
  200. if (error != NULL) {
  201. SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0,
  202. "Validate error message, expected: '%s', got: '%s'", expectedError, error);
  203. }
  204. return TEST_COMPLETED;
  205. }
  206. /**
  207. * @brief Call to SDL_GetPixelFormatName
  208. *
  209. * @sa http://wiki.libsdl.org/SDL_GetPixelFormatName
  210. */
  211. int
  212. pixels_getPixelFormatName(void *arg)
  213. {
  214. const char *unknownFormat = "SDL_PIXELFORMAT_UNKNOWN";
  215. const char *error;
  216. int i;
  217. Uint32 format;
  218. const char *result;
  219. /* Blank/undefined format */
  220. format = 0;
  221. SDLTest_Log("RGB Format: %s (%" SDL_PRIu32 ")", unknownFormat, format);
  222. /* Get name of format */
  223. result = SDL_GetPixelFormatName(format);
  224. SDLTest_AssertPass("Call to SDL_GetPixelFormatName()");
  225. SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");
  226. if (result != NULL) {
  227. SDLTest_AssertCheck(result[0] != '\0', "Verify result is non-empty");
  228. SDLTest_AssertCheck(SDL_strcmp(result, unknownFormat) == 0,
  229. "Verify result text; expected: %s, got %s", unknownFormat, result);
  230. }
  231. /* RGB formats */
  232. for (i = 0; i < _numRGBPixelFormats; i++) {
  233. format = _RGBPixelFormats[i];
  234. SDLTest_Log("RGB Format: %s (%" SDL_PRIu32 ")", _RGBPixelFormatsVerbose[i], format);
  235. /* Get name of format */
  236. result = SDL_GetPixelFormatName(format);
  237. SDLTest_AssertPass("Call to SDL_GetPixelFormatName()");
  238. SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");
  239. if (result != NULL) {
  240. SDLTest_AssertCheck(result[0] != '\0', "Verify result is non-empty");
  241. SDLTest_AssertCheck(SDL_strcmp(result, _RGBPixelFormatsVerbose[i]) == 0,
  242. "Verify result text; expected: %s, got %s", _RGBPixelFormatsVerbose[i], result);
  243. }
  244. }
  245. /* Non-RGB formats */
  246. for (i = 0; i < _numNonRGBPixelFormats; i++) {
  247. format = _nonRGBPixelFormats[i];
  248. SDLTest_Log("non-RGB Format: %s (%" SDL_PRIu32 ")", _nonRGBPixelFormatsVerbose[i], format);
  249. /* Get name of format */
  250. result = SDL_GetPixelFormatName(format);
  251. SDLTest_AssertPass("Call to SDL_GetPixelFormatName()");
  252. SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");
  253. if (result != NULL) {
  254. SDLTest_AssertCheck(result[0] != '\0', "Verify result is non-empty");
  255. SDLTest_AssertCheck(SDL_strcmp(result, _nonRGBPixelFormatsVerbose[i]) == 0,
  256. "Verify result text; expected: %s, got %s", _nonRGBPixelFormatsVerbose[i], result);
  257. }
  258. }
  259. /* Negative cases */
  260. /* Invalid Formats */
  261. SDL_ClearError();
  262. SDLTest_AssertPass("Call to SDL_ClearError()");
  263. for (i = 0; i < _numInvalidPixelFormats; i++) {
  264. format = _invalidPixelFormats[i];
  265. result = SDL_GetPixelFormatName(format);
  266. SDLTest_AssertPass("Call to SDL_GetPixelFormatName(%" SDL_PRIu32 ")", format);
  267. SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");
  268. if (result != NULL) {
  269. SDLTest_AssertCheck(result[0] != '\0',
  270. "Verify result is non-empty; got: %s", result);
  271. SDLTest_AssertCheck(SDL_strcmp(result, _invalidPixelFormatsVerbose[i]) == 0,
  272. "Validate name is UNKNOWN, expected: '%s', got: '%s'", _invalidPixelFormatsVerbose[i], result);
  273. }
  274. error = SDL_GetError();
  275. SDLTest_AssertPass("Call to SDL_GetError()");
  276. SDLTest_AssertCheck(error == NULL || error[0] == '\0', "Validate that error message is empty");
  277. }
  278. return TEST_COMPLETED;
  279. }
  280. /**
  281. * @brief Call to SDL_AllocPalette and SDL_FreePalette
  282. *
  283. * @sa http://wiki.libsdl.org/SDL_AllocPalette
  284. * @sa http://wiki.libsdl.org/SDL_FreePalette
  285. */
  286. int
  287. pixels_allocFreePalette(void *arg)
  288. {
  289. const char *expectedError1 = "Parameter 'ncolors' is invalid";
  290. const char *expectedError2 = "Parameter 'palette' is invalid";
  291. const char *error;
  292. int variation;
  293. int i;
  294. int ncolors;
  295. SDL_Palette* result;
  296. /* Allocate palette */
  297. for (variation = 1; variation <= 3; variation++) {
  298. switch (variation) {
  299. /* Just one color */
  300. default:
  301. case 1:
  302. ncolors = 1;
  303. break;
  304. /* Two colors */
  305. case 2:
  306. ncolors = 2;
  307. break;
  308. /* More than two colors */
  309. case 3:
  310. ncolors = SDLTest_RandomIntegerInRange(8, 16);
  311. break;
  312. }
  313. result = SDL_AllocPalette(ncolors);
  314. SDLTest_AssertPass("Call to SDL_AllocPalette(%d)", ncolors);
  315. SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");
  316. if (result != NULL) {
  317. SDLTest_AssertCheck(result->ncolors == ncolors, "Verify value of result.ncolors; expected: %u, got %u", ncolors, result->ncolors);
  318. if (result->ncolors > 0) {
  319. SDLTest_AssertCheck(result->colors != NULL, "Verify value of result.colors is not NULL");
  320. if (result->colors != NULL) {
  321. for (i = 0; i < result->ncolors; i++) {
  322. SDLTest_AssertCheck(result->colors[i].r == 255, "Verify value of result.colors[%d].r; expected: 255, got %u", i, result->colors[i].r);
  323. SDLTest_AssertCheck(result->colors[i].g == 255, "Verify value of result.colors[%d].g; expected: 255, got %u", i, result->colors[i].g);
  324. SDLTest_AssertCheck(result->colors[i].b == 255, "Verify value of result.colors[%d].b; expected: 255, got %u", i, result->colors[i].b);
  325. }
  326. }
  327. }
  328. /* Deallocate again */
  329. SDL_FreePalette(result);
  330. SDLTest_AssertPass("Call to SDL_FreePalette()");
  331. }
  332. }
  333. /* Negative cases */
  334. /* Invalid number of colors */
  335. for (ncolors = 0; ncolors > -3; ncolors--) {
  336. SDL_ClearError();
  337. SDLTest_AssertPass("Call to SDL_ClearError()");
  338. result = SDL_AllocPalette(ncolors);
  339. SDLTest_AssertPass("Call to SDL_AllocPalette(%d)", ncolors);
  340. SDLTest_AssertCheck(result == NULL, "Verify result is NULL");
  341. error = SDL_GetError();
  342. SDLTest_AssertPass("Call to SDL_GetError()");
  343. SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
  344. if (error != NULL) {
  345. SDLTest_AssertCheck(SDL_strcmp(error, expectedError1) == 0,
  346. "Validate error message, expected: '%s', got: '%s'", expectedError1, error);
  347. }
  348. }
  349. /* Invalid free pointer */
  350. SDL_ClearError();
  351. SDLTest_AssertPass("Call to SDL_ClearError()");
  352. SDL_FreePalette(NULL);
  353. SDLTest_AssertPass("Call to SDL_FreePalette(NULL)");
  354. error = SDL_GetError();
  355. SDLTest_AssertPass("Call to SDL_GetError()");
  356. SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
  357. if (error != NULL) {
  358. SDLTest_AssertCheck(SDL_strcmp(error, expectedError2) == 0,
  359. "Validate error message, expected: '%s', got: '%s'", expectedError2, error);
  360. }
  361. return TEST_COMPLETED;
  362. }
  363. /* ================= Test References ================== */
  364. /* Pixels test cases */
  365. static const SDLTest_TestCaseReference pixelsTest1 =
  366. { (SDLTest_TestCaseFp)pixels_allocFreeFormat, "pixels_allocFreeFormat", "Call to SDL_AllocFormat and SDL_FreeFormat", TEST_ENABLED };
  367. static const SDLTest_TestCaseReference pixelsTest2 =
  368. { (SDLTest_TestCaseFp)pixels_allocFreePalette, "pixels_allocFreePalette", "Call to SDL_AllocPalette and SDL_FreePalette", TEST_ENABLED };
  369. static const SDLTest_TestCaseReference pixelsTest3 =
  370. { (SDLTest_TestCaseFp)pixels_getPixelFormatName, "pixels_getPixelFormatName", "Call to SDL_GetPixelFormatName", TEST_ENABLED };
  371. /* Sequence of Pixels test cases */
  372. static const SDLTest_TestCaseReference *pixelsTests[] = {
  373. &pixelsTest1, &pixelsTest2, &pixelsTest3, NULL
  374. };
  375. /* Pixels test suite (global) */
  376. SDLTest_TestSuiteReference pixelsTestSuite = {
  377. "Pixels",
  378. NULL,
  379. pixelsTests,
  380. NULL
  381. };
  382. /* vi: set ts=4 sw=4 expandtab: */