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