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

283 lines
9.3 KiB

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