testautomation_rwops.c 24 KB

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