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. * 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. /* Run generic tests */
  226. testGenericRWopsValidations(rw, SDL_TRUE);
  227. /* Close */
  228. result = SDL_DestroyRW(rw);
  229. SDLTest_AssertPass("Call to SDL_DestroyRW() succeeded");
  230. SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
  231. return TEST_COMPLETED;
  232. }
  233. /**
  234. * Tests opening from memory.
  235. *
  236. * \sa SDL_RWFromConstMem
  237. * \sa SDL_RWClose
  238. */
  239. static int rwops_testConstMem(void *arg)
  240. {
  241. SDL_RWops *rw;
  242. int result;
  243. /* Open handle */
  244. rw = SDL_RWFromConstMem(RWopsHelloWorldCompString, sizeof(RWopsHelloWorldCompString) - 1);
  245. SDLTest_AssertPass("Call to SDL_RWFromConstMem() succeeded");
  246. SDLTest_AssertCheck(rw != NULL, "Verify opening memory with SDL_RWFromConstMem does not return NULL");
  247. /* Bail out if NULL */
  248. if (rw == NULL) {
  249. return TEST_ABORTED;
  250. }
  251. /* Run generic tests */
  252. testGenericRWopsValidations(rw, SDL_FALSE);
  253. /* Close handle */
  254. result = SDL_DestroyRW(rw);
  255. SDLTest_AssertPass("Call to SDL_DestroyRW() succeeded");
  256. SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
  257. return TEST_COMPLETED;
  258. }
  259. /**
  260. * Tests reading from file.
  261. *
  262. * \sa SDL_RWFromFile
  263. * \sa SDL_RWClose
  264. */
  265. static int rwops_testFileRead(void *arg)
  266. {
  267. SDL_RWops *rw;
  268. int result;
  269. /* Read test. */
  270. rw = SDL_RWFromFile(RWopsReadTestFilename, "r");
  271. SDLTest_AssertPass("Call to SDL_RWFromFile(..,\"r\") succeeded");
  272. SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_RWFromFile in read mode does not return NULL");
  273. /* Bail out if NULL */
  274. if (rw == NULL) {
  275. return TEST_ABORTED;
  276. }
  277. /* Run generic tests */
  278. testGenericRWopsValidations(rw, SDL_FALSE);
  279. /* Close handle */
  280. result = SDL_DestroyRW(rw);
  281. SDLTest_AssertPass("Call to SDL_DestroyRW() succeeded");
  282. SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
  283. return TEST_COMPLETED;
  284. }
  285. /**
  286. * Tests writing from file.
  287. *
  288. * \sa SDL_RWFromFile
  289. * \sa SDL_RWClose
  290. */
  291. static int rwops_testFileWrite(void *arg)
  292. {
  293. SDL_RWops *rw;
  294. int result;
  295. /* Write test. */
  296. rw = SDL_RWFromFile(RWopsWriteTestFilename, "w+");
  297. SDLTest_AssertPass("Call to SDL_RWFromFile(..,\"w+\") succeeded");
  298. SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_RWFromFile in write mode does not return NULL");
  299. /* Bail out if NULL */
  300. if (rw == NULL) {
  301. return TEST_ABORTED;
  302. }
  303. /* Run generic tests */
  304. testGenericRWopsValidations(rw, SDL_TRUE);
  305. /* Close handle */
  306. result = SDL_DestroyRW(rw);
  307. SDLTest_AssertPass("Call to SDL_DestroyRW() succeeded");
  308. SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
  309. return TEST_COMPLETED;
  310. }
  311. /**
  312. * Tests alloc and free RW context.
  313. *
  314. * \sa SDL_CreateRW
  315. * \sa SDL_DestroyRW
  316. */
  317. static int rwops_testAllocFree(void *arg)
  318. {
  319. /* Allocate context */
  320. SDL_RWopsInterface iface;
  321. SDL_RWops *rw;
  322. SDL_zero(iface);
  323. rw = SDL_CreateRW(&iface, NULL);
  324. SDLTest_AssertPass("Call to SDL_CreateRW() succeeded");
  325. SDLTest_AssertCheck(rw != NULL, "Validate result from SDL_CreateRW() is not NULL");
  326. if (rw == NULL) {
  327. return TEST_ABORTED;
  328. }
  329. /* Free context again */
  330. SDL_DestroyRW(rw);
  331. SDLTest_AssertPass("Call to SDL_DestroyRW() succeeded");
  332. return TEST_COMPLETED;
  333. }
  334. /**
  335. * Compare memory and file reads
  336. *
  337. * \sa SDL_RWFromMem
  338. * \sa SDL_RWFromFile
  339. */
  340. static int rwops_testCompareRWFromMemWithRWFromFile(void *arg)
  341. {
  342. int slen = 26;
  343. char buffer_file[27];
  344. char buffer_mem[27];
  345. size_t rv_file;
  346. size_t rv_mem;
  347. Uint64 sv_file;
  348. Uint64 sv_mem;
  349. SDL_RWops *rwops_file;
  350. SDL_RWops *rwops_mem;
  351. int size;
  352. int result;
  353. for (size = 5; size < 10; size++) {
  354. /* Terminate buffer */
  355. buffer_file[slen] = 0;
  356. buffer_mem[slen] = 0;
  357. /* Read/seek from memory */
  358. rwops_mem = SDL_RWFromMem((void *)RWopsAlphabetString, slen);
  359. SDLTest_AssertPass("Call to SDL_RWFromMem()");
  360. rv_mem = SDL_RWread(rwops_mem, buffer_mem, size * 6);
  361. SDLTest_AssertPass("Call to SDL_RWread(mem, size=%d)", size * 6);
  362. sv_mem = SDL_RWseek(rwops_mem, 0, SEEK_END);
  363. SDLTest_AssertPass("Call to SDL_RWseek(mem,SEEK_END)");
  364. result = SDL_DestroyRW(rwops_mem);
  365. SDLTest_AssertPass("Call to SDL_DestroyRW(mem)");
  366. SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
  367. /* Read/see from file */
  368. rwops_file = SDL_RWFromFile(RWopsAlphabetFilename, "r");
  369. SDLTest_AssertPass("Call to SDL_RWFromFile()");
  370. rv_file = SDL_RWread(rwops_file, buffer_file, size * 6);
  371. SDLTest_AssertPass("Call to SDL_RWread(file, size=%d)", size * 6);
  372. sv_file = SDL_RWseek(rwops_file, 0, SEEK_END);
  373. SDLTest_AssertPass("Call to SDL_RWseek(file,SEEK_END)");
  374. result = SDL_DestroyRW(rwops_file);
  375. SDLTest_AssertPass("Call to SDL_DestroyRW(file)");
  376. SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
  377. /* Compare */
  378. 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);
  379. 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);
  380. SDLTest_AssertCheck(buffer_mem[slen] == 0, "Verify mem buffer termination; expected: 0, got: %d", buffer_mem[slen]);
  381. SDLTest_AssertCheck(buffer_file[slen] == 0, "Verify file buffer termination; expected: 0, got: %d", buffer_file[slen]);
  382. SDLTest_AssertCheck(
  383. SDL_strncmp(buffer_mem, RWopsAlphabetString, slen) == 0,
  384. "Verify mem buffer contain alphabet string; expected: %s, got: %s", RWopsAlphabetString, buffer_mem);
  385. SDLTest_AssertCheck(
  386. SDL_strncmp(buffer_file, RWopsAlphabetString, slen) == 0,
  387. "Verify file buffer contain alphabet string; expected: %s, got: %s", RWopsAlphabetString, buffer_file);
  388. }
  389. return TEST_COMPLETED;
  390. }
  391. /**
  392. * Tests writing and reading from file using endian aware functions.
  393. *
  394. * \sa SDL_RWFromFile
  395. * \sa SDL_RWClose
  396. * \sa SDL_ReadU16BE
  397. * \sa SDL_WriteU16BE
  398. */
  399. static int rwops_testFileWriteReadEndian(void *arg)
  400. {
  401. SDL_RWops *rw;
  402. Sint64 result;
  403. int mode;
  404. Uint16 BE16value;
  405. Uint32 BE32value;
  406. Uint64 BE64value;
  407. Uint16 LE16value;
  408. Uint32 LE32value;
  409. Uint64 LE64value;
  410. Uint16 BE16test;
  411. Uint32 BE32test;
  412. Uint64 BE64test;
  413. Uint16 LE16test;
  414. Uint32 LE32test;
  415. Uint64 LE64test;
  416. SDL_bool bresult;
  417. int cresult;
  418. for (mode = 0; mode < 3; mode++) {
  419. /* Create test data */
  420. switch (mode) {
  421. default:
  422. case 0:
  423. SDLTest_Log("All 0 values");
  424. BE16value = 0;
  425. BE32value = 0;
  426. BE64value = 0;
  427. LE16value = 0;
  428. LE32value = 0;
  429. LE64value = 0;
  430. break;
  431. case 1:
  432. SDLTest_Log("All 1 values");
  433. BE16value = 1;
  434. BE32value = 1;
  435. BE64value = 1;
  436. LE16value = 1;
  437. LE32value = 1;
  438. LE64value = 1;
  439. break;
  440. case 2:
  441. SDLTest_Log("Random values");
  442. BE16value = SDLTest_RandomUint16();
  443. BE32value = SDLTest_RandomUint32();
  444. BE64value = SDLTest_RandomUint64();
  445. LE16value = SDLTest_RandomUint16();
  446. LE32value = SDLTest_RandomUint32();
  447. LE64value = SDLTest_RandomUint64();
  448. break;
  449. }
  450. /* Write test. */
  451. rw = SDL_RWFromFile(RWopsWriteTestFilename, "w+");
  452. SDLTest_AssertPass("Call to SDL_RWFromFile(..,\"w+\")");
  453. SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_RWFromFile in write mode does not return NULL");
  454. /* Bail out if NULL */
  455. if (rw == NULL) {
  456. return TEST_ABORTED;
  457. }
  458. /* Write test data */
  459. bresult = SDL_WriteU16BE(rw, BE16value);
  460. SDLTest_AssertPass("Call to SDL_WriteU16BE()");
  461. SDLTest_AssertCheck(bresult == SDL_TRUE, "Validate object written, expected: SDL_TRUE, got: SDL_FALSE");
  462. bresult = SDL_WriteU32BE(rw, BE32value);
  463. SDLTest_AssertPass("Call to SDL_WriteU32BE()");
  464. SDLTest_AssertCheck(bresult == SDL_TRUE, "Validate object written, expected: SDL_TRUE, got: SDL_FALSE");
  465. bresult = SDL_WriteU64BE(rw, BE64value);
  466. SDLTest_AssertPass("Call to SDL_WriteU64BE()");
  467. SDLTest_AssertCheck(bresult == SDL_TRUE, "Validate object written, expected: SDL_TRUE, got: SDL_FALSE");
  468. bresult = SDL_WriteU16LE(rw, LE16value);
  469. SDLTest_AssertPass("Call to SDL_WriteU16LE()");
  470. SDLTest_AssertCheck(bresult == SDL_TRUE, "Validate object written, expected: SDL_TRUE, got: SDL_FALSE");
  471. bresult = SDL_WriteU32LE(rw, LE32value);
  472. SDLTest_AssertPass("Call to SDL_WriteU32LE()");
  473. SDLTest_AssertCheck(bresult == SDL_TRUE, "Validate object written, expected: SDL_TRUE, got: SDL_FALSE");
  474. bresult = SDL_WriteU64LE(rw, LE64value);
  475. SDLTest_AssertPass("Call to SDL_WriteU64LE()");
  476. SDLTest_AssertCheck(bresult == SDL_TRUE, "Validate object written, expected: SDL_TRUE, got: SDL_FALSE");
  477. /* Test seek to start */
  478. result = SDL_RWseek(rw, 0, SDL_RW_SEEK_SET);
  479. SDLTest_AssertPass("Call to SDL_RWseek succeeded");
  480. SDLTest_AssertCheck(result == 0, "Verify result from position 0 with SDL_RWseek, expected 0, got %i", (int)result);
  481. /* Read test data */
  482. bresult = SDL_ReadU16BE(rw, &BE16test);
  483. SDLTest_AssertPass("Call to SDL_ReadU16BE()");
  484. SDLTest_AssertCheck(bresult == SDL_TRUE, "Validate object read, expected: SDL_TRUE, got: SDL_FALSE");
  485. SDLTest_AssertCheck(BE16test == BE16value, "Validate object read from SDL_ReadU16BE, expected: %hu, got: %hu", BE16value, BE16test);
  486. bresult = SDL_ReadU32BE(rw, &BE32test);
  487. SDLTest_AssertPass("Call to SDL_ReadU32BE()");
  488. SDLTest_AssertCheck(bresult == SDL_TRUE, "Validate object read, expected: SDL_TRUE, got: SDL_FALSE");
  489. SDLTest_AssertCheck(BE32test == BE32value, "Validate object read from SDL_ReadU32BE, expected: %" SDL_PRIu32 ", got: %" SDL_PRIu32, BE32value, BE32test);
  490. bresult = SDL_ReadU64BE(rw, &BE64test);
  491. SDLTest_AssertPass("Call to SDL_ReadU64BE()");
  492. SDLTest_AssertCheck(bresult == SDL_TRUE, "Validate object read, expected: SDL_TRUE, got: SDL_FALSE");
  493. SDLTest_AssertCheck(BE64test == BE64value, "Validate object read from SDL_ReadU64BE, expected: %" SDL_PRIu64 ", got: %" SDL_PRIu64, BE64value, BE64test);
  494. bresult = SDL_ReadU16LE(rw, &LE16test);
  495. SDLTest_AssertPass("Call to SDL_ReadU16LE()");
  496. SDLTest_AssertCheck(bresult == SDL_TRUE, "Validate object read, expected: SDL_TRUE, got: SDL_FALSE");
  497. SDLTest_AssertCheck(LE16test == LE16value, "Validate object read from SDL_ReadU16LE, expected: %hu, got: %hu", LE16value, LE16test);
  498. bresult = SDL_ReadU32LE(rw, &LE32test);
  499. SDLTest_AssertPass("Call to SDL_ReadU32LE()");
  500. SDLTest_AssertCheck(bresult == SDL_TRUE, "Validate object read, expected: SDL_TRUE, got: SDL_FALSE");
  501. SDLTest_AssertCheck(LE32test == LE32value, "Validate object read from SDL_ReadU32LE, expected: %" SDL_PRIu32 ", got: %" SDL_PRIu32, LE32value, LE32test);
  502. bresult = SDL_ReadU64LE(rw, &LE64test);
  503. SDLTest_AssertPass("Call to SDL_ReadU64LE()");
  504. SDLTest_AssertCheck(bresult == SDL_TRUE, "Validate object read, expected: SDL_TRUE, got: SDL_FALSE");
  505. SDLTest_AssertCheck(LE64test == LE64value, "Validate object read from SDL_ReadU64LE, expected: %" SDL_PRIu64 ", got: %" SDL_PRIu64, LE64value, LE64test);
  506. /* Close handle */
  507. cresult = SDL_DestroyRW(rw);
  508. SDLTest_AssertPass("Call to SDL_DestroyRW() 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. };