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