testfile.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely.
  9. */
  10. /* sanity tests on SDL_rwops.c (usefull for alternative implementations of stdio rwops) */
  11. /* quiet windows compiler warnings */
  12. #define _CRT_NONSTDC_NO_WARNINGS
  13. #include <stdlib.h>
  14. #ifndef _MSC_VER
  15. #include <unistd.h>
  16. #endif
  17. #include "SDL.h"
  18. #include "SDL_endian.h"
  19. #include <stdio.h>
  20. /* WARNING ! those 2 files will be destroyed by this test program */
  21. #ifdef __IPHONEOS__
  22. #define FBASENAME1 "../Documents/sdldata1" /* this file will be created during tests */
  23. #define FBASENAME2 "../Documents/sdldata2" /* this file should not exist before starting test */
  24. #else
  25. #define FBASENAME1 "sdldata1" /* this file will be created during tests */
  26. #define FBASENAME2 "sdldata2" /* this file should not exist before starting test */
  27. #endif
  28. #ifndef NULL
  29. #define NULL ((void *)0)
  30. #endif
  31. static void
  32. cleanup(void)
  33. {
  34. unlink(FBASENAME1);
  35. unlink(FBASENAME2);
  36. }
  37. static void
  38. rwops_error_quit(unsigned line, SDL_RWops * rwops)
  39. {
  40. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "testfile.c(%d): failed\n", line);
  41. if (rwops) {
  42. rwops->close(rwops); /* This calls SDL_FreeRW(rwops); */
  43. }
  44. cleanup();
  45. exit(1); /* quit with rwops error (test failed) */
  46. }
  47. #define RWOP_ERR_QUIT(x) rwops_error_quit( __LINE__, (x) )
  48. int
  49. main(int argc, char *argv[])
  50. {
  51. SDL_RWops *rwops = NULL;
  52. char test_buf[30];
  53. /* Enable standard application logging */
  54. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  55. cleanup();
  56. /* test 1 : basic argument test: all those calls to SDL_RWFromFile should fail */
  57. rwops = SDL_RWFromFile(NULL, NULL);
  58. if (rwops)
  59. RWOP_ERR_QUIT(rwops);
  60. rwops = SDL_RWFromFile(NULL, "ab+");
  61. if (rwops)
  62. RWOP_ERR_QUIT(rwops);
  63. rwops = SDL_RWFromFile(NULL, "sldfkjsldkfj");
  64. if (rwops)
  65. RWOP_ERR_QUIT(rwops);
  66. rwops = SDL_RWFromFile("something", "");
  67. if (rwops)
  68. RWOP_ERR_QUIT(rwops);
  69. rwops = SDL_RWFromFile("something", NULL);
  70. if (rwops)
  71. RWOP_ERR_QUIT(rwops);
  72. SDL_Log("test1 OK\n");
  73. /* test 2 : check that inexistent file is not successfully opened/created when required */
  74. /* modes : r, r+ imply that file MUST exist
  75. modes : a, a+, w, w+ checks that it succeeds (file may not exists)
  76. */
  77. rwops = SDL_RWFromFile(FBASENAME2, "rb"); /* this file doesn't exist that call must fail */
  78. if (rwops)
  79. RWOP_ERR_QUIT(rwops);
  80. rwops = SDL_RWFromFile(FBASENAME2, "rb+"); /* this file doesn't exist that call must fail */
  81. if (rwops)
  82. RWOP_ERR_QUIT(rwops);
  83. rwops = SDL_RWFromFile(FBASENAME2, "wb");
  84. if (!rwops)
  85. RWOP_ERR_QUIT(rwops);
  86. rwops->close(rwops);
  87. unlink(FBASENAME2);
  88. rwops = SDL_RWFromFile(FBASENAME2, "wb+");
  89. if (!rwops)
  90. RWOP_ERR_QUIT(rwops);
  91. rwops->close(rwops);
  92. unlink(FBASENAME2);
  93. rwops = SDL_RWFromFile(FBASENAME2, "ab");
  94. if (!rwops)
  95. RWOP_ERR_QUIT(rwops);
  96. rwops->close(rwops);
  97. unlink(FBASENAME2);
  98. rwops = SDL_RWFromFile(FBASENAME2, "ab+");
  99. if (!rwops)
  100. RWOP_ERR_QUIT(rwops);
  101. rwops->close(rwops);
  102. unlink(FBASENAME2);
  103. SDL_Log("test2 OK\n");
  104. /* test 3 : creation, writing , reading, seeking,
  105. test : w mode, r mode, w+ mode
  106. */
  107. rwops = SDL_RWFromFile(FBASENAME1, "wb"); /* write only */
  108. if (!rwops)
  109. RWOP_ERR_QUIT(rwops);
  110. if (1 != rwops->write(rwops, "1234567890", 10, 1))
  111. RWOP_ERR_QUIT(rwops);
  112. if (10 != rwops->write(rwops, "1234567890", 1, 10))
  113. RWOP_ERR_QUIT(rwops);
  114. if (7 != rwops->write(rwops, "1234567", 1, 7))
  115. RWOP_ERR_QUIT(rwops);
  116. if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
  117. RWOP_ERR_QUIT(rwops);
  118. if (0 != rwops->read(rwops, test_buf, 1, 1))
  119. RWOP_ERR_QUIT(rwops); /* we are in write only mode */
  120. rwops->close(rwops);
  121. rwops = SDL_RWFromFile(FBASENAME1, "rb"); /* read mode, file must exists */
  122. if (!rwops)
  123. RWOP_ERR_QUIT(rwops);
  124. if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
  125. RWOP_ERR_QUIT(rwops);
  126. if (20 != rwops->seek(rwops, -7, RW_SEEK_END))
  127. RWOP_ERR_QUIT(rwops);
  128. if (7 != rwops->read(rwops, test_buf, 1, 7))
  129. RWOP_ERR_QUIT(rwops);
  130. if (SDL_memcmp(test_buf, "1234567", 7))
  131. RWOP_ERR_QUIT(rwops);
  132. if (0 != rwops->read(rwops, test_buf, 1, 1))
  133. RWOP_ERR_QUIT(rwops);
  134. if (0 != rwops->read(rwops, test_buf, 10, 100))
  135. RWOP_ERR_QUIT(rwops);
  136. if (0 != rwops->seek(rwops, -27, RW_SEEK_CUR))
  137. RWOP_ERR_QUIT(rwops);
  138. if (2 != rwops->read(rwops, test_buf, 10, 3))
  139. RWOP_ERR_QUIT(rwops);
  140. if (SDL_memcmp(test_buf, "12345678901234567890", 20))
  141. RWOP_ERR_QUIT(rwops);
  142. if (0 != rwops->write(rwops, test_buf, 1, 1))
  143. RWOP_ERR_QUIT(rwops); /* readonly mode */
  144. rwops->close(rwops);
  145. /* test 3: same with w+ mode */
  146. rwops = SDL_RWFromFile(FBASENAME1, "wb+"); /* write + read + truncation */
  147. if (!rwops)
  148. RWOP_ERR_QUIT(rwops);
  149. if (1 != rwops->write(rwops, "1234567890", 10, 1))
  150. RWOP_ERR_QUIT(rwops);
  151. if (10 != rwops->write(rwops, "1234567890", 1, 10))
  152. RWOP_ERR_QUIT(rwops);
  153. if (7 != rwops->write(rwops, "1234567", 1, 7))
  154. RWOP_ERR_QUIT(rwops);
  155. if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
  156. RWOP_ERR_QUIT(rwops);
  157. if (1 != rwops->read(rwops, test_buf, 1, 1))
  158. RWOP_ERR_QUIT(rwops); /* we are in read/write mode */
  159. if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
  160. RWOP_ERR_QUIT(rwops);
  161. if (20 != rwops->seek(rwops, -7, RW_SEEK_END))
  162. RWOP_ERR_QUIT(rwops);
  163. if (7 != rwops->read(rwops, test_buf, 1, 7))
  164. RWOP_ERR_QUIT(rwops);
  165. if (SDL_memcmp(test_buf, "1234567", 7))
  166. RWOP_ERR_QUIT(rwops);
  167. if (0 != rwops->read(rwops, test_buf, 1, 1))
  168. RWOP_ERR_QUIT(rwops);
  169. if (0 != rwops->read(rwops, test_buf, 10, 100))
  170. RWOP_ERR_QUIT(rwops);
  171. if (0 != rwops->seek(rwops, -27, RW_SEEK_CUR))
  172. RWOP_ERR_QUIT(rwops);
  173. if (2 != rwops->read(rwops, test_buf, 10, 3))
  174. RWOP_ERR_QUIT(rwops);
  175. if (SDL_memcmp(test_buf, "12345678901234567890", 20))
  176. RWOP_ERR_QUIT(rwops);
  177. rwops->close(rwops);
  178. SDL_Log("test3 OK\n");
  179. /* test 4: same in r+ mode */
  180. rwops = SDL_RWFromFile(FBASENAME1, "rb+"); /* write + read + file must exists, no truncation */
  181. if (!rwops)
  182. RWOP_ERR_QUIT(rwops);
  183. if (1 != rwops->write(rwops, "1234567890", 10, 1))
  184. RWOP_ERR_QUIT(rwops);
  185. if (10 != rwops->write(rwops, "1234567890", 1, 10))
  186. RWOP_ERR_QUIT(rwops);
  187. if (7 != rwops->write(rwops, "1234567", 1, 7))
  188. RWOP_ERR_QUIT(rwops);
  189. if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
  190. RWOP_ERR_QUIT(rwops);
  191. if (1 != rwops->read(rwops, test_buf, 1, 1))
  192. RWOP_ERR_QUIT(rwops); /* we are in read/write mode */
  193. if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
  194. RWOP_ERR_QUIT(rwops);
  195. if (20 != rwops->seek(rwops, -7, RW_SEEK_END))
  196. RWOP_ERR_QUIT(rwops);
  197. if (7 != rwops->read(rwops, test_buf, 1, 7))
  198. RWOP_ERR_QUIT(rwops);
  199. if (SDL_memcmp(test_buf, "1234567", 7))
  200. RWOP_ERR_QUIT(rwops);
  201. if (0 != rwops->read(rwops, test_buf, 1, 1))
  202. RWOP_ERR_QUIT(rwops);
  203. if (0 != rwops->read(rwops, test_buf, 10, 100))
  204. RWOP_ERR_QUIT(rwops);
  205. if (0 != rwops->seek(rwops, -27, RW_SEEK_CUR))
  206. RWOP_ERR_QUIT(rwops);
  207. if (2 != rwops->read(rwops, test_buf, 10, 3))
  208. RWOP_ERR_QUIT(rwops);
  209. if (SDL_memcmp(test_buf, "12345678901234567890", 20))
  210. RWOP_ERR_QUIT(rwops);
  211. rwops->close(rwops);
  212. SDL_Log("test4 OK\n");
  213. /* test5 : append mode */
  214. rwops = SDL_RWFromFile(FBASENAME1, "ab+"); /* write + read + append */
  215. if (!rwops)
  216. RWOP_ERR_QUIT(rwops);
  217. if (1 != rwops->write(rwops, "1234567890", 10, 1))
  218. RWOP_ERR_QUIT(rwops);
  219. if (10 != rwops->write(rwops, "1234567890", 1, 10))
  220. RWOP_ERR_QUIT(rwops);
  221. if (7 != rwops->write(rwops, "1234567", 1, 7))
  222. RWOP_ERR_QUIT(rwops);
  223. if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
  224. RWOP_ERR_QUIT(rwops);
  225. if (1 != rwops->read(rwops, test_buf, 1, 1))
  226. RWOP_ERR_QUIT(rwops);
  227. if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
  228. RWOP_ERR_QUIT(rwops);
  229. if (20 + 27 != rwops->seek(rwops, -7, RW_SEEK_END))
  230. RWOP_ERR_QUIT(rwops);
  231. if (7 != rwops->read(rwops, test_buf, 1, 7))
  232. RWOP_ERR_QUIT(rwops);
  233. if (SDL_memcmp(test_buf, "1234567", 7))
  234. RWOP_ERR_QUIT(rwops);
  235. if (0 != rwops->read(rwops, test_buf, 1, 1))
  236. RWOP_ERR_QUIT(rwops);
  237. if (0 != rwops->read(rwops, test_buf, 10, 100))
  238. RWOP_ERR_QUIT(rwops);
  239. if (27 != rwops->seek(rwops, -27, RW_SEEK_CUR))
  240. RWOP_ERR_QUIT(rwops);
  241. if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
  242. RWOP_ERR_QUIT(rwops);
  243. if (3 != rwops->read(rwops, test_buf, 10, 3))
  244. RWOP_ERR_QUIT(rwops);
  245. if (SDL_memcmp(test_buf, "123456789012345678901234567123", 30))
  246. RWOP_ERR_QUIT(rwops);
  247. rwops->close(rwops);
  248. SDL_Log("test5 OK\n");
  249. cleanup();
  250. return 0; /* all ok */
  251. }