testautomation_rwops.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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. #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. * 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, SDL_bool write)
  80. {
  81. char buf[sizeof(RWopsHelloWorldTestString)];
  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_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 with SDL_RWwrite, expected %i, got %i", (int)sizeof(RWopsHelloWorldTestString) - 1, (int)s);
  96. } else {
  97. SDLTest_AssertCheck(s == 0, "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 == (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. /* Test seek back to start */
  119. i = SDL_RWseek(rw, 0, SDL_RW_SEEK_SET);
  120. SDLTest_AssertPass("Call to SDL_RWseek succeeded");
  121. SDLTest_AssertCheck(i == (Sint64)0, "Verify seek to 0 with SDL_RWseek (SDL_RW_SEEK_SET), expected 0, got %" SDL_PRIs64, i);
  122. /* Test printf */
  123. s = SDL_RWprintf(rw, "%s", RWopsHelloWorldTestString);
  124. SDLTest_AssertPass("Call to SDL_RWprintf succeeded");
  125. if (write) {
  126. SDLTest_AssertCheck(s == sizeof(RWopsHelloWorldTestString) - 1, "Verify result of writing with SDL_RWprintf, expected %i, got %i", (int)sizeof(RWopsHelloWorldTestString) - 1, (int)s);
  127. } else {
  128. SDLTest_AssertCheck(s == 0, "Verify result of writing with SDL_RWwrite, expected: 0, got %i", (int)s);
  129. }
  130. /* Test seek back to start */
  131. i = SDL_RWseek(rw, 0, SDL_RW_SEEK_SET);
  132. SDLTest_AssertPass("Call to SDL_RWseek succeeded");
  133. SDLTest_AssertCheck(i == (Sint64)0, "Verify seek to 0 with SDL_RWseek (SDL_RW_SEEK_SET), expected 0, got %" SDL_PRIs64, i);
  134. /* Test read */
  135. s = SDL_RWread(rw, buf, sizeof(RWopsHelloWorldTestString) - 1);
  136. SDLTest_AssertPass("Call to SDL_RWread succeeded");
  137. SDLTest_AssertCheck(
  138. s == (sizeof(RWopsHelloWorldTestString) - 1),
  139. "Verify result from SDL_RWread, expected %i, got %i",
  140. (int)(sizeof(RWopsHelloWorldTestString) - 1),
  141. (int)s);
  142. SDLTest_AssertCheck(
  143. SDL_memcmp(buf, RWopsHelloWorldTestString, sizeof(RWopsHelloWorldTestString) - 1) == 0,
  144. "Verify read bytes match expected string, expected '%s', got '%s'", RWopsHelloWorldTestString, buf);
  145. /* More seek tests. */
  146. i = SDL_RWseek(rw, -4, SDL_RW_SEEK_CUR);
  147. SDLTest_AssertPass("Call to SDL_RWseek(...,-4,SDL_RW_SEEK_CUR) succeeded");
  148. SDLTest_AssertCheck(
  149. i == (Sint64)(sizeof(RWopsHelloWorldTestString) - 5),
  150. "Verify seek to -4 with SDL_RWseek (SDL_RW_SEEK_CUR), expected %i, got %i",
  151. (int)(sizeof(RWopsHelloWorldTestString) - 5),
  152. (int)i);
  153. i = SDL_RWseek(rw, -1, SDL_RW_SEEK_END);
  154. SDLTest_AssertPass("Call to SDL_RWseek(...,-1,SDL_RW_SEEK_END) succeeded");
  155. SDLTest_AssertCheck(
  156. i == (Sint64)(sizeof(RWopsHelloWorldTestString) - 2),
  157. "Verify seek to -1 with SDL_RWseek (SDL_RW_SEEK_END), expected %i, got %i",
  158. (int)(sizeof(RWopsHelloWorldTestString) - 2),
  159. (int)i);
  160. /* Invalid whence seek */
  161. i = SDL_RWseek(rw, 0, 999);
  162. SDLTest_AssertPass("Call to SDL_RWseek(...,0,invalid_whence) succeeded");
  163. SDLTest_AssertCheck(
  164. i == (Sint64)(-1),
  165. "Verify seek with SDL_RWseek (invalid_whence); expected: -1, got %i",
  166. (int)i);
  167. }
  168. /**
  169. * Negative test for SDL_RWFromFile parameters
  170. *
  171. * \sa SDL_RWFromFile
  172. *
  173. */
  174. static int rwops_testParamNegative(void *arg)
  175. {
  176. SDL_RWops *rwops;
  177. /* These should all fail. */
  178. rwops = SDL_RWFromFile(NULL, NULL);
  179. SDLTest_AssertPass("Call to SDL_RWFromFile(NULL, NULL) succeeded");
  180. SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromFile(NULL, NULL) returns NULL");
  181. rwops = SDL_RWFromFile(NULL, "ab+");
  182. SDLTest_AssertPass("Call to SDL_RWFromFile(NULL, \"ab+\") succeeded");
  183. SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromFile(NULL, \"ab+\") returns NULL");
  184. rwops = SDL_RWFromFile(NULL, "sldfkjsldkfj");
  185. SDLTest_AssertPass("Call to SDL_RWFromFile(NULL, \"sldfkjsldkfj\") succeeded");
  186. SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromFile(NULL, \"sldfkjsldkfj\") returns NULL");
  187. rwops = SDL_RWFromFile("something", "");
  188. SDLTest_AssertPass("Call to SDL_RWFromFile(\"something\", \"\") succeeded");
  189. SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromFile(\"something\", \"\") returns NULL");
  190. rwops = SDL_RWFromFile("something", NULL);
  191. SDLTest_AssertPass("Call to SDL_RWFromFile(\"something\", NULL) succeeded");
  192. SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromFile(\"something\", NULL) returns NULL");
  193. rwops = SDL_RWFromMem(NULL, 10);
  194. SDLTest_AssertPass("Call to SDL_RWFromMem(NULL, 10) succeeded");
  195. SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromMem(NULL, 10) returns NULL");
  196. rwops = SDL_RWFromMem((void *)RWopsAlphabetString, 0);
  197. SDLTest_AssertPass("Call to SDL_RWFromMem(data, 0) succeeded");
  198. SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromMem(data, 0) returns NULL");
  199. rwops = SDL_RWFromConstMem((const void *)RWopsAlphabetString, 0);
  200. SDLTest_AssertPass("Call to SDL_RWFromConstMem(data, 0) succeeded");
  201. SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromConstMem(data, 0) returns NULL");
  202. return TEST_COMPLETED;
  203. }
  204. /**
  205. * Tests opening from memory.
  206. *
  207. * \sa SDL_RWFromMem
  208. * \sa SDL_RWClose
  209. */
  210. static int rwops_testMem(void *arg)
  211. {
  212. char mem[sizeof(RWopsHelloWorldTestString)];
  213. SDL_RWops *rw;
  214. int result;
  215. /* Clear buffer */
  216. SDL_zeroa(mem);
  217. /* Open */
  218. rw = SDL_RWFromMem(mem, sizeof(RWopsHelloWorldTestString) - 1);
  219. SDLTest_AssertPass("Call to SDL_RWFromMem() succeeded");
  220. SDLTest_AssertCheck(rw != NULL, "Verify opening memory with SDL_RWFromMem 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, "Verify RWops type is SDL_RWOPS_MEMORY; expected: %d, got: %" SDL_PRIu32, SDL_RWOPS_MEMORY, rw->type);
  227. /* Run generic tests */
  228. testGenericRWopsValidations(rw, SDL_TRUE);
  229. /* Close */
  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. * Tests opening from memory.
  237. *
  238. * \sa SDL_RWFromConstMem
  239. * \sa SDL_RWClose
  240. */
  241. static int rwops_testConstMem(void *arg)
  242. {
  243. SDL_RWops *rw;
  244. int result;
  245. /* Open handle */
  246. rw = SDL_RWFromConstMem(RWopsHelloWorldCompString, sizeof(RWopsHelloWorldCompString) - 1);
  247. SDLTest_AssertPass("Call to SDL_RWFromConstMem() succeeded");
  248. SDLTest_AssertCheck(rw != NULL, "Verify opening memory with SDL_RWFromConstMem does not return NULL");
  249. /* Bail out if NULL */
  250. if (rw == NULL) {
  251. return TEST_ABORTED;
  252. }
  253. /* Check type */
  254. 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);
  255. /* Run generic tests */
  256. testGenericRWopsValidations(rw, SDL_FALSE);
  257. /* Close handle */
  258. result = SDL_RWclose(rw);
  259. SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
  260. SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
  261. return TEST_COMPLETED;
  262. }
  263. /**
  264. * Tests reading from file.
  265. *
  266. * \sa SDL_RWFromFile
  267. * \sa SDL_RWClose
  268. */
  269. static int rwops_testFileRead(void *arg)
  270. {
  271. SDL_RWops *rw;
  272. int result;
  273. /* Read test. */
  274. rw = SDL_RWFromFile(RWopsReadTestFilename, "r");
  275. SDLTest_AssertPass("Call to SDL_RWFromFile(..,\"r\") succeeded");
  276. SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_RWFromFile in read mode does not return NULL");
  277. /* Bail out if NULL */
  278. if (rw == NULL) {
  279. return TEST_ABORTED;
  280. }
  281. /* Check type */
  282. #ifdef __ANDROID__
  283. SDLTest_AssertCheck(
  284. rw->type == SDL_RWOPS_STDFILE || rw->type == SDL_RWOPS_JNIFILE,
  285. "Verify RWops type is SDL_RWOPS_STDFILE or SDL_RWOPS_JNIFILE; expected: %d|%d, got: %d", SDL_RWOPS_STDFILE, SDL_RWOPS_JNIFILE, rw->type);
  286. #elif defined(__WIN32__)
  287. SDLTest_AssertCheck(
  288. rw->type == SDL_RWOPS_WINFILE,
  289. "Verify RWops type is SDL_RWOPS_WINFILE; expected: %d, got: %d", SDL_RWOPS_WINFILE, rw->type);
  290. #else
  291. SDLTest_AssertCheck(
  292. rw->type == SDL_RWOPS_STDFILE,
  293. "Verify RWops type is SDL_RWOPS_STDFILE; expected: %d, got: %" SDL_PRIu32, SDL_RWOPS_STDFILE, rw->type);
  294. #endif
  295. /* Run generic tests */
  296. testGenericRWopsValidations(rw, SDL_FALSE);
  297. /* Close handle */
  298. result = SDL_RWclose(rw);
  299. SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
  300. SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
  301. return TEST_COMPLETED;
  302. }
  303. /**
  304. * Tests writing from file.
  305. *
  306. * \sa SDL_RWFromFile
  307. * \sa SDL_RWClose
  308. */
  309. static int rwops_testFileWrite(void *arg)
  310. {
  311. SDL_RWops *rw;
  312. int result;
  313. /* Write test. */
  314. rw = SDL_RWFromFile(RWopsWriteTestFilename, "w+");
  315. SDLTest_AssertPass("Call to SDL_RWFromFile(..,\"w+\") succeeded");
  316. SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_RWFromFile in write mode does not return NULL");
  317. /* Bail out if NULL */
  318. if (rw == NULL) {
  319. return TEST_ABORTED;
  320. }
  321. /* Check type */
  322. #ifdef __ANDROID__
  323. SDLTest_AssertCheck(
  324. rw->type == SDL_RWOPS_STDFILE || rw->type == SDL_RWOPS_JNIFILE,
  325. "Verify RWops type is SDL_RWOPS_STDFILE or SDL_RWOPS_JNIFILE; expected: %d|%d, got: %d", SDL_RWOPS_STDFILE, SDL_RWOPS_JNIFILE, rw->type);
  326. #elif defined(__WIN32__)
  327. SDLTest_AssertCheck(
  328. rw->type == SDL_RWOPS_WINFILE,
  329. "Verify RWops type is SDL_RWOPS_WINFILE; expected: %d, got: %d", SDL_RWOPS_WINFILE, rw->type);
  330. #else
  331. SDLTest_AssertCheck(
  332. rw->type == SDL_RWOPS_STDFILE,
  333. "Verify RWops type is SDL_RWOPS_STDFILE; expected: %d, got: %" SDL_PRIu32, SDL_RWOPS_STDFILE, rw->type);
  334. #endif
  335. /* Run generic tests */
  336. testGenericRWopsValidations(rw, SDL_TRUE);
  337. /* Close handle */
  338. result = SDL_RWclose(rw);
  339. SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
  340. SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
  341. return TEST_COMPLETED;
  342. }
  343. /**
  344. * Tests alloc and free RW context.
  345. *
  346. * \sa SDL_CreateRW
  347. * \sa SDL_DestroyRW
  348. */
  349. static int rwops_testAllocFree(void *arg)
  350. {
  351. /* Allocate context */
  352. SDL_RWops *rw = SDL_CreateRW();
  353. SDLTest_AssertPass("Call to SDL_CreateRW() succeeded");
  354. SDLTest_AssertCheck(rw != NULL, "Validate result from SDL_CreateRW() is not NULL");
  355. if (rw == NULL) {
  356. return TEST_ABORTED;
  357. }
  358. /* Check type */
  359. SDLTest_AssertCheck(
  360. rw->type == SDL_RWOPS_UNKNOWN,
  361. "Verify RWops type is SDL_RWOPS_UNKNOWN; expected: %d, got: %" SDL_PRIu32, SDL_RWOPS_UNKNOWN, rw->type);
  362. /* Free context again */
  363. SDL_DestroyRW(rw);
  364. SDLTest_AssertPass("Call to SDL_DestroyRW() succeeded");
  365. return TEST_COMPLETED;
  366. }
  367. /**
  368. * Compare memory and file reads
  369. *
  370. * \sa SDL_RWFromMem
  371. * \sa SDL_RWFromFile
  372. */
  373. static int rwops_testCompareRWFromMemWithRWFromFile(void *arg)
  374. {
  375. int slen = 26;
  376. char buffer_file[27];
  377. char buffer_mem[27];
  378. size_t rv_file;
  379. size_t rv_mem;
  380. Uint64 sv_file;
  381. Uint64 sv_mem;
  382. SDL_RWops *rwops_file;
  383. SDL_RWops *rwops_mem;
  384. int size;
  385. int result;
  386. for (size = 5; size < 10; size++) {
  387. /* Terminate buffer */
  388. buffer_file[slen] = 0;
  389. buffer_mem[slen] = 0;
  390. /* Read/seek from memory */
  391. rwops_mem = SDL_RWFromMem((void *)RWopsAlphabetString, slen);
  392. SDLTest_AssertPass("Call to SDL_RWFromMem()");
  393. rv_mem = SDL_RWread(rwops_mem, buffer_mem, size * 6);
  394. SDLTest_AssertPass("Call to SDL_RWread(mem, size=%d)", size * 6);
  395. sv_mem = SDL_RWseek(rwops_mem, 0, SEEK_END);
  396. SDLTest_AssertPass("Call to SDL_RWseek(mem,SEEK_END)");
  397. result = SDL_RWclose(rwops_mem);
  398. SDLTest_AssertPass("Call to SDL_RWclose(mem)");
  399. SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
  400. /* Read/see from file */
  401. rwops_file = SDL_RWFromFile(RWopsAlphabetFilename, "r");
  402. SDLTest_AssertPass("Call to SDL_RWFromFile()");
  403. rv_file = SDL_RWread(rwops_file, buffer_file, size * 6);
  404. SDLTest_AssertPass("Call to SDL_RWread(file, size=%d)", size * 6);
  405. sv_file = SDL_RWseek(rwops_file, 0, SEEK_END);
  406. SDLTest_AssertPass("Call to SDL_RWseek(file,SEEK_END)");
  407. result = SDL_RWclose(rwops_file);
  408. SDLTest_AssertPass("Call to SDL_RWclose(file)");
  409. SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
  410. /* Compare */
  411. 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);
  412. 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);
  413. SDLTest_AssertCheck(buffer_mem[slen] == 0, "Verify mem buffer termination; expected: 0, got: %d", buffer_mem[slen]);
  414. SDLTest_AssertCheck(buffer_file[slen] == 0, "Verify file buffer termination; expected: 0, got: %d", buffer_file[slen]);
  415. SDLTest_AssertCheck(
  416. SDL_strncmp(buffer_mem, RWopsAlphabetString, slen) == 0,
  417. "Verify mem buffer contain alphabet string; expected: %s, got: %s", RWopsAlphabetString, buffer_mem);
  418. SDLTest_AssertCheck(
  419. SDL_strncmp(buffer_file, RWopsAlphabetString, slen) == 0,
  420. "Verify file buffer contain alphabet string; expected: %s, got: %s", RWopsAlphabetString, buffer_file);
  421. }
  422. return TEST_COMPLETED;
  423. }
  424. /**
  425. * Tests writing and reading from file using endian aware functions.
  426. *
  427. * \sa SDL_RWFromFile
  428. * \sa SDL_RWClose
  429. * \sa SDL_ReadU16BE
  430. * \sa SDL_WriteU16BE
  431. */
  432. static int rwops_testFileWriteReadEndian(void *arg)
  433. {
  434. SDL_RWops *rw;
  435. Sint64 result;
  436. int mode;
  437. Uint16 BE16value;
  438. Uint32 BE32value;
  439. Uint64 BE64value;
  440. Uint16 LE16value;
  441. Uint32 LE32value;
  442. Uint64 LE64value;
  443. Uint16 BE16test;
  444. Uint32 BE32test;
  445. Uint64 BE64test;
  446. Uint16 LE16test;
  447. Uint32 LE32test;
  448. Uint64 LE64test;
  449. SDL_bool bresult;
  450. int cresult;
  451. for (mode = 0; mode < 3; mode++) {
  452. /* Create test data */
  453. switch (mode) {
  454. default:
  455. case 0:
  456. SDLTest_Log("All 0 values");
  457. BE16value = 0;
  458. BE32value = 0;
  459. BE64value = 0;
  460. LE16value = 0;
  461. LE32value = 0;
  462. LE64value = 0;
  463. break;
  464. case 1:
  465. SDLTest_Log("All 1 values");
  466. BE16value = 1;
  467. BE32value = 1;
  468. BE64value = 1;
  469. LE16value = 1;
  470. LE32value = 1;
  471. LE64value = 1;
  472. break;
  473. case 2:
  474. SDLTest_Log("Random values");
  475. BE16value = SDLTest_RandomUint16();
  476. BE32value = SDLTest_RandomUint32();
  477. BE64value = SDLTest_RandomUint64();
  478. LE16value = SDLTest_RandomUint16();
  479. LE32value = SDLTest_RandomUint32();
  480. LE64value = SDLTest_RandomUint64();
  481. break;
  482. }
  483. /* Write test. */
  484. rw = SDL_RWFromFile(RWopsWriteTestFilename, "w+");
  485. SDLTest_AssertPass("Call to SDL_RWFromFile(..,\"w+\")");
  486. SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_RWFromFile in write mode does not return NULL");
  487. /* Bail out if NULL */
  488. if (rw == NULL) {
  489. return TEST_ABORTED;
  490. }
  491. /* Write test data */
  492. bresult = SDL_WriteU16BE(rw, BE16value);
  493. SDLTest_AssertPass("Call to SDL_WriteU16BE()");
  494. SDLTest_AssertCheck(bresult == SDL_TRUE, "Validate object written, expected: SDL_TRUE, got: SDL_FALSE");
  495. bresult = SDL_WriteU32BE(rw, BE32value);
  496. SDLTest_AssertPass("Call to SDL_WriteU32BE()");
  497. SDLTest_AssertCheck(bresult == SDL_TRUE, "Validate object written, expected: SDL_TRUE, got: SDL_FALSE");
  498. bresult = SDL_WriteU64BE(rw, BE64value);
  499. SDLTest_AssertPass("Call to SDL_WriteU64BE()");
  500. SDLTest_AssertCheck(bresult == SDL_TRUE, "Validate object written, expected: SDL_TRUE, got: SDL_FALSE");
  501. bresult = SDL_WriteU16LE(rw, LE16value);
  502. SDLTest_AssertPass("Call to SDL_WriteU16LE()");
  503. SDLTest_AssertCheck(bresult == SDL_TRUE, "Validate object written, expected: SDL_TRUE, got: SDL_FALSE");
  504. bresult = SDL_WriteU32LE(rw, LE32value);
  505. SDLTest_AssertPass("Call to SDL_WriteU32LE()");
  506. SDLTest_AssertCheck(bresult == SDL_TRUE, "Validate object written, expected: SDL_TRUE, got: SDL_FALSE");
  507. bresult = SDL_WriteU64LE(rw, LE64value);
  508. SDLTest_AssertPass("Call to SDL_WriteU64LE()");
  509. SDLTest_AssertCheck(bresult == SDL_TRUE, "Validate object written, expected: SDL_TRUE, got: SDL_FALSE");
  510. /* Test seek to start */
  511. result = SDL_RWseek(rw, 0, SDL_RW_SEEK_SET);
  512. SDLTest_AssertPass("Call to SDL_RWseek succeeded");
  513. SDLTest_AssertCheck(result == 0, "Verify result from position 0 with SDL_RWseek, expected 0, got %i", (int)result);
  514. /* Read test data */
  515. bresult = SDL_ReadU16BE(rw, &BE16test);
  516. SDLTest_AssertPass("Call to SDL_ReadU16BE()");
  517. SDLTest_AssertCheck(bresult == SDL_TRUE, "Validate object read, expected: SDL_TRUE, got: SDL_FALSE");
  518. SDLTest_AssertCheck(BE16test == BE16value, "Validate object read from SDL_ReadU16BE, expected: %hu, got: %hu", BE16value, BE16test);
  519. bresult = SDL_ReadU32BE(rw, &BE32test);
  520. SDLTest_AssertPass("Call to SDL_ReadU32BE()");
  521. SDLTest_AssertCheck(bresult == SDL_TRUE, "Validate object read, expected: SDL_TRUE, got: SDL_FALSE");
  522. SDLTest_AssertCheck(BE32test == BE32value, "Validate object read from SDL_ReadU32BE, expected: %" SDL_PRIu32 ", got: %" SDL_PRIu32, BE32value, BE32test);
  523. bresult = SDL_ReadU64BE(rw, &BE64test);
  524. SDLTest_AssertPass("Call to SDL_ReadU64BE()");
  525. SDLTest_AssertCheck(bresult == SDL_TRUE, "Validate object read, expected: SDL_TRUE, got: SDL_FALSE");
  526. SDLTest_AssertCheck(BE64test == BE64value, "Validate object read from SDL_ReadU64BE, expected: %" SDL_PRIu64 ", got: %" SDL_PRIu64, BE64value, BE64test);
  527. bresult = SDL_ReadU16LE(rw, &LE16test);
  528. SDLTest_AssertPass("Call to SDL_ReadU16LE()");
  529. SDLTest_AssertCheck(bresult == SDL_TRUE, "Validate object read, expected: SDL_TRUE, got: SDL_FALSE");
  530. SDLTest_AssertCheck(LE16test == LE16value, "Validate object read from SDL_ReadU16LE, expected: %hu, got: %hu", LE16value, LE16test);
  531. bresult = SDL_ReadU32LE(rw, &LE32test);
  532. SDLTest_AssertPass("Call to SDL_ReadU32LE()");
  533. SDLTest_AssertCheck(bresult == SDL_TRUE, "Validate object read, expected: SDL_TRUE, got: SDL_FALSE");
  534. SDLTest_AssertCheck(LE32test == LE32value, "Validate object read from SDL_ReadU32LE, expected: %" SDL_PRIu32 ", got: %" SDL_PRIu32, LE32value, LE32test);
  535. bresult = SDL_ReadU64LE(rw, &LE64test);
  536. SDLTest_AssertPass("Call to SDL_ReadU64LE()");
  537. SDLTest_AssertCheck(bresult == SDL_TRUE, "Validate object read, expected: SDL_TRUE, got: SDL_FALSE");
  538. SDLTest_AssertCheck(LE64test == LE64value, "Validate object read from SDL_ReadU64LE, expected: %" SDL_PRIu64 ", got: %" SDL_PRIu64, LE64value, LE64test);
  539. /* Close handle */
  540. cresult = SDL_RWclose(rw);
  541. SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
  542. SDLTest_AssertCheck(cresult == 0, "Verify result value is 0; got: %d", cresult);
  543. }
  544. return TEST_COMPLETED;
  545. }
  546. /* ================= Test References ================== */
  547. /* RWops test cases */
  548. static const SDLTest_TestCaseReference rwopsTest1 = {
  549. (SDLTest_TestCaseFp)rwops_testParamNegative, "rwops_testParamNegative", "Negative test for SDL_RWFromFile parameters", TEST_ENABLED
  550. };
  551. static const SDLTest_TestCaseReference rwopsTest2 = {
  552. (SDLTest_TestCaseFp)rwops_testMem, "rwops_testMem", "Tests opening from memory", TEST_ENABLED
  553. };
  554. static const SDLTest_TestCaseReference rwopsTest3 = {
  555. (SDLTest_TestCaseFp)rwops_testConstMem, "rwops_testConstMem", "Tests opening from (const) memory", TEST_ENABLED
  556. };
  557. static const SDLTest_TestCaseReference rwopsTest4 = {
  558. (SDLTest_TestCaseFp)rwops_testFileRead, "rwops_testFileRead", "Tests reading from a file", TEST_ENABLED
  559. };
  560. static const SDLTest_TestCaseReference rwopsTest5 = {
  561. (SDLTest_TestCaseFp)rwops_testFileWrite, "rwops_testFileWrite", "Test writing to a file", TEST_ENABLED
  562. };
  563. static const SDLTest_TestCaseReference rwopsTest6 = {
  564. (SDLTest_TestCaseFp)rwops_testAllocFree, "rwops_testAllocFree", "Test alloc and free of RW context", TEST_ENABLED
  565. };
  566. static const SDLTest_TestCaseReference rwopsTest7 = {
  567. (SDLTest_TestCaseFp)rwops_testFileWriteReadEndian, "rwops_testFileWriteReadEndian", "Test writing and reading via the Endian aware functions", TEST_ENABLED
  568. };
  569. static const SDLTest_TestCaseReference rwopsTest8 = {
  570. (SDLTest_TestCaseFp)rwops_testCompareRWFromMemWithRWFromFile, "rwops_testCompareRWFromMemWithRWFromFile", "Compare RWFromMem and RWFromFile RWops for read and seek", TEST_ENABLED
  571. };
  572. /* Sequence of RWops test cases */
  573. static const SDLTest_TestCaseReference *rwopsTests[] = {
  574. &rwopsTest1, &rwopsTest2, &rwopsTest3, &rwopsTest4, &rwopsTest5, &rwopsTest6,
  575. &rwopsTest7, &rwopsTest8, NULL
  576. };
  577. /* RWops test suite (global) */
  578. SDLTest_TestSuiteReference rwopsTestSuite = {
  579. "RWops",
  580. RWopsSetUp,
  581. rwopsTests,
  582. RWopsTearDown
  583. };