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