testautomation_iostream.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. /**
  2. * Automated SDL_IOStream test.
  3. *
  4. * Original code written by Edgar Simo "bobbens"
  5. * Ported by Markus Kauppila (markus.kauppila@gmail.com)
  6. * Updated and extended for SDL_test by aschiffler at ferzkopp dot net
  7. *
  8. * Released under Public Domain.
  9. */
  10. /* quiet windows compiler warnings */
  11. #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
  12. #define _CRT_SECURE_NO_WARNINGS
  13. #endif
  14. #include <stdio.h>
  15. #include <SDL3/SDL.h>
  16. #include <SDL3/SDL_test.h>
  17. #include "testautomation_suites.h"
  18. /* ================= Test Case Implementation ================== */
  19. static const char *IOStreamReadTestFilename = "iostrm_read";
  20. static const char *IOStreamWriteTestFilename = "iostrm_write";
  21. static const char *IOStreamAlphabetFilename = "iostrm_alphabet";
  22. static const char IOStreamHelloWorldTestString[] = "Hello World!";
  23. static const char IOStreamHelloWorldCompString[] = "Hello World!";
  24. static const char IOStreamAlphabetString[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  25. /* Fixture */
  26. static void SDLCALL IOStreamSetUp(void **arg)
  27. {
  28. size_t fileLen;
  29. FILE *handle;
  30. size_t writtenLen;
  31. int result;
  32. /* Clean up from previous runs (if any); ignore errors */
  33. (void)remove(IOStreamReadTestFilename);
  34. (void)remove(IOStreamWriteTestFilename);
  35. (void)remove(IOStreamAlphabetFilename);
  36. /* Create a test file */
  37. handle = fopen(IOStreamReadTestFilename, "w");
  38. SDLTest_AssertCheck(handle != NULL, "Verify creation of file '%s' returned non NULL handle", IOStreamReadTestFilename);
  39. if (handle == NULL) {
  40. return;
  41. }
  42. /* Write some known text into it */
  43. fileLen = SDL_strlen(IOStreamHelloWorldTestString);
  44. writtenLen = fwrite(IOStreamHelloWorldTestString, 1, fileLen, handle);
  45. SDLTest_AssertCheck(fileLen == writtenLen, "Verify number of written bytes, expected %i, got %i", (int)fileLen, (int)writtenLen);
  46. result = fclose(handle);
  47. SDLTest_AssertCheck(result == 0, "Verify result from fclose, expected 0, got %i", result);
  48. /* Create a second test file */
  49. handle = fopen(IOStreamAlphabetFilename, "w");
  50. SDLTest_AssertCheck(handle != NULL, "Verify creation of file '%s' returned non NULL handle", IOStreamAlphabetFilename);
  51. if (handle == NULL) {
  52. return;
  53. }
  54. /* Write alphabet text into it */
  55. fileLen = SDL_strlen(IOStreamAlphabetString);
  56. writtenLen = fwrite(IOStreamAlphabetString, 1, fileLen, handle);
  57. SDLTest_AssertCheck(fileLen == writtenLen, "Verify number of written bytes, expected %i, got %i", (int)fileLen, (int)writtenLen);
  58. result = fclose(handle);
  59. SDLTest_AssertCheck(result == 0, "Verify result from fclose, expected 0, got %i", result);
  60. SDLTest_AssertPass("Creation of test file completed");
  61. }
  62. static void SDLCALL IOStreamTearDown(void *arg)
  63. {
  64. int result;
  65. /* Remove the created files to clean up; ignore errors for write filename */
  66. result = remove(IOStreamReadTestFilename);
  67. SDLTest_AssertCheck(result == 0, "Verify result from remove(%s), expected 0, got %i", IOStreamReadTestFilename, result);
  68. (void)remove(IOStreamWriteTestFilename);
  69. result = remove(IOStreamAlphabetFilename);
  70. SDLTest_AssertCheck(result == 0, "Verify result from remove(%s), expected 0, got %i", IOStreamAlphabetFilename, result);
  71. SDLTest_AssertPass("Cleanup of test files completed");
  72. }
  73. /**
  74. * Makes sure parameters work properly. Local helper function.
  75. *
  76. * \sa SDL_SeekIO
  77. * \sa SDL_ReadIO
  78. */
  79. static void testGenericIOStreamValidations(SDL_IOStream *rw, bool write)
  80. {
  81. char buf[sizeof(IOStreamHelloWorldTestString)];
  82. Sint64 i;
  83. size_t s;
  84. int seekPos = SDLTest_RandomIntegerInRange(4, 8);
  85. /* Clear buffer */
  86. SDL_zeroa(buf);
  87. /* Set to start. */
  88. i = SDL_SeekIO(rw, 0, SDL_IO_SEEK_SET);
  89. SDLTest_AssertPass("Call to SDL_SeekIO succeeded");
  90. SDLTest_AssertCheck(i == (Sint64)0, "Verify seek to 0 with SDL_SeekIO (SDL_IO_SEEK_SET), expected 0, got %" SDL_PRIs64, i);
  91. /* Test write */
  92. s = SDL_WriteIO(rw, IOStreamHelloWorldTestString, sizeof(IOStreamHelloWorldTestString) - 1);
  93. SDLTest_AssertPass("Call to SDL_WriteIO succeeded");
  94. if (write) {
  95. SDLTest_AssertCheck(s == sizeof(IOStreamHelloWorldTestString) - 1, "Verify result of writing with SDL_WriteIO, expected %i, got %i", (int)sizeof(IOStreamHelloWorldTestString) - 1, (int)s);
  96. } else {
  97. SDLTest_AssertCheck(s == 0, "Verify result of writing with SDL_WriteIO, expected: 0, got %i", (int)s);
  98. }
  99. /* Test seek to random position */
  100. i = SDL_SeekIO(rw, seekPos, SDL_IO_SEEK_SET);
  101. SDLTest_AssertPass("Call to SDL_SeekIO succeeded");
  102. SDLTest_AssertCheck(i == (Sint64)seekPos, "Verify seek to %i with SDL_SeekIO (SDL_IO_SEEK_SET), expected %i, got %" SDL_PRIs64, seekPos, seekPos, i);
  103. /* Test seek back to start */
  104. i = SDL_SeekIO(rw, 0, SDL_IO_SEEK_SET);
  105. SDLTest_AssertPass("Call to SDL_SeekIO succeeded");
  106. SDLTest_AssertCheck(i == (Sint64)0, "Verify seek to 0 with SDL_SeekIO (SDL_IO_SEEK_SET), expected 0, got %" SDL_PRIs64, i);
  107. /* Test read */
  108. s = SDL_ReadIO(rw, buf, sizeof(IOStreamHelloWorldTestString) - 1);
  109. SDLTest_AssertPass("Call to SDL_ReadIO succeeded");
  110. SDLTest_AssertCheck(
  111. s == (sizeof(IOStreamHelloWorldTestString) - 1),
  112. "Verify result from SDL_ReadIO, expected %i, got %i",
  113. (int)(sizeof(IOStreamHelloWorldTestString) - 1),
  114. (int)s);
  115. SDLTest_AssertCheck(
  116. SDL_memcmp(buf, IOStreamHelloWorldTestString, sizeof(IOStreamHelloWorldTestString) - 1) == 0,
  117. "Verify read bytes match expected string, expected '%s', got '%s'", IOStreamHelloWorldTestString, buf);
  118. /* Test seek back to start */
  119. i = SDL_SeekIO(rw, 0, SDL_IO_SEEK_SET);
  120. SDLTest_AssertPass("Call to SDL_SeekIO succeeded");
  121. SDLTest_AssertCheck(i == (Sint64)0, "Verify seek to 0 with SDL_SeekIO (SDL_IO_SEEK_SET), expected 0, got %" SDL_PRIs64, i);
  122. /* Test printf */
  123. s = SDL_IOprintf(rw, "%s", IOStreamHelloWorldTestString);
  124. SDLTest_AssertPass("Call to SDL_IOprintf succeeded");
  125. if (write) {
  126. SDLTest_AssertCheck(s == sizeof(IOStreamHelloWorldTestString) - 1, "Verify result of writing with SDL_IOprintf, expected %i, got %i", (int)sizeof(IOStreamHelloWorldTestString) - 1, (int)s);
  127. } else {
  128. SDLTest_AssertCheck(s == 0, "Verify result of writing with SDL_WriteIO, expected: 0, got %i", (int)s);
  129. }
  130. /* Test seek back to start */
  131. i = SDL_SeekIO(rw, 0, SDL_IO_SEEK_SET);
  132. SDLTest_AssertPass("Call to SDL_SeekIO succeeded");
  133. SDLTest_AssertCheck(i == (Sint64)0, "Verify seek to 0 with SDL_SeekIO (SDL_IO_SEEK_SET), expected 0, got %" SDL_PRIs64, i);
  134. /* Test read */
  135. s = SDL_ReadIO(rw, buf, sizeof(IOStreamHelloWorldTestString) - 1);
  136. SDLTest_AssertPass("Call to SDL_ReadIO succeeded");
  137. SDLTest_AssertCheck(
  138. s == (sizeof(IOStreamHelloWorldTestString) - 1),
  139. "Verify result from SDL_ReadIO, expected %i, got %i",
  140. (int)(sizeof(IOStreamHelloWorldTestString) - 1),
  141. (int)s);
  142. SDLTest_AssertCheck(
  143. SDL_memcmp(buf, IOStreamHelloWorldTestString, sizeof(IOStreamHelloWorldTestString) - 1) == 0,
  144. "Verify read bytes match expected string, expected '%s', got '%s'", IOStreamHelloWorldTestString, buf);
  145. /* More seek tests. */
  146. i = SDL_SeekIO(rw, -4, SDL_IO_SEEK_CUR);
  147. SDLTest_AssertPass("Call to SDL_SeekIO(...,-4,SDL_IO_SEEK_CUR) succeeded");
  148. SDLTest_AssertCheck(
  149. i == (Sint64)(sizeof(IOStreamHelloWorldTestString) - 5),
  150. "Verify seek to -4 with SDL_SeekIO (SDL_IO_SEEK_CUR), expected %i, got %i",
  151. (int)(sizeof(IOStreamHelloWorldTestString) - 5),
  152. (int)i);
  153. i = SDL_SeekIO(rw, -1, SDL_IO_SEEK_END);
  154. SDLTest_AssertPass("Call to SDL_SeekIO(...,-1,SDL_IO_SEEK_END) succeeded");
  155. SDLTest_AssertCheck(
  156. i == (Sint64)(sizeof(IOStreamHelloWorldTestString) - 2),
  157. "Verify seek to -1 with SDL_SeekIO (SDL_IO_SEEK_END), expected %i, got %i",
  158. (int)(sizeof(IOStreamHelloWorldTestString) - 2),
  159. (int)i);
  160. /* Invalid whence seek */
  161. i = SDL_SeekIO(rw, 0, (SDL_IOWhence)999);
  162. SDLTest_AssertPass("Call to SDL_SeekIO(...,0,invalid_whence) succeeded");
  163. SDLTest_AssertCheck(
  164. i == (Sint64)(-1),
  165. "Verify seek with SDL_SeekIO (invalid_whence); expected: -1, got %i",
  166. (int)i);
  167. }
  168. /**
  169. * Negative test for SDL_IOFromFile parameters
  170. *
  171. * \sa SDL_IOFromFile
  172. *
  173. */
  174. static int SDLCALL iostrm_testParamNegative(void *arg)
  175. {
  176. SDL_IOStream *iostrm;
  177. /* These should all fail. */
  178. iostrm = SDL_IOFromFile(NULL, NULL);
  179. SDLTest_AssertPass("Call to SDL_IOFromFile(NULL, NULL) succeeded");
  180. SDLTest_AssertCheck(iostrm == NULL, "Verify SDL_IOFromFile(NULL, NULL) returns NULL");
  181. iostrm = SDL_IOFromFile(NULL, "ab+");
  182. SDLTest_AssertPass("Call to SDL_IOFromFile(NULL, \"ab+\") succeeded");
  183. SDLTest_AssertCheck(iostrm == NULL, "Verify SDL_IOFromFile(NULL, \"ab+\") returns NULL");
  184. iostrm = SDL_IOFromFile(NULL, "sldfkjsldkfj");
  185. SDLTest_AssertPass("Call to SDL_IOFromFile(NULL, \"sldfkjsldkfj\") succeeded");
  186. SDLTest_AssertCheck(iostrm == NULL, "Verify SDL_IOFromFile(NULL, \"sldfkjsldkfj\") returns NULL");
  187. iostrm = SDL_IOFromFile("something", "");
  188. SDLTest_AssertPass("Call to SDL_IOFromFile(\"something\", \"\") succeeded");
  189. SDLTest_AssertCheck(iostrm == NULL, "Verify SDL_IOFromFile(\"something\", \"\") returns NULL");
  190. iostrm = SDL_IOFromFile("something", NULL);
  191. SDLTest_AssertPass("Call to SDL_IOFromFile(\"something\", NULL) succeeded");
  192. SDLTest_AssertCheck(iostrm == NULL, "Verify SDL_IOFromFile(\"something\", NULL) returns NULL");
  193. iostrm = SDL_IOFromMem(NULL, 10);
  194. SDLTest_AssertPass("Call to SDL_IOFromMem(NULL, 10) succeeded");
  195. SDLTest_AssertCheck(iostrm == NULL, "Verify SDL_IOFromMem(NULL, 10) returns NULL");
  196. iostrm = SDL_IOFromMem((void *)IOStreamAlphabetString, 0);
  197. SDLTest_AssertPass("Call to SDL_IOFromMem(data, 0) succeeded");
  198. SDLTest_AssertCheck(iostrm == NULL, "Verify SDL_IOFromMem(data, 0) returns NULL");
  199. iostrm = SDL_IOFromConstMem((const void *)IOStreamAlphabetString, 0);
  200. SDLTest_AssertPass("Call to SDL_IOFromConstMem(data, 0) succeeded");
  201. SDLTest_AssertCheck(iostrm == NULL, "Verify SDL_IOFromConstMem(data, 0) returns NULL");
  202. return TEST_COMPLETED;
  203. }
  204. /**
  205. * Tests opening from memory.
  206. *
  207. * \sa SDL_IOFromMem
  208. * \sa SDL_CloseIO
  209. */
  210. static int SDLCALL iostrm_testMem(void *arg)
  211. {
  212. char mem[sizeof(IOStreamHelloWorldTestString)];
  213. SDL_IOStream *rw;
  214. int result;
  215. /* Clear buffer */
  216. SDL_zeroa(mem);
  217. /* Open */
  218. rw = SDL_IOFromMem(mem, sizeof(IOStreamHelloWorldTestString) - 1);
  219. SDLTest_AssertPass("Call to SDL_IOFromMem() succeeded");
  220. SDLTest_AssertCheck(rw != NULL, "Verify opening memory with SDL_IOFromMem does not return NULL");
  221. /* Bail out if NULL */
  222. if (rw == NULL) {
  223. return TEST_ABORTED;
  224. }
  225. /* Run generic tests */
  226. testGenericIOStreamValidations(rw, true);
  227. /* Close */
  228. result = SDL_CloseIO(rw);
  229. SDLTest_AssertPass("Call to SDL_CloseIO() succeeded");
  230. SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result);
  231. return TEST_COMPLETED;
  232. }
  233. /**
  234. * Tests opening from memory.
  235. *
  236. * \sa SDL_IOFromConstMem
  237. * \sa SDL_CloseIO
  238. */
  239. static int SDLCALL iostrm_testConstMem(void *arg)
  240. {
  241. SDL_IOStream *rw;
  242. int result;
  243. /* Open handle */
  244. rw = SDL_IOFromConstMem(IOStreamHelloWorldCompString, sizeof(IOStreamHelloWorldCompString) - 1);
  245. SDLTest_AssertPass("Call to SDL_IOFromConstMem() succeeded");
  246. SDLTest_AssertCheck(rw != NULL, "Verify opening memory with SDL_IOFromConstMem does not return NULL");
  247. /* Bail out if NULL */
  248. if (rw == NULL) {
  249. return TEST_ABORTED;
  250. }
  251. /* Run generic tests */
  252. testGenericIOStreamValidations(rw, false);
  253. /* Close handle */
  254. result = SDL_CloseIO(rw);
  255. SDLTest_AssertPass("Call to SDL_CloseIO() succeeded");
  256. SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result);
  257. return TEST_COMPLETED;
  258. }
  259. /**
  260. * Tests dynamic memory
  261. *
  262. * \sa SDL_IOFromDynamicMem
  263. * \sa SDL_CloseIO
  264. */
  265. static int SDLCALL iostrm_testDynamicMem(void *arg)
  266. {
  267. SDL_IOStream *rw;
  268. SDL_PropertiesID props;
  269. char *mem;
  270. int result;
  271. /* Open */
  272. rw = SDL_IOFromDynamicMem();
  273. SDLTest_AssertPass("Call to SDL_IOFromDynamicMem() succeeded");
  274. SDLTest_AssertCheck(rw != NULL, "Verify opening memory with SDL_IOFromDynamicMem does not return NULL");
  275. /* Bail out if NULL */
  276. if (rw == NULL) {
  277. return TEST_ABORTED;
  278. }
  279. /* Set the chunk size to 1 byte */
  280. props = SDL_GetIOProperties(rw);
  281. SDL_SetNumberProperty(props, SDL_PROP_IOSTREAM_DYNAMIC_CHUNKSIZE_NUMBER, 1);
  282. /* Run generic tests */
  283. testGenericIOStreamValidations(rw, true);
  284. /* Get the dynamic memory and verify it */
  285. mem = (char *)SDL_GetPointerProperty(props, SDL_PROP_IOSTREAM_DYNAMIC_MEMORY_POINTER, NULL);
  286. SDLTest_AssertPass("Call to SDL_GetPointerProperty(props, SDL_PROP_IOSTREAM_DYNAMIC_MEMORY_POINTER, NULL) succeeded");
  287. SDLTest_AssertCheck(mem != NULL, "Verify memory value is not NULL");
  288. mem[SDL_GetIOSize(rw)] = '\0';
  289. SDLTest_AssertCheck(SDL_strcmp(mem, IOStreamHelloWorldTestString) == 0, "Verify memory value is correct");
  290. /* Take the memory and free it ourselves */
  291. SDL_SetPointerProperty(props, SDL_PROP_IOSTREAM_DYNAMIC_MEMORY_POINTER, NULL);
  292. SDL_free(mem);
  293. /* Close */
  294. result = SDL_CloseIO(rw);
  295. SDLTest_AssertPass("Call to SDL_CloseIO() succeeded");
  296. SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result);
  297. return TEST_COMPLETED;
  298. }
  299. /**
  300. * Tests reading from file.
  301. *
  302. * \sa SDL_IOFromFile
  303. * \sa SDL_CloseIO
  304. */
  305. static int SDLCALL iostrm_testFileRead(void *arg)
  306. {
  307. SDL_IOStream *rw;
  308. int result;
  309. /* Read test. */
  310. rw = SDL_IOFromFile(IOStreamReadTestFilename, "r");
  311. SDLTest_AssertPass("Call to SDL_IOFromFile(..,\"r\") succeeded");
  312. SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_IOFromFile in read mode does not return NULL");
  313. /* Bail out if NULL */
  314. if (rw == NULL) {
  315. return TEST_ABORTED;
  316. }
  317. /* Run generic tests */
  318. testGenericIOStreamValidations(rw, false);
  319. /* Close handle */
  320. result = SDL_CloseIO(rw);
  321. SDLTest_AssertPass("Call to SDL_CloseIO() succeeded");
  322. SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result);
  323. return TEST_COMPLETED;
  324. }
  325. /**
  326. * Tests writing from file.
  327. *
  328. * \sa SDL_IOFromFile
  329. * \sa SDL_CloseIO
  330. */
  331. static int SDLCALL iostrm_testFileWrite(void *arg)
  332. {
  333. SDL_IOStream *rw;
  334. int result;
  335. /* Write test. */
  336. rw = SDL_IOFromFile(IOStreamWriteTestFilename, "w+");
  337. SDLTest_AssertPass("Call to SDL_IOFromFile(..,\"w+\") succeeded");
  338. SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_IOFromFile in write mode does not return NULL");
  339. /* Bail out if NULL */
  340. if (rw == NULL) {
  341. return TEST_ABORTED;
  342. }
  343. /* Run generic tests */
  344. testGenericIOStreamValidations(rw, true);
  345. /* Close handle */
  346. result = SDL_CloseIO(rw);
  347. SDLTest_AssertPass("Call to SDL_CloseIO() succeeded");
  348. SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result);
  349. return TEST_COMPLETED;
  350. }
  351. /**
  352. * Tests alloc and free RW context.
  353. *
  354. * \sa SDL_OpenIO
  355. * \sa SDL_CloseIO
  356. */
  357. static int SDLCALL iostrm_testAllocFree(void *arg)
  358. {
  359. /* Allocate context */
  360. SDL_IOStreamInterface iface;
  361. SDL_IOStream *rw;
  362. SDL_INIT_INTERFACE(&iface);
  363. rw = SDL_OpenIO(&iface, NULL);
  364. SDLTest_AssertPass("Call to SDL_OpenIO() succeeded");
  365. SDLTest_AssertCheck(rw != NULL, "Validate result from SDL_OpenIO() is not NULL");
  366. if (rw == NULL) {
  367. return TEST_ABORTED;
  368. }
  369. /* Free context again */
  370. SDL_CloseIO(rw);
  371. SDLTest_AssertPass("Call to SDL_CloseIO() succeeded");
  372. return TEST_COMPLETED;
  373. }
  374. /**
  375. * Compare memory and file reads
  376. *
  377. * \sa SDL_IOFromMem
  378. * \sa SDL_IOFromFile
  379. */
  380. static int SDLCALL iostrm_testCompareRWFromMemWithRWFromFile(void *arg)
  381. {
  382. int slen = 26;
  383. char buffer_file[27];
  384. char buffer_mem[27];
  385. size_t rv_file;
  386. size_t rv_mem;
  387. Uint64 sv_file;
  388. Uint64 sv_mem;
  389. SDL_IOStream *iostrm_file;
  390. SDL_IOStream *iostrm_mem;
  391. int size;
  392. int result;
  393. for (size = 5; size < 10; size++) {
  394. /* Terminate buffer */
  395. buffer_file[slen] = 0;
  396. buffer_mem[slen] = 0;
  397. /* Read/seek from memory */
  398. iostrm_mem = SDL_IOFromMem((void *)IOStreamAlphabetString, slen);
  399. SDLTest_AssertPass("Call to SDL_IOFromMem()");
  400. rv_mem = SDL_ReadIO(iostrm_mem, buffer_mem, size * 6);
  401. SDLTest_AssertPass("Call to SDL_ReadIO(mem, size=%d)", size * 6);
  402. sv_mem = SDL_SeekIO(iostrm_mem, 0, SEEK_END);
  403. SDLTest_AssertPass("Call to SDL_SeekIO(mem,SEEK_END)");
  404. result = SDL_CloseIO(iostrm_mem);
  405. SDLTest_AssertPass("Call to SDL_CloseIO(mem)");
  406. SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result);
  407. /* Read/see from file */
  408. iostrm_file = SDL_IOFromFile(IOStreamAlphabetFilename, "r");
  409. SDLTest_AssertPass("Call to SDL_IOFromFile()");
  410. rv_file = SDL_ReadIO(iostrm_file, buffer_file, size * 6);
  411. SDLTest_AssertPass("Call to SDL_ReadIO(file, size=%d)", size * 6);
  412. sv_file = SDL_SeekIO(iostrm_file, 0, SEEK_END);
  413. SDLTest_AssertPass("Call to SDL_SeekIO(file,SEEK_END)");
  414. result = SDL_CloseIO(iostrm_file);
  415. SDLTest_AssertPass("Call to SDL_CloseIO(file)");
  416. SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result);
  417. /* Compare */
  418. SDLTest_AssertCheck(rv_mem == rv_file, "Verify returned read blocks matches for mem and file reads; got: rv_mem=%d rv_file=%d", (int)rv_mem, (int)rv_file);
  419. SDLTest_AssertCheck(sv_mem == sv_file, "Verify SEEK_END position matches for mem and file seeks; got: sv_mem=%d sv_file=%d", (int)sv_mem, (int)sv_file);
  420. SDLTest_AssertCheck(buffer_mem[slen] == 0, "Verify mem buffer termination; expected: 0, got: %d", buffer_mem[slen]);
  421. SDLTest_AssertCheck(buffer_file[slen] == 0, "Verify file buffer termination; expected: 0, got: %d", buffer_file[slen]);
  422. SDLTest_AssertCheck(
  423. SDL_strncmp(buffer_mem, IOStreamAlphabetString, slen) == 0,
  424. "Verify mem buffer contain alphabet string; expected: %s, got: %s", IOStreamAlphabetString, buffer_mem);
  425. SDLTest_AssertCheck(
  426. SDL_strncmp(buffer_file, IOStreamAlphabetString, slen) == 0,
  427. "Verify file buffer contain alphabet string; expected: %s, got: %s", IOStreamAlphabetString, buffer_file);
  428. }
  429. return TEST_COMPLETED;
  430. }
  431. /**
  432. * Tests writing and reading from file using endian aware functions.
  433. *
  434. * \sa SDL_IOFromFile
  435. * \sa SDL_CloseIO
  436. * \sa SDL_ReadU16BE
  437. * \sa SDL_WriteU16BE
  438. */
  439. static int SDLCALL iostrm_testFileWriteReadEndian(void *arg)
  440. {
  441. SDL_IOStream *rw;
  442. Sint64 result;
  443. int mode;
  444. Uint16 BE16value;
  445. Uint32 BE32value;
  446. Uint64 BE64value;
  447. Uint16 LE16value;
  448. Uint32 LE32value;
  449. Uint64 LE64value;
  450. Uint16 BE16test;
  451. Uint32 BE32test;
  452. Uint64 BE64test;
  453. Uint16 LE16test;
  454. Uint32 LE32test;
  455. Uint64 LE64test;
  456. bool bresult;
  457. int cresult;
  458. for (mode = 0; mode < 3; mode++) {
  459. /* Create test data */
  460. switch (mode) {
  461. default:
  462. case 0:
  463. SDLTest_Log("All 0 values");
  464. BE16value = 0;
  465. BE32value = 0;
  466. BE64value = 0;
  467. LE16value = 0;
  468. LE32value = 0;
  469. LE64value = 0;
  470. break;
  471. case 1:
  472. SDLTest_Log("All 1 values");
  473. BE16value = 1;
  474. BE32value = 1;
  475. BE64value = 1;
  476. LE16value = 1;
  477. LE32value = 1;
  478. LE64value = 1;
  479. break;
  480. case 2:
  481. SDLTest_Log("Random values");
  482. BE16value = SDLTest_RandomUint16();
  483. BE32value = SDLTest_RandomUint32();
  484. BE64value = SDLTest_RandomUint64();
  485. LE16value = SDLTest_RandomUint16();
  486. LE32value = SDLTest_RandomUint32();
  487. LE64value = SDLTest_RandomUint64();
  488. break;
  489. }
  490. /* Write test. */
  491. rw = SDL_IOFromFile(IOStreamWriteTestFilename, "w+");
  492. SDLTest_AssertPass("Call to SDL_IOFromFile(..,\"w+\")");
  493. SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_IOFromFile in write mode does not return NULL");
  494. /* Bail out if NULL */
  495. if (rw == NULL) {
  496. return TEST_ABORTED;
  497. }
  498. /* Write test data */
  499. bresult = SDL_WriteU16BE(rw, BE16value);
  500. SDLTest_AssertPass("Call to SDL_WriteU16BE()");
  501. SDLTest_AssertCheck(bresult == true, "Validate object written, expected: true, got: false");
  502. bresult = SDL_WriteU32BE(rw, BE32value);
  503. SDLTest_AssertPass("Call to SDL_WriteU32BE()");
  504. SDLTest_AssertCheck(bresult == true, "Validate object written, expected: true, got: false");
  505. bresult = SDL_WriteU64BE(rw, BE64value);
  506. SDLTest_AssertPass("Call to SDL_WriteU64BE()");
  507. SDLTest_AssertCheck(bresult == true, "Validate object written, expected: true, got: false");
  508. bresult = SDL_WriteU16LE(rw, LE16value);
  509. SDLTest_AssertPass("Call to SDL_WriteU16LE()");
  510. SDLTest_AssertCheck(bresult == true, "Validate object written, expected: true, got: false");
  511. bresult = SDL_WriteU32LE(rw, LE32value);
  512. SDLTest_AssertPass("Call to SDL_WriteU32LE()");
  513. SDLTest_AssertCheck(bresult == true, "Validate object written, expected: true, got: false");
  514. bresult = SDL_WriteU64LE(rw, LE64value);
  515. SDLTest_AssertPass("Call to SDL_WriteU64LE()");
  516. SDLTest_AssertCheck(bresult == true, "Validate object written, expected: true, got: false");
  517. /* Test seek to start */
  518. result = SDL_SeekIO(rw, 0, SDL_IO_SEEK_SET);
  519. SDLTest_AssertPass("Call to SDL_SeekIO succeeded");
  520. SDLTest_AssertCheck(result == 0, "Verify result from position 0 with SDL_SeekIO, expected 0, got %i", (int)result);
  521. /* Read test data */
  522. bresult = SDL_ReadU16BE(rw, &BE16test);
  523. SDLTest_AssertPass("Call to SDL_ReadU16BE()");
  524. SDLTest_AssertCheck(bresult == true, "Validate object read, expected: true, got: false");
  525. SDLTest_AssertCheck(BE16test == BE16value, "Validate object read from SDL_ReadU16BE, expected: %hu, got: %hu", BE16value, BE16test);
  526. bresult = SDL_ReadU32BE(rw, &BE32test);
  527. SDLTest_AssertPass("Call to SDL_ReadU32BE()");
  528. SDLTest_AssertCheck(bresult == true, "Validate object read, expected: true, got: false");
  529. SDLTest_AssertCheck(BE32test == BE32value, "Validate object read from SDL_ReadU32BE, expected: %" SDL_PRIu32 ", got: %" SDL_PRIu32, BE32value, BE32test);
  530. bresult = SDL_ReadU64BE(rw, &BE64test);
  531. SDLTest_AssertPass("Call to SDL_ReadU64BE()");
  532. SDLTest_AssertCheck(bresult == true, "Validate object read, expected: true, got: false");
  533. SDLTest_AssertCheck(BE64test == BE64value, "Validate object read from SDL_ReadU64BE, expected: %" SDL_PRIu64 ", got: %" SDL_PRIu64, BE64value, BE64test);
  534. bresult = SDL_ReadU16LE(rw, &LE16test);
  535. SDLTest_AssertPass("Call to SDL_ReadU16LE()");
  536. SDLTest_AssertCheck(bresult == true, "Validate object read, expected: true, got: false");
  537. SDLTest_AssertCheck(LE16test == LE16value, "Validate object read from SDL_ReadU16LE, expected: %hu, got: %hu", LE16value, LE16test);
  538. bresult = SDL_ReadU32LE(rw, &LE32test);
  539. SDLTest_AssertPass("Call to SDL_ReadU32LE()");
  540. SDLTest_AssertCheck(bresult == true, "Validate object read, expected: true, got: false");
  541. SDLTest_AssertCheck(LE32test == LE32value, "Validate object read from SDL_ReadU32LE, expected: %" SDL_PRIu32 ", got: %" SDL_PRIu32, LE32value, LE32test);
  542. bresult = SDL_ReadU64LE(rw, &LE64test);
  543. SDLTest_AssertPass("Call to SDL_ReadU64LE()");
  544. SDLTest_AssertCheck(bresult == true, "Validate object read, expected: true, got: false");
  545. SDLTest_AssertCheck(LE64test == LE64value, "Validate object read from SDL_ReadU64LE, expected: %" SDL_PRIu64 ", got: %" SDL_PRIu64, LE64value, LE64test);
  546. /* Close handle */
  547. cresult = SDL_CloseIO(rw);
  548. SDLTest_AssertPass("Call to SDL_CloseIO() succeeded");
  549. SDLTest_AssertCheck(cresult == true, "Verify result value is true; got: %d", cresult);
  550. }
  551. return TEST_COMPLETED;
  552. }
  553. /* ================= Test References ================== */
  554. /* IOStream test cases */
  555. static const SDLTest_TestCaseReference iostrmTest1 = {
  556. iostrm_testParamNegative, "iostrm_testParamNegative", "Negative test for SDL_IOFromFile parameters", TEST_ENABLED
  557. };
  558. static const SDLTest_TestCaseReference iostrmTest2 = {
  559. iostrm_testMem, "iostrm_testMem", "Tests opening from memory", TEST_ENABLED
  560. };
  561. static const SDLTest_TestCaseReference iostrmTest3 = {
  562. iostrm_testConstMem, "iostrm_testConstMem", "Tests opening from (const) memory", TEST_ENABLED
  563. };
  564. static const SDLTest_TestCaseReference iostrmTest4 = {
  565. iostrm_testDynamicMem, "iostrm_testDynamicMem", "Tests opening dynamic memory", TEST_ENABLED
  566. };
  567. static const SDLTest_TestCaseReference iostrmTest5 = {
  568. iostrm_testFileRead, "iostrm_testFileRead", "Tests reading from a file", TEST_ENABLED
  569. };
  570. static const SDLTest_TestCaseReference iostrmTest6 = {
  571. iostrm_testFileWrite, "iostrm_testFileWrite", "Test writing to a file", TEST_ENABLED
  572. };
  573. static const SDLTest_TestCaseReference iostrmTest7 = {
  574. iostrm_testAllocFree, "iostrm_testAllocFree", "Test alloc and free of RW context", TEST_ENABLED
  575. };
  576. static const SDLTest_TestCaseReference iostrmTest8 = {
  577. iostrm_testFileWriteReadEndian, "iostrm_testFileWriteReadEndian", "Test writing and reading via the Endian aware functions", TEST_ENABLED
  578. };
  579. static const SDLTest_TestCaseReference iostrmTest9 = {
  580. iostrm_testCompareRWFromMemWithRWFromFile, "iostrm_testCompareRWFromMemWithRWFromFile", "Compare RWFromMem and RWFromFile IOStream for read and seek", TEST_ENABLED
  581. };
  582. /* Sequence of IOStream test cases */
  583. static const SDLTest_TestCaseReference *iostrmTests[] = {
  584. &iostrmTest1, &iostrmTest2, &iostrmTest3, &iostrmTest4, &iostrmTest5, &iostrmTest6,
  585. &iostrmTest7, &iostrmTest8, &iostrmTest9, NULL
  586. };
  587. /* IOStream test suite (global) */
  588. SDLTest_TestSuiteReference iostrmTestSuite = {
  589. "IOStream",
  590. IOStreamSetUp,
  591. iostrmTests,
  592. IOStreamTearDown
  593. };