🛠️🐜 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.

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