testautomation_iostream.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  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. SDL_IOStream *handle;
  30. size_t writtenLen;
  31. bool result;
  32. /* Clean up from previous runs (if any); ignore errors */
  33. SDL_RemovePath(IOStreamReadTestFilename);
  34. SDL_RemovePath(IOStreamWriteTestFilename);
  35. SDL_RemovePath(IOStreamAlphabetFilename);
  36. /* Create a test file */
  37. handle = SDL_IOFromFile(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 = SDL_WriteIO(handle, IOStreamHelloWorldTestString, fileLen);
  45. SDLTest_AssertCheck(fileLen == writtenLen, "Verify number of written bytes, expected %i, got %i", (int)fileLen, (int)writtenLen);
  46. result = SDL_CloseIO(handle);
  47. SDLTest_AssertCheck(result == true, "Verify result from SDL_CloseIO, expected true, got %s", result ? "true" : "false");
  48. /* Create a second test file */
  49. handle = SDL_IOFromFile(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 = SDL_WriteIO(handle, IOStreamAlphabetString, fileLen);
  57. SDLTest_AssertCheck(fileLen == writtenLen, "Verify number of written bytes, expected %i, got %i", (int)fileLen, (int)writtenLen);
  58. result = SDL_CloseIO(handle);
  59. SDLTest_AssertCheck(result == true, "Verify result from SDL_CloseIO, expected true, got %s", result ? "true" : "false");
  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. * Makes sure parameters work properly. Local helper function.
  170. *
  171. * \sa SDL_SeekIO
  172. * \sa SDL_ReadIO
  173. */
  174. static void testEmptyIOStreamValidations(SDL_IOStream *rw, bool write)
  175. {
  176. char con[sizeof(IOStreamHelloWorldTestString)];
  177. char buf[sizeof(IOStreamHelloWorldTestString)];
  178. Sint64 i;
  179. size_t s;
  180. int seekPos = SDLTest_RandomIntegerInRange(4, 8);
  181. /* Clear control & buffer */
  182. SDL_zeroa(con);
  183. SDL_zeroa(buf);
  184. /* Set to start. */
  185. i = SDL_SeekIO(rw, 0, SDL_IO_SEEK_SET);
  186. SDLTest_AssertPass("Call to SDL_SeekIO succeeded");
  187. SDLTest_AssertCheck(i == (Sint64)0, "Verify seek to 0 with SDL_SeekIO (SDL_IO_SEEK_SET), expected 0, got %" SDL_PRIs64, i);
  188. /* Test write */
  189. s = SDL_WriteIO(rw, IOStreamHelloWorldTestString, sizeof(IOStreamHelloWorldTestString) - 1);
  190. SDLTest_AssertPass("Call to SDL_WriteIO succeeded");
  191. if (write) {
  192. SDLTest_AssertCheck(s == 0, "Verify result of writing with SDL_WriteIO, expected 0, got %i", (int)s);
  193. } else {
  194. SDLTest_AssertCheck(s == 0, "Verify result of writing with SDL_WriteIO, expected 0, got %i", (int)s);
  195. }
  196. /* Test seek to random position */
  197. i = SDL_SeekIO(rw, seekPos, SDL_IO_SEEK_SET);
  198. SDLTest_AssertPass("Call to SDL_SeekIO succeeded");
  199. SDLTest_AssertCheck(i == 0, "Verify seek to %i with SDL_SeekIO (SDL_IO_SEEK_SET), expected 0, got %" SDL_PRIs64, seekPos, i);
  200. /* Test seek back to start */
  201. i = SDL_SeekIO(rw, 0, SDL_IO_SEEK_SET);
  202. SDLTest_AssertPass("Call to SDL_SeekIO succeeded");
  203. SDLTest_AssertCheck(i == (Sint64)0, "Verify seek to 0 with SDL_SeekIO (SDL_IO_SEEK_SET), expected 0, got %" SDL_PRIs64, i);
  204. /* Test read */
  205. s = SDL_ReadIO(rw, buf, sizeof(IOStreamHelloWorldTestString) - 1);
  206. SDLTest_AssertPass("Call to SDL_ReadIO succeeded");
  207. SDLTest_AssertCheck(s == 0, "Verify result from SDL_ReadIO, expected 0, got %i", (int)s);
  208. SDLTest_AssertCheck(
  209. SDL_memcmp(buf, con, sizeof(IOStreamHelloWorldTestString) - 1) == 0,
  210. "Verify that buffer remains unchanged, expected '%s', got '%s'", con, buf);
  211. /* Test seek back to start */
  212. i = SDL_SeekIO(rw, 0, SDL_IO_SEEK_SET);
  213. SDLTest_AssertPass("Call to SDL_SeekIO succeeded");
  214. SDLTest_AssertCheck(i == (Sint64)0, "Verify seek to 0 with SDL_SeekIO (SDL_IO_SEEK_SET), expected 0, got %" SDL_PRIs64, i);
  215. /* Test printf */
  216. s = SDL_IOprintf(rw, "%s", IOStreamHelloWorldTestString);
  217. SDLTest_AssertPass("Call to SDL_IOprintf succeeded");
  218. if (write) {
  219. SDLTest_AssertCheck(s == 0, "Verify result of writing with SDL_IOprintf, expected 0, got %i", (int)s);
  220. } else {
  221. SDLTest_AssertCheck(s == 0, "Verify result of writing with SDL_WriteIO, expected 0, got %i", (int)s);
  222. }
  223. /* Test seek back to start */
  224. i = SDL_SeekIO(rw, 0, SDL_IO_SEEK_SET);
  225. SDLTest_AssertPass("Call to SDL_SeekIO succeeded");
  226. SDLTest_AssertCheck(i == (Sint64)0, "Verify seek to 0 with SDL_SeekIO (SDL_IO_SEEK_SET), expected 0, got %" SDL_PRIs64, i);
  227. /* Test read */
  228. s = SDL_ReadIO(rw, buf, sizeof(IOStreamHelloWorldTestString) - 1);
  229. SDLTest_AssertPass("Call to SDL_ReadIO succeeded");
  230. SDLTest_AssertCheck(
  231. s == 0,
  232. "Verify result from SDL_ReadIO, expected 0, got %i",
  233. (int)s);
  234. SDLTest_AssertCheck(
  235. SDL_memcmp(buf, con, sizeof(IOStreamHelloWorldTestString) - 1) == 0,
  236. "Verify that buffer remains unchanged, expected '%s', got '%s'", con, buf);
  237. /* More seek tests. */
  238. i = SDL_SeekIO(rw, -4, SDL_IO_SEEK_CUR);
  239. SDLTest_AssertPass("Call to SDL_SeekIO(...,-4,SDL_IO_SEEK_CUR) succeeded");
  240. SDLTest_AssertCheck(
  241. i == 0,
  242. "Verify seek to -4 with SDL_SeekIO (SDL_IO_SEEK_CUR), expected 0, got %i",
  243. (int)i);
  244. i = SDL_SeekIO(rw, -1, SDL_IO_SEEK_END);
  245. SDLTest_AssertPass("Call to SDL_SeekIO(...,-1,SDL_IO_SEEK_END) succeeded");
  246. SDLTest_AssertCheck(
  247. i == 0,
  248. "Verify seek to -1 with SDL_SeekIO (SDL_IO_SEEK_END), expected 0, got %i",
  249. (int)i);
  250. /* Invalid whence seek */
  251. i = SDL_SeekIO(rw, 0, (SDL_IOWhence)999);
  252. SDLTest_AssertPass("Call to SDL_SeekIO(...,0,invalid_whence) succeeded");
  253. SDLTest_AssertCheck(
  254. i == (Sint64)(-1),
  255. "Verify seek with SDL_SeekIO (invalid_whence); expected: -1, got %i",
  256. (int)i);
  257. }
  258. /**
  259. * Negative test for SDL_IOFromFile parameters
  260. *
  261. * \sa SDL_IOFromFile
  262. *
  263. */
  264. static int SDLCALL iostrm_testParamNegative(void *arg)
  265. {
  266. SDL_IOStream *iostrm;
  267. /* These should all fail. */
  268. iostrm = SDL_IOFromFile(NULL, NULL);
  269. SDLTest_AssertPass("Call to SDL_IOFromFile(NULL, NULL) succeeded");
  270. SDLTest_AssertCheck(iostrm == NULL, "Verify SDL_IOFromFile(NULL, NULL) returns NULL");
  271. iostrm = SDL_IOFromFile(NULL, "ab+");
  272. SDLTest_AssertPass("Call to SDL_IOFromFile(NULL, \"ab+\") succeeded");
  273. SDLTest_AssertCheck(iostrm == NULL, "Verify SDL_IOFromFile(NULL, \"ab+\") returns NULL");
  274. iostrm = SDL_IOFromFile(NULL, "sldfkjsldkfj");
  275. SDLTest_AssertPass("Call to SDL_IOFromFile(NULL, \"sldfkjsldkfj\") succeeded");
  276. SDLTest_AssertCheck(iostrm == NULL, "Verify SDL_IOFromFile(NULL, \"sldfkjsldkfj\") returns NULL");
  277. iostrm = SDL_IOFromFile("something", "");
  278. SDLTest_AssertPass("Call to SDL_IOFromFile(\"something\", \"\") succeeded");
  279. SDLTest_AssertCheck(iostrm == NULL, "Verify SDL_IOFromFile(\"something\", \"\") returns NULL");
  280. iostrm = SDL_IOFromFile("something", NULL);
  281. SDLTest_AssertPass("Call to SDL_IOFromFile(\"something\", NULL) succeeded");
  282. SDLTest_AssertCheck(iostrm == NULL, "Verify SDL_IOFromFile(\"something\", NULL) returns NULL");
  283. iostrm = SDL_IOFromMem(NULL, 10);
  284. SDLTest_AssertPass("Call to SDL_IOFromMem(NULL, 10) succeeded");
  285. SDLTest_AssertCheck(iostrm == NULL, "Verify SDL_IOFromMem(NULL, 10) returns NULL");
  286. return TEST_COMPLETED;
  287. }
  288. /**
  289. * Tests opening from memory.
  290. *
  291. * \sa SDL_IOFromMem
  292. * \sa SDL_CloseIO
  293. */
  294. static int SDLCALL iostrm_testMem(void *arg)
  295. {
  296. char mem[sizeof(IOStreamHelloWorldTestString)];
  297. SDL_IOStream *rw;
  298. int result;
  299. /* Clear buffer */
  300. SDL_zeroa(mem);
  301. /* Open */
  302. rw = SDL_IOFromMem(mem, sizeof(IOStreamHelloWorldTestString) - 1);
  303. SDLTest_AssertPass("Call to SDL_IOFromMem() succeeded");
  304. SDLTest_AssertCheck(rw != NULL, "Verify opening memory with SDL_IOFromMem does not return NULL");
  305. /* Bail out if NULL */
  306. if (rw == NULL) {
  307. return TEST_ABORTED;
  308. }
  309. /* Run generic tests */
  310. testGenericIOStreamValidations(rw, true);
  311. /* Close */
  312. result = SDL_CloseIO(rw);
  313. SDLTest_AssertPass("Call to SDL_CloseIO() succeeded");
  314. SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result);
  315. return TEST_COMPLETED;
  316. }
  317. /**
  318. * Tests opening from memory.
  319. *
  320. * \sa SDL_IOFromConstMem
  321. * \sa SDL_CloseIO
  322. */
  323. static int SDLCALL iostrm_testConstMem(void *arg)
  324. {
  325. SDL_IOStream *rw;
  326. int result;
  327. /* Open handle */
  328. rw = SDL_IOFromConstMem(IOStreamHelloWorldCompString, sizeof(IOStreamHelloWorldCompString) - 1);
  329. SDLTest_AssertPass("Call to SDL_IOFromConstMem() succeeded");
  330. SDLTest_AssertCheck(rw != NULL, "Verify opening memory with SDL_IOFromConstMem does not return NULL");
  331. /* Bail out if NULL */
  332. if (rw == NULL) {
  333. return TEST_ABORTED;
  334. }
  335. /* Run generic tests */
  336. testGenericIOStreamValidations(rw, false);
  337. /* Close handle */
  338. result = SDL_CloseIO(rw);
  339. SDLTest_AssertPass("Call to SDL_CloseIO() succeeded");
  340. SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result);
  341. return TEST_COMPLETED;
  342. }
  343. /**
  344. * Tests opening nothing.
  345. *
  346. * \sa SDL_IOFromMem
  347. * \sa SDL_CloseIO
  348. */
  349. static int SDLCALL iostrm_testMemEmpty(void *arg)
  350. {
  351. char mem[sizeof(IOStreamHelloWorldTestString)];
  352. SDL_IOStream *rw;
  353. int result;
  354. /* Clear buffer */
  355. SDL_zeroa(mem);
  356. /* Open empty */
  357. rw = SDL_IOFromMem(mem, 0);
  358. SDLTest_AssertPass("Call to SDL_IOFromMem() succeeded");
  359. SDLTest_AssertCheck(rw != NULL, "Verify opening memory with SDL_IOFromMem does not return NULL");
  360. /* Bail out if NULL */
  361. if (rw == NULL) {
  362. return TEST_ABORTED;
  363. }
  364. /* Run generic tests */
  365. testEmptyIOStreamValidations(rw, true);
  366. /* Close */
  367. result = SDL_CloseIO(rw);
  368. SDLTest_AssertPass("Call to SDL_CloseIO() succeeded");
  369. SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result);
  370. return TEST_COMPLETED;
  371. }
  372. /**
  373. * Tests opening nothing.
  374. *
  375. * \sa SDL_IOFromMem
  376. * \sa SDL_CloseIO
  377. */
  378. static int SDLCALL iostrm_testConstMemEmpty(void *arg)
  379. {
  380. SDL_IOStream *rw;
  381. int result;
  382. /* Open handle */
  383. rw = SDL_IOFromConstMem(IOStreamHelloWorldCompString, 0);
  384. SDLTest_AssertPass("Call to SDL_IOFromConstMem() succeeded");
  385. SDLTest_AssertCheck(rw != NULL, "Verify opening memory with SDL_IOFromConstMem does not return NULL");
  386. /* Bail out if NULL */
  387. if (rw == NULL) {
  388. return TEST_ABORTED;
  389. }
  390. /* Run generic tests */
  391. testEmptyIOStreamValidations(rw, false);
  392. /* Close handle */
  393. result = SDL_CloseIO(rw);
  394. SDLTest_AssertPass("Call to SDL_CloseIO() succeeded");
  395. SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result);
  396. return TEST_COMPLETED;
  397. }
  398. static int free_call_count;
  399. void SDLCALL test_free(void* mem) {
  400. free_call_count++;
  401. SDL_free(mem);
  402. }
  403. static int SDLCALL iostrm_testMemWithFree(void *arg)
  404. {
  405. void *mem;
  406. SDL_IOStream *rw;
  407. int result;
  408. /* Allocate some memory */
  409. mem = SDL_malloc(sizeof(IOStreamHelloWorldCompString) - 1);
  410. if (mem == NULL) {
  411. return TEST_ABORTED;
  412. }
  413. /* Open handle */
  414. rw = SDL_IOFromMem(mem, sizeof(IOStreamHelloWorldCompString) - 1);
  415. SDLTest_AssertPass("Call to SDL_IOFromMem() succeeded");
  416. SDLTest_AssertCheck(rw != NULL, "Verify opening memory with SDL_IOFromMem does not return NULL");
  417. /* Bail out if NULL */
  418. if (rw == NULL) {
  419. return TEST_ABORTED;
  420. }
  421. /* Set the free function */
  422. free_call_count = 0;
  423. result = SDL_SetPointerProperty(SDL_GetIOProperties(rw), SDL_PROP_IOSTREAM_MEMORY_FREE_FUNC_POINTER, test_free);
  424. SDLTest_AssertPass("Call to SDL_SetPointerProperty() succeeded");
  425. SDLTest_AssertCheck(result == true, "Verify result value is true; got %d", result);
  426. /* Run generic tests */
  427. testGenericIOStreamValidations(rw, true);
  428. /* Close handle */
  429. result = SDL_CloseIO(rw);
  430. SDLTest_AssertPass("Call to SDL_CloseIO() succeeded");
  431. SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result);
  432. SDLTest_AssertCheck(free_call_count == 1, "Verify the custom free function was called once; call count: %d", free_call_count);
  433. return TEST_COMPLETED;
  434. }
  435. /**
  436. * Tests dynamic memory
  437. *
  438. * \sa SDL_IOFromDynamicMem
  439. * \sa SDL_CloseIO
  440. */
  441. static int SDLCALL iostrm_testDynamicMem(void *arg)
  442. {
  443. SDL_IOStream *rw;
  444. SDL_PropertiesID props;
  445. char *mem;
  446. int result;
  447. /* Open */
  448. rw = SDL_IOFromDynamicMem();
  449. SDLTest_AssertPass("Call to SDL_IOFromDynamicMem() succeeded");
  450. SDLTest_AssertCheck(rw != NULL, "Verify opening memory with SDL_IOFromDynamicMem does not return NULL");
  451. /* Bail out if NULL */
  452. if (rw == NULL) {
  453. return TEST_ABORTED;
  454. }
  455. /* Set the chunk size to 1 byte */
  456. props = SDL_GetIOProperties(rw);
  457. SDL_SetNumberProperty(props, SDL_PROP_IOSTREAM_DYNAMIC_CHUNKSIZE_NUMBER, 1);
  458. /* Run generic tests */
  459. testGenericIOStreamValidations(rw, true);
  460. /* Get the dynamic memory and verify it */
  461. mem = (char *)SDL_GetPointerProperty(props, SDL_PROP_IOSTREAM_DYNAMIC_MEMORY_POINTER, NULL);
  462. SDLTest_AssertPass("Call to SDL_GetPointerProperty(props, SDL_PROP_IOSTREAM_DYNAMIC_MEMORY_POINTER, NULL) succeeded");
  463. SDLTest_AssertCheck(mem != NULL, "Verify memory value is not NULL");
  464. mem[SDL_GetIOSize(rw)] = '\0';
  465. SDLTest_AssertCheck(SDL_strcmp(mem, IOStreamHelloWorldTestString) == 0, "Verify memory value is correct");
  466. /* Take the memory and free it ourselves */
  467. SDL_SetPointerProperty(props, SDL_PROP_IOSTREAM_DYNAMIC_MEMORY_POINTER, NULL);
  468. SDL_free(mem);
  469. /* Close */
  470. result = SDL_CloseIO(rw);
  471. SDLTest_AssertPass("Call to SDL_CloseIO() succeeded");
  472. SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result);
  473. return TEST_COMPLETED;
  474. }
  475. /**
  476. * Tests reading from file.
  477. *
  478. * \sa SDL_IOFromFile
  479. * \sa SDL_CloseIO
  480. */
  481. static int SDLCALL iostrm_testFileRead(void *arg)
  482. {
  483. SDL_IOStream *rw;
  484. int result;
  485. /* Read test. */
  486. rw = SDL_IOFromFile(IOStreamReadTestFilename, "r");
  487. SDLTest_AssertPass("Call to SDL_IOFromFile(..,\"r\") succeeded");
  488. SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_IOFromFile in read mode does not return NULL");
  489. /* Bail out if NULL */
  490. if (rw == NULL) {
  491. return TEST_ABORTED;
  492. }
  493. /* Run generic tests */
  494. testGenericIOStreamValidations(rw, false);
  495. /* Close handle */
  496. result = SDL_CloseIO(rw);
  497. SDLTest_AssertPass("Call to SDL_CloseIO() succeeded");
  498. SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result);
  499. return TEST_COMPLETED;
  500. }
  501. /**
  502. * Tests writing from file.
  503. *
  504. * \sa SDL_IOFromFile
  505. * \sa SDL_CloseIO
  506. */
  507. static int SDLCALL iostrm_testFileWrite(void *arg)
  508. {
  509. SDL_IOStream *rw;
  510. int result;
  511. /* Write test. */
  512. rw = SDL_IOFromFile(IOStreamWriteTestFilename, "w+x");
  513. SDLTest_AssertPass("Call to SDL_IOFromFile(..,\"w+x\") succeeded");
  514. SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_IOFromFile in exclusive write mode does not return NULL");
  515. /* Bail out if NULL */
  516. if (rw == NULL) {
  517. return TEST_ABORTED;
  518. }
  519. /* Run generic tests */
  520. testGenericIOStreamValidations(rw, true);
  521. /* Close handle */
  522. result = SDL_CloseIO(rw);
  523. SDLTest_AssertPass("Call to SDL_CloseIO() succeeded");
  524. SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result);
  525. /* Exclusively opening an existing file should fail. */
  526. rw = SDL_IOFromFile(IOStreamWriteTestFilename, "wx");
  527. SDLTest_AssertPass("Call to SDL_IOFromFile(..,\"wx\") succeeded");
  528. SDLTest_AssertCheck(rw == NULL, "Verify opening existing file with SDL_IOFromFile in exclusive write mode returns NULL");
  529. return TEST_COMPLETED;
  530. }
  531. /**
  532. * Tests alloc and free RW context.
  533. *
  534. * \sa SDL_OpenIO
  535. * \sa SDL_CloseIO
  536. */
  537. static int SDLCALL iostrm_testAllocFree(void *arg)
  538. {
  539. /* Allocate context */
  540. SDL_IOStreamInterface iface;
  541. SDL_IOStream *rw;
  542. SDL_INIT_INTERFACE(&iface);
  543. rw = SDL_OpenIO(&iface, NULL);
  544. SDLTest_AssertPass("Call to SDL_OpenIO() succeeded");
  545. SDLTest_AssertCheck(rw != NULL, "Validate result from SDL_OpenIO() is not NULL");
  546. if (rw == NULL) {
  547. return TEST_ABORTED;
  548. }
  549. /* Free context again */
  550. SDL_CloseIO(rw);
  551. SDLTest_AssertPass("Call to SDL_CloseIO() succeeded");
  552. return TEST_COMPLETED;
  553. }
  554. /**
  555. * Compare memory and file reads
  556. *
  557. * \sa SDL_IOFromMem
  558. * \sa SDL_IOFromFile
  559. */
  560. static int SDLCALL iostrm_testCompareRWFromMemWithRWFromFile(void *arg)
  561. {
  562. int slen = 26;
  563. char buffer_file[27];
  564. char buffer_mem[27];
  565. size_t rv_file;
  566. size_t rv_mem;
  567. Uint64 sv_file;
  568. Uint64 sv_mem;
  569. SDL_IOStream *iostrm_file;
  570. SDL_IOStream *iostrm_mem;
  571. int size;
  572. int result;
  573. for (size = 5; size < 10; size++) {
  574. /* Terminate buffer */
  575. buffer_file[slen] = 0;
  576. buffer_mem[slen] = 0;
  577. /* Read/seek from memory */
  578. iostrm_mem = SDL_IOFromMem((void *)IOStreamAlphabetString, slen);
  579. SDLTest_AssertPass("Call to SDL_IOFromMem()");
  580. rv_mem = SDL_ReadIO(iostrm_mem, buffer_mem, size * 6);
  581. SDLTest_AssertPass("Call to SDL_ReadIO(mem, size=%d)", size * 6);
  582. sv_mem = SDL_SeekIO(iostrm_mem, 0, SEEK_END);
  583. SDLTest_AssertPass("Call to SDL_SeekIO(mem,SEEK_END)");
  584. result = SDL_CloseIO(iostrm_mem);
  585. SDLTest_AssertPass("Call to SDL_CloseIO(mem)");
  586. SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result);
  587. /* Read/see from file */
  588. iostrm_file = SDL_IOFromFile(IOStreamAlphabetFilename, "r");
  589. SDLTest_AssertPass("Call to SDL_IOFromFile()");
  590. rv_file = SDL_ReadIO(iostrm_file, buffer_file, size * 6);
  591. SDLTest_AssertPass("Call to SDL_ReadIO(file, size=%d)", size * 6);
  592. sv_file = SDL_SeekIO(iostrm_file, 0, SEEK_END);
  593. SDLTest_AssertPass("Call to SDL_SeekIO(file,SEEK_END)");
  594. result = SDL_CloseIO(iostrm_file);
  595. SDLTest_AssertPass("Call to SDL_CloseIO(file)");
  596. SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result);
  597. /* Compare */
  598. 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);
  599. 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);
  600. SDLTest_AssertCheck(buffer_mem[slen] == 0, "Verify mem buffer termination; expected: 0, got: %d", buffer_mem[slen]);
  601. SDLTest_AssertCheck(buffer_file[slen] == 0, "Verify file buffer termination; expected: 0, got: %d", buffer_file[slen]);
  602. SDLTest_AssertCheck(
  603. SDL_strncmp(buffer_mem, IOStreamAlphabetString, slen) == 0,
  604. "Verify mem buffer contain alphabet string; expected: %s, got: %s", IOStreamAlphabetString, buffer_mem);
  605. SDLTest_AssertCheck(
  606. SDL_strncmp(buffer_file, IOStreamAlphabetString, slen) == 0,
  607. "Verify file buffer contain alphabet string; expected: %s, got: %s", IOStreamAlphabetString, buffer_file);
  608. }
  609. return TEST_COMPLETED;
  610. }
  611. /**
  612. * Tests writing and reading from file using endian aware functions.
  613. *
  614. * \sa SDL_IOFromFile
  615. * \sa SDL_CloseIO
  616. * \sa SDL_ReadU16BE
  617. * \sa SDL_WriteU16BE
  618. */
  619. static int SDLCALL iostrm_testFileWriteReadEndian(void *arg)
  620. {
  621. SDL_IOStream *rw;
  622. Sint64 result;
  623. int mode;
  624. Uint16 BE16value;
  625. Uint32 BE32value;
  626. Uint64 BE64value;
  627. Uint16 LE16value;
  628. Uint32 LE32value;
  629. Uint64 LE64value;
  630. Uint16 BE16test;
  631. Uint32 BE32test;
  632. Uint64 BE64test;
  633. Uint16 LE16test;
  634. Uint32 LE32test;
  635. Uint64 LE64test;
  636. bool bresult;
  637. int cresult;
  638. for (mode = 0; mode < 3; mode++) {
  639. /* Create test data */
  640. switch (mode) {
  641. default:
  642. case 0:
  643. SDLTest_Log("All 0 values");
  644. BE16value = 0;
  645. BE32value = 0;
  646. BE64value = 0;
  647. LE16value = 0;
  648. LE32value = 0;
  649. LE64value = 0;
  650. break;
  651. case 1:
  652. SDLTest_Log("All 1 values");
  653. BE16value = 1;
  654. BE32value = 1;
  655. BE64value = 1;
  656. LE16value = 1;
  657. LE32value = 1;
  658. LE64value = 1;
  659. break;
  660. case 2:
  661. SDLTest_Log("Random values");
  662. BE16value = SDLTest_RandomUint16();
  663. BE32value = SDLTest_RandomUint32();
  664. BE64value = SDLTest_RandomUint64();
  665. LE16value = SDLTest_RandomUint16();
  666. LE32value = SDLTest_RandomUint32();
  667. LE64value = SDLTest_RandomUint64();
  668. break;
  669. }
  670. /* Write test. */
  671. rw = SDL_IOFromFile(IOStreamWriteTestFilename, "w+");
  672. SDLTest_AssertPass("Call to SDL_IOFromFile(..,\"w+\")");
  673. SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_IOFromFile in write mode does not return NULL");
  674. /* Bail out if NULL */
  675. if (rw == NULL) {
  676. return TEST_ABORTED;
  677. }
  678. /* Write test data */
  679. bresult = SDL_WriteU16BE(rw, BE16value);
  680. SDLTest_AssertPass("Call to SDL_WriteU16BE()");
  681. SDLTest_AssertCheck(bresult == true, "Validate object written, expected: true, got: false");
  682. bresult = SDL_WriteU32BE(rw, BE32value);
  683. SDLTest_AssertPass("Call to SDL_WriteU32BE()");
  684. SDLTest_AssertCheck(bresult == true, "Validate object written, expected: true, got: false");
  685. bresult = SDL_WriteU64BE(rw, BE64value);
  686. SDLTest_AssertPass("Call to SDL_WriteU64BE()");
  687. SDLTest_AssertCheck(bresult == true, "Validate object written, expected: true, got: false");
  688. bresult = SDL_WriteU16LE(rw, LE16value);
  689. SDLTest_AssertPass("Call to SDL_WriteU16LE()");
  690. SDLTest_AssertCheck(bresult == true, "Validate object written, expected: true, got: false");
  691. bresult = SDL_WriteU32LE(rw, LE32value);
  692. SDLTest_AssertPass("Call to SDL_WriteU32LE()");
  693. SDLTest_AssertCheck(bresult == true, "Validate object written, expected: true, got: false");
  694. bresult = SDL_WriteU64LE(rw, LE64value);
  695. SDLTest_AssertPass("Call to SDL_WriteU64LE()");
  696. SDLTest_AssertCheck(bresult == true, "Validate object written, expected: true, got: false");
  697. /* Test seek to start */
  698. result = SDL_SeekIO(rw, 0, SDL_IO_SEEK_SET);
  699. SDLTest_AssertPass("Call to SDL_SeekIO succeeded");
  700. SDLTest_AssertCheck(result == 0, "Verify result from position 0 with SDL_SeekIO, expected 0, got %i", (int)result);
  701. /* Read test data */
  702. bresult = SDL_ReadU16BE(rw, &BE16test);
  703. SDLTest_AssertPass("Call to SDL_ReadU16BE()");
  704. SDLTest_AssertCheck(bresult == true, "Validate object read, expected: true, got: false");
  705. SDLTest_AssertCheck(BE16test == BE16value, "Validate object read from SDL_ReadU16BE, expected: %hu, got: %hu", BE16value, BE16test);
  706. bresult = SDL_ReadU32BE(rw, &BE32test);
  707. SDLTest_AssertPass("Call to SDL_ReadU32BE()");
  708. SDLTest_AssertCheck(bresult == true, "Validate object read, expected: true, got: false");
  709. SDLTest_AssertCheck(BE32test == BE32value, "Validate object read from SDL_ReadU32BE, expected: %" SDL_PRIu32 ", got: %" SDL_PRIu32, BE32value, BE32test);
  710. bresult = SDL_ReadU64BE(rw, &BE64test);
  711. SDLTest_AssertPass("Call to SDL_ReadU64BE()");
  712. SDLTest_AssertCheck(bresult == true, "Validate object read, expected: true, got: false");
  713. SDLTest_AssertCheck(BE64test == BE64value, "Validate object read from SDL_ReadU64BE, expected: %" SDL_PRIu64 ", got: %" SDL_PRIu64, BE64value, BE64test);
  714. bresult = SDL_ReadU16LE(rw, &LE16test);
  715. SDLTest_AssertPass("Call to SDL_ReadU16LE()");
  716. SDLTest_AssertCheck(bresult == true, "Validate object read, expected: true, got: false");
  717. SDLTest_AssertCheck(LE16test == LE16value, "Validate object read from SDL_ReadU16LE, expected: %hu, got: %hu", LE16value, LE16test);
  718. bresult = SDL_ReadU32LE(rw, &LE32test);
  719. SDLTest_AssertPass("Call to SDL_ReadU32LE()");
  720. SDLTest_AssertCheck(bresult == true, "Validate object read, expected: true, got: false");
  721. SDLTest_AssertCheck(LE32test == LE32value, "Validate object read from SDL_ReadU32LE, expected: %" SDL_PRIu32 ", got: %" SDL_PRIu32, LE32value, LE32test);
  722. bresult = SDL_ReadU64LE(rw, &LE64test);
  723. SDLTest_AssertPass("Call to SDL_ReadU64LE()");
  724. SDLTest_AssertCheck(bresult == true, "Validate object read, expected: true, got: false");
  725. SDLTest_AssertCheck(LE64test == LE64value, "Validate object read from SDL_ReadU64LE, expected: %" SDL_PRIu64 ", got: %" SDL_PRIu64, LE64value, LE64test);
  726. /* Close handle */
  727. cresult = SDL_CloseIO(rw);
  728. SDLTest_AssertPass("Call to SDL_CloseIO() succeeded");
  729. SDLTest_AssertCheck(cresult == true, "Verify result value is true; got: %d", cresult);
  730. }
  731. return TEST_COMPLETED;
  732. }
  733. /* ================= Test References ================== */
  734. /* IOStream test cases */
  735. static const SDLTest_TestCaseReference iostrmTest1 = {
  736. iostrm_testParamNegative, "iostrm_testParamNegative", "Negative test for SDL_IOFromFile parameters", TEST_ENABLED
  737. };
  738. static const SDLTest_TestCaseReference iostrmTest2 = {
  739. iostrm_testMem, "iostrm_testMem", "Tests opening from memory", TEST_ENABLED
  740. };
  741. static const SDLTest_TestCaseReference iostrmTest3 = {
  742. iostrm_testConstMem, "iostrm_testConstMem", "Tests opening from (const) memory", TEST_ENABLED
  743. };
  744. static const SDLTest_TestCaseReference iostrmTest4 = {
  745. iostrm_testDynamicMem, "iostrm_testDynamicMem", "Tests opening dynamic memory", TEST_ENABLED
  746. };
  747. static const SDLTest_TestCaseReference iostrmTest5 = {
  748. iostrm_testFileRead, "iostrm_testFileRead", "Tests reading from a file", TEST_ENABLED
  749. };
  750. static const SDLTest_TestCaseReference iostrmTest6 = {
  751. iostrm_testFileWrite, "iostrm_testFileWrite", "Test writing to a file", TEST_ENABLED
  752. };
  753. static const SDLTest_TestCaseReference iostrmTest7 = {
  754. iostrm_testAllocFree, "iostrm_testAllocFree", "Test alloc and free of RW context", TEST_ENABLED
  755. };
  756. static const SDLTest_TestCaseReference iostrmTest8 = {
  757. iostrm_testFileWriteReadEndian, "iostrm_testFileWriteReadEndian", "Test writing and reading via the Endian aware functions", TEST_ENABLED
  758. };
  759. static const SDLTest_TestCaseReference iostrmTest9 = {
  760. iostrm_testCompareRWFromMemWithRWFromFile, "iostrm_testCompareRWFromMemWithRWFromFile", "Compare RWFromMem and RWFromFile IOStream for read and seek", TEST_ENABLED
  761. };
  762. static const SDLTest_TestCaseReference iostrmTest10 = {
  763. iostrm_testMemWithFree, "iostrm_testMemWithFree", "Tests opening from memory with free on close", TEST_ENABLED
  764. };
  765. static const SDLTest_TestCaseReference iostrmTest11 = {
  766. iostrm_testMemEmpty, "iostrm_testMemEmpty", "Tests opening empty memory stream", TEST_ENABLED
  767. };
  768. static const SDLTest_TestCaseReference iostrmTest12 = {
  769. iostrm_testConstMemEmpty, "iostrm_testConstMemEmpty", "Tests opening empty (const) memory stream", TEST_ENABLED
  770. };
  771. /* Sequence of IOStream test cases */
  772. static const SDLTest_TestCaseReference *iostrmTests[] = {
  773. &iostrmTest1, &iostrmTest2, &iostrmTest3, &iostrmTest4, &iostrmTest5, &iostrmTest6,
  774. &iostrmTest7, &iostrmTest8, &iostrmTest9, &iostrmTest10, &iostrmTest11, &iostrmTest12, NULL
  775. };
  776. /* IOStream test suite (global) */
  777. SDLTest_TestSuiteReference iostrmTestSuite = {
  778. "IOStream",
  779. IOStreamSetUp,
  780. iostrmTests,
  781. IOStreamTearDown
  782. };