testautomation_rwops.c 24 KB

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