🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

584 lines
17 KiB

  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", (unsigned long)sizeof(Uint8) );
  30. ret = _compareSizeOfType( sizeof(Uint16), 2 );
  31. SDLTest_AssertCheck( ret == 0, "sizeof(Uint16) = %lu, expected 2", (unsigned long)sizeof(Uint16) );
  32. ret = _compareSizeOfType( sizeof(Uint32), 4 );
  33. SDLTest_AssertCheck( ret == 0, "sizeof(Uint32) = %lu, expected 4", (unsigned long)sizeof(Uint32) );
  34. ret = _compareSizeOfType( sizeof(Uint64), 8 );
  35. SDLTest_AssertCheck( ret == 0, "sizeof(Uint64) = %lu, expected 8", (unsigned long)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. "SDL_Swap64(): 64 bit swapped: 0x%"SDL_PRIX64" => 0x%"SDL_PRIX64,
  77. value64, SDL_Swap64(value64) );
  78. return TEST_COMPLETED;
  79. }
  80. /* !
  81. * \brief Tests SDL_GetXYZ() functions
  82. * \sa
  83. * http://wiki.libsdl.org/moin.cgi/SDL_GetPlatform
  84. * http://wiki.libsdl.org/moin.cgi/SDL_GetCPUCount
  85. * http://wiki.libsdl.org/moin.cgi/SDL_GetCPUCacheLineSize
  86. * http://wiki.libsdl.org/moin.cgi/SDL_GetRevision
  87. * http://wiki.libsdl.org/moin.cgi/SDL_GetRevisionNumber
  88. */
  89. int platform_testGetFunctions (void *arg)
  90. {
  91. char *platform;
  92. char *revision;
  93. int ret;
  94. size_t len;
  95. platform = (char *)SDL_GetPlatform();
  96. SDLTest_AssertPass("SDL_GetPlatform()");
  97. SDLTest_AssertCheck(platform != NULL, "SDL_GetPlatform() != NULL");
  98. if (platform != NULL) {
  99. len = SDL_strlen(platform);
  100. SDLTest_AssertCheck(len > 0,
  101. "SDL_GetPlatform(): expected non-empty platform, was platform: '%s', len: %i",
  102. platform,
  103. (int) len);
  104. }
  105. ret = SDL_GetCPUCount();
  106. SDLTest_AssertPass("SDL_GetCPUCount()");
  107. SDLTest_AssertCheck(ret > 0,
  108. "SDL_GetCPUCount(): expected count > 0, was: %i",
  109. ret);
  110. ret = SDL_GetCPUCacheLineSize();
  111. SDLTest_AssertPass("SDL_GetCPUCacheLineSize()");
  112. SDLTest_AssertCheck(ret >= 0,
  113. "SDL_GetCPUCacheLineSize(): expected size >= 0, was: %i",
  114. ret);
  115. revision = (char *)SDL_GetRevision();
  116. SDLTest_AssertPass("SDL_GetRevision()");
  117. SDLTest_AssertCheck(revision != NULL, "SDL_GetRevision() != NULL");
  118. ret = SDL_GetRevisionNumber();
  119. SDLTest_AssertPass("SDL_GetRevisionNumber()");
  120. return TEST_COMPLETED;
  121. }
  122. /* !
  123. * \brief Tests SDL_HasXYZ() functions
  124. * \sa
  125. * http://wiki.libsdl.org/moin.cgi/SDL_Has3DNow
  126. * http://wiki.libsdl.org/moin.cgi/SDL_HasAltiVec
  127. * http://wiki.libsdl.org/moin.cgi/SDL_HasMMX
  128. * http://wiki.libsdl.org/moin.cgi/SDL_HasRDTSC
  129. * http://wiki.libsdl.org/moin.cgi/SDL_HasSSE
  130. * http://wiki.libsdl.org/moin.cgi/SDL_HasSSE2
  131. * http://wiki.libsdl.org/moin.cgi/SDL_HasSSE3
  132. * http://wiki.libsdl.org/moin.cgi/SDL_HasSSE41
  133. * http://wiki.libsdl.org/moin.cgi/SDL_HasSSE42
  134. * http://wiki.libsdl.org/moin.cgi/SDL_HasAVX
  135. */
  136. int platform_testHasFunctions (void *arg)
  137. {
  138. int ret;
  139. /* TODO: independently determine and compare values as well */
  140. ret = SDL_HasRDTSC();
  141. SDLTest_AssertPass("SDL_HasRDTSC()");
  142. ret = SDL_HasAltiVec();
  143. SDLTest_AssertPass("SDL_HasAltiVec()");
  144. ret = SDL_HasMMX();
  145. SDLTest_AssertPass("SDL_HasMMX()");
  146. ret = SDL_Has3DNow();
  147. SDLTest_AssertPass("SDL_Has3DNow()");
  148. ret = SDL_HasSSE();
  149. SDLTest_AssertPass("SDL_HasSSE()");
  150. ret = SDL_HasSSE2();
  151. SDLTest_AssertPass("SDL_HasSSE2()");
  152. ret = SDL_HasSSE3();
  153. SDLTest_AssertPass("SDL_HasSSE3()");
  154. ret = SDL_HasSSE41();
  155. SDLTest_AssertPass("SDL_HasSSE41()");
  156. ret = SDL_HasSSE42();
  157. SDLTest_AssertPass("SDL_HasSSE42()");
  158. ret = SDL_HasAVX();
  159. SDLTest_AssertPass("SDL_HasAVX()");
  160. return TEST_COMPLETED;
  161. }
  162. /* !
  163. * \brief Tests SDL_GetVersion
  164. * \sa
  165. * http://wiki.libsdl.org/moin.cgi/SDL_GetVersion
  166. */
  167. int platform_testGetVersion(void *arg)
  168. {
  169. SDL_version linked;
  170. int major = SDL_MAJOR_VERSION;
  171. int minor = SDL_MINOR_VERSION;
  172. SDL_GetVersion(&linked);
  173. SDLTest_AssertCheck( linked.major >= major,
  174. "SDL_GetVersion(): returned major %i (>= %i)",
  175. linked.major,
  176. major);
  177. SDLTest_AssertCheck( linked.minor >= minor,
  178. "SDL_GetVersion(): returned minor %i (>= %i)",
  179. linked.minor,
  180. minor);
  181. return TEST_COMPLETED;
  182. }
  183. /* !
  184. * \brief Tests SDL_VERSION macro
  185. */
  186. int platform_testSDLVersion(void *arg)
  187. {
  188. SDL_version compiled;
  189. int major = SDL_MAJOR_VERSION;
  190. int minor = SDL_MINOR_VERSION;
  191. SDL_VERSION(&compiled);
  192. SDLTest_AssertCheck( compiled.major >= major,
  193. "SDL_VERSION() returned major %i (>= %i)",
  194. compiled.major,
  195. major);
  196. SDLTest_AssertCheck( compiled.minor >= minor,
  197. "SDL_VERSION() returned minor %i (>= %i)",
  198. compiled.minor,
  199. minor);
  200. return TEST_COMPLETED;
  201. }
  202. /* !
  203. * \brief Tests default SDL_Init
  204. */
  205. int platform_testDefaultInit(void *arg)
  206. {
  207. int ret;
  208. int subsystem;
  209. subsystem = SDL_WasInit(SDL_INIT_EVERYTHING);
  210. SDLTest_AssertCheck( subsystem != 0,
  211. "SDL_WasInit(0): returned %i, expected != 0",
  212. subsystem);
  213. ret = SDL_Init(SDL_WasInit(SDL_INIT_EVERYTHING));
  214. SDLTest_AssertCheck( ret == 0,
  215. "SDL_Init(0): returned %i, expected 0, error: %s",
  216. ret,
  217. SDL_GetError());
  218. return TEST_COMPLETED;
  219. }
  220. /* !
  221. * \brief Tests SDL_Get/Set/ClearError
  222. * \sa
  223. * http://wiki.libsdl.org/moin.cgi/SDL_GetError
  224. * http://wiki.libsdl.org/moin.cgi/SDL_SetError
  225. * http://wiki.libsdl.org/moin.cgi/SDL_ClearError
  226. */
  227. int platform_testGetSetClearError(void *arg)
  228. {
  229. int result;
  230. const char *testError = "Testing";
  231. char *lastError;
  232. size_t 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", (int) len);
  244. }
  245. result = SDL_SetError("%s", testError);
  246. SDLTest_AssertPass("SDL_SetError()");
  247. SDLTest_AssertCheck(result == -1, "SDL_SetError: expected -1, got: %i", result);
  248. lastError = (char *)SDL_GetError();
  249. SDLTest_AssertCheck(lastError != NULL,
  250. "SDL_GetError() != NULL");
  251. if (lastError != NULL)
  252. {
  253. len = SDL_strlen(lastError);
  254. SDLTest_AssertCheck(len == SDL_strlen(testError),
  255. "SDL_GetError(): expected message len %i, was len: %i",
  256. (int) SDL_strlen(testError),
  257. (int) len);
  258. SDLTest_AssertCheck(SDL_strcmp(lastError, testError) == 0,
  259. "SDL_GetError(): expected message %s, was message: %s",
  260. testError,
  261. lastError);
  262. }
  263. /* Clean up */
  264. SDL_ClearError();
  265. SDLTest_AssertPass("SDL_ClearError()");
  266. return TEST_COMPLETED;
  267. }
  268. /* !
  269. * \brief Tests SDL_SetError with empty input
  270. * \sa
  271. * http://wiki.libsdl.org/moin.cgi/SDL_SetError
  272. */
  273. int platform_testSetErrorEmptyInput(void *arg)
  274. {
  275. int result;
  276. const char *testError = "";
  277. char *lastError;
  278. size_t len;
  279. result = SDL_SetError("%s", testError);
  280. SDLTest_AssertPass("SDL_SetError()");
  281. SDLTest_AssertCheck(result == -1, "SDL_SetError: expected -1, got: %i", result);
  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. (int) SDL_strlen(testError),
  291. (int) 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. int result;
  310. const char *invalidError = NULL;
  311. const char *probeError = "Testing";
  312. char *lastError;
  313. size_t len;
  314. /* Reset */
  315. SDL_ClearError();
  316. SDLTest_AssertPass("SDL_ClearError()");
  317. /* Check for no-op */
  318. result = SDL_SetError("%s", invalidError);
  319. SDLTest_AssertPass("SDL_SetError()");
  320. SDLTest_AssertCheck(result == -1, "SDL_SetError: expected -1, got: %i", result);
  321. lastError = (char *)SDL_GetError();
  322. SDLTest_AssertCheck(lastError != NULL,
  323. "SDL_GetError() != NULL");
  324. if (lastError != NULL)
  325. {
  326. len = SDL_strlen(lastError);
  327. SDLTest_AssertCheck(len == 0,
  328. "SDL_GetError(): expected message len 0, was len: %i",
  329. (int) len);
  330. }
  331. /* Set */
  332. result = SDL_SetError("%s", probeError);
  333. SDLTest_AssertPass("SDL_SetError('%s')", probeError);
  334. SDLTest_AssertCheck(result == -1, "SDL_SetError: expected -1, got: %i", result);
  335. /* Check for no-op */
  336. result = SDL_SetError("%s", invalidError);
  337. SDLTest_AssertPass("SDL_SetError(NULL)");
  338. SDLTest_AssertCheck(result == -1, "SDL_SetError: expected -1, got: %i", result);
  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 == 0,
  346. "SDL_GetError(): expected message len 0, was len: %i",
  347. (int) len);
  348. }
  349. /* Reset */
  350. SDL_ClearError();
  351. SDLTest_AssertPass("SDL_ClearError()");
  352. /* Set and check */
  353. result = SDL_SetError("%s", probeError);
  354. SDLTest_AssertPass("SDL_SetError()");
  355. SDLTest_AssertCheck(result == -1, "SDL_SetError: expected -1, got: %i", result);
  356. lastError = (char *)SDL_GetError();
  357. SDLTest_AssertCheck(lastError != NULL,
  358. "SDL_GetError() != NULL");
  359. if (lastError != NULL)
  360. {
  361. len = SDL_strlen(lastError);
  362. SDLTest_AssertCheck(len == SDL_strlen(probeError),
  363. "SDL_GetError(): expected message len %i, was len: %i",
  364. (int) SDL_strlen(probeError),
  365. (int) len);
  366. SDLTest_AssertCheck(SDL_strcmp(lastError, probeError) == 0,
  367. "SDL_GetError(): expected message '%s', was message: '%s'",
  368. probeError,
  369. lastError);
  370. }
  371. /* Clean up */
  372. SDL_ClearError();
  373. SDLTest_AssertPass("SDL_ClearError()");
  374. return TEST_COMPLETED;
  375. }
  376. /* !
  377. * \brief Tests SDL_GetPowerInfo
  378. * \sa
  379. * http://wiki.libsdl.org/moin.cgi/SDL_GetPowerInfo
  380. */
  381. int platform_testGetPowerInfo(void *arg)
  382. {
  383. SDL_PowerState state;
  384. SDL_PowerState stateAgain;
  385. int secs;
  386. int secsAgain;
  387. int pct;
  388. int pctAgain;
  389. state = SDL_GetPowerInfo(&secs, &pct);
  390. SDLTest_AssertPass("SDL_GetPowerInfo()");
  391. SDLTest_AssertCheck(
  392. state==SDL_POWERSTATE_UNKNOWN ||
  393. state==SDL_POWERSTATE_ON_BATTERY ||
  394. state==SDL_POWERSTATE_NO_BATTERY ||
  395. state==SDL_POWERSTATE_CHARGING ||
  396. state==SDL_POWERSTATE_CHARGED,
  397. "SDL_GetPowerInfo(): state %i is one of the expected values",
  398. (int)state);
  399. if (state==SDL_POWERSTATE_ON_BATTERY)
  400. {
  401. SDLTest_AssertCheck(
  402. secs >= 0,
  403. "SDL_GetPowerInfo(): on battery, secs >= 0, was: %i",
  404. secs);
  405. SDLTest_AssertCheck(
  406. (pct >= 0) && (pct <= 100),
  407. "SDL_GetPowerInfo(): on battery, pct=[0,100], was: %i",
  408. pct);
  409. }
  410. if (state==SDL_POWERSTATE_UNKNOWN ||
  411. state==SDL_POWERSTATE_NO_BATTERY)
  412. {
  413. SDLTest_AssertCheck(
  414. secs == -1,
  415. "SDL_GetPowerInfo(): no battery, secs == -1, was: %i",
  416. secs);
  417. SDLTest_AssertCheck(
  418. pct == -1,
  419. "SDL_GetPowerInfo(): no battery, pct == -1, was: %i",
  420. pct);
  421. }
  422. /* Partial return value variations */
  423. stateAgain = SDL_GetPowerInfo(&secsAgain, NULL);
  424. SDLTest_AssertCheck(
  425. state==stateAgain,
  426. "State %i returned when only 'secs' requested",
  427. stateAgain);
  428. SDLTest_AssertCheck(
  429. secs==secsAgain,
  430. "Value %i matches when only 'secs' requested",
  431. secsAgain);
  432. stateAgain = SDL_GetPowerInfo(NULL, &pctAgain);
  433. SDLTest_AssertCheck(
  434. state==stateAgain,
  435. "State %i returned when only 'pct' requested",
  436. stateAgain);
  437. SDLTest_AssertCheck(
  438. pct==pctAgain,
  439. "Value %i matches when only 'pct' requested",
  440. pctAgain);
  441. stateAgain = SDL_GetPowerInfo(NULL, NULL);
  442. SDLTest_AssertCheck(
  443. state==stateAgain,
  444. "State %i returned when no value requested",
  445. stateAgain);
  446. return TEST_COMPLETED;
  447. }
  448. /* ================= Test References ================== */
  449. /* Platform test cases */
  450. static const SDLTest_TestCaseReference platformTest1 =
  451. { (SDLTest_TestCaseFp)platform_testTypes, "platform_testTypes", "Tests predefined types", TEST_ENABLED};
  452. static const SDLTest_TestCaseReference platformTest2 =
  453. { (SDLTest_TestCaseFp)platform_testEndianessAndSwap, "platform_testEndianessAndSwap", "Tests endianess and swap functions", TEST_ENABLED};
  454. static const SDLTest_TestCaseReference platformTest3 =
  455. { (SDLTest_TestCaseFp)platform_testGetFunctions, "platform_testGetFunctions", "Tests various SDL_GetXYZ functions", TEST_ENABLED};
  456. static const SDLTest_TestCaseReference platformTest4 =
  457. { (SDLTest_TestCaseFp)platform_testHasFunctions, "platform_testHasFunctions", "Tests various SDL_HasXYZ functions", TEST_ENABLED};
  458. static const SDLTest_TestCaseReference platformTest5 =
  459. { (SDLTest_TestCaseFp)platform_testGetVersion, "platform_testGetVersion", "Tests SDL_GetVersion function", TEST_ENABLED};
  460. static const SDLTest_TestCaseReference platformTest6 =
  461. { (SDLTest_TestCaseFp)platform_testSDLVersion, "platform_testSDLVersion", "Tests SDL_VERSION macro", TEST_ENABLED};
  462. static const SDLTest_TestCaseReference platformTest7 =
  463. { (SDLTest_TestCaseFp)platform_testDefaultInit, "platform_testDefaultInit", "Tests default SDL_Init", TEST_ENABLED};
  464. static const SDLTest_TestCaseReference platformTest8 =
  465. { (SDLTest_TestCaseFp)platform_testGetSetClearError, "platform_testGetSetClearError", "Tests SDL_Get/Set/ClearError", TEST_ENABLED};
  466. static const SDLTest_TestCaseReference platformTest9 =
  467. { (SDLTest_TestCaseFp)platform_testSetErrorEmptyInput, "platform_testSetErrorEmptyInput", "Tests SDL_SetError with empty input", TEST_ENABLED};
  468. static const SDLTest_TestCaseReference platformTest10 =
  469. { (SDLTest_TestCaseFp)platform_testSetErrorInvalidInput, "platform_testSetErrorInvalidInput", "Tests SDL_SetError with invalid input", TEST_ENABLED};
  470. static const SDLTest_TestCaseReference platformTest11 =
  471. { (SDLTest_TestCaseFp)platform_testGetPowerInfo, "platform_testGetPowerInfo", "Tests SDL_GetPowerInfo function", TEST_ENABLED };
  472. /* Sequence of Platform test cases */
  473. static const SDLTest_TestCaseReference *platformTests[] = {
  474. &platformTest1,
  475. &platformTest2,
  476. &platformTest3,
  477. &platformTest4,
  478. &platformTest5,
  479. &platformTest6,
  480. &platformTest7,
  481. &platformTest8,
  482. &platformTest9,
  483. &platformTest10,
  484. &platformTest11,
  485. NULL
  486. };
  487. /* Platform test suite (global) */
  488. SDLTest_TestSuiteReference platformTestSuite = {
  489. "Platform",
  490. NULL,
  491. platformTests,
  492. NULL
  493. };