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