testautomation_platform.c 17 KB

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