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

Latest commit

 

History

History
261 lines (231 loc) · 8.41 KB

testfile.c

File metadata and controls

261 lines (231 loc) · 8.41 KB
 
1
2
3
/* sanity tests on SDL_rwops.c (usefull for alternative implementations of stdio rwops) */
Mar 8, 2006
Mar 8, 2006
4
#include <stdlib.h>
May 17, 2006
May 17, 2006
5
#include <unistd.h>
6
7
8
9
10
#include "SDL.h"
#include "SDL_endian.h"
May 28, 2006
May 28, 2006
11
#include <stdio.h>
12
13
/* WARNING ! those 2 files will be destroyed by this test program */
May 28, 2006
May 28, 2006
14
15
#define FBASENAME1 "sdldata1" /* this file will be created during tests */
#define FBASENAME2 "sdldata2" /* this file should not exists before starting test */
16
17
18
19
#ifndef NULL
#define NULL ((void *)0)
May 28, 2006
May 28, 2006
20
#endif
21
May 28, 2006
May 28, 2006
22
static void
May 29, 2006
May 29, 2006
23
cleanup(void)
May 28, 2006
May 28, 2006
24
{
25
May 29, 2006
May 29, 2006
26
27
unlink(FBASENAME1);
unlink(FBASENAME2);
28
29
}
May 28, 2006
May 28, 2006
30
static void
May 29, 2006
May 29, 2006
31
rwops_error_quit(unsigned line, SDL_RWops * rwops)
May 28, 2006
May 28, 2006
32
33
{
May 29, 2006
May 29, 2006
34
printf("testfile.c(%d): failed\n", line);
May 28, 2006
May 28, 2006
35
if (rwops) {
May 29, 2006
May 29, 2006
36
rwops->close(rwops); /* This calls SDL_FreeRW(rwops); */
May 28, 2006
May 28, 2006
37
}
May 29, 2006
May 29, 2006
38
39
cleanup();
exit(1); /* quit with rwops error (test failed) */
40
41
42
43
44
45
}
#define RWOP_ERR_QUIT(x) rwops_error_quit( __LINE__, (x) )
May 28, 2006
May 28, 2006
46
int
May 29, 2006
May 29, 2006
47
main(int argc, char *argv[])
48
{
May 28, 2006
May 28, 2006
49
50
51
SDL_RWops *rwops = NULL;
char test_buf[30];
May 29, 2006
May 29, 2006
52
cleanup();
53
54
/* test 1 : basic argument test: all those calls to SDL_RWFromFile should fail */
May 28, 2006
May 28, 2006
55
May 29, 2006
May 29, 2006
56
rwops = SDL_RWFromFile(NULL, NULL);
May 28, 2006
May 28, 2006
57
if (rwops)
May 29, 2006
May 29, 2006
58
59
RWOP_ERR_QUIT(rwops);
rwops = SDL_RWFromFile(NULL, "ab+");
May 28, 2006
May 28, 2006
60
if (rwops)
May 29, 2006
May 29, 2006
61
62
RWOP_ERR_QUIT(rwops);
rwops = SDL_RWFromFile(NULL, "sldfkjsldkfj");
May 28, 2006
May 28, 2006
63
if (rwops)
May 29, 2006
May 29, 2006
64
65
RWOP_ERR_QUIT(rwops);
rwops = SDL_RWFromFile("something", "");
May 28, 2006
May 28, 2006
66
if (rwops)
May 29, 2006
May 29, 2006
67
68
RWOP_ERR_QUIT(rwops);
rwops = SDL_RWFromFile("something", NULL);
May 28, 2006
May 28, 2006
69
if (rwops)
May 29, 2006
May 29, 2006
70
71
RWOP_ERR_QUIT(rwops);
printf("test1 OK\n");
72
73
74
75
76
77
/* 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)
*/
May 29, 2006
May 29, 2006
78
rwops = SDL_RWFromFile(FBASENAME2, "rb"); /* this file doesn't exist that call must fail */
May 28, 2006
May 28, 2006
79
if (rwops)
May 29, 2006
May 29, 2006
80
81
RWOP_ERR_QUIT(rwops);
rwops = SDL_RWFromFile(FBASENAME2, "rb+"); /* this file doesn't exist that call must fail */
May 28, 2006
May 28, 2006
82
if (rwops)
May 29, 2006
May 29, 2006
83
84
RWOP_ERR_QUIT(rwops);
rwops = SDL_RWFromFile(FBASENAME2, "wb");
May 28, 2006
May 28, 2006
85
if (!rwops)
May 29, 2006
May 29, 2006
86
87
88
89
RWOP_ERR_QUIT(rwops);
rwops->close(rwops);
unlink(FBASENAME2);
rwops = SDL_RWFromFile(FBASENAME2, "wb+");
May 28, 2006
May 28, 2006
90
if (!rwops)
May 29, 2006
May 29, 2006
91
92
93
94
RWOP_ERR_QUIT(rwops);
rwops->close(rwops);
unlink(FBASENAME2);
rwops = SDL_RWFromFile(FBASENAME2, "ab");
May 28, 2006
May 28, 2006
95
if (!rwops)
May 29, 2006
May 29, 2006
96
97
98
99
RWOP_ERR_QUIT(rwops);
rwops->close(rwops);
unlink(FBASENAME2);
rwops = SDL_RWFromFile(FBASENAME2, "ab+");
May 28, 2006
May 28, 2006
100
if (!rwops)
May 29, 2006
May 29, 2006
101
102
103
104
RWOP_ERR_QUIT(rwops);
rwops->close(rwops);
unlink(FBASENAME2);
printf("test2 OK\n");
105
106
107
108
/* test 3 : creation, writing , reading, seeking,
test : w mode, r mode, w+ mode
*/
May 29, 2006
May 29, 2006
109
rwops = SDL_RWFromFile(FBASENAME1, "wb"); /* write only */
May 28, 2006
May 28, 2006
110
if (!rwops)
May 29, 2006
May 29, 2006
111
112
113
114
115
116
117
118
119
120
121
122
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);
May 28, 2006
May 28, 2006
123
May 29, 2006
May 29, 2006
124
rwops = SDL_RWFromFile(FBASENAME1, "rb"); /* read mode, file must exists */
May 28, 2006
May 28, 2006
125
if (!rwops)
May 29, 2006
May 29, 2006
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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);
148
149
/* test 3: same with w+ mode */
May 29, 2006
May 29, 2006
150
rwops = SDL_RWFromFile(FBASENAME1, "wb+"); /* write + read + truncation */
May 28, 2006
May 28, 2006
151
if (!rwops)
May 29, 2006
May 29, 2006
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
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");
183
184
/* test 4: same in r+ mode */
May 29, 2006
May 29, 2006
185
rwops = SDL_RWFromFile(FBASENAME1, "rb+"); /* write + read + file must exists, no truncation */
May 28, 2006
May 28, 2006
186
if (!rwops)
May 29, 2006
May 29, 2006
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
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");
218
219
/* test5 : append mode */
May 29, 2006
May 29, 2006
220
rwops = SDL_RWFromFile(FBASENAME1, "ab+"); /* write + read + append */
May 28, 2006
May 28, 2006
221
if (!rwops)
May 29, 2006
May 29, 2006
222
223
224
225
226
227
228
229
230
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);
May 28, 2006
May 28, 2006
231
May 29, 2006
May 29, 2006
232
233
234
235
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);
May 28, 2006
May 28, 2006
236
May 29, 2006
May 29, 2006
237
238
239
240
241
242
243
244
245
246
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);
May 28, 2006
May 28, 2006
247
May 29, 2006
May 29, 2006
248
249
if (27 != rwops->seek(rwops, -27, RW_SEEK_CUR))
RWOP_ERR_QUIT(rwops);
May 28, 2006
May 28, 2006
250
May 29, 2006
May 29, 2006
251
252
253
254
255
256
257
258
259
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();
May 28, 2006
May 28, 2006
260
return 0; /* all ok */
261
}