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