Debian Codegraph @

test/testautomation_platform.c

raw
1 /**
2 * Original code: automated SDL platform test written by Edgar Simo "bobbens"
3 * Extended and updated by aschiffler at ferzkopp dot net
4 */
5
6 #include <stdio.h>
7
8 #include "SDL.h"
9 #include "SDL_test.h"
10
11 /* ================= Test Case Implementation ================== */
12
13 /* Helper functions */
14
15 /**
16 * @brief Compare sizes of types.
17 *
18 * @note Watcom C flags these as Warning 201: "Unreachable code" if you just
19 * compare them directly, so we push it through a function to keep the
20 * compiler quiet. --ryan.
21 */
22 static int _compareSizeOfType(size_t sizeoftype, size_t hardcodetype)
23 {
24 return sizeoftype != hardcodetype;
25 }
26
27 /* Test case functions */
28
29 /**
30 * @brief Tests type sizes.
31 */
32 int platform_testTypes(void *arg)
33 {
34 int ret;
35
36 ret = _compareSizeOfType(sizeof(Uint8), 1);
37 SDLTest_AssertCheck(ret == 0, "sizeof(Uint8) = %u, expected 1", (unsigned int)sizeof(Uint8));
38
39 ret = _compareSizeOfType(sizeof(Uint16), 2);
40 SDLTest_AssertCheck(ret == 0, "sizeof(Uint16) = %u, expected 2", (unsigned int)sizeof(Uint16));
41
42 ret = _compareSizeOfType(sizeof(Uint32), 4);
43 SDLTest_AssertCheck(ret == 0, "sizeof(Uint32) = %u, expected 4", (unsigned int)sizeof(Uint32));
44
45 ret = _compareSizeOfType(sizeof(Uint64), 8);
46 SDLTest_AssertCheck(ret == 0, "sizeof(Uint64) = %u, expected 8", (unsigned int)sizeof(Uint64));
47
48 return TEST_COMPLETED;
49 }
50
51 /**
52 * @brief Tests platform endianness and SDL_SwapXY functions.
53 */
54 int platform_testEndianessAndSwap(void *arg)
55 {
56 int real_byteorder;
57 int real_floatwordorder = 0;
58 Uint16 value = 0x1234;
59 Uint16 value16 = 0xCDAB;
60 Uint16 swapped16 = 0xABCD;
61 Uint32 value32 = 0xEFBEADDE;
62 Uint32 swapped32 = 0xDEADBEEF;
63
64 union
65 {
66 double d;
67 Uint32 ui32[2];
68 } value_double;
69
70 Uint64 value64, swapped64;
71 value64 = 0xEFBEADDE;
72 value64 <<= 32;
73 value64 |= 0xCDAB3412;
74 swapped64 = 0x1234ABCD;
75 swapped64 <<= 32;
76 swapped64 |= 0xDEADBEEF;
77 value_double.d = 3.141593;
78
79 if ((*((char *)&value) >> 4) == 0x1) {
80 real_byteorder = SDL_BIG_ENDIAN;
81 } else {
82 real_byteorder = SDL_LIL_ENDIAN;
83 }
84
85 /* Test endianness. */
86 SDLTest_AssertCheck(real_byteorder == SDL_BYTEORDER,
87 "Machine detected as %s endian, appears to be %s endian.",
88 (SDL_BYTEORDER == SDL_LIL_ENDIAN) ? "little" : "big",
89 (real_byteorder == SDL_LIL_ENDIAN) ? "little" : "big");
90
91 if (value_double.ui32[0] == 0x82c2bd7f && value_double.ui32[1] == 0x400921fb) {
92 real_floatwordorder = SDL_LIL_ENDIAN;
93 } else if (value_double.ui32[0] == 0x400921fb && value_double.ui32[1] == 0x82c2bd7f) {
94 real_floatwordorder = SDL_BIG_ENDIAN;
95 }
96
97 /* Test endianness. */
98 SDLTest_AssertCheck(real_floatwordorder == SDL_FLOATWORDORDER,
99 "Machine detected as having %s endian float word order, appears to be %s endian.",
100 (SDL_FLOATWORDORDER == SDL_LIL_ENDIAN) ? "little" : "big",
101 (real_floatwordorder == SDL_LIL_ENDIAN) ? "little" : (real_floatwordorder == SDL_BIG_ENDIAN) ? "big"
102 : "unknown");
103
104 /* Test 16 swap. */
105 SDLTest_AssertCheck(SDL_Swap16(value16) == swapped16,
106 "SDL_Swap16(): 16 bit swapped: 0x%X => 0x%X",
107 value16, SDL_Swap16(value16));
108
109 /* Test 32 swap. */
110 SDLTest_AssertCheck(SDL_Swap32(value32) == swapped32,
111 "SDL_Swap32(): 32 bit swapped: 0x%" SDL_PRIX32 " => 0x%" SDL_PRIX32,
112 value32, SDL_Swap32(value32));
113
114 /* Test 64 swap. */
115 SDLTest_AssertCheck(SDL_Swap64(value64) == swapped64,
116 "SDL_Swap64(): 64 bit swapped: 0x%" SDL_PRIX64 " => 0x%" SDL_PRIX64,
117 value64, SDL_Swap64(value64));
118
119 return TEST_COMPLETED;
120 }
121
122 /* !
123 * \brief Tests SDL_GetXYZ() functions
124 * \sa
125 * http://wiki.libsdl.org/SDL_GetPlatform
126 * http://wiki.libsdl.org/SDL_GetCPUCount
127 * http://wiki.libsdl.org/SDL_GetCPUCacheLineSize
128 * http://wiki.libsdl.org/SDL_GetRevision
129 * http://wiki.libsdl.org/SDL_GetRevisionNumber
130 */
131 int platform_testGetFunctions(void *arg)
132 {
133 char *platform;
134 char *revision;
135 int ret;
136 size_t len;
137
138 platform = (char *)SDL_GetPlatform();
139 SDLTest_AssertPass("SDL_GetPlatform()");
140 SDLTest_AssertCheck(platform != NULL, "SDL_GetPlatform() != NULL");
141 if (platform != NULL) {
142 len = SDL_strlen(platform);
143 SDLTest_AssertCheck(len > 0,
144 "SDL_GetPlatform(): expected non-empty platform, was platform: '%s', len: %i",
145 platform,
146 (int)len);
147 }
148
149 ret = SDL_GetCPUCount();
150 SDLTest_AssertPass("SDL_GetCPUCount()");
151 SDLTest_AssertCheck(ret > 0,
152 "SDL_GetCPUCount(): expected count > 0, was: %i",
153 ret);
154
155 ret = SDL_GetCPUCacheLineSize();
156 SDLTest_AssertPass("SDL_GetCPUCacheLineSize()");
157 SDLTest_AssertCheck(ret >= 0,
158 "SDL_GetCPUCacheLineSize(): expected size >= 0, was: %i",
159 ret);
160
161 revision = (char *)SDL_GetRevision();
162 SDLTest_AssertPass("SDL_GetRevision()");
163 SDLTest_AssertCheck(revision != NULL, "SDL_GetRevision() != NULL");
164
165 return TEST_COMPLETED;
166 }
167
168 /* !
169 * \brief Tests SDL_HasXYZ() functions
170 * \sa
171 * http://wiki.libsdl.org/SDL_Has3DNow
172 * http://wiki.libsdl.org/SDL_HasAltiVec
173 * http://wiki.libsdl.org/SDL_HasMMX
174 * http://wiki.libsdl.org/SDL_HasRDTSC
175 * http://wiki.libsdl.org/SDL_HasSSE
176 * http://wiki.libsdl.org/SDL_HasSSE2
177 * http://wiki.libsdl.org/SDL_HasSSE3
178 * http://wiki.libsdl.org/SDL_HasSSE41
179 * http://wiki.libsdl.org/SDL_HasSSE42
180 * http://wiki.libsdl.org/SDL_HasAVX
181 */
182 int platform_testHasFunctions(void *arg)
183 {
184 /* TODO: independently determine and compare values as well */
185
186 SDL_HasRDTSC();
187 SDLTest_AssertPass("SDL_HasRDTSC()");
188
189 SDL_HasAltiVec();
190 SDLTest_AssertPass("SDL_HasAltiVec()");
191
192 SDL_HasMMX();
193 SDLTest_AssertPass("SDL_HasMMX()");
194
195 SDL_Has3DNow();
196 SDLTest_AssertPass("SDL_Has3DNow()");
197
198 SDL_HasSSE();
199 SDLTest_AssertPass("SDL_HasSSE()");
200
201 SDL_HasSSE2();
202 SDLTest_AssertPass("SDL_HasSSE2()");
203
204 SDL_HasSSE3();
205 SDLTest_AssertPass("SDL_HasSSE3()");
206
207 SDL_HasSSE41();
208 SDLTest_AssertPass("SDL_HasSSE41()");
209
210 SDL_HasSSE42();
211 SDLTest_AssertPass("SDL_HasSSE42()");
212
213 SDL_HasAVX();
214 SDLTest_AssertPass("SDL_HasAVX()");
215
216 return TEST_COMPLETED;
217 }
218
219 /* !
220 * \brief Tests SDL_GetVersion
221 * \sa
222 * http://wiki.libsdl.org/SDL_GetVersion
223 */
224 int platform_testGetVersion(void *arg)
225 {
226 SDL_version linked;
227 int major = SDL_MAJOR_VERSION;
228 int minor = SDL_MINOR_VERSION;
229
230 SDL_GetVersion(&linked);
231 SDLTest_AssertCheck(linked.major >= major,
232 "SDL_GetVersion(): returned major %i (>= %i)",
233 linked.major,
234 major);
235 SDLTest_AssertCheck(linked.minor >= minor,
236 "SDL_GetVersion(): returned minor %i (>= %i)",
237 linked.minor,
238 minor);
239
240 return TEST_COMPLETED;
241 }
242
243 /* !
244 * \brief Tests SDL_VERSION macro
245 */
246 int platform_testSDLVersion(void *arg)
247 {
248 SDL_version compiled;
249 int major = SDL_MAJOR_VERSION;
250 int minor = SDL_MINOR_VERSION;
251
252 SDL_VERSION(&compiled);
253 SDLTest_AssertCheck(compiled.major >= major,
254 "SDL_VERSION() returned major %i (>= %i)",
255 compiled.major,
256 major);
257 SDLTest_AssertCheck(compiled.minor >= minor,
258 "SDL_VERSION() returned minor %i (>= %i)",
259 compiled.minor,
260 minor);
261
262 return TEST_COMPLETED;
263 }
264
265 /* !
266 * \brief Tests default SDL_Init
267 */
268 int platform_testDefaultInit(void *arg)
269 {
270 int ret;
271 int subsystem;
272
273 subsystem = SDL_WasInit(SDL_INIT_EVERYTHING);
274 SDLTest_AssertCheck(subsystem != 0,
275 "SDL_WasInit(0): returned %i, expected != 0",
276 subsystem);
277
278 ret = SDL_Init(SDL_WasInit(SDL_INIT_EVERYTHING));
279 SDLTest_AssertCheck(ret == 0,
280 "SDL_Init(0): returned %i, expected 0, error: %s",
281 ret,
282 SDL_GetError());
283
284 return TEST_COMPLETED;
285 }
286
287 /* !
288 * \brief Tests SDL_Get/Set/ClearError
289 * \sa
290 * http://wiki.libsdl.org/SDL_GetError
291 * http://wiki.libsdl.org/SDL_SetError
292 * http://wiki.libsdl.org/SDL_ClearError
293 */
294 int platform_testGetSetClearError(void *arg)
295 {
296 int result;
297 const char *testError = "Testing";
298 char *lastError;
299 size_t len;
300
301 SDL_ClearError();
302 SDLTest_AssertPass("SDL_ClearError()");
303
304 lastError = (char *)SDL_GetError();
305 SDLTest_AssertPass("SDL_GetError()");
306 SDLTest_AssertCheck(lastError != NULL,
307 "SDL_GetError() != NULL");
308 if (lastError != NULL) {
309 len = SDL_strlen(lastError);
310 SDLTest_AssertCheck(len == 0,
311 "SDL_GetError(): no message expected, len: %i", (int)len);
312 }
313
314 result = SDL_SetError("%s", testError);
315 SDLTest_AssertPass("SDL_SetError()");
316 SDLTest_AssertCheck(result == -1, "SDL_SetError: expected -1, got: %i", result);
317 lastError = (char *)SDL_GetError();
318 SDLTest_AssertCheck(lastError != NULL,
319 "SDL_GetError() != NULL");
320 if (lastError != NULL) {
321 len = SDL_strlen(lastError);
322 SDLTest_AssertCheck(len == SDL_strlen(testError),
323 "SDL_GetError(): expected message len %i, was len: %i",
324 (int)SDL_strlen(testError),
325 (int)len);
326 SDLTest_AssertCheck(SDL_strcmp(lastError, testError) == 0,
327 "SDL_GetError(): expected message %s, was message: %s",
328 testError,
329 lastError);
330 }
331
332 /* Clean up */
333 SDL_ClearError();
334 SDLTest_AssertPass("SDL_ClearError()");
335
336 return TEST_COMPLETED;
337 }
338
339 /* !
340 * \brief Tests SDL_SetError with empty input
341 * \sa
342 * http://wiki.libsdl.org/SDL_SetError
343 */
344 int platform_testSetErrorEmptyInput(void *arg)
345 {
346 int result;
347 const char *testError = "";
348 char *lastError;
349 size_t len;
350
351 result = SDL_SetError("%s", testError);
352 SDLTest_AssertPass("SDL_SetError()");
353 SDLTest_AssertCheck(result == -1, "SDL_SetError: expected -1, got: %i", result);
354 lastError = (char *)SDL_GetError();
355 SDLTest_AssertCheck(lastError != NULL,
356 "SDL_GetError() != NULL");
357 if (lastError != NULL) {
358 len = SDL_strlen(lastError);
359 SDLTest_AssertCheck(len == SDL_strlen(testError),
360 "SDL_GetError(): expected message len %i, was len: %i",
361 (int)SDL_strlen(testError),
362 (int)len);
363 SDLTest_AssertCheck(SDL_strcmp(lastError, testError) == 0,
364 "SDL_GetError(): expected message '%s', was message: '%s'",
365 testError,
366 lastError);
367 }
368
369 /* Clean up */
370 SDL_ClearError();
371 SDLTest_AssertPass("SDL_ClearError()");
372
373 return TEST_COMPLETED;
374 }
375
376 #if defined(HAVE_WFORMAT_OVERFLOW)
377 #pragma GCC diagnostic push
378 #pragma GCC diagnostic ignored "-Wformat-overflow"
379 #endif
380
381 /* !
382 * \brief Tests SDL_SetError with invalid input
383 * \sa
384 * http://wiki.libsdl.org/SDL_SetError
385 */
386 int platform_testSetErrorInvalidInput(void *arg)
387 {
388 int result;
389 const char *invalidError = "";
390 const char *probeError = "Testing";
391 char *lastError;
392 size_t len;
393
394 /* Reset */
395 SDL_ClearError();
396 SDLTest_AssertPass("SDL_ClearError()");
397
398 /* Check for no-op */
399 result = SDL_SetError("%s", invalidError);
400 SDLTest_AssertPass("SDL_SetError()");
401 SDLTest_AssertCheck(result == -1, "SDL_SetError: expected -1, got: %i", result);
402 lastError = (char *)SDL_GetError();
403 SDLTest_AssertCheck(lastError != NULL,
404 "SDL_GetError() != NULL");
405 if (lastError != NULL) {
406 len = SDL_strlen(lastError);
407 SDLTest_AssertCheck(len == 0 || SDL_strcmp(lastError, "(null)") == 0,
408 "SDL_GetError(): expected message len 0, was len: %i",
409 (int)len);
410 }
411
412 /* Set */
413 result = SDL_SetError("%s", probeError);
414 SDLTest_AssertPass("SDL_SetError('%s')", probeError);
415 SDLTest_AssertCheck(result == -1, "SDL_SetError: expected -1, got: %i", result);
416
417 /* Check for no-op */
418 result = SDL_SetError("%s", invalidError);
419 SDLTest_AssertPass("SDL_SetError(NULL)");
420 SDLTest_AssertCheck(result == -1, "SDL_SetError: expected -1, got: %i", result);
421 lastError = (char *)SDL_GetError();
422 SDLTest_AssertCheck(lastError != NULL,
423 "SDL_GetError() != NULL");
424 if (lastError != NULL) {
425 len = SDL_strlen(lastError);
426 SDLTest_AssertCheck(len == 0 || SDL_strcmp(lastError, "(null)") == 0,
427 "SDL_GetError(): expected message len 0, was len: %i",
428 (int)len);
429 }
430
431 /* Reset */
432 SDL_ClearError();
433 SDLTest_AssertPass("SDL_ClearError()");
434
435 /* Set and check */
436 result = SDL_SetError("%s", probeError);
437 SDLTest_AssertPass("SDL_SetError()");
438 SDLTest_AssertCheck(result == -1, "SDL_SetError: expected -1, got: %i", result);
439 lastError = (char *)SDL_GetError();
440 SDLTest_AssertCheck(lastError != NULL,
441 "SDL_GetError() != NULL");
442 if (lastError != NULL) {
443 len = SDL_strlen(lastError);
444 SDLTest_AssertCheck(len == SDL_strlen(probeError),
445 "SDL_GetError(): expected message len %i, was len: %i",
446 (int)SDL_strlen(probeError),
447 (int)len);
448 SDLTest_AssertCheck(SDL_strcmp(lastError, probeError) == 0,
449 "SDL_GetError(): expected message '%s', was message: '%s'",
450 probeError,
451 lastError);
452 }
453
454 /* Clean up */
455 SDL_ClearError();
456 SDLTest_AssertPass("SDL_ClearError()");
457
458 return TEST_COMPLETED;
459 }
460
461 #if defined(HAVE_WFORMAT_OVERFLOW)
462 #pragma GCC diagnostic pop
463 #endif
464
465 /* !
466 * \brief Tests SDL_GetPowerInfo
467 * \sa
468 * http://wiki.libsdl.org/SDL_GetPowerInfo
469 */
470 int platform_testGetPowerInfo(void *arg)
471 {
472 SDL_PowerState state;
473 SDL_PowerState stateAgain;
474 int secs;
475 int secsAgain;
476 int pct;
477 int pctAgain;
478
479 state = SDL_GetPowerInfo(&secs, &pct);
480 SDLTest_AssertPass("SDL_GetPowerInfo()");
481 SDLTest_AssertCheck(
482 state == SDL_POWERSTATE_UNKNOWN ||
483 state == SDL_POWERSTATE_ON_BATTERY ||
484 state == SDL_POWERSTATE_NO_BATTERY ||
485 state == SDL_POWERSTATE_CHARGING ||
486 state == SDL_POWERSTATE_CHARGED,
487 "SDL_GetPowerInfo(): state %i is one of the expected values",
488 (int)state);
489
490 if (state == SDL_POWERSTATE_ON_BATTERY) {
491 SDLTest_AssertCheck(
492 secs >= 0,
493 "SDL_GetPowerInfo(): on battery, secs >= 0, was: %i",
494 secs);
495 SDLTest_AssertCheck(
496 (pct >= 0) && (pct <= 100),
497 "SDL_GetPowerInfo(): on battery, pct=[0,100], was: %i",
498 pct);
499 }
500
501 if (state == SDL_POWERSTATE_UNKNOWN ||
502 state == SDL_POWERSTATE_NO_BATTERY) {
503 SDLTest_AssertCheck(
504 secs == -1,
505 "SDL_GetPowerInfo(): no battery, secs == -1, was: %i",
506 secs);
507 SDLTest_AssertCheck(
508 pct == -1,
509 "SDL_GetPowerInfo(): no battery, pct == -1, was: %i",
510 pct);
511 }
512
513 /* Partial return value variations */
514 stateAgain = SDL_GetPowerInfo(&secsAgain, NULL);
515 SDLTest_AssertCheck(
516 state == stateAgain,
517 "State %i returned when only 'secs' requested",
518 stateAgain);
519 SDLTest_AssertCheck(
520 secs == secsAgain,
521 "Value %i matches when only 'secs' requested",
522 secsAgain);
523 stateAgain = SDL_GetPowerInfo(NULL, &pctAgain);
524 SDLTest_AssertCheck(
525 state == stateAgain,
526 "State %i returned when only 'pct' requested",
527 stateAgain);
528 SDLTest_AssertCheck(
529 pct == pctAgain,
530 "Value %i matches when only 'pct' requested",
531 pctAgain);
532 stateAgain = SDL_GetPowerInfo(NULL, NULL);
533 SDLTest_AssertCheck(
534 state == stateAgain,
535 "State %i returned when no value requested",
536 stateAgain);
537
538 return TEST_COMPLETED;
539 }
540
541 /* ================= Test References ================== */
542
543 /* Platform test cases */
544 static const SDLTest_TestCaseReference platformTest1 = {
545 (SDLTest_TestCaseFp)platform_testTypes, "platform_testTypes", "Tests predefined types", TEST_ENABLED
546 };
547
548 static const SDLTest_TestCaseReference platformTest2 = {
549 (SDLTest_TestCaseFp)platform_testEndianessAndSwap, "platform_testEndianessAndSwap", "Tests endianness and swap functions", TEST_ENABLED
550 };
551
552 static const SDLTest_TestCaseReference platformTest3 = {
553 (SDLTest_TestCaseFp)platform_testGetFunctions, "platform_testGetFunctions", "Tests various SDL_GetXYZ functions", TEST_ENABLED
554 };
555
556 static const SDLTest_TestCaseReference platformTest4 = {
557 (SDLTest_TestCaseFp)platform_testHasFunctions, "platform_testHasFunctions", "Tests various SDL_HasXYZ functions", TEST_ENABLED
558 };
559
560 static const SDLTest_TestCaseReference platformTest5 = {
561 (SDLTest_TestCaseFp)platform_testGetVersion, "platform_testGetVersion", "Tests SDL_GetVersion function", TEST_ENABLED
562 };
563
564 static const SDLTest_TestCaseReference platformTest6 = {
565 (SDLTest_TestCaseFp)platform_testSDLVersion, "platform_testSDLVersion", "Tests SDL_VERSION macro", TEST_ENABLED
566 };
567
568 static const SDLTest_TestCaseReference platformTest7 = {
569 (SDLTest_TestCaseFp)platform_testDefaultInit, "platform_testDefaultInit", "Tests default SDL_Init", TEST_ENABLED
570 };
571
572 static const SDLTest_TestCaseReference platformTest8 = {
573 (SDLTest_TestCaseFp)platform_testGetSetClearError, "platform_testGetSetClearError", "Tests SDL_Get/Set/ClearError", TEST_ENABLED
574 };
575
576 static const SDLTest_TestCaseReference platformTest9 = {
577 (SDLTest_TestCaseFp)platform_testSetErrorEmptyInput, "platform_testSetErrorEmptyInput", "Tests SDL_SetError with empty input", TEST_ENABLED
578 };
579
580 static const SDLTest_TestCaseReference platformTest10 = {
581 (SDLTest_TestCaseFp)platform_testSetErrorInvalidInput, "platform_testSetErrorInvalidInput", "Tests SDL_SetError with invalid input", TEST_ENABLED
582 };
583
584 static const SDLTest_TestCaseReference platformTest11 = {
585 (SDLTest_TestCaseFp)platform_testGetPowerInfo, "platform_testGetPowerInfo", "Tests SDL_GetPowerInfo function", TEST_ENABLED
586 };
587
588 /* Sequence of Platform test cases */
589 static const SDLTest_TestCaseReference *platformTests[] = {
590 &platformTest1,
591 &platformTest2,
592 &platformTest3,
593 &platformTest4,
594 &platformTest5,
595 &platformTest6,
596 &platformTest7,
597 &platformTest8,
598 &platformTest9,
599 &platformTest10,
600 &platformTest11,
601 NULL
602 };
603
604 /* Platform test suite (global) */
605 SDLTest_TestSuiteReference platformTestSuite = {
606 "Platform",
607 NULL,
608 platformTests,
609 NULL
610 };