Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
283 lines (249 loc) · 9.11 KB

testfile.c

File metadata and controls

283 lines (249 loc) · 9.11 KB
 
Apr 8, 2011
Apr 8, 2011
1
2
3
4
5
6
7
8
9
10
11
/*
Copyright (C) 1997-2011 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.
*/
12
13
14
/* sanity tests on SDL_rwops.c (usefull for alternative implementations of stdio rwops) */
Jan 21, 2011
Jan 21, 2011
15
// quiet windows compiler warnings
Sep 16, 2010
Sep 16, 2010
16
17
#define _CRT_NONSTDC_NO_WARNINGS
Mar 8, 2006
Mar 8, 2006
18
#include <stdlib.h>
Mar 30, 2007
Mar 30, 2007
19
20
#ifndef _MSC_VER
May 17, 2006
May 17, 2006
21
#include <unistd.h>
Mar 30, 2007
Mar 30, 2007
22
#endif
23
24
25
26
27
#include "SDL.h"
#include "SDL_endian.h"
Jul 10, 2006
Jul 10, 2006
28
#include <stdio.h>
29
30
31
/* WARNING ! those 2 files will be destroyed by this test program */
Oct 4, 2008
Oct 4, 2008
32
33
34
35
36
37
38
#ifdef __IPHONEOS__
#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
39
40
41
#ifndef NULL
#define NULL ((void *)0)
Jul 10, 2006
Jul 10, 2006
42
#endif
43
Jul 10, 2006
Jul 10, 2006
44
45
46
static void
cleanup(void)
{
47
Jul 10, 2006
Jul 10, 2006
48
49
unlink(FBASENAME1);
unlink(FBASENAME2);
50
51
}
Jul 10, 2006
Jul 10, 2006
52
53
54
55
56
57
58
59
60
61
static void
rwops_error_quit(unsigned line, SDL_RWops * rwops)
{
printf("testfile.c(%d): failed\n", line);
if (rwops) {
rwops->close(rwops); /* This calls SDL_FreeRW(rwops); */
}
cleanup();
exit(1); /* quit with rwops error (test failed) */
62
63
64
65
66
67
}
#define RWOP_ERR_QUIT(x) rwops_error_quit( __LINE__, (x) )
Jul 10, 2006
Jul 10, 2006
68
69
int
main(int argc, char *argv[])
70
{
Jul 10, 2006
Jul 10, 2006
71
72
73
74
SDL_RWops *rwops = NULL;
char test_buf[30];
cleanup();
75
76
/* test 1 : basic argument test: all those calls to SDL_RWFromFile should fail */
Jul 10, 2006
Jul 10, 2006
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
rwops = SDL_RWFromFile(NULL, NULL);
if (rwops)
RWOP_ERR_QUIT(rwops);
rwops = SDL_RWFromFile(NULL, "ab+");
if (rwops)
RWOP_ERR_QUIT(rwops);
rwops = SDL_RWFromFile(NULL, "sldfkjsldkfj");
if (rwops)
RWOP_ERR_QUIT(rwops);
rwops = SDL_RWFromFile("something", "");
if (rwops)
RWOP_ERR_QUIT(rwops);
rwops = SDL_RWFromFile("something", NULL);
if (rwops)
RWOP_ERR_QUIT(rwops);
printf("test1 OK\n");
94
95
96
97
98
99
/* test 2 : check that inexistant file is not successfully opened/created when required */
/* modes : r, r+ implie that file MUST exist
modes : a, a+, w, w+ checks that it succeeds (file may not exists)
*/
Jul 10, 2006
Jul 10, 2006
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
rwops = SDL_RWFromFile(FBASENAME2, "rb"); /* this file doesn't exist that call must fail */
if (rwops)
RWOP_ERR_QUIT(rwops);
rwops = SDL_RWFromFile(FBASENAME2, "rb+"); /* this file doesn't exist that call must fail */
if (rwops)
RWOP_ERR_QUIT(rwops);
rwops = SDL_RWFromFile(FBASENAME2, "wb");
if (!rwops)
RWOP_ERR_QUIT(rwops);
rwops->close(rwops);
unlink(FBASENAME2);
rwops = SDL_RWFromFile(FBASENAME2, "wb+");
if (!rwops)
RWOP_ERR_QUIT(rwops);
rwops->close(rwops);
unlink(FBASENAME2);
rwops = SDL_RWFromFile(FBASENAME2, "ab");
if (!rwops)
RWOP_ERR_QUIT(rwops);
rwops->close(rwops);
unlink(FBASENAME2);
rwops = SDL_RWFromFile(FBASENAME2, "ab+");
if (!rwops)
RWOP_ERR_QUIT(rwops);
rwops->close(rwops);
unlink(FBASENAME2);
printf("test2 OK\n");
127
128
129
130
/* test 3 : creation, writing , reading, seeking,
test : w mode, r mode, w+ mode
*/
Jul 10, 2006
Jul 10, 2006
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
rwops = SDL_RWFromFile(FBASENAME1, "wb"); /* write only */
if (!rwops)
RWOP_ERR_QUIT(rwops);
if (1 != rwops->write(rwops, "1234567890", 10, 1))
RWOP_ERR_QUIT(rwops);
if (10 != rwops->write(rwops, "1234567890", 1, 10))
RWOP_ERR_QUIT(rwops);
if (7 != rwops->write(rwops, "1234567", 1, 7))
RWOP_ERR_QUIT(rwops);
if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
RWOP_ERR_QUIT(rwops);
if (0 != rwops->read(rwops, test_buf, 1, 1))
RWOP_ERR_QUIT(rwops); /* we are in write only mode */
rwops->close(rwops);
rwops = SDL_RWFromFile(FBASENAME1, "rb"); /* read mode, file must exists */
if (!rwops)
RWOP_ERR_QUIT(rwops);
if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
RWOP_ERR_QUIT(rwops);
if (20 != rwops->seek(rwops, -7, RW_SEEK_END))
RWOP_ERR_QUIT(rwops);
if (7 != rwops->read(rwops, test_buf, 1, 7))
RWOP_ERR_QUIT(rwops);
if (SDL_memcmp(test_buf, "1234567", 7))
RWOP_ERR_QUIT(rwops);
if (0 != rwops->read(rwops, test_buf, 1, 1))
RWOP_ERR_QUIT(rwops);
if (0 != rwops->read(rwops, test_buf, 10, 100))
RWOP_ERR_QUIT(rwops);
if (0 != rwops->seek(rwops, -27, RW_SEEK_CUR))
RWOP_ERR_QUIT(rwops);
if (2 != rwops->read(rwops, test_buf, 10, 3))
RWOP_ERR_QUIT(rwops);
if (SDL_memcmp(test_buf, "12345678901234567890", 20))
RWOP_ERR_QUIT(rwops);
if (0 != rwops->write(rwops, test_buf, 1, 1))
RWOP_ERR_QUIT(rwops); /* readonly mode */
rwops->close(rwops);
170
171
/* test 3: same with w+ mode */
Jul 10, 2006
Jul 10, 2006
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
rwops = SDL_RWFromFile(FBASENAME1, "wb+"); /* write + read + truncation */
if (!rwops)
RWOP_ERR_QUIT(rwops);
if (1 != rwops->write(rwops, "1234567890", 10, 1))
RWOP_ERR_QUIT(rwops);
if (10 != rwops->write(rwops, "1234567890", 1, 10))
RWOP_ERR_QUIT(rwops);
if (7 != rwops->write(rwops, "1234567", 1, 7))
RWOP_ERR_QUIT(rwops);
if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
RWOP_ERR_QUIT(rwops);
if (1 != rwops->read(rwops, test_buf, 1, 1))
RWOP_ERR_QUIT(rwops); /* we are in read/write mode */
if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
RWOP_ERR_QUIT(rwops);
if (20 != rwops->seek(rwops, -7, RW_SEEK_END))
RWOP_ERR_QUIT(rwops);
if (7 != rwops->read(rwops, test_buf, 1, 7))
RWOP_ERR_QUIT(rwops);
if (SDL_memcmp(test_buf, "1234567", 7))
RWOP_ERR_QUIT(rwops);
if (0 != rwops->read(rwops, test_buf, 1, 1))
RWOP_ERR_QUIT(rwops);
if (0 != rwops->read(rwops, test_buf, 10, 100))
RWOP_ERR_QUIT(rwops);
if (0 != rwops->seek(rwops, -27, RW_SEEK_CUR))
RWOP_ERR_QUIT(rwops);
if (2 != rwops->read(rwops, test_buf, 10, 3))
RWOP_ERR_QUIT(rwops);
if (SDL_memcmp(test_buf, "12345678901234567890", 20))
RWOP_ERR_QUIT(rwops);
rwops->close(rwops);
printf("test3 OK\n");
205
206
/* test 4: same in r+ mode */
Jul 10, 2006
Jul 10, 2006
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
rwops = SDL_RWFromFile(FBASENAME1, "rb+"); /* write + read + file must exists, no truncation */
if (!rwops)
RWOP_ERR_QUIT(rwops);
if (1 != rwops->write(rwops, "1234567890", 10, 1))
RWOP_ERR_QUIT(rwops);
if (10 != rwops->write(rwops, "1234567890", 1, 10))
RWOP_ERR_QUIT(rwops);
if (7 != rwops->write(rwops, "1234567", 1, 7))
RWOP_ERR_QUIT(rwops);
if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
RWOP_ERR_QUIT(rwops);
if (1 != rwops->read(rwops, test_buf, 1, 1))
RWOP_ERR_QUIT(rwops); /* we are in read/write mode */
if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
RWOP_ERR_QUIT(rwops);
if (20 != rwops->seek(rwops, -7, RW_SEEK_END))
RWOP_ERR_QUIT(rwops);
if (7 != rwops->read(rwops, test_buf, 1, 7))
RWOP_ERR_QUIT(rwops);
if (SDL_memcmp(test_buf, "1234567", 7))
RWOP_ERR_QUIT(rwops);
if (0 != rwops->read(rwops, test_buf, 1, 1))
RWOP_ERR_QUIT(rwops);
if (0 != rwops->read(rwops, test_buf, 10, 100))
RWOP_ERR_QUIT(rwops);
if (0 != rwops->seek(rwops, -27, RW_SEEK_CUR))
RWOP_ERR_QUIT(rwops);
if (2 != rwops->read(rwops, test_buf, 10, 3))
RWOP_ERR_QUIT(rwops);
if (SDL_memcmp(test_buf, "12345678901234567890", 20))
RWOP_ERR_QUIT(rwops);
rwops->close(rwops);
printf("test4 OK\n");
240
241
/* test5 : append mode */
Jul 10, 2006
Jul 10, 2006
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
rwops = SDL_RWFromFile(FBASENAME1, "ab+"); /* write + read + append */
if (!rwops)
RWOP_ERR_QUIT(rwops);
if (1 != rwops->write(rwops, "1234567890", 10, 1))
RWOP_ERR_QUIT(rwops);
if (10 != rwops->write(rwops, "1234567890", 1, 10))
RWOP_ERR_QUIT(rwops);
if (7 != rwops->write(rwops, "1234567", 1, 7))
RWOP_ERR_QUIT(rwops);
if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
RWOP_ERR_QUIT(rwops);
if (1 != rwops->read(rwops, test_buf, 1, 1))
RWOP_ERR_QUIT(rwops);
if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
RWOP_ERR_QUIT(rwops);
if (20 + 27 != rwops->seek(rwops, -7, RW_SEEK_END))
RWOP_ERR_QUIT(rwops);
if (7 != rwops->read(rwops, test_buf, 1, 7))
RWOP_ERR_QUIT(rwops);
if (SDL_memcmp(test_buf, "1234567", 7))
RWOP_ERR_QUIT(rwops);
if (0 != rwops->read(rwops, test_buf, 1, 1))
RWOP_ERR_QUIT(rwops);
if (0 != rwops->read(rwops, test_buf, 10, 100))
RWOP_ERR_QUIT(rwops);
if (27 != rwops->seek(rwops, -27, RW_SEEK_CUR))
RWOP_ERR_QUIT(rwops);
if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
RWOP_ERR_QUIT(rwops);
if (3 != rwops->read(rwops, test_buf, 10, 3))
RWOP_ERR_QUIT(rwops);
if (SDL_memcmp(test_buf, "123456789012345678901234567123", 30))
RWOP_ERR_QUIT(rwops);
rwops->close(rwops);
printf("test5 OK\n");
cleanup();
return 0; /* all ok */
283
}