Debian Codegraph @

test/testautomation_pixels.c

raw
1 /**
2 * Pixels test suite
3 */
4
5 #include <stdio.h>
6
7 #include "SDL.h"
8 #include "SDL_test.h"
9
10 /* Test case functions */
11
12 /* Definition of all RGB formats used to test pixel conversions */
13 const int _numRGBPixelFormats = 31;
14 Uint32 _RGBPixelFormats[] = {
15 SDL_PIXELFORMAT_INDEX1LSB,
16 SDL_PIXELFORMAT_INDEX1MSB,
17 SDL_PIXELFORMAT_INDEX2LSB,
18 SDL_PIXELFORMAT_INDEX2MSB,
19 SDL_PIXELFORMAT_INDEX4LSB,
20 SDL_PIXELFORMAT_INDEX4MSB,
21 SDL_PIXELFORMAT_INDEX8,
22 SDL_PIXELFORMAT_RGB332,
23 SDL_PIXELFORMAT_RGB444,
24 SDL_PIXELFORMAT_BGR444,
25 SDL_PIXELFORMAT_RGB555,
26 SDL_PIXELFORMAT_BGR555,
27 SDL_PIXELFORMAT_ARGB4444,
28 SDL_PIXELFORMAT_RGBA4444,
29 SDL_PIXELFORMAT_ABGR4444,
30 SDL_PIXELFORMAT_BGRA4444,
31 SDL_PIXELFORMAT_ARGB1555,
32 SDL_PIXELFORMAT_RGBA5551,
33 SDL_PIXELFORMAT_ABGR1555,
34 SDL_PIXELFORMAT_BGRA5551,
35 SDL_PIXELFORMAT_RGB565,
36 SDL_PIXELFORMAT_BGR565,
37 SDL_PIXELFORMAT_RGB24,
38 SDL_PIXELFORMAT_BGR24,
39 SDL_PIXELFORMAT_RGB888,
40 SDL_PIXELFORMAT_RGBX8888,
41 SDL_PIXELFORMAT_BGR888,
42 SDL_PIXELFORMAT_BGRX8888,
43 SDL_PIXELFORMAT_ARGB8888,
44 SDL_PIXELFORMAT_RGBA8888,
45 SDL_PIXELFORMAT_ABGR8888,
46 SDL_PIXELFORMAT_BGRA8888,
47 SDL_PIXELFORMAT_ARGB2101010
48 };
49 const char *_RGBPixelFormatsVerbose[] = {
50 "SDL_PIXELFORMAT_INDEX1LSB",
51 "SDL_PIXELFORMAT_INDEX1MSB",
52 "SDL_PIXELFORMAT_INDEX2LSB",
53 "SDL_PIXELFORMAT_INDEX2MSB",
54 "SDL_PIXELFORMAT_INDEX4LSB",
55 "SDL_PIXELFORMAT_INDEX4MSB",
56 "SDL_PIXELFORMAT_INDEX8",
57 "SDL_PIXELFORMAT_RGB332",
58 "SDL_PIXELFORMAT_RGB444",
59 "SDL_PIXELFORMAT_BGR444",
60 "SDL_PIXELFORMAT_RGB555",
61 "SDL_PIXELFORMAT_BGR555",
62 "SDL_PIXELFORMAT_ARGB4444",
63 "SDL_PIXELFORMAT_RGBA4444",
64 "SDL_PIXELFORMAT_ABGR4444",
65 "SDL_PIXELFORMAT_BGRA4444",
66 "SDL_PIXELFORMAT_ARGB1555",
67 "SDL_PIXELFORMAT_RGBA5551",
68 "SDL_PIXELFORMAT_ABGR1555",
69 "SDL_PIXELFORMAT_BGRA5551",
70 "SDL_PIXELFORMAT_RGB565",
71 "SDL_PIXELFORMAT_BGR565",
72 "SDL_PIXELFORMAT_RGB24",
73 "SDL_PIXELFORMAT_BGR24",
74 "SDL_PIXELFORMAT_RGB888",
75 "SDL_PIXELFORMAT_RGBX8888",
76 "SDL_PIXELFORMAT_BGR888",
77 "SDL_PIXELFORMAT_BGRX8888",
78 "SDL_PIXELFORMAT_ARGB8888",
79 "SDL_PIXELFORMAT_RGBA8888",
80 "SDL_PIXELFORMAT_ABGR8888",
81 "SDL_PIXELFORMAT_BGRA8888",
82 "SDL_PIXELFORMAT_ARGB2101010"
83 };
84
85 /* Definition of all Non-RGB formats used to test pixel conversions */
86 const int _numNonRGBPixelFormats = 7;
87 Uint32 _nonRGBPixelFormats[] = {
88 SDL_PIXELFORMAT_YV12,
89 SDL_PIXELFORMAT_IYUV,
90 SDL_PIXELFORMAT_YUY2,
91 SDL_PIXELFORMAT_UYVY,
92 SDL_PIXELFORMAT_YVYU,
93 SDL_PIXELFORMAT_NV12,
94 SDL_PIXELFORMAT_NV21
95 };
96 const char *_nonRGBPixelFormatsVerbose[] = {
97 "SDL_PIXELFORMAT_YV12",
98 "SDL_PIXELFORMAT_IYUV",
99 "SDL_PIXELFORMAT_YUY2",
100 "SDL_PIXELFORMAT_UYVY",
101 "SDL_PIXELFORMAT_YVYU",
102 "SDL_PIXELFORMAT_NV12",
103 "SDL_PIXELFORMAT_NV21"
104 };
105
106 /* Definition of some invalid formats for negative tests */
107 const int _numInvalidPixelFormats = 2;
108 Uint32 _invalidPixelFormats[] = {
109 SDL_DEFINE_PIXELFORMAT(SDL_PIXELTYPE_PACKED32, SDL_PACKEDORDER_ABGR, SDL_PACKEDLAYOUT_1010102 + 1, 32, 4),
110 SDL_DEFINE_PIXELFORMAT(SDL_PIXELTYPE_PACKED32, SDL_PACKEDORDER_ABGR, SDL_PACKEDLAYOUT_1010102 + 2, 32, 4)
111 };
112 const char *_invalidPixelFormatsVerbose[] = {
113 "SDL_PIXELFORMAT_UNKNOWN",
114 "SDL_PIXELFORMAT_UNKNOWN"
115 };
116
117 /* Test case functions */
118
119 /**
120 * @brief Call to SDL_AllocFormat and SDL_FreeFormat
121 *
122 * @sa http://wiki.libsdl.org/SDL_AllocFormat
123 * @sa http://wiki.libsdl.org/SDL_FreeFormat
124 */
125 int pixels_allocFreeFormat(void *arg)
126 {
127 const char *unknownFormat = "SDL_PIXELFORMAT_UNKNOWN";
128 const char *error;
129 int i;
130 Uint32 format;
131 Uint32 masks;
132 SDL_PixelFormat *result;
133
134 /* Blank/unknown format */
135 format = 0;
136 SDLTest_Log("RGB Format: %s (%" SDL_PRIu32 ")", unknownFormat, format);
137
138 /* Allocate format */
139 result = SDL_AllocFormat(format);
140 SDLTest_AssertPass("Call to SDL_AllocFormat()");
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 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_FreeFormat(result);
151 SDLTest_AssertPass("Call to SDL_FreeFormat()");
152 }
153
154 /* RGB formats */
155 for (i = 0; i < _numRGBPixelFormats; i++) {
156 format = _RGBPixelFormats[i];
157 SDLTest_Log("RGB Format: %s (%" SDL_PRIu32 ")", _RGBPixelFormatsVerbose[i], format);
158
159 /* Allocate format */
160 result = SDL_AllocFormat(format);
161 SDLTest_AssertPass("Call to SDL_AllocFormat()");
162 SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");
163 if (result != NULL) {
164 SDLTest_AssertCheck(result->format == format, "Verify value of result.format; expected: %" SDL_PRIu32 ", got %" SDL_PRIu32, format, result->format);
165 SDLTest_AssertCheck(result->BitsPerPixel > 0, "Verify value of result.BitsPerPixel; expected: >0, got %u", result->BitsPerPixel);
166 SDLTest_AssertCheck(result->BytesPerPixel > 0, "Verify value of result.BytesPerPixel; expected: >0, got %u", result->BytesPerPixel);
167 if (result->palette != NULL) {
168 masks = result->Rmask | result->Gmask | result->Bmask | result->Amask;
169 SDLTest_AssertCheck(masks > 0, "Verify value of result.[RGBA]mask combined; expected: >0, got %" SDL_PRIu32, masks);
170 }
171
172 /* Deallocate again */
173 SDL_FreeFormat(result);
174 SDLTest_AssertPass("Call to SDL_FreeFormat()");
175 }
176 }
177
178 #if 0 /* This succeeds for SDL3, but we don't expect SDL2 applications to call SDL_AllocFormat() for FOURCC formats directly */
179 /* Non-RGB formats */
180 for (i = 0; i < _numNonRGBPixelFormats; i++) {
181 format = _nonRGBPixelFormats[i];
182 SDLTest_Log("non-RGB Format: %s (%" SDL_PRIu32 ")", _nonRGBPixelFormatsVerbose[i], format);
183
184 /* Try to allocate format */
185 result = SDL_AllocFormat(format);
186 SDLTest_AssertPass("Call to SDL_AllocFormat()");
187 SDLTest_AssertCheck(result == NULL, "Verify result is NULL");
188 }
189 #endif
190
191 /* Negative cases */
192
193 /* Invalid Formats */
194 for (i = 0; i < _numInvalidPixelFormats; i++) {
195 SDL_ClearError();
196 SDLTest_AssertPass("Call to SDL_ClearError()");
197 format = _invalidPixelFormats[i];
198 result = SDL_AllocFormat(format);
199 SDLTest_AssertPass("Call to SDL_AllocFormat(%" SDL_PRIu32 ")", format);
200 SDLTest_AssertCheck(result == NULL, "Verify result is NULL");
201 error = SDL_GetError();
202 SDLTest_AssertPass("Call to SDL_GetError()");
203 SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
204 }
205
206 return TEST_COMPLETED;
207 }
208
209 /**
210 * @brief Call to SDL_GetPixelFormatName
211 *
212 * @sa http://wiki.libsdl.org/SDL_GetPixelFormatName
213 */
214 int pixels_getPixelFormatName(void *arg)
215 {
216 const char *unknownFormat = "SDL_PIXELFORMAT_UNKNOWN";
217 const char *error;
218 int i;
219 Uint32 format;
220 const char *result;
221
222 /* Blank/undefined format */
223 format = 0;
224 SDLTest_Log("RGB Format: %s (%" SDL_PRIu32 ")", unknownFormat, format);
225
226 /* Get name of format */
227 result = SDL_GetPixelFormatName(format);
228 SDLTest_AssertPass("Call to SDL_GetPixelFormatName()");
229 SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");
230 if (result != NULL) {
231 SDLTest_AssertCheck(result[0] != '\0', "Verify result is non-empty");
232 SDLTest_AssertCheck(SDL_strcmp(result, unknownFormat) == 0,
233 "Verify result text; expected: %s, got %s", unknownFormat, result);
234 }
235
236 /* RGB formats */
237 for (i = 0; i < _numRGBPixelFormats; i++) {
238 format = _RGBPixelFormats[i];
239 SDLTest_Log("RGB Format: %s (%" SDL_PRIu32 ")", _RGBPixelFormatsVerbose[i], format);
240
241 /* Get name of format */
242 result = SDL_GetPixelFormatName(format);
243 SDLTest_AssertPass("Call to SDL_GetPixelFormatName()");
244 SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");
245 if (result != NULL) {
246 SDLTest_AssertCheck(result[0] != '\0', "Verify result is non-empty");
247 SDLTest_AssertCheck(SDL_strcmp(result, _RGBPixelFormatsVerbose[i]) == 0,
248 "Verify result text; expected: %s, got %s", _RGBPixelFormatsVerbose[i], result);
249 }
250 }
251
252 /* Non-RGB formats */
253 for (i = 0; i < _numNonRGBPixelFormats; i++) {
254 format = _nonRGBPixelFormats[i];
255 SDLTest_Log("non-RGB Format: %s (%" SDL_PRIu32 ")", _nonRGBPixelFormatsVerbose[i], format);
256
257 /* Get name of format */
258 result = SDL_GetPixelFormatName(format);
259 SDLTest_AssertPass("Call to SDL_GetPixelFormatName()");
260 SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");
261 if (result != NULL) {
262 SDLTest_AssertCheck(result[0] != '\0', "Verify result is non-empty");
263 SDLTest_AssertCheck(SDL_strcmp(result, _nonRGBPixelFormatsVerbose[i]) == 0,
264 "Verify result text; expected: %s, got %s", _nonRGBPixelFormatsVerbose[i], result);
265 }
266 }
267
268 /* Negative cases */
269
270 /* Invalid Formats */
271 SDL_ClearError();
272 SDLTest_AssertPass("Call to SDL_ClearError()");
273 for (i = 0; i < _numInvalidPixelFormats; i++) {
274 format = _invalidPixelFormats[i];
275 result = SDL_GetPixelFormatName(format);
276 SDLTest_AssertPass("Call to SDL_GetPixelFormatName(%" SDL_PRIu32 ")", format);
277 SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");
278 if (result != NULL) {
279 SDLTest_AssertCheck(result[0] != '\0',
280 "Verify result is non-empty; got: %s", result);
281 SDLTest_AssertCheck(SDL_strcmp(result, _invalidPixelFormatsVerbose[i]) == 0,
282 "Validate name is UNKNOWN, expected: '%s', got: '%s'", _invalidPixelFormatsVerbose[i], result);
283 }
284 error = SDL_GetError();
285 SDLTest_AssertPass("Call to SDL_GetError()");
286 SDLTest_AssertCheck(error == NULL || error[0] == '\0', "Validate that error message is empty");
287 }
288
289 return TEST_COMPLETED;
290 }
291
292 /**
293 * @brief Call to SDL_AllocPalette and SDL_FreePalette
294 *
295 * @sa http://wiki.libsdl.org/SDL_AllocPalette
296 * @sa http://wiki.libsdl.org/SDL_FreePalette
297 */
298 int pixels_allocFreePalette(void *arg)
299 {
300 const char *expectedError1 = "Parameter 'ncolors' is invalid";
301 const char *error;
302 int variation;
303 int i;
304 int ncolors;
305 SDL_Palette *result;
306
307 /* Allocate palette */
308 for (variation = 1; variation <= 3; variation++) {
309 switch (variation) {
310 /* Just one color */
311 default:
312 case 1:
313 ncolors = 1;
314 break;
315 /* Two colors */
316 case 2:
317 ncolors = 2;
318 break;
319 /* More than two colors */
320 case 3:
321 ncolors = SDLTest_RandomIntegerInRange(8, 16);
322 break;
323 }
324
325 result = SDL_AllocPalette(ncolors);
326 SDLTest_AssertPass("Call to SDL_AllocPalette(%d)", ncolors);
327 SDLTest_AssertCheck(result != NULL, "Verify result is not NULL");
328 if (result != NULL) {
329 SDLTest_AssertCheck(result->ncolors == ncolors, "Verify value of result.ncolors; expected: %u, got %u", ncolors, result->ncolors);
330 if (result->ncolors > 0) {
331 SDLTest_AssertCheck(result->colors != NULL, "Verify value of result.colors is not NULL");
332 if (result->colors != NULL) {
333 for (i = 0; i < result->ncolors; i++) {
334 SDLTest_AssertCheck(result->colors[i].r == 255, "Verify value of result.colors[%d].r; expected: 255, got %u", i, result->colors[i].r);
335 SDLTest_AssertCheck(result->colors[i].g == 255, "Verify value of result.colors[%d].g; expected: 255, got %u", i, result->colors[i].g);
336 SDLTest_AssertCheck(result->colors[i].b == 255, "Verify value of result.colors[%d].b; expected: 255, got %u", i, result->colors[i].b);
337 }
338 }
339 }
340
341 /* Deallocate again */
342 SDL_FreePalette(result);
343 SDLTest_AssertPass("Call to SDL_FreePalette()");
344 }
345 }
346
347 /* Negative cases */
348
349 /* Invalid number of colors */
350 for (ncolors = 0; ncolors > -3; ncolors--) {
351 SDL_ClearError();
352 SDLTest_AssertPass("Call to SDL_ClearError()");
353 result = SDL_AllocPalette(ncolors);
354 SDLTest_AssertPass("Call to SDL_AllocPalette(%d)", ncolors);
355 SDLTest_AssertCheck(result == NULL, "Verify result is NULL");
356 error = SDL_GetError();
357 SDLTest_AssertPass("Call to SDL_GetError()");
358 SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
359 if (error != NULL) {
360 SDLTest_AssertCheck(SDL_strcmp(error, expectedError1) == 0,
361 "Validate error message, expected: '%s', got: '%s'", expectedError1, error);
362 }
363 }
364
365 return TEST_COMPLETED;
366 }
367
368 /**
369 * @brief Call to SDL_CalculateGammaRamp
370 *
371 * @sa http://wiki.libsdl.org/SDL_CalculateGammaRamp
372 */
373 int
374 pixels_calcGammaRamp(void *arg)
375 {
376 const char *expectedError1 = "Parameter 'gamma' is invalid";
377 const char *expectedError2 = "Parameter 'ramp' is invalid";
378 const char *error;
379 float gamma;
380 Uint16 *ramp;
381 int variation;
382 int i;
383 int changed;
384 Uint16 magic = 0xbeef;
385
386 /* Allocate temp ramp array and fill with some value */
387 ramp = (Uint16 *)SDL_malloc(256 * sizeof(Uint16));
388 SDLTest_AssertCheck(ramp != NULL, "Validate temp ramp array could be allocated");
389 if (ramp == NULL) return TEST_ABORTED;
390
391 /* Make call with different gamma values */
392 for (variation = 0; variation < 4; variation++) {
393 switch (variation) {
394 /* gamma = 0 all black */
395 default:
396 case 0:
397 gamma = 0.0f;
398 break;
399 /* gamma = 1 identity */
400 case 1:
401 gamma = 1.0f;
402 break;
403 /* gamma = [0.2,0.8] normal range */
404 case 2:
405 gamma = 0.2f + 0.8f * SDLTest_RandomUnitFloat();
406 break;
407 /* gamma = >1.1 non-standard range */
408 case 3:
409 gamma = 1.1f + SDLTest_RandomUnitFloat();
410 break;
411 }
412
413 /* Make call and check that values were updated */
414 for (i = 0; i < 256; i++) ramp[i] = magic;
415 SDL_CalculateGammaRamp(gamma, ramp);
416 SDLTest_AssertPass("Call to SDL_CalculateGammaRamp(%f)", gamma);
417 changed = 0;
418 for (i = 0; i < 256; i++) if (ramp[i] != magic) changed++;
419 SDLTest_AssertCheck(changed > 250, "Validate that ramp was calculated; expected: >250 values changed, got: %d values changed", changed);
420
421 /* Additional value checks for some cases */
422 i = SDLTest_RandomIntegerInRange(64,192);
423 switch (variation) {
424 case 0:
425 SDLTest_AssertCheck(ramp[i] == 0, "Validate value at position %d; expected: 0, got: %d", i, ramp[i]);
426 break;
427 case 1:
428 SDLTest_AssertCheck(ramp[i] == ((i << 8) | i), "Validate value at position %d; expected: %d, got: %d", i, (i << 8) | i, ramp[i]);
429 break;
430 case 2:
431 case 3:
432 SDLTest_AssertCheck(ramp[i] > 0, "Validate value at position %d; expected: >0, got: %d", i, ramp[i]);
433 break;
434 }
435 }
436
437 /* Negative cases */
438 SDL_ClearError();
439 SDLTest_AssertPass("Call to SDL_ClearError()");
440 gamma = -1;
441 for (i=0; i<256; i++) ramp[i] = magic;
442 SDL_CalculateGammaRamp(gamma, ramp);
443 SDLTest_AssertPass("Call to SDL_CalculateGammaRamp(%f)", gamma);
444 error = SDL_GetError();
445 SDLTest_AssertPass("Call to SDL_GetError()");
446 SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
447 if (error != NULL) {
448 SDLTest_AssertCheck(SDL_strcmp(error, expectedError1) == 0,
449 "Validate error message, expected: '%s', got: '%s'", expectedError1, error);
450 }
451 changed = 0;
452 for (i = 0; i < 256; i++) if (ramp[i] != magic) changed++;
453 SDLTest_AssertCheck(changed ==0, "Validate that ramp unchanged; expected: 0 values changed got: %d values changed", changed);
454
455 SDL_CalculateGammaRamp(0.5f, NULL);
456 SDLTest_AssertPass("Call to SDL_CalculateGammaRamp(0.5,NULL)");
457 error = SDL_GetError();
458 SDLTest_AssertPass("Call to SDL_GetError()");
459 SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL");
460 if (error != NULL) {
461 SDLTest_AssertCheck(SDL_strcmp(error, expectedError2) == 0,
462 "Validate error message, expected: '%s', got: '%s'", expectedError2, error);
463 }
464
465 /* Cleanup */
466 SDL_free(ramp);
467
468
469 return TEST_COMPLETED;
470 }
471
472 /* ================= Test References ================== */
473
474 /* Pixels test cases */
475 static const SDLTest_TestCaseReference pixelsTest1 = {
476 (SDLTest_TestCaseFp)pixels_allocFreeFormat, "pixels_allocFreeFormat", "Call to SDL_AllocFormat and SDL_FreeFormat", TEST_ENABLED
477 };
478
479 static const SDLTest_TestCaseReference pixelsTest2 = {
480 (SDLTest_TestCaseFp)pixels_allocFreePalette, "pixels_allocFreePalette", "Call to SDL_AllocPalette and SDL_FreePalette", TEST_ENABLED
481 };
482
483 static const SDLTest_TestCaseReference pixelsTest3 =
484 { (SDLTest_TestCaseFp)pixels_calcGammaRamp, "pixels_calcGammaRamp", "Call to SDL_CalculateGammaRamp", TEST_ENABLED };
485
486 static const SDLTest_TestCaseReference pixelsTest4 =
487 { (SDLTest_TestCaseFp)pixels_getPixelFormatName, "pixels_getPixelFormatName", "Call to SDL_GetPixelFormatName", TEST_ENABLED };
488
489 /* Sequence of Pixels test cases */
490 static const SDLTest_TestCaseReference *pixelsTests[] = {
491 &pixelsTest1, &pixelsTest2, &pixelsTest3, &pixelsTest4, NULL
492 };
493
494 /* Pixels test suite (global) */
495 SDLTest_TestSuiteReference pixelsTestSuite = {
496 "Pixels",
497 NULL,
498 pixelsTests,
499 NULL
500 };