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

357 lines
10 KiB

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