testautomation_platform.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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. #include <stdio.h>
  6. #include "SDL.h"
  7. #include "SDL_test.h"
  8. /* ================= Test Case Implementation ================== */
  9. /* Helper functions */
  10. /**
  11. * @brief Compare sizes of types.
  12. *
  13. * @note Watcom C flags these as Warning 201: "Unreachable code" if you just
  14. * compare them directly, so we push it through a function to keep the
  15. * compiler quiet. --ryan.
  16. */
  17. static int _compareSizeOfType( size_t sizeoftype, size_t hardcodetype )
  18. {
  19. return sizeoftype != hardcodetype;
  20. }
  21. /* Test case functions */
  22. /**
  23. * @brief Tests type sizes.
  24. */
  25. int platform_testTypes(void *arg)
  26. {
  27. int ret;
  28. ret = _compareSizeOfType( sizeof(Uint8), 1 );
  29. SDLTest_AssertCheck( ret == 0, "sizeof(Uint8) = %lu, expected 1", sizeof(Uint8) );
  30. ret = _compareSizeOfType( sizeof(Uint16), 2 );
  31. SDLTest_AssertCheck( ret == 0, "sizeof(Uint16) = %lu, expected 2", sizeof(Uint16) );
  32. ret = _compareSizeOfType( sizeof(Uint32), 4 );
  33. SDLTest_AssertCheck( ret == 0, "sizeof(Uint32) = %lu, expected 4", sizeof(Uint32) );
  34. ret = _compareSizeOfType( sizeof(Uint64), 8 );
  35. SDLTest_AssertCheck( ret == 0, "sizeof(Uint64) = %lu, expected 8", sizeof(Uint64) );
  36. return TEST_COMPLETED;
  37. }
  38. /**
  39. * @brief Tests platform endianness and SDL_SwapXY functions.
  40. */
  41. int platform_testEndianessAndSwap(void *arg)
  42. {
  43. int real_byteorder;
  44. Uint16 value = 0x1234;
  45. Uint16 value16 = 0xCDAB;
  46. Uint16 swapped16 = 0xABCD;
  47. Uint32 value32 = 0xEFBEADDE;
  48. Uint32 swapped32 = 0xDEADBEEF;
  49. Uint64 value64, swapped64;
  50. value64 = 0xEFBEADDE;
  51. value64 <<= 32;
  52. value64 |= 0xCDAB3412;
  53. swapped64 = 0x1234ABCD;
  54. swapped64 <<= 32;
  55. swapped64 |= 0xDEADBEEF;
  56. if ((*((char *) &value) >> 4) == 0x1) {
  57. real_byteorder = SDL_BIG_ENDIAN;
  58. } else {
  59. real_byteorder = SDL_LIL_ENDIAN;
  60. }
  61. /* Test endianness. */
  62. SDLTest_AssertCheck( real_byteorder == SDL_BYTEORDER,
  63. "Machine detected as %s endian, appears to be %s endian.",
  64. (SDL_BYTEORDER == SDL_LIL_ENDIAN) ? "little" : "big",
  65. (real_byteorder == SDL_LIL_ENDIAN) ? "little" : "big" );
  66. /* Test 16 swap. */
  67. SDLTest_AssertCheck( SDL_Swap16(value16) == swapped16,
  68. "SDL_Swap16(): 16 bit swapped: 0x%X => 0x%X",
  69. value16, SDL_Swap16(value16) );
  70. /* Test 32 swap. */
  71. SDLTest_AssertCheck( SDL_Swap32(value32) == swapped32,
  72. "SDL_Swap32(): 32 bit swapped: 0x%X => 0x%X",
  73. value32, SDL_Swap32(value32) );
  74. /* Test 64 swap. */
  75. SDLTest_AssertCheck( SDL_Swap64(value64) == swapped64,
  76. #ifdef _MSC_VER
  77. "SDL_Swap64(): 64 bit swapped: 0x%I64X => 0x%I64X",
  78. #else
  79. "SDL_Swap64(): 64 bit swapped: 0x%llX => 0x%llX",
  80. #endif
  81. value64, SDL_Swap64(value64) );
  82. return TEST_COMPLETED;
  83. }
  84. /* !
  85. * \brief Tests SDL_GetXYZ() functions
  86. * \sa
  87. * http://wiki.libsdl.org/moin.cgi/SDL_GetPlatform
  88. * http://wiki.libsdl.org/moin.cgi/SDL_GetCPUCount
  89. * http://wiki.libsdl.org/moin.cgi/SDL_GetCPUCacheLineSize
  90. * http://wiki.libsdl.org/moin.cgi/SDL_GetRevision
  91. * http://wiki.libsdl.org/moin.cgi/SDL_GetRevisionNumber
  92. */
  93. int platform_testGetFunctions (void *arg)
  94. {
  95. char *platform;
  96. char *revision;
  97. int ret;
  98. int len;
  99. platform = (char *)SDL_GetPlatform();
  100. SDLTest_AssertPass("SDL_GetPlatform()");
  101. SDLTest_AssertCheck(platform != NULL, "SDL_GetPlatform() != NULL");
  102. if (platform != NULL) {
  103. len = SDL_strlen(platform);
  104. SDLTest_AssertCheck(len > 0,
  105. "SDL_GetPlatform(): expected non-empty platform, was platform: '%s', len: %i",
  106. platform,
  107. len);
  108. }
  109. ret = SDL_GetCPUCount();
  110. SDLTest_AssertPass("SDL_GetCPUCount()");
  111. SDLTest_AssertCheck(ret > 0,
  112. "SDL_GetCPUCount(): expected count > 0, was: %i",
  113. ret);
  114. ret = SDL_GetCPUCacheLineSize();
  115. SDLTest_AssertPass("SDL_GetCPUCacheLineSize()");
  116. SDLTest_AssertCheck(ret >= 0,
  117. "SDL_GetCPUCacheLineSize(): expected size >= 0, was: %i",
  118. ret);
  119. revision = (char *)SDL_GetRevision();
  120. SDLTest_AssertPass("SDL_GetRevision()");
  121. SDLTest_AssertCheck(revision != NULL, "SDL_GetRevision() != NULL");
  122. ret = SDL_GetRevisionNumber();
  123. SDLTest_AssertPass("SDL_GetRevisionNumber()");
  124. return TEST_COMPLETED;
  125. }
  126. /* !
  127. * \brief Tests SDL_HasXYZ() functions
  128. * \sa
  129. * http://wiki.libsdl.org/moin.cgi/SDL_Has3DNow
  130. * http://wiki.libsdl.org/moin.cgi/SDL_HasAltiVec
  131. * http://wiki.libsdl.org/moin.cgi/SDL_HasMMX
  132. * http://wiki.libsdl.org/moin.cgi/SDL_HasRDTSC
  133. * http://wiki.libsdl.org/moin.cgi/SDL_HasSSE
  134. * http://wiki.libsdl.org/moin.cgi/SDL_HasSSE2
  135. * http://wiki.libsdl.org/moin.cgi/SDL_HasSSE3
  136. * http://wiki.libsdl.org/moin.cgi/SDL_HasSSE41
  137. * http://wiki.libsdl.org/moin.cgi/SDL_HasSSE42
  138. */
  139. int platform_testHasFunctions (void *arg)
  140. {
  141. int ret;
  142. /* TODO: independently determine and compare values as well */
  143. ret = SDL_HasRDTSC();
  144. SDLTest_AssertPass("SDL_HasRDTSC()");
  145. ret = SDL_HasAltiVec();
  146. SDLTest_AssertPass("SDL_HasAltiVec()");
  147. ret = SDL_HasMMX();
  148. SDLTest_AssertPass("SDL_HasMMX()");
  149. ret = SDL_Has3DNow();
  150. SDLTest_AssertPass("SDL_Has3DNow()");
  151. ret = SDL_HasSSE();
  152. SDLTest_AssertPass("SDL_HasSSE()");
  153. ret = SDL_HasSSE2();
  154. SDLTest_AssertPass("SDL_HasSSE2()");
  155. ret = SDL_HasSSE3();
  156. SDLTest_AssertPass("SDL_HasSSE3()");
  157. ret = SDL_HasSSE41();
  158. SDLTest_AssertPass("SDL_HasSSE41()");
  159. ret = SDL_HasSSE42();
  160. SDLTest_AssertPass("SDL_HasSSE42()");
  161. return TEST_COMPLETED;
  162. }
  163. /* !
  164. * \brief Tests SDL_GetVersion
  165. * \sa
  166. * http://wiki.libsdl.org/moin.cgi/SDL_GetVersion
  167. */
  168. int platform_testGetVersion(void *arg)
  169. {
  170. SDL_version linked;
  171. int major = SDL_MAJOR_VERSION;
  172. int minor = SDL_MINOR_VERSION;
  173. SDL_GetVersion(&linked);
  174. SDLTest_AssertCheck( linked.major >= major,
  175. "SDL_GetVersion(): returned major %i (>= %i)",
  176. linked.major,
  177. major);
  178. SDLTest_AssertCheck( linked.minor >= minor,
  179. "SDL_GetVersion(): returned minor %i (>= %i)",
  180. linked.minor,
  181. minor);
  182. return TEST_COMPLETED;
  183. }
  184. /* !
  185. * \brief Tests SDL_VERSION macro
  186. */
  187. int platform_testSDLVersion(void *arg)
  188. {
  189. SDL_version compiled;
  190. int major = SDL_MAJOR_VERSION;
  191. int minor = SDL_MINOR_VERSION;
  192. SDL_VERSION(&compiled);
  193. SDLTest_AssertCheck( compiled.major >= major,
  194. "SDL_VERSION() returned major %i (>= %i)",
  195. compiled.major,
  196. major);
  197. SDLTest_AssertCheck( compiled.minor >= minor,
  198. "SDL_VERSION() returned minor %i (>= %i)",
  199. compiled.minor,
  200. minor);
  201. return TEST_COMPLETED;
  202. }
  203. /* !
  204. * \brief Tests default SDL_Init
  205. */
  206. int platform_testDefaultInit(void *arg)
  207. {
  208. int ret;
  209. int subsystem;
  210. subsystem = SDL_WasInit(SDL_INIT_EVERYTHING);
  211. SDLTest_AssertCheck( subsystem != 0,
  212. "SDL_WasInit(0): returned %i, expected != 0",
  213. subsystem);
  214. ret = SDL_Init(SDL_WasInit(SDL_INIT_EVERYTHING));
  215. SDLTest_AssertCheck( ret == 0,
  216. "SDL_Init(0): returned %i, expected 0, error: %s",
  217. ret,
  218. SDL_GetError());
  219. return TEST_COMPLETED;
  220. }
  221. /* !
  222. * \brief Tests SDL_Get/Set/ClearError
  223. * \sa
  224. * http://wiki.libsdl.org/moin.cgi/SDL_GetError
  225. * http://wiki.libsdl.org/moin.cgi/SDL_SetError
  226. * http://wiki.libsdl.org/moin.cgi/SDL_ClearError
  227. */
  228. int platform_testGetSetClearError(void *arg)
  229. {
  230. const char *testError = "Testing";
  231. char *lastError;
  232. int len;
  233. SDL_ClearError();
  234. SDLTest_AssertPass("SDL_ClearError()");
  235. lastError = (char *)SDL_GetError();
  236. SDLTest_AssertPass("SDL_GetError()");
  237. SDLTest_AssertCheck(lastError != NULL,
  238. "SDL_GetError() != NULL");
  239. if (lastError != NULL)
  240. {
  241. len = SDL_strlen(lastError);
  242. SDLTest_AssertCheck(len == 0,
  243. "SDL_GetError(): no message expected, len: %i", len);
  244. }
  245. SDL_SetError("%s", testError);
  246. SDLTest_AssertPass("SDL_SetError()");
  247. lastError = (char *)SDL_GetError();
  248. SDLTest_AssertCheck(lastError != NULL,
  249. "SDL_GetError() != NULL");
  250. if (lastError != NULL)
  251. {
  252. len = SDL_strlen(lastError);
  253. SDLTest_AssertCheck(len == SDL_strlen(testError),
  254. "SDL_GetError(): expected message len %i, was len: %i",
  255. SDL_strlen(testError),
  256. len);
  257. SDLTest_AssertCheck(SDL_strcmp(lastError, testError) == 0,
  258. "SDL_GetError(): expected message %s, was message: %s",
  259. testError,
  260. lastError);
  261. }
  262. /* Clean up */
  263. SDL_ClearError();
  264. SDLTest_AssertPass("SDL_ClearError()");
  265. return TEST_COMPLETED;
  266. }
  267. /* !
  268. * \brief Tests SDL_SetError with empty input
  269. * \sa
  270. * http://wiki.libsdl.org/moin.cgi/SDL_SetError
  271. */
  272. int platform_testSetErrorEmptyInput(void *arg)
  273. {
  274. const char *testError = "";
  275. char *lastError;
  276. int len;
  277. SDL_SetError("%s", testError);
  278. SDLTest_AssertPass("SDL_SetError()");
  279. lastError = (char *)SDL_GetError();
  280. SDLTest_AssertCheck(lastError != NULL,
  281. "SDL_GetError() != NULL");
  282. if (lastError != NULL)
  283. {
  284. len = SDL_strlen(lastError);
  285. SDLTest_AssertCheck(len == SDL_strlen(testError),
  286. "SDL_GetError(): expected message len %i, was len: %i",
  287. SDL_strlen(testError),
  288. len);
  289. SDLTest_AssertCheck(SDL_strcmp(lastError, testError) == 0,
  290. "SDL_GetError(): expected message '%s', was message: '%s'",
  291. testError,
  292. lastError);
  293. }
  294. /* Clean up */
  295. SDL_ClearError();
  296. SDLTest_AssertPass("SDL_ClearError()");
  297. return TEST_COMPLETED;
  298. }
  299. /* !
  300. * \brief Tests SDL_SetError with invalid input
  301. * \sa
  302. * http://wiki.libsdl.org/moin.cgi/SDL_SetError
  303. */
  304. int platform_testSetErrorInvalidInput(void *arg)
  305. {
  306. const char *testError = NULL;
  307. const char *probeError = "Testing";
  308. char *lastError;
  309. int len;
  310. /* Reset */
  311. SDL_ClearError();
  312. SDLTest_AssertPass("SDL_ClearError()");
  313. /* Check for no-op */
  314. SDL_SetError(testError);
  315. SDLTest_AssertPass("SDL_SetError()");
  316. lastError = (char *)SDL_GetError();
  317. SDLTest_AssertCheck(lastError != NULL,
  318. "SDL_GetError() != NULL");
  319. if (lastError != NULL)
  320. {
  321. len = SDL_strlen(lastError);
  322. SDLTest_AssertCheck(len == 0,
  323. "SDL_GetError(): expected message len 0, was len: %i",
  324. 0,
  325. len);
  326. SDLTest_AssertCheck(SDL_strcmp(lastError, "") == 0,
  327. "SDL_GetError(): expected message '', was message: '%s'",
  328. lastError);
  329. }
  330. /* Set */
  331. SDL_SetError(probeError);
  332. SDLTest_AssertPass("SDL_SetError()");
  333. /* Check for no-op */
  334. SDL_SetError(testError);
  335. SDLTest_AssertPass("SDL_SetError()");
  336. lastError = (char *)SDL_GetError();
  337. SDLTest_AssertCheck(lastError != NULL,
  338. "SDL_GetError() != NULL");
  339. if (lastError != NULL)
  340. {
  341. len = SDL_strlen(lastError);
  342. SDLTest_AssertCheck(len == SDL_strlen(probeError),
  343. "SDL_GetError(): expected message len %i, was len: %i",
  344. SDL_strlen(probeError),
  345. len);
  346. SDLTest_AssertCheck(SDL_strcmp(lastError, probeError) == 0,
  347. "SDL_GetError(): expected message '%s', was message: '%s'",
  348. probeError,
  349. lastError);
  350. }
  351. /* Clean up */
  352. SDL_ClearError();
  353. SDLTest_AssertPass("SDL_ClearError()");
  354. return TEST_COMPLETED;
  355. }
  356. /* !
  357. * \brief Tests SDL_GetPowerInfo
  358. * \sa
  359. * http://wiki.libsdl.org/moin.cgi/SDL_GetPowerInfo
  360. */
  361. int platform_testGetPowerInfo(void *arg)
  362. {
  363. SDL_PowerState state;
  364. SDL_PowerState stateAgain;
  365. int secs;
  366. int secsAgain;
  367. int pct;
  368. int pctAgain;
  369. state = SDL_GetPowerInfo(&secs, &pct);
  370. SDLTest_AssertPass("SDL_GetPowerInfo()");
  371. SDLTest_AssertCheck(
  372. state==SDL_POWERSTATE_UNKNOWN ||
  373. state==SDL_POWERSTATE_ON_BATTERY ||
  374. state==SDL_POWERSTATE_NO_BATTERY ||
  375. state==SDL_POWERSTATE_CHARGING ||
  376. state==SDL_POWERSTATE_CHARGED,
  377. "SDL_GetPowerInfo(): state %i is one of the expected values",
  378. (int)state);
  379. if (state==SDL_POWERSTATE_ON_BATTERY)
  380. {
  381. SDLTest_AssertCheck(
  382. secs >= 0,
  383. "SDL_GetPowerInfo(): on battery, secs >= 0, was: %i",
  384. secs);
  385. SDLTest_AssertCheck(
  386. (pct >= 0) && (pct <= 100),
  387. "SDL_GetPowerInfo(): on battery, pct=[0,100], was: %i",
  388. pct);
  389. }
  390. if (state==SDL_POWERSTATE_UNKNOWN ||
  391. state==SDL_POWERSTATE_NO_BATTERY)
  392. {
  393. SDLTest_AssertCheck(
  394. secs == -1,
  395. "SDL_GetPowerInfo(): no battery, secs == -1, was: %i",
  396. secs);
  397. SDLTest_AssertCheck(
  398. pct == -1,
  399. "SDL_GetPowerInfo(): no battery, pct == -1, was: %i",
  400. pct);
  401. }
  402. /* Partial return value variations */
  403. stateAgain = SDL_GetPowerInfo(&secsAgain, NULL);
  404. SDLTest_AssertCheck(
  405. state==stateAgain,
  406. "State %i returned when only 'secs' requested",
  407. stateAgain);
  408. SDLTest_AssertCheck(
  409. secs==secsAgain,
  410. "Value %i matches when only 'secs' requested",
  411. secsAgain);
  412. stateAgain = SDL_GetPowerInfo(NULL, &pctAgain);
  413. SDLTest_AssertCheck(
  414. state==stateAgain,
  415. "State %i returned when only 'pct' requested",
  416. stateAgain);
  417. SDLTest_AssertCheck(
  418. pct==pctAgain,
  419. "Value %i matches when only 'pct' requested",
  420. pctAgain);
  421. stateAgain = SDL_GetPowerInfo(NULL, NULL);
  422. SDLTest_AssertCheck(
  423. state==stateAgain,
  424. "State %i returned when no value requested",
  425. stateAgain);
  426. return TEST_COMPLETED;
  427. }
  428. /* ================= Test References ================== */
  429. /* Platform test cases */
  430. static const SDLTest_TestCaseReference platformTest1 =
  431. { (SDLTest_TestCaseFp)platform_testTypes, "platform_testTypes", "Tests predefined types", TEST_ENABLED};
  432. static const SDLTest_TestCaseReference platformTest2 =
  433. { (SDLTest_TestCaseFp)platform_testEndianessAndSwap, "platform_testEndianessAndSwap", "Tests endianess and swap functions", TEST_ENABLED};
  434. static const SDLTest_TestCaseReference platformTest3 =
  435. { (SDLTest_TestCaseFp)platform_testGetFunctions, "platform_testGetFunctions", "Tests various SDL_GetXYZ functions", TEST_ENABLED};
  436. static const SDLTest_TestCaseReference platformTest4 =
  437. { (SDLTest_TestCaseFp)platform_testHasFunctions, "platform_testHasFunctions", "Tests various SDL_HasXYZ functions", TEST_ENABLED};
  438. static const SDLTest_TestCaseReference platformTest5 =
  439. { (SDLTest_TestCaseFp)platform_testGetVersion, "platform_testGetVersion", "Tests SDL_GetVersion function", TEST_ENABLED};
  440. static const SDLTest_TestCaseReference platformTest6 =
  441. { (SDLTest_TestCaseFp)platform_testSDLVersion, "platform_testSDLVersion", "Tests SDL_VERSION macro", TEST_ENABLED};
  442. static const SDLTest_TestCaseReference platformTest7 =
  443. { (SDLTest_TestCaseFp)platform_testDefaultInit, "platform_testDefaultInit", "Tests default SDL_Init", TEST_ENABLED};
  444. static const SDLTest_TestCaseReference platformTest8 =
  445. { (SDLTest_TestCaseFp)platform_testGetSetClearError, "platform_testGetSetClearError", "Tests SDL_Get/Set/ClearError", TEST_ENABLED};
  446. static const SDLTest_TestCaseReference platformTest9 =
  447. { (SDLTest_TestCaseFp)platform_testSetErrorEmptyInput, "platform_testSetErrorEmptyInput", "Tests SDL_SetError with empty input", TEST_ENABLED};
  448. static const SDLTest_TestCaseReference platformTest10 =
  449. { (SDLTest_TestCaseFp)platform_testSetErrorInvalidInput, "platform_testSetErrorInvalidInput", "Tests SDL_SetError with invalid input", TEST_ENABLED};
  450. static const SDLTest_TestCaseReference platformTest11 =
  451. { (SDLTest_TestCaseFp)platform_testGetPowerInfo, "platform_testGetPowerInfo", "Tests SDL_GetPowerInfo function", TEST_ENABLED };
  452. /* Sequence of Platform test cases */
  453. static const SDLTest_TestCaseReference *platformTests[] = {
  454. &platformTest1,
  455. &platformTest2,
  456. &platformTest3,
  457. &platformTest4,
  458. &platformTest5,
  459. &platformTest6,
  460. &platformTest7,
  461. &platformTest8,
  462. &platformTest9,
  463. &platformTest10,
  464. &platformTest11,
  465. NULL
  466. };
  467. /* Platform test suite (global) */
  468. SDLTest_TestSuiteReference platformTestSuite = {
  469. "Platform",
  470. NULL,
  471. platformTests,
  472. NULL
  473. };