🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

760 lines
27 KiB

  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 "SDL.h"
  16. #include "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
  76. * http://wiki.libsdl.org/SDL_RWseek
  77. * http://wiki.libsdl.org/SDL_RWread
  78. */
  79. void _testGenericRWopsValidations(SDL_RWops *rw, int 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, RW_SEEK_SET);
  89. SDLTest_AssertPass("Call to SDL_RWseek succeeded");
  90. SDLTest_AssertCheck(i == (Sint64)0, "Verify seek to 0 with SDL_RWseek (RW_SEEK_SET), expected 0, got %" SDL_PRIs64, i);
  91. /* Test write. */
  92. s = SDL_RWwrite(rw, RWopsHelloWorldTestString, sizeof(RWopsHelloWorldTestString) - 1, 1);
  93. SDLTest_AssertPass("Call to SDL_RWwrite succeeded");
  94. if (write) {
  95. SDLTest_AssertCheck(s == (size_t)1, "Verify result of writing one byte with SDL_RWwrite, expected 1, got %i", (int)s);
  96. } else {
  97. SDLTest_AssertCheck(s == (size_t)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, RW_SEEK_SET);
  101. SDLTest_AssertPass("Call to SDL_RWseek succeeded");
  102. SDLTest_AssertCheck(i == (Sint64)seekPos, "Verify seek to %i with SDL_RWseek (RW_SEEK_SET), expected %i, got %" SDL_PRIs64, seekPos, seekPos, i);
  103. /* Test seek back to start */
  104. i = SDL_RWseek(rw, 0, RW_SEEK_SET);
  105. SDLTest_AssertPass("Call to SDL_RWseek succeeded");
  106. SDLTest_AssertCheck(i == (Sint64)0, "Verify seek to 0 with SDL_RWseek (RW_SEEK_SET), expected 0, got %" SDL_PRIs64, i);
  107. /* Test read */
  108. s = SDL_RWread(rw, buf, 1, 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, RW_SEEK_CUR);
  120. SDLTest_AssertPass("Call to SDL_RWseek(...,-4,RW_SEEK_CUR) succeeded");
  121. SDLTest_AssertCheck(
  122. i == (Sint64)(sizeof(RWopsHelloWorldTestString) - 5),
  123. "Verify seek to -4 with SDL_RWseek (RW_SEEK_CUR), expected %i, got %i",
  124. (int)(sizeof(RWopsHelloWorldTestString) - 5),
  125. (int)i);
  126. i = SDL_RWseek(rw, -1, RW_SEEK_END);
  127. SDLTest_AssertPass("Call to SDL_RWseek(...,-1,RW_SEEK_END) succeeded");
  128. SDLTest_AssertCheck(
  129. i == (Sint64)(sizeof(RWopsHelloWorldTestString) - 2),
  130. "Verify seek to -1 with SDL_RWseek (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 http://wiki.libsdl.org/SDL_RWFromFile
  145. *
  146. */
  147. int rwops_testParamNegative(void)
  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 http://wiki.libsdl.org/SDL_RWFromMem
  181. * \sa http://wiki.libsdl.org/SDL_RWClose
  182. */
  183. int rwops_testMem(void)
  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
  212. * http://wiki.libsdl.org/SDL_RWFromConstMem
  213. * http://wiki.libsdl.org/SDL_RWClose
  214. */
  215. int rwops_testConstMem(void)
  216. {
  217. SDL_RWops *rw;
  218. int result;
  219. /* Open handle */
  220. rw = SDL_RWFromConstMem(RWopsHelloWorldCompString, sizeof(RWopsHelloWorldCompString) - 1);
  221. SDLTest_AssertPass("Call to SDL_RWFromConstMem() succeeded");
  222. SDLTest_AssertCheck(rw != NULL, "Verify opening memory with SDL_RWFromConstMem does not return NULL");
  223. /* Bail out if NULL */
  224. if (rw == NULL) {
  225. return TEST_ABORTED;
  226. }
  227. /* Check type */
  228. 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);
  229. /* Run generic tests */
  230. _testGenericRWopsValidations(rw, 0);
  231. /* Close handle */
  232. result = SDL_RWclose(rw);
  233. SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
  234. SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
  235. return TEST_COMPLETED;
  236. }
  237. /**
  238. * @brief Tests reading from file.
  239. *
  240. * \sa
  241. * http://wiki.libsdl.org/SDL_RWFromFile
  242. * http://wiki.libsdl.org/SDL_RWClose
  243. */
  244. int rwops_testFileRead(void)
  245. {
  246. SDL_RWops *rw;
  247. int result;
  248. /* Read test. */
  249. rw = SDL_RWFromFile(RWopsReadTestFilename, "r");
  250. SDLTest_AssertPass("Call to SDL_RWFromFile(..,\"r\") succeeded");
  251. SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_RWFromFile in read mode does not return NULL");
  252. /* Bail out if NULL */
  253. if (rw == NULL) {
  254. return TEST_ABORTED;
  255. }
  256. /* Check type */
  257. #if defined(__ANDROID__)
  258. SDLTest_AssertCheck(
  259. rw->type == SDL_RWOPS_STDFILE || rw->type == SDL_RWOPS_JNIFILE,
  260. "Verify RWops type is SDL_RWOPS_STDFILE or SDL_RWOPS_JNIFILE; expected: %d|%d, got: %d", SDL_RWOPS_STDFILE, SDL_RWOPS_JNIFILE, rw->type);
  261. #elif defined(__WIN32__)
  262. SDLTest_AssertCheck(
  263. rw->type == SDL_RWOPS_WINFILE,
  264. "Verify RWops type is SDL_RWOPS_WINFILE; expected: %d, got: %d", SDL_RWOPS_WINFILE, rw->type);
  265. #else
  266. SDLTest_AssertCheck(
  267. rw->type == SDL_RWOPS_STDFILE,
  268. "Verify RWops type is SDL_RWOPS_STDFILE; expected: %d, got: %" SDL_PRIu32, SDL_RWOPS_STDFILE, rw->type);
  269. #endif
  270. /* Run generic tests */
  271. _testGenericRWopsValidations(rw, 0);
  272. /* Close handle */
  273. result = SDL_RWclose(rw);
  274. SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
  275. SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
  276. return TEST_COMPLETED;
  277. }
  278. /**
  279. * @brief Tests writing from file.
  280. *
  281. * \sa
  282. * http://wiki.libsdl.org/SDL_RWFromFile
  283. * http://wiki.libsdl.org/SDL_RWClose
  284. */
  285. int rwops_testFileWrite(void)
  286. {
  287. SDL_RWops *rw;
  288. int result;
  289. /* Write test. */
  290. rw = SDL_RWFromFile(RWopsWriteTestFilename, "w+");
  291. SDLTest_AssertPass("Call to SDL_RWFromFile(..,\"w+\") succeeded");
  292. SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_RWFromFile in write mode does not return NULL");
  293. /* Bail out if NULL */
  294. if (rw == NULL) {
  295. return TEST_ABORTED;
  296. }
  297. /* Check type */
  298. #if defined(__ANDROID__)
  299. SDLTest_AssertCheck(
  300. rw->type == SDL_RWOPS_STDFILE || rw->type == SDL_RWOPS_JNIFILE,
  301. "Verify RWops type is SDL_RWOPS_STDFILE or SDL_RWOPS_JNIFILE; expected: %d|%d, got: %d", SDL_RWOPS_STDFILE, SDL_RWOPS_JNIFILE, rw->type);
  302. #elif defined(__WIN32__)
  303. SDLTest_AssertCheck(
  304. rw->type == SDL_RWOPS_WINFILE,
  305. "Verify RWops type is SDL_RWOPS_WINFILE; expected: %d, got: %d", SDL_RWOPS_WINFILE, rw->type);
  306. #else
  307. SDLTest_AssertCheck(
  308. rw->type == SDL_RWOPS_STDFILE,
  309. "Verify RWops type is SDL_RWOPS_STDFILE; expected: %d, got: %" SDL_PRIu32, SDL_RWOPS_STDFILE, rw->type);
  310. #endif
  311. /* Run generic tests */
  312. _testGenericRWopsValidations(rw, 1);
  313. /* Close handle */
  314. result = SDL_RWclose(rw);
  315. SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
  316. SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
  317. return TEST_COMPLETED;
  318. }
  319. /**
  320. * @brief Tests reading from file handle
  321. *
  322. * \sa
  323. * http://wiki.libsdl.org/SDL_RWFromFP
  324. * http://wiki.libsdl.org/SDL_RWClose
  325. *
  326. */
  327. int
  328. rwops_testFPRead(void)
  329. {
  330. #ifdef HAVE_LIBC
  331. FILE *fp;
  332. SDL_RWops *rw;
  333. int result;
  334. /* Run read tests. */
  335. fp = fopen(RWopsReadTestFilename, "r");
  336. SDLTest_AssertCheck(fp != NULL, "Verify handle from opening file '%s' in read mode is not NULL", RWopsReadTestFilename);
  337. /* Bail out if NULL */
  338. if (fp == NULL) return TEST_ABORTED;
  339. /* Open */
  340. rw = SDL_RWFromFP( fp, SDL_TRUE );
  341. SDLTest_AssertPass("Call to SDL_RWFromFP() succeeded");
  342. SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_RWFromFP in read mode does not return NULL");
  343. /* Bail out if NULL */
  344. if (rw == NULL) {
  345. fclose(fp);
  346. return TEST_ABORTED;
  347. }
  348. /* Check type */
  349. SDLTest_AssertCheck(
  350. rw->type == SDL_RWOPS_STDFILE,
  351. "Verify RWops type is SDL_RWOPS_STDFILE; expected: %d, got: %" SDL_PRIu32, SDL_RWOPS_STDFILE, rw->type);
  352. /* Run generic tests */
  353. _testGenericRWopsValidations( rw, 0 );
  354. /* Close handle - does fclose() */
  355. result = SDL_RWclose(rw);
  356. SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
  357. SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
  358. #endif /* HAVE_LIBC */
  359. return TEST_COMPLETED;
  360. }
  361. /**
  362. * @brief Tests writing to file handle
  363. *
  364. * \sa
  365. * http://wiki.libsdl.org/SDL_RWFromFP
  366. * http://wiki.libsdl.org/SDL_RWClose
  367. *
  368. */
  369. int
  370. rwops_testFPWrite(void)
  371. {
  372. #ifdef HAVE_LIBC
  373. FILE *fp;
  374. SDL_RWops *rw;
  375. int result;
  376. /* Run write tests. */
  377. fp = fopen(RWopsWriteTestFilename, "w+");
  378. SDLTest_AssertCheck(fp != NULL, "Verify handle from opening file '%s' in write mode is not NULL", RWopsWriteTestFilename);
  379. /* Bail out if NULL */
  380. if (fp == NULL) return TEST_ABORTED;
  381. /* Open */
  382. rw = SDL_RWFromFP( fp, SDL_TRUE );
  383. SDLTest_AssertPass("Call to SDL_RWFromFP() succeeded");
  384. SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_RWFromFP in write mode does not return NULL");
  385. /* Bail out if NULL */
  386. if (rw == NULL) {
  387. fclose(fp);
  388. return TEST_ABORTED;
  389. }
  390. /* Check type */
  391. SDLTest_AssertCheck(
  392. rw->type == SDL_RWOPS_STDFILE,
  393. "Verify RWops type is SDL_RWOPS_STDFILE; expected: %d, got: %" SDL_PRIu32, SDL_RWOPS_STDFILE, rw->type);
  394. /* Run generic tests */
  395. _testGenericRWopsValidations( rw, 1 );
  396. /* Close handle - does fclose() */
  397. result = SDL_RWclose(rw);
  398. SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
  399. SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
  400. #endif /* HAVE_LIBC */
  401. return TEST_COMPLETED;
  402. }
  403. /**
  404. * @brief Tests alloc and free RW context.
  405. *
  406. * \sa http://wiki.libsdl.org/SDL_AllocRW
  407. * \sa http://wiki.libsdl.org/SDL_FreeRW
  408. */
  409. int rwops_testAllocFree(void)
  410. {
  411. /* Allocate context */
  412. SDL_RWops *rw = SDL_AllocRW();
  413. SDLTest_AssertPass("Call to SDL_AllocRW() succeeded");
  414. SDLTest_AssertCheck(rw != NULL, "Validate result from SDL_AllocRW() is not NULL");
  415. if (rw == NULL) {
  416. return TEST_ABORTED;
  417. }
  418. /* Check type */
  419. SDLTest_AssertCheck(
  420. rw->type == SDL_RWOPS_UNKNOWN,
  421. "Verify RWops type is SDL_RWOPS_UNKNOWN; expected: %d, got: %" SDL_PRIu32, SDL_RWOPS_UNKNOWN, rw->type);
  422. /* Free context again */
  423. SDL_FreeRW(rw);
  424. SDLTest_AssertPass("Call to SDL_FreeRW() succeeded");
  425. return TEST_COMPLETED;
  426. }
  427. /**
  428. * @brief Compare memory and file reads
  429. *
  430. * \sa http://wiki.libsdl.org/SDL_RWFromMem
  431. * \sa http://wiki.libsdl.org/SDL_RWFromFile
  432. */
  433. int rwops_testCompareRWFromMemWithRWFromFile(void)
  434. {
  435. int slen = 26;
  436. char buffer_file[27];
  437. char buffer_mem[27];
  438. size_t rv_file;
  439. size_t rv_mem;
  440. Uint64 sv_file;
  441. Uint64 sv_mem;
  442. SDL_RWops *rwops_file;
  443. SDL_RWops *rwops_mem;
  444. int size;
  445. int result;
  446. for (size = 5; size < 10; size++) {
  447. /* Terminate buffer */
  448. buffer_file[slen] = 0;
  449. buffer_mem[slen] = 0;
  450. /* Read/seek from memory */
  451. rwops_mem = SDL_RWFromMem((void *)RWopsAlphabetString, slen);
  452. SDLTest_AssertPass("Call to SDL_RWFromMem()");
  453. rv_mem = SDL_RWread(rwops_mem, buffer_mem, size, 6);
  454. SDLTest_AssertPass("Call to SDL_RWread(mem, size=%d)", size);
  455. sv_mem = SDL_RWseek(rwops_mem, 0, SEEK_END);
  456. SDLTest_AssertPass("Call to SDL_RWseek(mem,SEEK_END)");
  457. result = SDL_RWclose(rwops_mem);
  458. SDLTest_AssertPass("Call to SDL_RWclose(mem)");
  459. SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
  460. /* Read/see from file */
  461. rwops_file = SDL_RWFromFile(RWopsAlphabetFilename, "r");
  462. SDLTest_AssertPass("Call to SDL_RWFromFile()");
  463. rv_file = SDL_RWread(rwops_file, buffer_file, size, 6);
  464. SDLTest_AssertPass("Call to SDL_RWread(file, size=%d)", size);
  465. sv_file = SDL_RWseek(rwops_file, 0, SEEK_END);
  466. SDLTest_AssertPass("Call to SDL_RWseek(file,SEEK_END)");
  467. result = SDL_RWclose(rwops_file);
  468. SDLTest_AssertPass("Call to SDL_RWclose(file)");
  469. SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
  470. /* Compare */
  471. 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);
  472. 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);
  473. SDLTest_AssertCheck(buffer_mem[slen] == 0, "Verify mem buffer termination; expected: 0, got: %d", buffer_mem[slen]);
  474. SDLTest_AssertCheck(buffer_file[slen] == 0, "Verify file buffer termination; expected: 0, got: %d", buffer_file[slen]);
  475. SDLTest_AssertCheck(
  476. SDL_strncmp(buffer_mem, RWopsAlphabetString, slen) == 0,
  477. "Verify mem buffer contain alphabet string; expected: %s, got: %s", RWopsAlphabetString, buffer_mem);
  478. SDLTest_AssertCheck(
  479. SDL_strncmp(buffer_file, RWopsAlphabetString, slen) == 0,
  480. "Verify file buffer contain alphabet string; expected: %s, got: %s", RWopsAlphabetString, buffer_file);
  481. }
  482. return TEST_COMPLETED;
  483. }
  484. /**
  485. * @brief Tests writing and reading from file using endian aware functions.
  486. *
  487. * \sa
  488. * http://wiki.libsdl.org/SDL_RWFromFile
  489. * http://wiki.libsdl.org/SDL_RWClose
  490. * http://wiki.libsdl.org/SDL_ReadBE16
  491. * http://wiki.libsdl.org/SDL_WriteBE16
  492. */
  493. int rwops_testFileWriteReadEndian(void)
  494. {
  495. SDL_RWops *rw;
  496. Sint64 result;
  497. int mode;
  498. size_t objectsWritten;
  499. Uint16 BE16value;
  500. Uint32 BE32value;
  501. Uint64 BE64value;
  502. Uint16 LE16value;
  503. Uint32 LE32value;
  504. Uint64 LE64value;
  505. Uint16 BE16test;
  506. Uint32 BE32test;
  507. Uint64 BE64test;
  508. Uint16 LE16test;
  509. Uint32 LE32test;
  510. Uint64 LE64test;
  511. int cresult;
  512. for (mode = 0; mode < 3; mode++) {
  513. /* Create test data */
  514. switch (mode) {
  515. default:
  516. case 0:
  517. SDLTest_Log("All 0 values");
  518. BE16value = 0;
  519. BE32value = 0;
  520. BE64value = 0;
  521. LE16value = 0;
  522. LE32value = 0;
  523. LE64value = 0;
  524. break;
  525. case 1:
  526. SDLTest_Log("All 1 values");
  527. BE16value = 1;
  528. BE32value = 1;
  529. BE64value = 1;
  530. LE16value = 1;
  531. LE32value = 1;
  532. LE64value = 1;
  533. break;
  534. case 2:
  535. SDLTest_Log("Random values");
  536. BE16value = SDLTest_RandomUint16();
  537. BE32value = SDLTest_RandomUint32();
  538. BE64value = SDLTest_RandomUint64();
  539. LE16value = SDLTest_RandomUint16();
  540. LE32value = SDLTest_RandomUint32();
  541. LE64value = SDLTest_RandomUint64();
  542. break;
  543. }
  544. /* Write test. */
  545. rw = SDL_RWFromFile(RWopsWriteTestFilename, "w+");
  546. SDLTest_AssertPass("Call to SDL_RWFromFile(..,\"w+\")");
  547. SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_RWFromFile in write mode does not return NULL");
  548. /* Bail out if NULL */
  549. if (rw == NULL) {
  550. return TEST_ABORTED;
  551. }
  552. /* Write test data */
  553. objectsWritten = SDL_WriteBE16(rw, BE16value);
  554. SDLTest_AssertPass("Call to SDL_WriteBE16()");
  555. SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", (int)objectsWritten);
  556. objectsWritten = SDL_WriteBE32(rw, BE32value);
  557. SDLTest_AssertPass("Call to SDL_WriteBE32()");
  558. SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", (int)objectsWritten);
  559. objectsWritten = SDL_WriteBE64(rw, BE64value);
  560. SDLTest_AssertPass("Call to SDL_WriteBE64()");
  561. SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", (int)objectsWritten);
  562. objectsWritten = SDL_WriteLE16(rw, LE16value);
  563. SDLTest_AssertPass("Call to SDL_WriteLE16()");
  564. SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", (int)objectsWritten);
  565. objectsWritten = SDL_WriteLE32(rw, LE32value);
  566. SDLTest_AssertPass("Call to SDL_WriteLE32()");
  567. SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", (int)objectsWritten);
  568. objectsWritten = SDL_WriteLE64(rw, LE64value);
  569. SDLTest_AssertPass("Call to SDL_WriteLE64()");
  570. SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", (int)objectsWritten);
  571. /* Test seek to start */
  572. result = SDL_RWseek(rw, 0, RW_SEEK_SET);
  573. SDLTest_AssertPass("Call to SDL_RWseek succeeded");
  574. SDLTest_AssertCheck(result == 0, "Verify result from position 0 with SDL_RWseek, expected 0, got %i", (int)result);
  575. /* Read test data */
  576. BE16test = SDL_ReadBE16(rw);
  577. SDLTest_AssertPass("Call to SDL_ReadBE16()");
  578. SDLTest_AssertCheck(BE16test == BE16value, "Validate return value from SDL_ReadBE16, expected: %hu, got: %hu", BE16value, BE16test);
  579. BE32test = SDL_ReadBE32(rw);
  580. SDLTest_AssertPass("Call to SDL_ReadBE32()");
  581. SDLTest_AssertCheck(BE32test == BE32value, "Validate return value from SDL_ReadBE32, expected: %" SDL_PRIu32 ", got: %" SDL_PRIu32, BE32value, BE32test);
  582. BE64test = SDL_ReadBE64(rw);
  583. SDLTest_AssertPass("Call to SDL_ReadBE64()");
  584. SDLTest_AssertCheck(BE64test == BE64value, "Validate return value from SDL_ReadBE64, expected: %" SDL_PRIu64 ", got: %" SDL_PRIu64, BE64value, BE64test);
  585. LE16test = SDL_ReadLE16(rw);
  586. SDLTest_AssertPass("Call to SDL_ReadLE16()");
  587. SDLTest_AssertCheck(LE16test == LE16value, "Validate return value from SDL_ReadLE16, expected: %hu, got: %hu", LE16value, LE16test);
  588. LE32test = SDL_ReadLE32(rw);
  589. SDLTest_AssertPass("Call to SDL_ReadLE32()");
  590. SDLTest_AssertCheck(LE32test == LE32value, "Validate return value from SDL_ReadLE32, expected: %" SDL_PRIu32 ", got: %" SDL_PRIu32, LE32value, LE32test);
  591. LE64test = SDL_ReadLE64(rw);
  592. SDLTest_AssertPass("Call to SDL_ReadLE64()");
  593. SDLTest_AssertCheck(LE64test == LE64value, "Validate return value from SDL_ReadLE64, expected: %" SDL_PRIu64 ", got: %" SDL_PRIu64, LE64value, LE64test);
  594. /* Close handle */
  595. cresult = SDL_RWclose(rw);
  596. SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
  597. SDLTest_AssertCheck(cresult == 0, "Verify result value is 0; got: %d", cresult);
  598. }
  599. return TEST_COMPLETED;
  600. }
  601. /* ================= Test References ================== */
  602. /* RWops test cases */
  603. static const SDLTest_TestCaseReference rwopsTest1 = {
  604. (SDLTest_TestCaseFp)rwops_testParamNegative, "rwops_testParamNegative", "Negative test for SDL_RWFromFile parameters", TEST_ENABLED
  605. };
  606. static const SDLTest_TestCaseReference rwopsTest2 = {
  607. (SDLTest_TestCaseFp)rwops_testMem, "rwops_testMem", "Tests opening from memory", TEST_ENABLED
  608. };
  609. static const SDLTest_TestCaseReference rwopsTest3 = {
  610. (SDLTest_TestCaseFp)rwops_testConstMem, "rwops_testConstMem", "Tests opening from (const) memory", TEST_ENABLED
  611. };
  612. static const SDLTest_TestCaseReference rwopsTest4 = {
  613. (SDLTest_TestCaseFp)rwops_testFileRead, "rwops_testFileRead", "Tests reading from a file", TEST_ENABLED
  614. };
  615. static const SDLTest_TestCaseReference rwopsTest5 = {
  616. (SDLTest_TestCaseFp)rwops_testFileWrite, "rwops_testFileWrite", "Test writing to a file", TEST_ENABLED
  617. };
  618. static const SDLTest_TestCaseReference rwopsTest6 =
  619. { (SDLTest_TestCaseFp)rwops_testFPRead, "rwops_testFPRead", "Test reading from file pointer", TEST_ENABLED };
  620. static const SDLTest_TestCaseReference rwopsTest7 =
  621. { (SDLTest_TestCaseFp)rwops_testFPWrite, "rwops_testFPWrite", "Test writing to file pointer", TEST_ENABLED };
  622. static const SDLTest_TestCaseReference rwopsTest8 =
  623. { (SDLTest_TestCaseFp)rwops_testAllocFree, "rwops_testAllocFree", "Test alloc and free of RW context", TEST_ENABLED };
  624. static const SDLTest_TestCaseReference rwopsTest9 =
  625. { (SDLTest_TestCaseFp)rwops_testFileWriteReadEndian, "rwops_testFileWriteReadEndian", "Test writing and reading via the Endian aware functions", TEST_ENABLED };
  626. static const SDLTest_TestCaseReference rwopsTest10 =
  627. { (SDLTest_TestCaseFp)rwops_testCompareRWFromMemWithRWFromFile, "rwops_testCompareRWFromMemWithRWFromFile", "Compare RWFromMem and RWFromFile RWops for read and seek", TEST_ENABLED };
  628. /* Sequence of RWops test cases */
  629. static const SDLTest_TestCaseReference *rwopsTests[] = {
  630. &rwopsTest1, &rwopsTest2, &rwopsTest3, &rwopsTest4, &rwopsTest5, &rwopsTest6,
  631. &rwopsTest7, &rwopsTest8, &rwopsTest9, &rwopsTest10, NULL
  632. };
  633. /* RWops test suite (global) */
  634. SDLTest_TestSuiteReference rwopsTestSuite = {
  635. "RWops",
  636. RWopsSetUp,
  637. rwopsTests,
  638. RWopsTearDown
  639. };