testfile.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. Copyright (C) 1997-2024 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_iostream.c (useful for alternative implementations of stdio iostream) */
  11. /* quiet windows compiler warnings */
  12. #if defined(_MSC_VER) && !defined(_CRT_NONSTDC_NO_WARNINGS)
  13. #define _CRT_NONSTDC_NO_WARNINGS
  14. #endif
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #ifndef _MSC_VER
  18. #include <unistd.h>
  19. #endif
  20. #include <SDL3/SDL.h>
  21. #include <SDL3/SDL_main.h>
  22. #include <SDL3/SDL_test.h>
  23. /* WARNING ! those 2 files will be destroyed by this test program */
  24. #ifdef SDL_PLATFORM_IOS
  25. #define FBASENAME1 "../Documents/sdldata1" /* this file will be created during tests */
  26. #define FBASENAME2 "../Documents/sdldata2" /* this file should not exist before starting test */
  27. #else
  28. #define FBASENAME1 "sdldata1" /* this file will be created during tests */
  29. #define FBASENAME2 "sdldata2" /* this file should not exist before starting test */
  30. #endif
  31. #ifndef NULL
  32. #define NULL ((void *)0)
  33. #endif
  34. static SDLTest_CommonState *state;
  35. static void
  36. cleanup(void)
  37. {
  38. unlink(FBASENAME1);
  39. unlink(FBASENAME2);
  40. }
  41. static void
  42. iostrm_error_quit(unsigned line, SDL_IOStream *iostrm)
  43. {
  44. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "testfile.c(%d): failed\n", line);
  45. if (iostrm) {
  46. SDL_CloseIO(iostrm);
  47. }
  48. cleanup();
  49. SDL_Quit();
  50. SDLTest_CommonDestroyState(state);
  51. exit(1); /* quit with iostrm error (test failed) */
  52. }
  53. #define RWOP_ERR_QUIT(x) iostrm_error_quit(__LINE__, (x))
  54. int main(int argc, char *argv[])
  55. {
  56. SDL_IOStream *iostrm = NULL;
  57. char test_buf[30];
  58. /* Initialize test framework */
  59. state = SDLTest_CommonCreateState(argv, 0);
  60. if (!state) {
  61. return 1;
  62. }
  63. /* Parse commandline */
  64. if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
  65. return 1;
  66. }
  67. cleanup();
  68. /* test 1 : basic argument test: all those calls to SDL_IOFromFile should fail */
  69. iostrm = SDL_IOFromFile(NULL, NULL);
  70. if (iostrm) {
  71. RWOP_ERR_QUIT(iostrm);
  72. }
  73. iostrm = SDL_IOFromFile(NULL, "ab+");
  74. if (iostrm) {
  75. RWOP_ERR_QUIT(iostrm);
  76. }
  77. iostrm = SDL_IOFromFile(NULL, "sldfkjsldkfj");
  78. if (iostrm) {
  79. RWOP_ERR_QUIT(iostrm);
  80. }
  81. iostrm = SDL_IOFromFile("something", "");
  82. if (iostrm) {
  83. RWOP_ERR_QUIT(iostrm);
  84. }
  85. iostrm = SDL_IOFromFile("something", NULL);
  86. if (iostrm) {
  87. RWOP_ERR_QUIT(iostrm);
  88. }
  89. SDL_Log("test1 OK\n");
  90. /* test 2 : check that inexistent file is not successfully opened/created when required */
  91. /* modes : r, r+ imply that file MUST exist
  92. modes : a, a+, w, w+ checks that it succeeds (file may not exists)
  93. */
  94. iostrm = SDL_IOFromFile(FBASENAME2, "rb"); /* this file doesn't exist that call must fail */
  95. if (iostrm) {
  96. RWOP_ERR_QUIT(iostrm);
  97. }
  98. iostrm = SDL_IOFromFile(FBASENAME2, "rb+"); /* this file doesn't exist that call must fail */
  99. if (iostrm) {
  100. RWOP_ERR_QUIT(iostrm);
  101. }
  102. iostrm = SDL_IOFromFile(FBASENAME2, "wb");
  103. if (!iostrm) {
  104. RWOP_ERR_QUIT(iostrm);
  105. }
  106. SDL_CloseIO(iostrm);
  107. unlink(FBASENAME2);
  108. iostrm = SDL_IOFromFile(FBASENAME2, "wb+");
  109. if (!iostrm) {
  110. RWOP_ERR_QUIT(iostrm);
  111. }
  112. SDL_CloseIO(iostrm);
  113. unlink(FBASENAME2);
  114. iostrm = SDL_IOFromFile(FBASENAME2, "ab");
  115. if (!iostrm) {
  116. RWOP_ERR_QUIT(iostrm);
  117. }
  118. SDL_CloseIO(iostrm);
  119. unlink(FBASENAME2);
  120. iostrm = SDL_IOFromFile(FBASENAME2, "ab+");
  121. if (!iostrm) {
  122. RWOP_ERR_QUIT(iostrm);
  123. }
  124. SDL_CloseIO(iostrm);
  125. unlink(FBASENAME2);
  126. SDL_Log("test2 OK\n");
  127. /* test 3 : creation, writing , reading, seeking,
  128. test : w mode, r mode, w+ mode
  129. */
  130. iostrm = SDL_IOFromFile(FBASENAME1, "wb"); /* write only */
  131. if (!iostrm) {
  132. RWOP_ERR_QUIT(iostrm);
  133. }
  134. if (10 != SDL_WriteIO(iostrm, "1234567890", 10)) {
  135. RWOP_ERR_QUIT(iostrm);
  136. }
  137. if (10 != SDL_WriteIO(iostrm, "1234567890", 10)) {
  138. RWOP_ERR_QUIT(iostrm);
  139. }
  140. if (7 != SDL_WriteIO(iostrm, "1234567", 7)) {
  141. RWOP_ERR_QUIT(iostrm);
  142. }
  143. if (0 != SDL_SeekIO(iostrm, 0L, SDL_IO_SEEK_SET)) {
  144. RWOP_ERR_QUIT(iostrm);
  145. }
  146. if (0 != SDL_ReadIO(iostrm, test_buf, 1)) {
  147. RWOP_ERR_QUIT(iostrm); /* we are in write only mode */
  148. }
  149. SDL_CloseIO(iostrm);
  150. iostrm = SDL_IOFromFile(FBASENAME1, "rb"); /* read mode, file must exist */
  151. if (!iostrm) {
  152. RWOP_ERR_QUIT(iostrm);
  153. }
  154. if (0 != SDL_SeekIO(iostrm, 0L, SDL_IO_SEEK_SET)) {
  155. RWOP_ERR_QUIT(iostrm);
  156. }
  157. if (20 != SDL_SeekIO(iostrm, -7, SDL_IO_SEEK_END)) {
  158. RWOP_ERR_QUIT(iostrm);
  159. }
  160. if (7 != SDL_ReadIO(iostrm, test_buf, 7)) {
  161. RWOP_ERR_QUIT(iostrm);
  162. }
  163. if (SDL_memcmp(test_buf, "1234567", 7) != 0) {
  164. RWOP_ERR_QUIT(iostrm);
  165. }
  166. if (0 != SDL_ReadIO(iostrm, test_buf, 1)) {
  167. RWOP_ERR_QUIT(iostrm);
  168. }
  169. if (0 != SDL_ReadIO(iostrm, test_buf, 1000)) {
  170. RWOP_ERR_QUIT(iostrm);
  171. }
  172. if (0 != SDL_SeekIO(iostrm, -27, SDL_IO_SEEK_CUR)) {
  173. RWOP_ERR_QUIT(iostrm);
  174. }
  175. if (27 != SDL_ReadIO(iostrm, test_buf, 30)) {
  176. RWOP_ERR_QUIT(iostrm);
  177. }
  178. if (SDL_memcmp(test_buf, "12345678901234567890", 20) != 0) {
  179. RWOP_ERR_QUIT(iostrm);
  180. }
  181. if (0 != SDL_WriteIO(iostrm, test_buf, 1)) {
  182. RWOP_ERR_QUIT(iostrm); /* readonly mode */
  183. }
  184. SDL_CloseIO(iostrm);
  185. /* test 3: same with w+ mode */
  186. iostrm = SDL_IOFromFile(FBASENAME1, "wb+"); /* write + read + truncation */
  187. if (!iostrm) {
  188. RWOP_ERR_QUIT(iostrm);
  189. }
  190. if (10 != SDL_WriteIO(iostrm, "1234567890", 10)) {
  191. RWOP_ERR_QUIT(iostrm);
  192. }
  193. if (10 != SDL_WriteIO(iostrm, "1234567890", 10)) {
  194. RWOP_ERR_QUIT(iostrm);
  195. }
  196. if (7 != SDL_WriteIO(iostrm, "1234567", 7)) {
  197. RWOP_ERR_QUIT(iostrm);
  198. }
  199. if (0 != SDL_SeekIO(iostrm, 0L, SDL_IO_SEEK_SET)) {
  200. RWOP_ERR_QUIT(iostrm);
  201. }
  202. if (1 != SDL_ReadIO(iostrm, test_buf, 1)) {
  203. RWOP_ERR_QUIT(iostrm); /* we are in read/write mode */
  204. }
  205. if (0 != SDL_SeekIO(iostrm, 0L, SDL_IO_SEEK_SET)) {
  206. RWOP_ERR_QUIT(iostrm);
  207. }
  208. if (20 != SDL_SeekIO(iostrm, -7, SDL_IO_SEEK_END)) {
  209. RWOP_ERR_QUIT(iostrm);
  210. }
  211. if (7 != SDL_ReadIO(iostrm, test_buf, 7)) {
  212. RWOP_ERR_QUIT(iostrm);
  213. }
  214. if (SDL_memcmp(test_buf, "1234567", 7) != 0) {
  215. RWOP_ERR_QUIT(iostrm);
  216. }
  217. if (0 != SDL_ReadIO(iostrm, test_buf, 1)) {
  218. RWOP_ERR_QUIT(iostrm);
  219. }
  220. if (0 != SDL_ReadIO(iostrm, test_buf, 1000)) {
  221. RWOP_ERR_QUIT(iostrm);
  222. }
  223. if (0 != SDL_SeekIO(iostrm, -27, SDL_IO_SEEK_CUR)) {
  224. RWOP_ERR_QUIT(iostrm);
  225. }
  226. if (27 != SDL_ReadIO(iostrm, test_buf, 30)) {
  227. RWOP_ERR_QUIT(iostrm);
  228. }
  229. if (SDL_memcmp(test_buf, "12345678901234567890", 20) != 0) {
  230. RWOP_ERR_QUIT(iostrm);
  231. }
  232. SDL_CloseIO(iostrm);
  233. SDL_Log("test3 OK\n");
  234. /* test 4: same in r+ mode */
  235. iostrm = SDL_IOFromFile(FBASENAME1, "rb+"); /* write + read + file must exists, no truncation */
  236. if (!iostrm) {
  237. RWOP_ERR_QUIT(iostrm);
  238. }
  239. if (10 != SDL_WriteIO(iostrm, "1234567890", 10)) {
  240. RWOP_ERR_QUIT(iostrm);
  241. }
  242. if (10 != SDL_WriteIO(iostrm, "1234567890", 10)) {
  243. RWOP_ERR_QUIT(iostrm);
  244. }
  245. if (7 != SDL_WriteIO(iostrm, "1234567", 7)) {
  246. RWOP_ERR_QUIT(iostrm);
  247. }
  248. if (0 != SDL_SeekIO(iostrm, 0L, SDL_IO_SEEK_SET)) {
  249. RWOP_ERR_QUIT(iostrm);
  250. }
  251. if (1 != SDL_ReadIO(iostrm, test_buf, 1)) {
  252. RWOP_ERR_QUIT(iostrm); /* we are in read/write mode */
  253. }
  254. if (0 != SDL_SeekIO(iostrm, 0L, SDL_IO_SEEK_SET)) {
  255. RWOP_ERR_QUIT(iostrm);
  256. }
  257. if (20 != SDL_SeekIO(iostrm, -7, SDL_IO_SEEK_END)) {
  258. RWOP_ERR_QUIT(iostrm);
  259. }
  260. if (7 != SDL_ReadIO(iostrm, test_buf, 7)) {
  261. RWOP_ERR_QUIT(iostrm);
  262. }
  263. if (SDL_memcmp(test_buf, "1234567", 7) != 0) {
  264. RWOP_ERR_QUIT(iostrm);
  265. }
  266. if (0 != SDL_ReadIO(iostrm, test_buf, 1)) {
  267. RWOP_ERR_QUIT(iostrm);
  268. }
  269. if (0 != SDL_ReadIO(iostrm, test_buf, 1000)) {
  270. RWOP_ERR_QUIT(iostrm);
  271. }
  272. if (0 != SDL_SeekIO(iostrm, -27, SDL_IO_SEEK_CUR)) {
  273. RWOP_ERR_QUIT(iostrm);
  274. }
  275. if (27 != SDL_ReadIO(iostrm, test_buf, 30)) {
  276. RWOP_ERR_QUIT(iostrm);
  277. }
  278. if (SDL_memcmp(test_buf, "12345678901234567890", 20) != 0) {
  279. RWOP_ERR_QUIT(iostrm);
  280. }
  281. SDL_CloseIO(iostrm);
  282. SDL_Log("test4 OK\n");
  283. /* test5 : append mode */
  284. iostrm = SDL_IOFromFile(FBASENAME1, "ab+"); /* write + read + append */
  285. if (!iostrm) {
  286. RWOP_ERR_QUIT(iostrm);
  287. }
  288. if (10 != SDL_WriteIO(iostrm, "1234567890", 10)) {
  289. RWOP_ERR_QUIT(iostrm);
  290. }
  291. if (10 != SDL_WriteIO(iostrm, "1234567890", 10)) {
  292. RWOP_ERR_QUIT(iostrm);
  293. }
  294. if (7 != SDL_WriteIO(iostrm, "1234567", 7)) {
  295. RWOP_ERR_QUIT(iostrm);
  296. }
  297. if (0 != SDL_SeekIO(iostrm, 0L, SDL_IO_SEEK_SET)) {
  298. RWOP_ERR_QUIT(iostrm);
  299. }
  300. if (1 != SDL_ReadIO(iostrm, test_buf, 1)) {
  301. RWOP_ERR_QUIT(iostrm);
  302. }
  303. if (0 != SDL_SeekIO(iostrm, 0L, SDL_IO_SEEK_SET)) {
  304. RWOP_ERR_QUIT(iostrm);
  305. }
  306. if (20 + 27 != SDL_SeekIO(iostrm, -7, SDL_IO_SEEK_END)) {
  307. RWOP_ERR_QUIT(iostrm);
  308. }
  309. if (7 != SDL_ReadIO(iostrm, test_buf, 7)) {
  310. RWOP_ERR_QUIT(iostrm);
  311. }
  312. if (SDL_memcmp(test_buf, "1234567", 7) != 0) {
  313. RWOP_ERR_QUIT(iostrm);
  314. }
  315. if (0 != SDL_ReadIO(iostrm, test_buf, 1)) {
  316. RWOP_ERR_QUIT(iostrm);
  317. }
  318. if (0 != SDL_ReadIO(iostrm, test_buf, 1000)) {
  319. RWOP_ERR_QUIT(iostrm);
  320. }
  321. if (27 != SDL_SeekIO(iostrm, -27, SDL_IO_SEEK_CUR)) {
  322. RWOP_ERR_QUIT(iostrm);
  323. }
  324. if (0 != SDL_SeekIO(iostrm, 0L, SDL_IO_SEEK_SET)) {
  325. RWOP_ERR_QUIT(iostrm);
  326. }
  327. if (30 != SDL_ReadIO(iostrm, test_buf, 30)) {
  328. RWOP_ERR_QUIT(iostrm);
  329. }
  330. if (SDL_memcmp(test_buf, "123456789012345678901234567123", 30) != 0) {
  331. RWOP_ERR_QUIT(iostrm);
  332. }
  333. SDL_CloseIO(iostrm);
  334. SDL_Log("test5 OK\n");
  335. cleanup();
  336. SDL_Quit();
  337. SDLTest_CommonDestroyState(state);
  338. return 0; /* all ok */
  339. }