Atlas - testfile.c
Home / ext / SDL / test Lines: 1 | Size: 10765 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1/* 2 Copyright (C) 1997-2026 Sam Lantinga <[email protected]> 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 44static SDLTest_CommonState *state; 45 46static void 47cleanup(void) 48{ 49 unlink(FBASENAME1); 50 unlink(FBASENAME2); 51} 52 53static SDL_NORETURN void 54iostrm_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 68int 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#ifdef __DJGPP__ 170 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "DJGPP allowed read on write-only file"); 171#else 172 RWOP_ERR_QUIT(iostrm); /* we are in write only mode */ 173#endif 174 } 175 176 SDL_CloseIO(iostrm); 177 178 iostrm = SDL_IOFromFile(FBASENAME1, "rb"); /* read mode, file must exist */ 179 if (!iostrm) { 180 RWOP_ERR_QUIT(iostrm); 181 } 182 if (0 != SDL_SeekIO(iostrm, 0L, SDL_IO_SEEK_SET)) { 183 RWOP_ERR_QUIT(iostrm); 184 } 185 if (20 != SDL_SeekIO(iostrm, -7, SDL_IO_SEEK_END)) { 186 RWOP_ERR_QUIT(iostrm); 187 } 188 if (7 != SDL_ReadIO(iostrm, test_buf, 7)) { 189 RWOP_ERR_QUIT(iostrm); 190 } 191 if (SDL_memcmp(test_buf, "1234567", 7) != 0) { 192 RWOP_ERR_QUIT(iostrm); 193 } 194 if (0 != SDL_ReadIO(iostrm, test_buf, 1)) { 195 RWOP_ERR_QUIT(iostrm); 196 } 197 if (0 != SDL_ReadIO(iostrm, test_buf, 1000)) { 198 RWOP_ERR_QUIT(iostrm); 199 } 200 if (0 != SDL_SeekIO(iostrm, -27, SDL_IO_SEEK_CUR)) { 201 RWOP_ERR_QUIT(iostrm); 202 } 203 if (27 != SDL_ReadIO(iostrm, test_buf, 30)) { 204 RWOP_ERR_QUIT(iostrm); 205 } 206 if (SDL_memcmp(test_buf, "12345678901234567890", 20) != 0) { 207 RWOP_ERR_QUIT(iostrm); 208 } 209 if (0 != SDL_WriteIO(iostrm, test_buf, 1)) { 210#ifdef __DJGPP__ 211 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "DJGPP allowed write on read-only file"); 212#else 213 RWOP_ERR_QUIT(iostrm); /* readonly mode */ 214#endif 215 } 216 217 SDL_CloseIO(iostrm); 218 219 /* test 3: same with w+ mode */ 220 iostrm = SDL_IOFromFile(FBASENAME1, "wb+"); /* write + read + truncation */ 221 if (!iostrm) { 222 RWOP_ERR_QUIT(iostrm); 223 } 224 if (10 != SDL_WriteIO(iostrm, "1234567890", 10)) { 225 RWOP_ERR_QUIT(iostrm); 226 } 227 if (10 != SDL_WriteIO(iostrm, "1234567890", 10)) { 228 RWOP_ERR_QUIT(iostrm); 229 } 230 if (7 != SDL_WriteIO(iostrm, "1234567", 7)) { 231 RWOP_ERR_QUIT(iostrm); 232 } 233 if (0 != SDL_SeekIO(iostrm, 0L, SDL_IO_SEEK_SET)) { 234 RWOP_ERR_QUIT(iostrm); 235 } 236 if (1 != SDL_ReadIO(iostrm, test_buf, 1)) { 237 RWOP_ERR_QUIT(iostrm); /* we are in read/write mode */ 238 } 239 240 if (0 != SDL_SeekIO(iostrm, 0L, SDL_IO_SEEK_SET)) { 241 RWOP_ERR_QUIT(iostrm); 242 } 243 if (20 != SDL_SeekIO(iostrm, -7, SDL_IO_SEEK_END)) { 244 RWOP_ERR_QUIT(iostrm); 245 } 246 if (7 != SDL_ReadIO(iostrm, test_buf, 7)) { 247 RWOP_ERR_QUIT(iostrm); 248 } 249 if (SDL_memcmp(test_buf, "1234567", 7) != 0) { 250 RWOP_ERR_QUIT(iostrm); 251 } 252 if (0 != SDL_ReadIO(iostrm, test_buf, 1)) { 253 RWOP_ERR_QUIT(iostrm); 254 } 255 if (0 != SDL_ReadIO(iostrm, test_buf, 1000)) { 256 RWOP_ERR_QUIT(iostrm); 257 } 258 if (0 != SDL_SeekIO(iostrm, -27, SDL_IO_SEEK_CUR)) { 259 RWOP_ERR_QUIT(iostrm); 260 } 261 if (27 != SDL_ReadIO(iostrm, test_buf, 30)) { 262 RWOP_ERR_QUIT(iostrm); 263 } 264 if (SDL_memcmp(test_buf, "12345678901234567890", 20) != 0) { 265 RWOP_ERR_QUIT(iostrm); 266 } 267 SDL_CloseIO(iostrm); 268 SDL_Log("test3 OK"); 269 270 /* test 4: same in r+ mode */ 271 iostrm = SDL_IOFromFile(FBASENAME1, "rb+"); /* write + read + file must exists, no truncation */ 272 if (!iostrm) { 273 RWOP_ERR_QUIT(iostrm); 274 } 275 if (10 != SDL_WriteIO(iostrm, "1234567890", 10)) { 276 RWOP_ERR_QUIT(iostrm); 277 } 278 if (10 != SDL_WriteIO(iostrm, "1234567890", 10)) { 279 RWOP_ERR_QUIT(iostrm); 280 } 281 if (7 != SDL_WriteIO(iostrm, "1234567", 7)) { 282 RWOP_ERR_QUIT(iostrm); 283 } 284 if (0 != SDL_SeekIO(iostrm, 0L, SDL_IO_SEEK_SET)) { 285 RWOP_ERR_QUIT(iostrm); 286 } 287 if (1 != SDL_ReadIO(iostrm, test_buf, 1)) { 288 RWOP_ERR_QUIT(iostrm); /* we are in read/write mode */ 289 } 290 291 if (0 != SDL_SeekIO(iostrm, 0L, SDL_IO_SEEK_SET)) { 292 RWOP_ERR_QUIT(iostrm); 293 } 294 if (20 != SDL_SeekIO(iostrm, -7, SDL_IO_SEEK_END)) { 295 RWOP_ERR_QUIT(iostrm); 296 } 297 if (7 != SDL_ReadIO(iostrm, test_buf, 7)) { 298 RWOP_ERR_QUIT(iostrm); 299 } 300 if (SDL_memcmp(test_buf, "1234567", 7) != 0) { 301 RWOP_ERR_QUIT(iostrm); 302 } 303 if (0 != SDL_ReadIO(iostrm, test_buf, 1)) { 304 RWOP_ERR_QUIT(iostrm); 305 } 306 if (0 != SDL_ReadIO(iostrm, test_buf, 1000)) { 307 RWOP_ERR_QUIT(iostrm); 308 } 309 if (0 != SDL_SeekIO(iostrm, -27, SDL_IO_SEEK_CUR)) { 310 RWOP_ERR_QUIT(iostrm); 311 } 312 if (27 != SDL_ReadIO(iostrm, test_buf, 30)) { 313 RWOP_ERR_QUIT(iostrm); 314 } 315 if (SDL_memcmp(test_buf, "12345678901234567890", 20) != 0) { 316 RWOP_ERR_QUIT(iostrm); 317 } 318 SDL_CloseIO(iostrm); 319 SDL_Log("test4 OK"); 320 321 /* test5 : append mode */ 322 iostrm = SDL_IOFromFile(FBASENAME1, "ab+"); /* write + read + append */ 323 if (!iostrm) { 324 RWOP_ERR_QUIT(iostrm); 325 } 326 if (10 != SDL_WriteIO(iostrm, "1234567890", 10)) { 327 RWOP_ERR_QUIT(iostrm); 328 } 329 if (10 != SDL_WriteIO(iostrm, "1234567890", 10)) { 330 RWOP_ERR_QUIT(iostrm); 331 } 332 if (7 != SDL_WriteIO(iostrm, "1234567", 7)) { 333 RWOP_ERR_QUIT(iostrm); 334 } 335 if (0 != SDL_SeekIO(iostrm, 0L, SDL_IO_SEEK_SET)) { 336 RWOP_ERR_QUIT(iostrm); 337 } 338 339 if (1 != SDL_ReadIO(iostrm, test_buf, 1)) { 340 RWOP_ERR_QUIT(iostrm); 341 } 342 if (0 != SDL_SeekIO(iostrm, 0L, SDL_IO_SEEK_SET)) { 343 RWOP_ERR_QUIT(iostrm); 344 } 345 346 if (20 + 27 != SDL_SeekIO(iostrm, -7, SDL_IO_SEEK_END)) { 347 RWOP_ERR_QUIT(iostrm); 348 } 349 if (7 != SDL_ReadIO(iostrm, test_buf, 7)) { 350 RWOP_ERR_QUIT(iostrm); 351 } 352 if (SDL_memcmp(test_buf, "1234567", 7) != 0) { 353 RWOP_ERR_QUIT(iostrm); 354 } 355 if (0 != SDL_ReadIO(iostrm, test_buf, 1)) { 356 RWOP_ERR_QUIT(iostrm); 357 } 358 if (0 != SDL_ReadIO(iostrm, test_buf, 1000)) { 359 RWOP_ERR_QUIT(iostrm); 360 } 361 362 if (27 != SDL_SeekIO(iostrm, -27, SDL_IO_SEEK_CUR)) { 363 RWOP_ERR_QUIT(iostrm); 364 } 365 366 if (0 != SDL_SeekIO(iostrm, 0L, SDL_IO_SEEK_SET)) { 367 RWOP_ERR_QUIT(iostrm); 368 } 369 if (30 != SDL_ReadIO(iostrm, test_buf, 30)) { 370 RWOP_ERR_QUIT(iostrm); 371 } 372 if (SDL_memcmp(test_buf, "123456789012345678901234567123", 30) != 0) { 373 RWOP_ERR_QUIT(iostrm); 374 } 375 SDL_CloseIO(iostrm); 376 SDL_Log("test5 OK"); 377 cleanup(); 378 SDL_Quit(); 379 SDLTest_CommonDestroyState(state); 380 return 0; /* all ok */ 381} 382[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.