testautomation_platform.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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. * http://wiki.libsdl.org/moin.cgi/SDL_HasAVX
  139. */
  140. int platform_testHasFunctions (void *arg)
  141. {
  142. int ret;
  143. /* TODO: independently determine and compare values as well */
  144. ret = SDL_HasRDTSC();
  145. SDLTest_AssertPass("SDL_HasRDTSC()");
  146. ret = SDL_HasAltiVec();
  147. SDLTest_AssertPass("SDL_HasAltiVec()");
  148. ret = SDL_HasMMX();
  149. SDLTest_AssertPass("SDL_HasMMX()");
  150. ret = SDL_Has3DNow();
  151. SDLTest_AssertPass("SDL_Has3DNow()");
  152. ret = SDL_HasSSE();
  153. SDLTest_AssertPass("SDL_HasSSE()");
  154. ret = SDL_HasSSE2();
  155. SDLTest_AssertPass("SDL_HasSSE2()");
  156. ret = SDL_HasSSE3();
  157. SDLTest_AssertPass("SDL_HasSSE3()");
  158. ret = SDL_HasSSE41();
  159. SDLTest_AssertPass("SDL_HasSSE41()");
  160. ret = SDL_HasSSE42();
  161. SDLTest_AssertPass("SDL_HasSSE42()");
  162. ret = SDL_HasAVX();
  163. SDLTest_AssertPass("SDL_HasAVX()");
  164. return TEST_COMPLETED;
  165. }
  166. /* !
  167. * \brief Tests SDL_GetVersion
  168. * \sa
  169. * http://wiki.libsdl.org/moin.cgi/SDL_GetVersion
  170. */
  171. int platform_testGetVersion(void *arg)
  172. {
  173. SDL_version linked;
  174. int major = SDL_MAJOR_VERSION;
  175. int minor = SDL_MINOR_VERSION;
  176. SDL_GetVersion(&linked);
  177. SDLTest_AssertCheck( linked.major >= major,
  178. "SDL_GetVersion(): returned major %i (>= %i)",
  179. linked.major,
  180. major);
  181. SDLTest_AssertCheck( linked.minor >= minor,
  182. "SDL_GetVersion(): returned minor %i (>= %i)",
  183. linked.minor,
  184. minor);
  185. return TEST_COMPLETED;
  186. }
  187. /* !
  188. * \brief Tests SDL_VERSION macro
  189. */
  190. int platform_testSDLVersion(void *arg)
  191. {
  192. SDL_version compiled;
  193. int major = SDL_MAJOR_VERSION;
  194. int minor = SDL_MINOR_VERSION;
  195. SDL_VERSION(&compiled);
  196. SDLTest_AssertCheck( compiled.major >= major,
  197. "SDL_VERSION() returned major %i (>= %i)",
  198. compiled.major,
  199. major);
  200. SDLTest_AssertCheck( compiled.minor >= minor,
  201. "SDL_VERSION() returned minor %i (>= %i)",
  202. compiled.minor,
  203. minor);
  204. return TEST_COMPLETED;
  205. }
  206. /* !
  207. * \brief Tests default SDL_Init
  208. */
  209. int platform_testDefaultInit(void *arg)
  210. {
  211. int ret;
  212. int subsystem;
  213. subsystem = SDL_WasInit(SDL_INIT_EVERYTHING);
  214. SDLTest_AssertCheck( subsystem != 0,
  215. "SDL_WasInit(0): returned %i, expected != 0",
  216. subsystem);
  217. ret = SDL_Init(SDL_WasInit(SDL_INIT_EVERYTHING));
  218. SDLTest_AssertCheck( ret == 0,
  219. "SDL_Init(0): returned %i, expected 0, error: %s",
  220. ret,
  221. SDL_GetError());
  222. return TEST_COMPLETED;
  223. }
  224. /* !
  225. * \brief Tests SDL_Get/Set/ClearError
  226. * \sa
  227. * http://wiki.libsdl.org/moin.cgi/SDL_GetError
  228. * http://wiki.libsdl.org/moin.cgi/SDL_SetError
  229. * http://wiki.libsdl.org/moin.cgi/SDL_ClearError
  230. */
  231. int platform_testGetSetClearError(void *arg)
  232. {
  233. const char *testError = "Testing";
  234. char *lastError;
  235. int len;
  236. SDL_ClearError();
  237. SDLTest_AssertPass("SDL_ClearError()");
  238. lastError = (char *)SDL_GetError();
  239. SDLTest_AssertPass("SDL_GetError()");
  240. SDLTest_AssertCheck(lastError != NULL,
  241. "SDL_GetError() != NULL");
  242. if (lastError != NULL)
  243. {
  244. len = SDL_strlen(lastError);
  245. SDLTest_AssertCheck(len == 0,
  246. "SDL_GetError(): no message expected, len: %i", len);
  247. }
  248. SDL_SetError("%s", testError);
  249. SDLTest_AssertPass("SDL_SetError()");
  250. lastError = (char *)SDL_GetError();
  251. SDLTest_AssertCheck(lastError != NULL,
  252. "SDL_GetError() != NULL");
  253. if (lastError != NULL)
  254. {
  255. len = SDL_strlen(lastError);
  256. SDLTest_AssertCheck(len == SDL_strlen(testError),
  257. "SDL_GetError(): expected message len %i, was len: %i",
  258. SDL_strlen(testError),
  259. len);
  260. SDLTest_AssertCheck(SDL_strcmp(lastError, testError) == 0,
  261. "SDL_GetError(): expected message %s, was message: %s",
  262. testError,
  263. lastError);
  264. }
  265. /* Clean up */
  266. SDL_ClearError();
  267. SDLTest_AssertPass("SDL_ClearError()");
  268. return TEST_COMPLETED;
  269. }
  270. /* !
  271. * \brief Tests SDL_SetError with empty input
  272. * \sa
  273. * http://wiki.libsdl.org/moin.cgi/SDL_SetError
  274. */
  275. int platform_testSetErrorEmptyInput(void *arg)
  276. {
  277. const char *testError = "";
  278. char *lastError;
  279. int len;
  280. SDL_SetError("%s", testError);
  281. SDLTest_AssertPass("SDL_SetError()");
  282. lastError = (char *)SDL_GetError();
  283. SDLTest_AssertCheck(lastError != NULL,
  284. "SDL_GetError() != NULL");
  285. if (lastError != NULL)
  286. {
  287. len = SDL_strlen(lastError);
  288. SDLTest_AssertCheck(len == SDL_strlen(testError),
  289. "SDL_GetError(): expected message len %i, was len: %i",
  290. SDL_strlen(testError),
  291. len);
  292. SDLTest_AssertCheck(SDL_strcmp(lastError, testError) == 0,
  293. "SDL_GetError(): expected message '%s', was message: '%s'",
  294. testError,
  295. lastError);
  296. }
  297. /* Clean up */
  298. SDL_ClearError();
  299. SDLTest_AssertPass("SDL_ClearError()");
  300. return TEST_COMPLETED;
  301. }
  302. /* !
  303. * \brief Tests SDL_SetError with invalid input
  304. * \sa
  305. * http://wiki.libsdl.org/moin.cgi/SDL_SetError
  306. */
  307. int platform_testSetErrorInvalidInput(void *arg)
  308. {
  309. const char *testError = NULL;
  310. const char *probeError = "Testing";
  311. char *lastError;
  312. int len;
  313. /* Reset */
  314. SDL_ClearError();
  315. SDLTest_AssertPass("SDL_ClearError()");
  316. /* Check for no-op */
  317. SDL_SetError(testError);
  318. SDLTest_AssertPass("SDL_SetError()");
  319. lastError = (char *)SDL_GetError();
  320. SDLTest_AssertCheck(lastError != NULL,
  321. "SDL_GetError() != NULL");
  322. if (lastError != NULL)
  323. {
  324. len = SDL_strlen(lastError);
  325. SDLTest_AssertCheck(len == 0,
  326. "SDL_GetError(): expected message len 0, was len: %i",
  327. 0,
  328. len);
  329. SDLTest_AssertCheck(SDL_strcmp(lastError, "") == 0,
  330. "SDL_GetError(): expected message '', was message: '%s'",
  331. lastError);
  332. }
  333. /* Set */
  334. SDL_SetError(probeError);
  335. SDLTest_AssertPass("SDL_SetError()");
  336. /* Check for no-op */
  337. SDL_SetError(testError);
  338. SDLTest_AssertPass("SDL_SetError()");
  339. lastError = (char *)SDL_GetError();
  340. SDLTest_AssertCheck(lastError != NULL,
  341. "SDL_GetError() != NULL");
  342. if (lastError != NULL)
  343. {
  344. len = SDL_strlen(lastError);
  345. SDLTest_AssertCheck(len == SDL_strlen(probeError),
  346. "SDL_GetError(): expected message len %i, was len: %i",
  347. SDL_strlen(probeError),
  348. len);
  349. SDLTest_AssertCheck(SDL_strcmp(lastError, probeError) == 0,
  350. "SDL_GetError(): expected message '%s', was message: '%s'",
  351. probeError,
  352. lastError);
  353. }
  354. /* Clean up */
  355. SDL_ClearError();
  356. SDLTest_AssertPass("SDL_ClearError()");
  357. return TEST_COMPLETED;
  358. }
  359. /* !
  360. * \brief Tests SDL_GetPowerInfo
  361. * \sa
  362. * http://wiki.libsdl.org/moin.cgi/SDL_GetPowerInfo
  363. */
  364. int platform_testGetPowerInfo(void *arg)
  365. {
  366. SDL_PowerState state;
  367. SDL_PowerState stateAgain;
  368. int secs;
  369. int secsAgain;
  370. int pct;
  371. int pctAgain;
  372. state = SDL_GetPowerInfo(&secs, &pct);
  373. SDLTest_AssertPass("SDL_GetPowerInfo()");
  374. SDLTest_AssertCheck(
  375. state==SDL_POWERSTATE_UNKNOWN ||
  376. state==SDL_POWERSTATE_ON_BATTERY ||
  377. state==SDL_POWERSTATE_NO_BATTERY ||
  378. state==SDL_POWERSTATE_CHARGING ||
  379. state==SDL_POWERSTATE_CHARGED,
  380. "SDL_GetPowerInfo(): state %i is one of the expected values",
  381. (int)state);
  382. if (state==SDL_POWERSTATE_ON_BATTERY)
  383. {
  384. SDLTest_AssertCheck(
  385. secs >= 0,
  386. "SDL_GetPowerInfo(): on battery, secs >= 0, was: %i",
  387. secs);
  388. SDLTest_AssertCheck(
  389. (pct >= 0) && (pct <= 100),
  390. "SDL_GetPowerInfo(): on battery, pct=[0,100], was: %i",
  391. pct);
  392. }
  393. if (state==SDL_POWERSTATE_UNKNOWN ||
  394. state==SDL_POWERSTATE_NO_BATTERY)
  395. {
  396. SDLTest_AssertCheck(
  397. secs == -1,
  398. "SDL_GetPowerInfo(): no battery, secs == -1, was: %i",
  399. secs);
  400. SDLTest_AssertCheck(
  401. pct == -1,
  402. "SDL_GetPowerInfo(): no battery, pct == -1, was: %i",
  403. pct);
  404. }
  405. /* Partial return value variations */
  406. stateAgain = SDL_GetPowerInfo(&secsAgain, NULL);
  407. SDLTest_AssertCheck(
  408. state==stateAgain,
  409. "State %i returned when only 'secs' requested",
  410. stateAgain);
  411. SDLTest_AssertCheck(
  412. secs==secsAgain,
  413. "Value %i matches when only 'secs' requested",
  414. secsAgain);
  415. stateAgain = SDL_GetPowerInfo(NULL, &pctAgain);
  416. SDLTest_AssertCheck(
  417. state==stateAgain,
  418. "State %i returned when only 'pct' requested",
  419. stateAgain);
  420. SDLTest_AssertCheck(
  421. pct==pctAgain,
  422. "Value %i matches when only 'pct' requested",
  423. pctAgain);
  424. stateAgain = SDL_GetPowerInfo(NULL, NULL);
  425. SDLTest_AssertCheck(
  426. state==stateAgain,
  427. "State %i returned when no value requested",
  428. stateAgain);
  429. return TEST_COMPLETED;
  430. }
  431. /* ================= Test References ================== */
  432. /* Platform test cases */
  433. static const SDLTest_TestCaseReference platformTest1 =
  434. { (SDLTest_TestCaseFp)platform_testTypes, "platform_testTypes", "Tests predefined types", TEST_ENABLED};
  435. static const SDLTest_TestCaseReference platformTest2 =
  436. { (SDLTest_TestCaseFp)platform_testEndianessAndSwap, "platform_testEndianessAndSwap", "Tests endianess and swap functions", TEST_ENABLED};
  437. static const SDLTest_TestCaseReference platformTest3 =
  438. { (SDLTest_TestCaseFp)platform_testGetFunctions, "platform_testGetFunctions", "Tests various SDL_GetXYZ functions", TEST_ENABLED};
  439. static const SDLTest_TestCaseReference platformTest4 =
  440. { (SDLTest_TestCaseFp)platform_testHasFunctions, "platform_testHasFunctions", "Tests various SDL_HasXYZ functions", TEST_ENABLED};
  441. static const SDLTest_TestCaseReference platformTest5 =
  442. { (SDLTest_TestCaseFp)platform_testGetVersion, "platform_testGetVersion", "Tests SDL_GetVersion function", TEST_ENABLED};
  443. static const SDLTest_TestCaseReference platformTest6 =
  444. { (SDLTest_TestCaseFp)platform_testSDLVersion, "platform_testSDLVersion", "Tests SDL_VERSION macro", TEST_ENABLED};
  445. static const SDLTest_TestCaseReference platformTest7 =
  446. { (SDLTest_TestCaseFp)platform_testDefaultInit, "platform_testDefaultInit", "Tests default SDL_Init", TEST_ENABLED};
  447. static const SDLTest_TestCaseReference platformTest8 =
  448. { (SDLTest_TestCaseFp)platform_testGetSetClearError, "platform_testGetSetClearError", "Tests SDL_Get/Set/ClearError", TEST_ENABLED};
  449. static const SDLTest_TestCaseReference platformTest9 =
  450. { (SDLTest_TestCaseFp)platform_testSetErrorEmptyInput, "platform_testSetErrorEmptyInput", "Tests SDL_SetError with empty input", TEST_ENABLED};
  451. static const SDLTest_TestCaseReference platformTest10 =
  452. { (SDLTest_TestCaseFp)platform_testSetErrorInvalidInput, "platform_testSetErrorInvalidInput", "Tests SDL_SetError with invalid input", TEST_ENABLED};
  453. static const SDLTest_TestCaseReference platformTest11 =
  454. { (SDLTest_TestCaseFp)platform_testGetPowerInfo, "platform_testGetPowerInfo", "Tests SDL_GetPowerInfo function", TEST_ENABLED };
  455. /* Sequence of Platform test cases */
  456. static const SDLTest_TestCaseReference *platformTests[] = {
  457. &platformTest1,
  458. &platformTest2,
  459. &platformTest3,
  460. &platformTest4,
  461. &platformTest5,
  462. &platformTest6,
  463. &platformTest7,
  464. &platformTest8,
  465. &platformTest9,
  466. &platformTest10,
  467. &platformTest11,
  468. NULL
  469. };
  470. /* Platform test suite (global) */
  471. SDLTest_TestSuiteReference platformTestSuite = {
  472. "Platform",
  473. NULL,
  474. platformTests,
  475. NULL
  476. };