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