🛠️🐜 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.

757 lines
26 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
  26. 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. remove(RWopsReadTestFilename);
  34. remove(RWopsWriteTestFilename);
  35. 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) return;
  40. /* Write some known text into it */
  41. fileLen = SDL_strlen(RWopsHelloWorldTestString);
  42. writtenLen = fwrite(RWopsHelloWorldTestString, 1, fileLen, handle);
  43. SDLTest_AssertCheck(fileLen == writtenLen, "Verify number of written bytes, expected %i, got %i", (int) fileLen, (int) writtenLen);
  44. result = fclose(handle);
  45. SDLTest_AssertCheck(result == 0, "Verify result from fclose, expected 0, got %i", result);
  46. /* Create a second test file */
  47. handle = fopen(RWopsAlphabetFilename, "w");
  48. SDLTest_AssertCheck(handle != NULL, "Verify creation of file '%s' returned non NULL handle", RWopsAlphabetFilename);
  49. if (handle == NULL) return;
  50. /* Write alphabet text into it */
  51. fileLen = SDL_strlen(RWopsAlphabetString);
  52. writtenLen = fwrite(RWopsAlphabetString, 1, fileLen, handle);
  53. SDLTest_AssertCheck(fileLen == writtenLen, "Verify number of written bytes, expected %i, got %i", (int) fileLen, (int) writtenLen);
  54. result = fclose(handle);
  55. SDLTest_AssertCheck(result == 0, "Verify result from fclose, expected 0, got %i", result);
  56. SDLTest_AssertPass("Creation of test file completed");
  57. }
  58. void
  59. RWopsTearDown(void *arg)
  60. {
  61. int result;
  62. /* Remove the created files to clean up; ignore errors for write filename */
  63. result = remove(RWopsReadTestFilename);
  64. SDLTest_AssertCheck(result == 0, "Verify result from remove(%s), expected 0, got %i", RWopsReadTestFilename, result);
  65. remove(RWopsWriteTestFilename);
  66. result = remove(RWopsAlphabetFilename);
  67. SDLTest_AssertCheck(result == 0, "Verify result from remove(%s), expected 0, got %i", RWopsAlphabetFilename, result);
  68. SDLTest_AssertPass("Cleanup of test files completed");
  69. }
  70. /**
  71. * @brief Makes sure parameters work properly. Local helper function.
  72. *
  73. * \sa
  74. * http://wiki.libsdl.org/SDL_RWseek
  75. * http://wiki.libsdl.org/SDL_RWread
  76. */
  77. void
  78. _testGenericRWopsValidations(SDL_RWops *rw, int write)
  79. {
  80. char buf[sizeof(RWopsHelloWorldTestString)];
  81. Sint64 i;
  82. size_t 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, RW_SEEK_SET );
  88. SDLTest_AssertPass("Call to SDL_RWseek succeeded");
  89. SDLTest_AssertCheck(i == (Sint64)0, "Verify seek to 0 with SDL_RWseek (RW_SEEK_SET), expected 0, got %"SDL_PRIs64, i);
  90. /* Test write. */
  91. s = SDL_RWwrite(rw, RWopsHelloWorldTestString, sizeof(RWopsHelloWorldTestString)-1, 1);
  92. SDLTest_AssertPass("Call to SDL_RWwrite succeeded");
  93. if (write) {
  94. SDLTest_AssertCheck(s == (size_t)1, "Verify result of writing one byte with SDL_RWwrite, expected 1, got %i", (int) s);
  95. }
  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
  148. rwops_testParamNegative (void)
  149. {
  150. SDL_RWops *rwops;
  151. /* These should all fail. */
  152. rwops = SDL_RWFromFile(NULL, NULL);
  153. SDLTest_AssertPass("Call to SDL_RWFromFile(NULL, NULL) succeeded");
  154. SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromFile(NULL, NULL) returns NULL");
  155. rwops = SDL_RWFromFile(NULL, "ab+");
  156. SDLTest_AssertPass("Call to SDL_RWFromFile(NULL, \"ab+\") succeeded");
  157. SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromFile(NULL, \"ab+\") returns NULL");
  158. rwops = SDL_RWFromFile(NULL, "sldfkjsldkfj");
  159. SDLTest_AssertPass("Call to SDL_RWFromFile(NULL, \"sldfkjsldkfj\") succeeded");
  160. SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromFile(NULL, \"sldfkjsldkfj\") returns NULL");
  161. rwops = SDL_RWFromFile("something", "");
  162. SDLTest_AssertPass("Call to SDL_RWFromFile(\"something\", \"\") succeeded");
  163. SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromFile(\"something\", \"\") returns NULL");
  164. rwops = SDL_RWFromFile("something", NULL);
  165. SDLTest_AssertPass("Call to SDL_RWFromFile(\"something\", NULL) succeeded");
  166. SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromFile(\"something\", NULL) returns NULL");
  167. rwops = SDL_RWFromMem((void *)NULL, 10);
  168. SDLTest_AssertPass("Call to SDL_RWFromMem(NULL, 10) succeeded");
  169. SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromMem(NULL, 10) returns NULL");
  170. rwops = SDL_RWFromMem((void *)RWopsAlphabetString, 0);
  171. SDLTest_AssertPass("Call to SDL_RWFromMem(data, 0) succeeded");
  172. SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromMem(data, 0) returns NULL");
  173. rwops = SDL_RWFromConstMem((const void *)RWopsAlphabetString, 0);
  174. SDLTest_AssertPass("Call to SDL_RWFromConstMem(data, 0) succeeded");
  175. SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromConstMem(data, 0) returns NULL");
  176. return TEST_COMPLETED;
  177. }
  178. /**
  179. * @brief Tests opening from memory.
  180. *
  181. * \sa http://wiki.libsdl.org/SDL_RWFromMem
  182. * \sa http://wiki.libsdl.org/SDL_RWClose
  183. */
  184. int
  185. rwops_testMem (void)
  186. {
  187. char mem[sizeof(RWopsHelloWorldTestString)];
  188. SDL_RWops *rw;
  189. int result;
  190. /* Clear buffer */
  191. SDL_zeroa(mem);
  192. /* Open */
  193. rw = SDL_RWFromMem(mem, sizeof(RWopsHelloWorldTestString)-1);
  194. SDLTest_AssertPass("Call to SDL_RWFromMem() succeeded");
  195. SDLTest_AssertCheck(rw != NULL, "Verify opening memory with SDL_RWFromMem does not return NULL");
  196. /* Bail out if NULL */
  197. if (rw == NULL) return TEST_ABORTED;
  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
  216. rwops_testConstMem (void)
  217. {
  218. SDL_RWops *rw;
  219. int result;
  220. /* Open handle */
  221. rw = SDL_RWFromConstMem( RWopsHelloWorldCompString, sizeof(RWopsHelloWorldCompString)-1 );
  222. SDLTest_AssertPass("Call to SDL_RWFromConstMem() succeeded");
  223. SDLTest_AssertCheck(rw != NULL, "Verify opening memory with SDL_RWFromConstMem does not return NULL");
  224. /* Bail out if NULL */
  225. if (rw == NULL) return TEST_ABORTED;
  226. /* Check type */
  227. SDLTest_AssertCheck(rw->type == SDL_RWOPS_MEMORY_RO, "Verify RWops type is SDL_RWOPS_MEMORY_RO; expected: %d, got: %" SDL_PRIu32, SDL_RWOPS_MEMORY_RO, rw->type);
  228. /* Run generic tests */
  229. _testGenericRWopsValidations( rw, 0 );
  230. /* Close handle */
  231. result = SDL_RWclose(rw);
  232. SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
  233. SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
  234. return TEST_COMPLETED;
  235. }
  236. /**
  237. * @brief Tests reading from file.
  238. *
  239. * \sa
  240. * http://wiki.libsdl.org/SDL_RWFromFile
  241. * http://wiki.libsdl.org/SDL_RWClose
  242. */
  243. int
  244. 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) return TEST_ABORTED;
  254. /* Check type */
  255. #if defined(__ANDROID__)
  256. SDLTest_AssertCheck(
  257. rw->type == SDL_RWOPS_STDFILE || rw->type == SDL_RWOPS_JNIFILE,
  258. "Verify RWops type is SDL_RWOPS_STDFILE or SDL_RWOPS_JNIFILE; expected: %d|%d, got: %d", SDL_RWOPS_STDFILE, SDL_RWOPS_JNIFILE, rw->type);
  259. #elif defined(__WIN32__)
  260. SDLTest_AssertCheck(
  261. rw->type == SDL_RWOPS_WINFILE,
  262. "Verify RWops type is SDL_RWOPS_WINFILE; expected: %d, got: %d", SDL_RWOPS_WINFILE, rw->type);
  263. #else
  264. SDLTest_AssertCheck(
  265. rw->type == SDL_RWOPS_STDFILE,
  266. "Verify RWops type is SDL_RWOPS_STDFILE; expected: %d, got: %" SDL_PRIu32, SDL_RWOPS_STDFILE, rw->type);
  267. #endif
  268. /* Run generic tests */
  269. _testGenericRWopsValidations( rw, 0 );
  270. /* Close handle */
  271. result = SDL_RWclose(rw);
  272. SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
  273. SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
  274. return TEST_COMPLETED;
  275. }
  276. /**
  277. * @brief Tests writing from file.
  278. *
  279. * \sa
  280. * http://wiki.libsdl.org/SDL_RWFromFile
  281. * http://wiki.libsdl.org/SDL_RWClose
  282. */
  283. int
  284. rwops_testFileWrite(void)
  285. {
  286. SDL_RWops *rw;
  287. int result;
  288. /* Write test. */
  289. rw = SDL_RWFromFile(RWopsWriteTestFilename, "w+");
  290. SDLTest_AssertPass("Call to SDL_RWFromFile(..,\"w+\") succeeded");
  291. SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_RWFromFile in write mode does not return NULL");
  292. /* Bail out if NULL */
  293. if (rw == NULL) return TEST_ABORTED;
  294. /* Check type */
  295. #if defined(__ANDROID__)
  296. SDLTest_AssertCheck(
  297. rw->type == SDL_RWOPS_STDFILE || rw->type == SDL_RWOPS_JNIFILE,
  298. "Verify RWops type is SDL_RWOPS_STDFILE or SDL_RWOPS_JNIFILE; expected: %d|%d, got: %d", SDL_RWOPS_STDFILE, SDL_RWOPS_JNIFILE, rw->type);
  299. #elif defined(__WIN32__)
  300. SDLTest_AssertCheck(
  301. rw->type == SDL_RWOPS_WINFILE,
  302. "Verify RWops type is SDL_RWOPS_WINFILE; expected: %d, got: %d", SDL_RWOPS_WINFILE, rw->type);
  303. #else
  304. SDLTest_AssertCheck(
  305. rw->type == SDL_RWOPS_STDFILE,
  306. "Verify RWops type is SDL_RWOPS_STDFILE; expected: %d, got: %" SDL_PRIu32, SDL_RWOPS_STDFILE, rw->type);
  307. #endif
  308. /* Run generic tests */
  309. _testGenericRWopsValidations( rw, 1 );
  310. /* Close handle */
  311. result = SDL_RWclose(rw);
  312. SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
  313. SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
  314. return TEST_COMPLETED;
  315. }
  316. /**
  317. * @brief Tests reading from file handle
  318. *
  319. * \sa
  320. * http://wiki.libsdl.org/SDL_RWFromFP
  321. * http://wiki.libsdl.org/SDL_RWClose
  322. *
  323. */
  324. int
  325. rwops_testFPRead(void)
  326. {
  327. #ifdef HAVE_LIBC
  328. FILE *fp;
  329. SDL_RWops *rw;
  330. int result;
  331. /* Run read tests. */
  332. fp = fopen(RWopsReadTestFilename, "r");
  333. SDLTest_AssertCheck(fp != NULL, "Verify handle from opening file '%s' in read mode is not NULL", RWopsReadTestFilename);
  334. /* Bail out if NULL */
  335. if (fp == NULL) return TEST_ABORTED;
  336. /* Open */
  337. rw = SDL_RWFromFP( fp, SDL_TRUE );
  338. SDLTest_AssertPass("Call to SDL_RWFromFP() succeeded");
  339. SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_RWFromFP in read mode does not return NULL");
  340. /* Bail out if NULL */
  341. if (rw == NULL) {
  342. fclose(fp);
  343. return TEST_ABORTED;
  344. }
  345. /* Check type */
  346. SDLTest_AssertCheck(
  347. rw->type == SDL_RWOPS_STDFILE,
  348. "Verify RWops type is SDL_RWOPS_STDFILE; expected: %d, got: %" SDL_PRIu32, SDL_RWOPS_STDFILE, rw->type);
  349. /* Run generic tests */
  350. _testGenericRWopsValidations( rw, 0 );
  351. /* Close handle - does fclose() */
  352. result = SDL_RWclose(rw);
  353. SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
  354. SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
  355. #endif /* HAVE_LIBC */
  356. return TEST_COMPLETED;
  357. }
  358. /**
  359. * @brief Tests writing to file handle
  360. *
  361. * \sa
  362. * http://wiki.libsdl.org/SDL_RWFromFP
  363. * http://wiki.libsdl.org/SDL_RWClose
  364. *
  365. */
  366. int
  367. rwops_testFPWrite(void)
  368. {
  369. #ifdef HAVE_LIBC
  370. FILE *fp;
  371. SDL_RWops *rw;
  372. int result;
  373. /* Run write tests. */
  374. fp = fopen(RWopsWriteTestFilename, "w+");
  375. SDLTest_AssertCheck(fp != NULL, "Verify handle from opening file '%s' in write mode is not NULL", RWopsWriteTestFilename);
  376. /* Bail out if NULL */
  377. if (fp == NULL) return TEST_ABORTED;
  378. /* Open */
  379. rw = SDL_RWFromFP( fp, SDL_TRUE );
  380. SDLTest_AssertPass("Call to SDL_RWFromFP() succeeded");
  381. SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_RWFromFP in write mode does not return NULL");
  382. /* Bail out if NULL */
  383. if (rw == NULL) {
  384. fclose(fp);
  385. return TEST_ABORTED;
  386. }
  387. /* Check type */
  388. SDLTest_AssertCheck(
  389. rw->type == SDL_RWOPS_STDFILE,
  390. "Verify RWops type is SDL_RWOPS_STDFILE; expected: %d, got: %" SDL_PRIu32, SDL_RWOPS_STDFILE, rw->type);
  391. /* Run generic tests */
  392. _testGenericRWopsValidations( rw, 1 );
  393. /* Close handle - does fclose() */
  394. result = SDL_RWclose(rw);
  395. SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
  396. SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
  397. #endif /* HAVE_LIBC */
  398. return TEST_COMPLETED;
  399. }
  400. /**
  401. * @brief Tests alloc and free RW context.
  402. *
  403. * \sa http://wiki.libsdl.org/SDL_AllocRW
  404. * \sa http://wiki.libsdl.org/SDL_FreeRW
  405. */
  406. int
  407. rwops_testAllocFree (void)
  408. {
  409. /* Allocate context */
  410. SDL_RWops *rw = SDL_AllocRW();
  411. SDLTest_AssertPass("Call to SDL_AllocRW() succeeded");
  412. SDLTest_AssertCheck(rw != NULL, "Validate result from SDL_AllocRW() is not NULL");
  413. if (rw==NULL) return TEST_ABORTED;
  414. /* Check type */
  415. SDLTest_AssertCheck(
  416. rw->type == SDL_RWOPS_UNKNOWN,
  417. "Verify RWops type is SDL_RWOPS_UNKNOWN; expected: %d, got: %" SDL_PRIu32, SDL_RWOPS_UNKNOWN, rw->type);
  418. /* Free context again */
  419. SDL_FreeRW(rw);
  420. SDLTest_AssertPass("Call to SDL_FreeRW() succeeded");
  421. return TEST_COMPLETED;
  422. }
  423. /**
  424. * @brief Compare memory and file reads
  425. *
  426. * \sa http://wiki.libsdl.org/SDL_RWFromMem
  427. * \sa http://wiki.libsdl.org/SDL_RWFromFile
  428. */
  429. int
  430. rwops_testCompareRWFromMemWithRWFromFile(void)
  431. {
  432. int slen = 26;
  433. char buffer_file[27];
  434. char buffer_mem[27];
  435. size_t rv_file;
  436. size_t rv_mem;
  437. Uint64 sv_file;
  438. Uint64 sv_mem;
  439. SDL_RWops* rwops_file;
  440. SDL_RWops* rwops_mem;
  441. int size;
  442. int result;
  443. for (size=5; size<10; size++)
  444. {
  445. /* Terminate buffer */
  446. buffer_file[slen] = 0;
  447. buffer_mem[slen] = 0;
  448. /* Read/seek from memory */
  449. rwops_mem = SDL_RWFromMem((void *)RWopsAlphabetString, slen);
  450. SDLTest_AssertPass("Call to SDL_RWFromMem()");
  451. rv_mem = SDL_RWread(rwops_mem, buffer_mem, size, 6);
  452. SDLTest_AssertPass("Call to SDL_RWread(mem, size=%d)", size);
  453. sv_mem = SDL_RWseek(rwops_mem, 0, SEEK_END);
  454. SDLTest_AssertPass("Call to SDL_RWseek(mem,SEEK_END)");
  455. result = SDL_RWclose(rwops_mem);
  456. SDLTest_AssertPass("Call to SDL_RWclose(mem)");
  457. SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
  458. /* Read/see from file */
  459. rwops_file = SDL_RWFromFile(RWopsAlphabetFilename, "r");
  460. SDLTest_AssertPass("Call to SDL_RWFromFile()");
  461. rv_file = SDL_RWread(rwops_file, buffer_file, size, 6);
  462. SDLTest_AssertPass("Call to SDL_RWread(file, size=%d)", size);
  463. sv_file = SDL_RWseek(rwops_file, 0, SEEK_END);
  464. SDLTest_AssertPass("Call to SDL_RWseek(file,SEEK_END)");
  465. result = SDL_RWclose(rwops_file);
  466. SDLTest_AssertPass("Call to SDL_RWclose(file)");
  467. SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
  468. /* Compare */
  469. 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);
  470. 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);
  471. SDLTest_AssertCheck(buffer_mem[slen] == 0, "Verify mem buffer termination; expected: 0, got: %d", buffer_mem[slen]);
  472. SDLTest_AssertCheck(buffer_file[slen] == 0, "Verify file buffer termination; expected: 0, got: %d", buffer_file[slen]);
  473. SDLTest_AssertCheck(
  474. SDL_strncmp(buffer_mem, RWopsAlphabetString, slen) == 0,
  475. "Verify mem buffer contain alphabet string; expected: %s, got: %s", RWopsAlphabetString, buffer_mem);
  476. SDLTest_AssertCheck(
  477. SDL_strncmp(buffer_file, RWopsAlphabetString, slen) == 0,
  478. "Verify file buffer contain alphabet string; expected: %s, got: %s", RWopsAlphabetString, buffer_file);
  479. }
  480. return TEST_COMPLETED;
  481. }
  482. /**
  483. * @brief Tests writing and reading from file using endian aware functions.
  484. *
  485. * \sa
  486. * http://wiki.libsdl.org/SDL_RWFromFile
  487. * http://wiki.libsdl.org/SDL_RWClose
  488. * http://wiki.libsdl.org/SDL_ReadBE16
  489. * http://wiki.libsdl.org/SDL_WriteBE16
  490. */
  491. int
  492. rwops_testFileWriteReadEndian(void)
  493. {
  494. SDL_RWops *rw;
  495. Sint64 result;
  496. int mode;
  497. size_t objectsWritten;
  498. Uint16 BE16value;
  499. Uint32 BE32value;
  500. Uint64 BE64value;
  501. Uint16 LE16value;
  502. Uint32 LE32value;
  503. Uint64 LE64value;
  504. Uint16 BE16test;
  505. Uint32 BE32test;
  506. Uint64 BE64test;
  507. Uint16 LE16test;
  508. Uint32 LE32test;
  509. Uint64 LE64test;
  510. int cresult;
  511. for (mode = 0; mode < 3; mode++) {
  512. /* Create test data */
  513. switch (mode) {
  514. default:
  515. case 0:
  516. SDLTest_Log("All 0 values");
  517. BE16value = 0;
  518. BE32value = 0;
  519. BE64value = 0;
  520. LE16value = 0;
  521. LE32value = 0;
  522. LE64value = 0;
  523. break;
  524. case 1:
  525. SDLTest_Log("All 1 values");
  526. BE16value = 1;
  527. BE32value = 1;
  528. BE64value = 1;
  529. LE16value = 1;
  530. LE32value = 1;
  531. LE64value = 1;
  532. break;
  533. case 2:
  534. SDLTest_Log("Random values");
  535. BE16value = SDLTest_RandomUint16();
  536. BE32value = SDLTest_RandomUint32();
  537. BE64value = SDLTest_RandomUint64();
  538. LE16value = SDLTest_RandomUint16();
  539. LE32value = SDLTest_RandomUint32();
  540. LE64value = SDLTest_RandomUint64();
  541. break;
  542. }
  543. /* Write test. */
  544. rw = SDL_RWFromFile(RWopsWriteTestFilename, "w+");
  545. SDLTest_AssertPass("Call to SDL_RWFromFile(..,\"w+\")");
  546. SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_RWFromFile in write mode does not return NULL");
  547. /* Bail out if NULL */
  548. if (rw == NULL) return TEST_ABORTED;
  549. /* Write test data */
  550. objectsWritten = SDL_WriteBE16(rw, BE16value);
  551. SDLTest_AssertPass("Call to SDL_WriteBE16()");
  552. SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", (int) objectsWritten);
  553. objectsWritten = SDL_WriteBE32(rw, BE32value);
  554. SDLTest_AssertPass("Call to SDL_WriteBE32()");
  555. SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", (int) objectsWritten);
  556. objectsWritten = SDL_WriteBE64(rw, BE64value);
  557. SDLTest_AssertPass("Call to SDL_WriteBE64()");
  558. SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", (int) objectsWritten);
  559. objectsWritten = SDL_WriteLE16(rw, LE16value);
  560. SDLTest_AssertPass("Call to SDL_WriteLE16()");
  561. SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", (int) objectsWritten);
  562. objectsWritten = SDL_WriteLE32(rw, LE32value);
  563. SDLTest_AssertPass("Call to SDL_WriteLE32()");
  564. SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", (int) objectsWritten);
  565. objectsWritten = SDL_WriteLE64(rw, LE64value);
  566. SDLTest_AssertPass("Call to SDL_WriteLE64()");
  567. SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", (int) objectsWritten);
  568. /* Test seek to start */
  569. result = SDL_RWseek( rw, 0, RW_SEEK_SET );
  570. SDLTest_AssertPass("Call to SDL_RWseek succeeded");
  571. SDLTest_AssertCheck(result == 0, "Verify result from position 0 with SDL_RWseek, expected 0, got %i", (int) result);
  572. /* Read test data */
  573. BE16test = SDL_ReadBE16(rw);
  574. SDLTest_AssertPass("Call to SDL_ReadBE16()");
  575. SDLTest_AssertCheck(BE16test == BE16value, "Validate return value from SDL_ReadBE16, expected: %hu, got: %hu", BE16value, BE16test);
  576. BE32test = SDL_ReadBE32(rw);
  577. SDLTest_AssertPass("Call to SDL_ReadBE32()");
  578. SDLTest_AssertCheck(BE32test == BE32value, "Validate return value from SDL_ReadBE32, expected: %" SDL_PRIu32 ", got: %" SDL_PRIu32, BE32value, BE32test);
  579. BE64test = SDL_ReadBE64(rw);
  580. SDLTest_AssertPass("Call to SDL_ReadBE64()");
  581. SDLTest_AssertCheck(BE64test == BE64value, "Validate return value from SDL_ReadBE64, expected: %"SDL_PRIu64", got: %"SDL_PRIu64, BE64value, BE64test);
  582. LE16test = SDL_ReadLE16(rw);
  583. SDLTest_AssertPass("Call to SDL_ReadLE16()");
  584. SDLTest_AssertCheck(LE16test == LE16value, "Validate return value from SDL_ReadLE16, expected: %hu, got: %hu", LE16value, LE16test);
  585. LE32test = SDL_ReadLE32(rw);
  586. SDLTest_AssertPass("Call to SDL_ReadLE32()");
  587. SDLTest_AssertCheck(LE32test == LE32value, "Validate return value from SDL_ReadLE32, expected: %" SDL_PRIu32 ", got: %" SDL_PRIu32, LE32value, LE32test);
  588. LE64test = SDL_ReadLE64(rw);
  589. SDLTest_AssertPass("Call to SDL_ReadLE64()");
  590. SDLTest_AssertCheck(LE64test == LE64value, "Validate return value from SDL_ReadLE64, expected: %"SDL_PRIu64", got: %"SDL_PRIu64, LE64value, LE64test);
  591. /* Close handle */
  592. cresult = SDL_RWclose(rw);
  593. SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
  594. SDLTest_AssertCheck(cresult == 0, "Verify result value is 0; got: %d", cresult);
  595. }
  596. return TEST_COMPLETED;
  597. }
  598. /* ================= Test References ================== */
  599. /* RWops test cases */
  600. static const SDLTest_TestCaseReference rwopsTest1 =
  601. { (SDLTest_TestCaseFp)rwops_testParamNegative, "rwops_testParamNegative", "Negative test for SDL_RWFromFile parameters", TEST_ENABLED };
  602. static const SDLTest_TestCaseReference rwopsTest2 =
  603. { (SDLTest_TestCaseFp)rwops_testMem, "rwops_testMem", "Tests opening from memory", TEST_ENABLED };
  604. static const SDLTest_TestCaseReference rwopsTest3 =
  605. { (SDLTest_TestCaseFp)rwops_testConstMem, "rwops_testConstMem", "Tests opening from (const) memory", TEST_ENABLED };
  606. static const SDLTest_TestCaseReference rwopsTest4 =
  607. { (SDLTest_TestCaseFp)rwops_testFileRead, "rwops_testFileRead", "Tests reading from a file", TEST_ENABLED };
  608. static const SDLTest_TestCaseReference rwopsTest5 =
  609. { (SDLTest_TestCaseFp)rwops_testFileWrite, "rwops_testFileWrite", "Test writing to a file", TEST_ENABLED };
  610. static const SDLTest_TestCaseReference rwopsTest6 =
  611. { (SDLTest_TestCaseFp)rwops_testFPRead, "rwops_testFPRead", "Test reading from file pointer", TEST_ENABLED };
  612. static const SDLTest_TestCaseReference rwopsTest7 =
  613. { (SDLTest_TestCaseFp)rwops_testFPWrite, "rwops_testFPWrite", "Test writing to file pointer", TEST_ENABLED };
  614. static const SDLTest_TestCaseReference rwopsTest8 =
  615. { (SDLTest_TestCaseFp)rwops_testAllocFree, "rwops_testAllocFree", "Test alloc and free of RW context", TEST_ENABLED };
  616. static const SDLTest_TestCaseReference rwopsTest9 =
  617. { (SDLTest_TestCaseFp)rwops_testFileWriteReadEndian, "rwops_testFileWriteReadEndian", "Test writing and reading via the Endian aware functions", TEST_ENABLED };
  618. static const SDLTest_TestCaseReference rwopsTest10 =
  619. { (SDLTest_TestCaseFp)rwops_testCompareRWFromMemWithRWFromFile, "rwops_testCompareRWFromMemWithRWFromFile", "Compare RWFromMem and RWFromFile RWops for read and seek", TEST_ENABLED };
  620. /* Sequence of RWops test cases */
  621. static const SDLTest_TestCaseReference *rwopsTests[] = {
  622. &rwopsTest1, &rwopsTest2, &rwopsTest3, &rwopsTest4, &rwopsTest5, &rwopsTest6,
  623. &rwopsTest7, &rwopsTest8, &rwopsTest9, &rwopsTest10, NULL
  624. };
  625. /* RWops test suite (global) */
  626. SDLTest_TestSuiteReference rwopsTestSuite = {
  627. "RWops",
  628. RWopsSetUp,
  629. rwopsTests,
  630. RWopsTearDown
  631. };