testautomation_rwops.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. /**
  2. * Automated SDL_RWops 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. #define _CRT_SECURE_NO_WARNINGS
  12. #include <stdio.h>
  13. #include "SDL.h"
  14. #include "SDL_test.h"
  15. /* ================= Test Case Implementation ================== */
  16. const char* RWopsReadTestFilename = "rwops_read";
  17. const char* RWopsWriteTestFilename = "rwops_write";
  18. const char* RWopsAlphabetFilename = "rwops_alphabet";
  19. static const char RWopsHelloWorldTestString[] = "Hello World!";
  20. static const char RWopsHelloWorldCompString[] = "Hello World!";
  21. static const char RWopsAlphabetString[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  22. /* Fixture */
  23. void
  24. RWopsSetUp(void *arg)
  25. {
  26. int fileLen;
  27. FILE *handle;
  28. int writtenLen;
  29. int result;
  30. /* Clean up from previous runs (if any); ignore errors */
  31. remove(RWopsReadTestFilename);
  32. remove(RWopsWriteTestFilename);
  33. remove(RWopsAlphabetFilename);
  34. /* Create a test file */
  35. handle = fopen(RWopsReadTestFilename, "w");
  36. SDLTest_AssertCheck(handle != NULL, "Verify creation of file '%s' returned non NULL handle", RWopsReadTestFilename);
  37. if (handle == NULL) return;
  38. /* Write some known text into it */
  39. fileLen = SDL_strlen(RWopsHelloWorldTestString);
  40. writtenLen = (int)fwrite(RWopsHelloWorldTestString, 1, fileLen, handle);
  41. SDLTest_AssertCheck(fileLen == writtenLen, "Verify number of written bytes, expected %i, got %i", fileLen, writtenLen);
  42. result = fclose(handle);
  43. SDLTest_AssertCheck(result == 0, "Verify result from fclose, expected 0, got %i", result);
  44. /* Create a second test file */
  45. handle = fopen(RWopsAlphabetFilename, "w");
  46. SDLTest_AssertCheck(handle != NULL, "Verify creation of file '%s' returned non NULL handle", RWopsAlphabetFilename);
  47. if (handle == NULL) return;
  48. /* Write alphabet text into it */
  49. fileLen = SDL_strlen(RWopsAlphabetString);
  50. writtenLen = (int)fwrite(RWopsAlphabetString, 1, fileLen, handle);
  51. SDLTest_AssertCheck(fileLen == writtenLen, "Verify number of written bytes, expected %i, got %i", fileLen, writtenLen);
  52. result = fclose(handle);
  53. SDLTest_AssertCheck(result == 0, "Verify result from fclose, expected 0, got %i", result);
  54. SDLTest_AssertPass("Creation of test file completed");
  55. }
  56. void
  57. RWopsTearDown(void *arg)
  58. {
  59. int result;
  60. /* Remove the created files to clean up; ignore errors for write filename */
  61. result = remove(RWopsReadTestFilename);
  62. SDLTest_AssertCheck(result == 0, "Verify result from remove(%s), expected 0, got %i", RWopsReadTestFilename, result);
  63. remove(RWopsWriteTestFilename);
  64. result = remove(RWopsAlphabetFilename);
  65. SDLTest_AssertCheck(result == 0, "Verify result from remove(%s), expected 0, got %i", RWopsAlphabetFilename, result);
  66. SDLTest_AssertPass("Cleanup of test files completed");
  67. }
  68. /**
  69. * @brief Makes sure parameters work properly. Local helper function.
  70. *
  71. * \sa
  72. * http://wiki.libsdl.org/moin.cgi/SDL_RWseek
  73. * http://wiki.libsdl.org/moin.cgi/SDL_RWread
  74. */
  75. void
  76. _testGenericRWopsValidations(SDL_RWops *rw, int write)
  77. {
  78. char buf[sizeof(RWopsHelloWorldTestString)];
  79. Sint64 i;
  80. size_t s;
  81. int seekPos = SDLTest_RandomIntegerInRange(4, 8);
  82. /* Clear buffer */
  83. SDL_zero(buf);
  84. /* Set to start. */
  85. i = SDL_RWseek(rw, 0, RW_SEEK_SET );
  86. SDLTest_AssertPass("Call to SDL_RWseek succeeded");
  87. SDLTest_AssertCheck(i == (Sint64)0, "Verify seek to 0 with SDL_RWseek (RW_SEEK_SET), expected 0, got %i", i);
  88. /* Test write. */
  89. s = SDL_RWwrite(rw, RWopsHelloWorldTestString, sizeof(RWopsHelloWorldTestString)-1, 1);
  90. SDLTest_AssertPass("Call to SDL_RWwrite succeeded");
  91. if (write) {
  92. SDLTest_AssertCheck(s == (size_t)1, "Verify result of writing one byte with SDL_RWwrite, expected 1, got %i", s);
  93. }
  94. else {
  95. SDLTest_AssertCheck(s == (size_t)0, "Verify result of writing with SDL_RWwrite, expected: 0, got %i", s);
  96. }
  97. /* Test seek to random position */
  98. i = SDL_RWseek( rw, seekPos, RW_SEEK_SET );
  99. SDLTest_AssertPass("Call to SDL_RWseek succeeded");
  100. SDLTest_AssertCheck(i == (Sint64)seekPos, "Verify seek to %i with SDL_RWseek (RW_SEEK_SET), expected %i, got %i", seekPos, seekPos, i);
  101. /* Test seek back to start */
  102. i = SDL_RWseek(rw, 0, RW_SEEK_SET );
  103. SDLTest_AssertPass("Call to SDL_RWseek succeeded");
  104. SDLTest_AssertCheck(i == (Sint64)0, "Verify seek to 0 with SDL_RWseek (RW_SEEK_SET), expected 0, got %i", i);
  105. /* Test read */
  106. s = SDL_RWread( rw, buf, 1, sizeof(RWopsHelloWorldTestString)-1 );
  107. SDLTest_AssertPass("Call to SDL_RWread succeeded");
  108. SDLTest_AssertCheck(
  109. s == (size_t)(sizeof(RWopsHelloWorldTestString)-1),
  110. "Verify result from SDL_RWread, expected %i, got %i",
  111. sizeof(RWopsHelloWorldTestString)-1,
  112. s);
  113. SDLTest_AssertCheck(
  114. SDL_memcmp(buf, RWopsHelloWorldTestString, sizeof(RWopsHelloWorldTestString)-1 ) == 0,
  115. "Verify read bytes match expected string, expected '%s', got '%s'", RWopsHelloWorldTestString, buf);
  116. /* More seek tests. */
  117. i = SDL_RWseek( rw, -4, RW_SEEK_CUR );
  118. SDLTest_AssertPass("Call to SDL_RWseek(...,-4,RW_SEEK_CUR) succeeded");
  119. SDLTest_AssertCheck(
  120. i == (Sint64)(sizeof(RWopsHelloWorldTestString)-5),
  121. "Verify seek to -4 with SDL_RWseek (RW_SEEK_CUR), expected %i, got %i",
  122. sizeof(RWopsHelloWorldTestString)-5,
  123. i);
  124. i = SDL_RWseek( rw, -1, RW_SEEK_END );
  125. SDLTest_AssertPass("Call to SDL_RWseek(...,-1,RW_SEEK_END) succeeded");
  126. SDLTest_AssertCheck(
  127. i == (Sint64)(sizeof(RWopsHelloWorldTestString)-2),
  128. "Verify seek to -1 with SDL_RWseek (RW_SEEK_END), expected %i, got %i",
  129. sizeof(RWopsHelloWorldTestString)-2,
  130. i);
  131. /* Invalid whence seek */
  132. i = SDL_RWseek( rw, 0, 999 );
  133. SDLTest_AssertPass("Call to SDL_RWseek(...,0,invalid_whence) succeeded");
  134. SDLTest_AssertCheck(
  135. i == (Sint64)(-1),
  136. "Verify seek with SDL_RWseek (invalid_whence); expected: -1, got %i",
  137. i);
  138. }
  139. /* !
  140. * Negative test for SDL_RWFromFile parameters
  141. *
  142. * \sa http://wiki.libsdl.org/moin.cgi/SDL_RWFromFile
  143. *
  144. */
  145. int
  146. rwops_testParamNegative (void)
  147. {
  148. SDL_RWops *rwops;
  149. /* These should all fail. */
  150. rwops = SDL_RWFromFile(NULL, NULL);
  151. SDLTest_AssertPass("Call to SDL_RWFromFile(NULL, NULL) succeeded");
  152. SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromFile(NULL, NULL) returns NULL");
  153. rwops = SDL_RWFromFile(NULL, "ab+");
  154. SDLTest_AssertPass("Call to SDL_RWFromFile(NULL, \"ab+\") succeeded");
  155. SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromFile(NULL, \"ab+\") returns NULL");
  156. rwops = SDL_RWFromFile(NULL, "sldfkjsldkfj");
  157. SDLTest_AssertPass("Call to SDL_RWFromFile(NULL, \"sldfkjsldkfj\") succeeded");
  158. SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromFile(NULL, \"sldfkjsldkfj\") returns NULL");
  159. rwops = SDL_RWFromFile("something", "");
  160. SDLTest_AssertPass("Call to SDL_RWFromFile(\"something\", \"\") succeeded");
  161. SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromFile(\"something\", \"\") returns NULL");
  162. rwops = SDL_RWFromFile("something", NULL);
  163. SDLTest_AssertPass("Call to SDL_RWFromFile(\"something\", NULL) succeeded");
  164. SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromFile(\"something\", NULL) returns NULL");
  165. rwops = SDL_RWFromMem((void *)NULL, 10);
  166. SDLTest_AssertPass("Call to SDL_RWFromMem(NULL, 10) succeeded");
  167. SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromMem(NULL, 10) returns NULL");
  168. rwops = SDL_RWFromMem((void *)RWopsAlphabetString, 0);
  169. SDLTest_AssertPass("Call to SDL_RWFromMem(data, 0) succeeded");
  170. SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromMem(data, 0) returns NULL");
  171. rwops = SDL_RWFromConstMem((const void *)RWopsAlphabetString, 0);
  172. SDLTest_AssertPass("Call to SDL_RWFromConstMem(data, 0) succeeded");
  173. SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromConstMem(data, 0) returns NULL");
  174. return TEST_COMPLETED;
  175. }
  176. /**
  177. * @brief Tests opening from memory.
  178. *
  179. * \sa http://wiki.libsdl.org/moin.cgi/SDL_RWFromMem
  180. * \sa http://wiki.libsdl.org/moin.cgi/SDL_RWClose
  181. */
  182. int
  183. rwops_testMem (void)
  184. {
  185. char mem[sizeof(RWopsHelloWorldTestString)];
  186. SDL_RWops *rw;
  187. int result;
  188. /* Clear buffer */
  189. SDL_zero(mem);
  190. /* Open */
  191. rw = SDL_RWFromMem(mem, sizeof(RWopsHelloWorldTestString)-1);
  192. SDLTest_AssertPass("Call to SDL_RWFromMem() succeeded");
  193. SDLTest_AssertCheck(rw != NULL, "Verify opening memory with SDL_RWFromMem does not return NULL");
  194. /* Bail out if NULL */
  195. if (rw == NULL) return TEST_ABORTED;
  196. /* Check type */
  197. SDLTest_AssertCheck(rw->type == SDL_RWOPS_MEMORY, "Verify RWops type is SDL_RWOPS_MEMORY; expected: %d, got: %d", SDL_RWOPS_MEMORY, rw->type);
  198. /* Run generic tests */
  199. _testGenericRWopsValidations(rw, 1);
  200. /* Close */
  201. result = SDL_RWclose(rw);
  202. SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
  203. SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
  204. return TEST_COMPLETED;
  205. }
  206. /**
  207. * @brief Tests opening from memory.
  208. *
  209. * \sa
  210. * http://wiki.libsdl.org/moin.cgi/SDL_RWFromConstMem
  211. * http://wiki.libsdl.org/moin.cgi/SDL_RWClose
  212. */
  213. int
  214. rwops_testConstMem (void)
  215. {
  216. SDL_RWops *rw;
  217. int result;
  218. /* Open handle */
  219. rw = SDL_RWFromConstMem( RWopsHelloWorldCompString, sizeof(RWopsHelloWorldCompString)-1 );
  220. SDLTest_AssertPass("Call to SDL_RWFromConstMem() succeeded");
  221. SDLTest_AssertCheck(rw != NULL, "Verify opening memory with SDL_RWFromConstMem does not return NULL");
  222. /* Bail out if NULL */
  223. if (rw == NULL) return TEST_ABORTED;
  224. /* Check type */
  225. SDLTest_AssertCheck(rw->type == SDL_RWOPS_MEMORY_RO, "Verify RWops type is SDL_RWOPS_MEMORY_RO; expected: %d, got: %d", SDL_RWOPS_MEMORY_RO, rw->type);
  226. /* Run generic tests */
  227. _testGenericRWopsValidations( rw, 0 );
  228. /* Close handle */
  229. result = SDL_RWclose(rw);
  230. SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
  231. SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
  232. return TEST_COMPLETED;
  233. }
  234. /**
  235. * @brief Tests reading from file.
  236. *
  237. * \sa
  238. * http://wiki.libsdl.org/moin.cgi/SDL_RWFromFile
  239. * http://wiki.libsdl.org/moin.cgi/SDL_RWClose
  240. */
  241. int
  242. rwops_testFileRead(void)
  243. {
  244. SDL_RWops *rw;
  245. int result;
  246. /* Read test. */
  247. rw = SDL_RWFromFile(RWopsReadTestFilename, "r");
  248. SDLTest_AssertPass("Call to SDL_RWFromFile(..,\"r\") succeeded");
  249. SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_RWFromFile in read mode does not return NULL");
  250. /* Bail out if NULL */
  251. if (rw == NULL) return TEST_ABORTED;
  252. /* Check type */
  253. #if defined(ANDROID)
  254. SDLTest_AssertCheck(
  255. rw->type == SDL_RWOPS_STDFILE || rw->type == SDL_RWOPS_JNIFILE,
  256. "Verify RWops type is SDL_RWOPS_STDFILE or SDL_RWOPS_JNIFILE; expected: %d|%d, got: %d", SDL_RWOPS_STDFILE, SDL_RWOPS_JNIFILE, rw->type);
  257. #elif defined(__WIN32__)
  258. SDLTest_AssertCheck(
  259. rw->type == SDL_RWOPS_WINFILE,
  260. "Verify RWops type is SDL_RWOPS_WINFILE; expected: %d, got: %d", SDL_RWOPS_WINFILE, rw->type);
  261. #else
  262. SDLTest_AssertCheck(
  263. rw->type == SDL_RWOPS_STDFILE,
  264. "Verify RWops type is SDL_RWOPS_STDFILE; expected: %d, got: %d", SDL_RWOPS_STDFILE, rw->type);
  265. #endif
  266. /* Run generic tests */
  267. _testGenericRWopsValidations( rw, 0 );
  268. /* Close handle */
  269. result = SDL_RWclose(rw);
  270. SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
  271. SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
  272. return TEST_COMPLETED;
  273. }
  274. /**
  275. * @brief Tests writing from file.
  276. *
  277. * \sa
  278. * http://wiki.libsdl.org/moin.cgi/SDL_RWFromFile
  279. * http://wiki.libsdl.org/moin.cgi/SDL_RWClose
  280. */
  281. int
  282. rwops_testFileWrite(void)
  283. {
  284. SDL_RWops *rw;
  285. int result;
  286. /* Write test. */
  287. rw = SDL_RWFromFile(RWopsWriteTestFilename, "w+");
  288. SDLTest_AssertPass("Call to SDL_RWFromFile(..,\"w+\") succeeded");
  289. SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_RWFromFile in write mode does not return NULL");
  290. /* Bail out if NULL */
  291. if (rw == NULL) return TEST_ABORTED;
  292. /* Check type */
  293. #if defined(ANDROID)
  294. SDLTest_AssertCheck(
  295. rw->type == SDL_RWOPS_STDFILE || rw->type == SDL_RWOPS_JNIFILE,
  296. "Verify RWops type is SDL_RWOPS_STDFILE or SDL_RWOPS_JNIFILE; expected: %d|%d, got: %d", SDL_RWOPS_STDFILE, SDL_RWOPS_JNIFILE, rw->type);
  297. #elif defined(__WIN32__)
  298. SDLTest_AssertCheck(
  299. rw->type == SDL_RWOPS_WINFILE,
  300. "Verify RWops type is SDL_RWOPS_WINFILE; expected: %d, got: %d", SDL_RWOPS_WINFILE, rw->type);
  301. #else
  302. SDLTest_AssertCheck(
  303. rw->type == SDL_RWOPS_STDFILE,
  304. "Verify RWops type is SDL_RWOPS_STDFILE; expected: %d, got: %d", SDL_RWOPS_STDFILE, rw->type);
  305. #endif
  306. /* Run generic tests */
  307. _testGenericRWopsValidations( rw, 1 );
  308. /* Close handle */
  309. result = SDL_RWclose(rw);
  310. SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
  311. SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
  312. return TEST_COMPLETED;
  313. }
  314. /**
  315. * @brief Tests reading from file handle
  316. *
  317. * \sa
  318. * http://wiki.libsdl.org/moin.cgi/SDL_RWFromFP
  319. * http://wiki.libsdl.org/moin.cgi/SDL_RWClose
  320. *
  321. */
  322. int
  323. rwops_testFPRead(void)
  324. {
  325. FILE *fp;
  326. SDL_RWops *rw;
  327. int result;
  328. /* Run read tests. */
  329. fp = fopen(RWopsReadTestFilename, "r");
  330. SDLTest_AssertCheck(fp != NULL, "Verify handle from opening file '%s' in read mode is not NULL", RWopsReadTestFilename);
  331. /* Bail out if NULL */
  332. if (fp == NULL) return TEST_ABORTED;
  333. /* Open */
  334. rw = SDL_RWFromFP( fp, SDL_TRUE );
  335. SDLTest_AssertPass("Call to SDL_RWFromFP() succeeded");
  336. SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_RWFromFP in read mode does not return NULL");
  337. /* Bail out if NULL */
  338. if (rw == NULL) {
  339. fclose(fp);
  340. return TEST_ABORTED;
  341. }
  342. /* Check type */
  343. SDLTest_AssertCheck(
  344. rw->type == SDL_RWOPS_STDFILE,
  345. "Verify RWops type is SDL_RWOPS_STDFILE; expected: %d, got: %d", SDL_RWOPS_STDFILE, rw->type);
  346. /* Run generic tests */
  347. _testGenericRWopsValidations( rw, 0 );
  348. /* Close handle - does fclose() */
  349. result = SDL_RWclose(rw);
  350. SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
  351. SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
  352. return TEST_COMPLETED;
  353. }
  354. /**
  355. * @brief Tests writing to file handle
  356. *
  357. * \sa
  358. * http://wiki.libsdl.org/moin.cgi/SDL_RWFromFP
  359. * http://wiki.libsdl.org/moin.cgi/SDL_RWClose
  360. *
  361. */
  362. int
  363. rwops_testFPWrite(void)
  364. {
  365. FILE *fp;
  366. SDL_RWops *rw;
  367. int result;
  368. /* Run write tests. */
  369. fp = fopen(RWopsWriteTestFilename, "w+");
  370. SDLTest_AssertCheck(fp != NULL, "Verify handle from opening file '%s' in write mode is not NULL", RWopsWriteTestFilename);
  371. /* Bail out if NULL */
  372. if (fp == NULL) return TEST_ABORTED;
  373. /* Open */
  374. rw = SDL_RWFromFP( fp, SDL_TRUE );
  375. SDLTest_AssertPass("Call to SDL_RWFromFP() succeeded");
  376. SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_RWFromFP in write mode does not return NULL");
  377. /* Bail out if NULL */
  378. if (rw == NULL) {
  379. fclose(fp);
  380. return TEST_ABORTED;
  381. }
  382. /* Check type */
  383. SDLTest_AssertCheck(
  384. rw->type == SDL_RWOPS_STDFILE,
  385. "Verify RWops type is SDL_RWOPS_STDFILE; expected: %d, got: %d", SDL_RWOPS_STDFILE, rw->type);
  386. /* Run generic tests */
  387. _testGenericRWopsValidations( rw, 1 );
  388. /* Close handle - does fclose() */
  389. result = SDL_RWclose(rw);
  390. SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
  391. SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
  392. return TEST_COMPLETED;
  393. }
  394. /**
  395. * @brief Tests alloc and free RW context.
  396. *
  397. * \sa http://wiki.libsdl.org/moin.cgi/SDL_AllocRW
  398. * \sa http://wiki.libsdl.org/moin.cgi/SDL_FreeRW
  399. */
  400. int
  401. rwops_testAllocFree (void)
  402. {
  403. /* Allocate context */
  404. SDL_RWops *rw = SDL_AllocRW();
  405. SDLTest_AssertPass("Call to SDL_AllocRW() succeeded");
  406. SDLTest_AssertCheck(rw != NULL, "Validate result from SDL_AllocRW() is not NULL");
  407. if (rw==NULL) return TEST_ABORTED;
  408. /* Check type */
  409. SDLTest_AssertCheck(
  410. rw->type == SDL_RWOPS_UNKNOWN,
  411. "Verify RWops type is SDL_RWOPS_UNKNOWN; expected: %d, got: %d", SDL_RWOPS_UNKNOWN, rw->type);
  412. /* Free context again */
  413. SDL_FreeRW(rw);
  414. SDLTest_AssertPass("Call to SDL_FreeRW() succeeded");
  415. return TEST_COMPLETED;
  416. }
  417. /**
  418. * @brief Compare memory and file reads
  419. *
  420. * \sa http://wiki.libsdl.org/moin.cgi/SDL_RWFromMem
  421. * \sa http://wiki.libsdl.org/moin.cgi/SDL_RWFromFile
  422. */
  423. int
  424. rwops_testCompareRWFromMemWithRWFromFile(void)
  425. {
  426. int slen = 26;
  427. char buffer_file[27];
  428. char buffer_mem[27];
  429. size_t rv_file;
  430. size_t rv_mem;
  431. Uint64 sv_file;
  432. Uint64 sv_mem;
  433. SDL_RWops* rwops_file;
  434. SDL_RWops* rwops_mem;
  435. int size;
  436. int result;
  437. for (size=5; size<10; size++)
  438. {
  439. /* Terminate buffer */
  440. buffer_file[slen] = 0;
  441. buffer_mem[slen] = 0;
  442. /* Read/seek from memory */
  443. rwops_mem = SDL_RWFromMem((void *)RWopsAlphabetString, slen);
  444. SDLTest_AssertPass("Call to SDL_RWFromMem()");
  445. rv_mem = SDL_RWread(rwops_mem, buffer_mem, size, 6);
  446. SDLTest_AssertPass("Call to SDL_RWread(mem, size=%d)", size);
  447. sv_mem = SDL_RWseek(rwops_mem, 0, SEEK_END);
  448. SDLTest_AssertPass("Call to SDL_RWseek(mem,SEEK_END)");
  449. result = SDL_RWclose(rwops_mem);
  450. SDLTest_AssertPass("Call to SDL_RWclose(mem)");
  451. SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
  452. /* Read/see from file */
  453. rwops_file = SDL_RWFromFile(RWopsAlphabetFilename, "r");
  454. SDLTest_AssertPass("Call to SDL_RWFromFile()");
  455. rv_file = SDL_RWread(rwops_file, buffer_file, size, 6);
  456. SDLTest_AssertPass("Call to SDL_RWread(file, size=%d)", size);
  457. sv_file = SDL_RWseek(rwops_file, 0, SEEK_END);
  458. SDLTest_AssertPass("Call to SDL_RWseek(file,SEEK_END)");
  459. result = SDL_RWclose(rwops_file);
  460. SDLTest_AssertPass("Call to SDL_RWclose(file)");
  461. SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
  462. /* Compare */
  463. SDLTest_AssertCheck(rv_mem == rv_file, "Verify returned read blocks matches for mem and file reads; got: rv_mem=%d rv_file=%d", rv_mem, rv_file);
  464. SDLTest_AssertCheck(sv_mem == sv_file, "Verify SEEK_END position matches for mem and file seeks; got: sv_mem=%llu sv_file=%llu", sv_mem, sv_file);
  465. SDLTest_AssertCheck(buffer_mem[slen] == 0, "Verify mem buffer termination; expected: 0, got: %d", buffer_mem[slen]);
  466. SDLTest_AssertCheck(buffer_file[slen] == 0, "Verify file buffer termination; expected: 0, got: %d", buffer_file[slen]);
  467. SDLTest_AssertCheck(
  468. SDL_strncmp(buffer_mem, RWopsAlphabetString, slen) == 0,
  469. "Verify mem buffer contain alphabet string; expected: %s, got: %s", RWopsAlphabetString, buffer_mem);
  470. SDLTest_AssertCheck(
  471. SDL_strncmp(buffer_file, RWopsAlphabetString, slen) == 0,
  472. "Verify file buffer contain alphabet string; expected: %s, got: %s", RWopsAlphabetString, buffer_file);
  473. }
  474. return TEST_COMPLETED;
  475. }
  476. /**
  477. * @brief Tests writing and reading from file using endian aware functions.
  478. *
  479. * \sa
  480. * http://wiki.libsdl.org/moin.cgi/SDL_RWFromFile
  481. * http://wiki.libsdl.org/moin.cgi/SDL_RWClose
  482. * http://wiki.libsdl.org/moin.cgi/SDL_ReadBE16
  483. * http://wiki.libsdl.org/moin.cgi/SDL_WriteBE16
  484. */
  485. int
  486. rwops_testFileWriteReadEndian(void)
  487. {
  488. SDL_RWops *rw;
  489. Sint64 result;
  490. int mode;
  491. size_t objectsWritten;
  492. Uint16 BE16value;
  493. Uint32 BE32value;
  494. Uint64 BE64value;
  495. Uint16 LE16value;
  496. Uint32 LE32value;
  497. Uint64 LE64value;
  498. Uint16 BE16test;
  499. Uint32 BE32test;
  500. Uint64 BE64test;
  501. Uint16 LE16test;
  502. Uint32 LE32test;
  503. Uint64 LE64test;
  504. int cresult;
  505. for (mode = 0; mode < 3; mode++) {
  506. /* Create test data */
  507. switch (mode) {
  508. case 0:
  509. SDLTest_Log("All 0 values");
  510. BE16value = 0;
  511. BE32value = 0;
  512. BE64value = 0;
  513. LE16value = 0;
  514. LE32value = 0;
  515. LE64value = 0;
  516. break;
  517. case 1:
  518. SDLTest_Log("All 1 values");
  519. BE16value = 1;
  520. BE32value = 1;
  521. BE64value = 1;
  522. LE16value = 1;
  523. LE32value = 1;
  524. LE64value = 1;
  525. break;
  526. case 2:
  527. SDLTest_Log("Random values");
  528. BE16value = SDLTest_RandomUint16();
  529. BE32value = SDLTest_RandomUint32();
  530. BE64value = SDLTest_RandomUint64();
  531. LE16value = SDLTest_RandomUint16();
  532. LE32value = SDLTest_RandomUint32();
  533. LE64value = SDLTest_RandomUint64();
  534. break;
  535. }
  536. /* Write test. */
  537. rw = SDL_RWFromFile(RWopsWriteTestFilename, "w+");
  538. SDLTest_AssertPass("Call to SDL_RWFromFile(..,\"w+\")");
  539. SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_RWFromFile in write mode does not return NULL");
  540. /* Bail out if NULL */
  541. if (rw == NULL) return TEST_ABORTED;
  542. /* Write test data */
  543. objectsWritten = SDL_WriteBE16(rw, BE16value);
  544. SDLTest_AssertPass("Call to SDL_WriteBE16");
  545. SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", objectsWritten);
  546. objectsWritten = SDL_WriteBE32(rw, BE32value);
  547. SDLTest_AssertPass("Call to SDL_WriteBE32");
  548. SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", objectsWritten);
  549. objectsWritten = SDL_WriteBE64(rw, BE64value);
  550. SDLTest_AssertPass("Call to SDL_WriteBE64");
  551. SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", objectsWritten);
  552. objectsWritten = SDL_WriteLE16(rw, LE16value);
  553. SDLTest_AssertPass("Call to SDL_WriteLE16");
  554. SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", objectsWritten);
  555. objectsWritten = SDL_WriteLE32(rw, LE32value);
  556. SDLTest_AssertPass("Call to SDL_WriteLE32");
  557. SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", objectsWritten);
  558. objectsWritten = SDL_WriteLE64(rw, LE64value);
  559. SDLTest_AssertPass("Call to SDL_WriteLE64");
  560. SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", objectsWritten);
  561. /* Test seek to start */
  562. result = SDL_RWseek( rw, 0, RW_SEEK_SET );
  563. SDLTest_AssertPass("Call to SDL_RWseek succeeded");
  564. SDLTest_AssertCheck(result == 0, "Verify result from position 0 with SDL_RWseek, expected 0, got %i", result);
  565. /* Read test data */
  566. BE16test = SDL_ReadBE16(rw);
  567. SDLTest_AssertPass("Call to SDL_ReadBE16");
  568. SDLTest_AssertCheck(BE16test == BE16value, "Validate return value from SDL_ReadBE16, expected: %hu, got: %hu", BE16value, BE16test);
  569. BE32test = SDL_ReadBE32(rw);
  570. SDLTest_AssertPass("Call to SDL_ReadBE32");
  571. SDLTest_AssertCheck(BE32test == BE32value, "Validate return value from SDL_ReadBE32, expected: %u, got: %u", BE32value, BE32test);
  572. BE64test = SDL_ReadBE64(rw);
  573. SDLTest_AssertPass("Call to SDL_ReadBE64");
  574. SDLTest_AssertCheck(BE64test == BE64value, "Validate return value from SDL_ReadBE64, expected: %llu, got: %llu", BE64value, BE64test);
  575. LE16test = SDL_ReadLE16(rw);
  576. SDLTest_AssertPass("Call to SDL_ReadLE16");
  577. SDLTest_AssertCheck(LE16test == LE16value, "Validate return value from SDL_ReadLE16, expected: %hu, got: %hu", LE16value, LE16test);
  578. LE32test = SDL_ReadLE32(rw);
  579. SDLTest_AssertPass("Call to SDL_ReadLE32");
  580. SDLTest_AssertCheck(LE32test == LE32value, "Validate return value from SDL_ReadLE32, expected: %u, got: %u", LE32value, LE32test);
  581. LE64test = SDL_ReadLE64(rw);
  582. SDLTest_AssertPass("Call to SDL_ReadLE64");
  583. SDLTest_AssertCheck(LE64test == LE64value, "Validate return value from SDL_ReadLE64, expected: %llu, got: %llu", LE64value, LE64test);
  584. /* Close handle */
  585. cresult = SDL_RWclose(rw);
  586. SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
  587. SDLTest_AssertCheck(cresult == 0, "Verify result value is 0; got: %d", cresult);
  588. }
  589. return TEST_COMPLETED;
  590. }
  591. /* ================= Test References ================== */
  592. /* RWops test cases */
  593. static const SDLTest_TestCaseReference rwopsTest1 =
  594. { (SDLTest_TestCaseFp)rwops_testParamNegative, "rwops_testParamNegative", "Negative test for SDL_RWFromFile parameters", TEST_ENABLED };
  595. static const SDLTest_TestCaseReference rwopsTest2 =
  596. { (SDLTest_TestCaseFp)rwops_testMem, "rwops_testMem", "Tests opening from memory", TEST_ENABLED };
  597. static const SDLTest_TestCaseReference rwopsTest3 =
  598. { (SDLTest_TestCaseFp)rwops_testConstMem, "rwops_testConstMem", "Tests opening from (const) memory", TEST_ENABLED };
  599. static const SDLTest_TestCaseReference rwopsTest4 =
  600. { (SDLTest_TestCaseFp)rwops_testFileRead, "rwops_testFileRead", "Tests reading from a file", TEST_ENABLED };
  601. static const SDLTest_TestCaseReference rwopsTest5 =
  602. { (SDLTest_TestCaseFp)rwops_testFileWrite, "rwops_testFileWrite", "Test writing to a file", TEST_ENABLED };
  603. static const SDLTest_TestCaseReference rwopsTest6 =
  604. { (SDLTest_TestCaseFp)rwops_testFPRead, "rwops_testFPRead", "Test reading from file pointer", TEST_ENABLED };
  605. static const SDLTest_TestCaseReference rwopsTest7 =
  606. { (SDLTest_TestCaseFp)rwops_testFPWrite, "rwops_testFPWrite", "Test writing to file pointer", TEST_ENABLED };
  607. static const SDLTest_TestCaseReference rwopsTest8 =
  608. { (SDLTest_TestCaseFp)rwops_testAllocFree, "rwops_testAllocFree", "Test alloc and free of RW context", TEST_ENABLED };
  609. static const SDLTest_TestCaseReference rwopsTest9 =
  610. { (SDLTest_TestCaseFp)rwops_testFileWriteReadEndian, "rwops_testFileWriteReadEndian", "Test writing and reading via the Endian aware functions", TEST_ENABLED };
  611. static const SDLTest_TestCaseReference rwopsTest10 =
  612. { (SDLTest_TestCaseFp)rwops_testCompareRWFromMemWithRWFromFile, "rwops_testCompareRWFromMemWithRWFromFile", "Compare RWFromMem and RWFromFile RWops for read and seek", TEST_ENABLED };
  613. /* Sequence of RWops test cases */
  614. static const SDLTest_TestCaseReference *rwopsTests[] = {
  615. &rwopsTest1, &rwopsTest2, &rwopsTest3, &rwopsTest4, &rwopsTest5, &rwopsTest6,
  616. &rwopsTest7, &rwopsTest8, &rwopsTest9, &rwopsTest10, NULL
  617. };
  618. /* RWops test suite (global) */
  619. SDLTest_TestSuiteReference rwopsTestSuite = {
  620. "RWops",
  621. RWopsSetUp,
  622. rwopsTests,
  623. RWopsTearDown
  624. };