Mercurial > sdl-ios-xcode
comparison test/automated/rwops/rwops.c @ 3259:22ac66da0765
Merged Edgar's code changes from Google Summer of Code 2009
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Mon, 07 Sep 2009 05:06:34 +0000 |
parents | |
children | c32c53fca10d |
comparison
equal
deleted
inserted
replaced
3258:e786366ea23b | 3259:22ac66da0765 |
---|---|
1 /** | |
2 * Automated SDL_RWops test. | |
3 * | |
4 * Written by Edgar Simo "bobbens" | |
5 * | |
6 * Released under Public Domain. | |
7 */ | |
8 | |
9 | |
10 #include "SDL.h" | |
11 #include "SDL_at.h" | |
12 | |
13 | |
14 #define RWOPS_READ "rwops/read" | |
15 #define RWOPS_WRITE "rwops/write" | |
16 | |
17 static const char hello_world[] = "Hello World!"; | |
18 | |
19 | |
20 /** | |
21 * @brief Makes sure parameters work properly. | |
22 */ | |
23 static void rwops_testParam (void) | |
24 { | |
25 SDL_RWops *rwops; | |
26 | |
27 /* Begin testcase. */ | |
28 SDL_ATbegin( "RWops Parameters" ); | |
29 | |
30 /* These should all fail. */ | |
31 rwops = SDL_RWFromFile(NULL, NULL); | |
32 if (SDL_ATassert( "SDL_RWFromFile(NULL, NULL) worked", rwops == NULL )) | |
33 return; | |
34 rwops = SDL_RWFromFile(NULL, "ab+"); | |
35 if (SDL_ATassert( "SDL_RWFromFile(NULL, \"ab+\") worked", rwops == NULL )) | |
36 return; | |
37 rwops = SDL_RWFromFile(NULL, "sldfkjsldkfj"); | |
38 if (SDL_ATassert( "SDL_RWFromFile(NULL, \"sldfkjsldkfj\") worked", rwops == NULL )) | |
39 return; | |
40 rwops = SDL_RWFromFile("something", ""); | |
41 if (SDL_ATassert( "SDL_RWFromFile(\"something\", \"\") worked", rwops == NULL )) | |
42 return; | |
43 rwops = SDL_RWFromFile("something", NULL); | |
44 if (SDL_ATassert( "SDL_RWFromFile(\"something\", NULL) worked", rwops == NULL )) | |
45 return; | |
46 | |
47 | |
48 /* End testcase. */ | |
49 SDL_ATend(); | |
50 } | |
51 | |
52 | |
53 /** | |
54 * @brief Does a generic rwops test. | |
55 * | |
56 * RWops should have "Hello World!" in it already if write is disabled. | |
57 * | |
58 * @param write Test writing also. | |
59 * @return 1 if an assert is failed. | |
60 */ | |
61 static int rwops_testGeneric( SDL_RWops *rw, int write ) | |
62 { | |
63 char buf[sizeof(hello_world)]; | |
64 int i; | |
65 | |
66 /* Set to start. */ | |
67 i = SDL_RWseek( rw, 0, RW_SEEK_SET ); | |
68 if (SDL_ATvassert( i == 0, | |
69 "Seeking with SDL_RWseek (RW_SEEK_SET): got %d, expected %d", | |
70 i, 0 )) | |
71 return 1; | |
72 | |
73 /* Test write. */ | |
74 i = SDL_RWwrite( rw, hello_world, sizeof(hello_world)-1, 1 ); | |
75 if (write) { | |
76 if (SDL_ATassert( "Writing with SDL_RWwrite (failed to write)", i == 1 )) | |
77 return 1; | |
78 } | |
79 else { | |
80 if (SDL_ATassert( "Writing with SDL_RWwrite (wrote when shouldn't have)", i <= 0 )) | |
81 return 1; | |
82 } | |
83 | |
84 /* Test seek. */ | |
85 i = SDL_RWseek( rw, 6, RW_SEEK_SET ); | |
86 if (SDL_ATvassert( i == 6, | |
87 "Seeking with SDL_RWseek (RW_SEEK_SET): got %d, expected %d", | |
88 i, 0 )) | |
89 return 1; | |
90 | |
91 /* Test seek. */ | |
92 i = SDL_RWseek( rw, 0, RW_SEEK_SET ); | |
93 if (SDL_ATvassert( i == 0, | |
94 "Seeking with SDL_RWseek (RW_SEEK_SET): got %d, expected %d", | |
95 i, 0 )) | |
96 return 1; | |
97 | |
98 /* Test read. */ | |
99 i = SDL_RWread( rw, buf, 1, sizeof(hello_world)-1 ); | |
100 if (SDL_ATassert( "Reading with SDL_RWread", i == sizeof(hello_world)-1 )) | |
101 return 1; | |
102 if (SDL_ATassert( "Memory read does not match memory written", | |
103 memcmp( buf, hello_world, sizeof(hello_world)-1 ) == 0 )) | |
104 return 1; | |
105 | |
106 /* More seek tests. */ | |
107 i = SDL_RWseek( rw, -4, RW_SEEK_CUR ); | |
108 if (SDL_ATvassert( i == sizeof(hello_world)-5, | |
109 "Seeking with SDL_RWseek (RW_SEEK_CUR): got %d, expected %d", | |
110 i, sizeof(hello_world)-5 )) | |
111 return 1; | |
112 i = SDL_RWseek( rw, -1, RW_SEEK_END ); | |
113 if (SDL_ATvassert( i == sizeof(hello_world)-2, | |
114 "Seeking with SDL_RWseek (RW_SEEK_END): got %d, expected %d", | |
115 i, sizeof(hello_world)-2 )) | |
116 return 1; | |
117 | |
118 return 0; | |
119 } | |
120 | |
121 | |
122 /** | |
123 * @brief Tests opening from memory. | |
124 */ | |
125 static void rwops_testMem (void) | |
126 { | |
127 char mem[sizeof(hello_world)]; | |
128 SDL_RWops *rw; | |
129 | |
130 /* Begin testcase. */ | |
131 SDL_ATbegin( "SDL_RWFromMem" ); | |
132 | |
133 /* Open. */ | |
134 rw = SDL_RWFromMem( mem, sizeof(hello_world)-1 ); | |
135 if (SDL_ATassert( "Opening memory with SDL_RWFromMem", rw != NULL )) | |
136 return; | |
137 | |
138 /* Run generic tests. */ | |
139 if (rwops_testGeneric( rw, 1 )) | |
140 return; | |
141 | |
142 /* Close. */ | |
143 SDL_FreeRW( rw ); | |
144 | |
145 /* End testcase. */ | |
146 SDL_ATend(); | |
147 } | |
148 | |
149 | |
150 static const char const_mem[] = "Hello World!"; | |
151 /** | |
152 * @brief Tests opening from memory. | |
153 */ | |
154 static void rwops_testConstMem (void) | |
155 { | |
156 SDL_RWops *rw; | |
157 | |
158 /* Begin testcase. */ | |
159 SDL_ATbegin( "SDL_RWFromConstMem" ); | |
160 | |
161 /* Open. */ | |
162 rw = SDL_RWFromConstMem( const_mem, sizeof(const_mem)-1 ); | |
163 if (SDL_ATassert( "Opening memory with SDL_RWFromConstMem", rw != NULL )) | |
164 return; | |
165 | |
166 /* Run generic tests. */ | |
167 if (rwops_testGeneric( rw, 0 )) | |
168 return; | |
169 | |
170 /* Close. */ | |
171 SDL_FreeRW( rw ); | |
172 | |
173 /* End testcase. */ | |
174 SDL_ATend(); | |
175 } | |
176 | |
177 | |
178 /** | |
179 * @brief Tests opening from memory. | |
180 */ | |
181 static void rwops_testFile (void) | |
182 { | |
183 SDL_RWops *rw; | |
184 | |
185 /* Begin testcase. */ | |
186 SDL_ATbegin( "SDL_RWFromFile" ); | |
187 | |
188 /* Read test. */ | |
189 rw = SDL_RWFromFile( RWOPS_READ, "r" ); | |
190 if (SDL_ATassert( "Opening memory with SDL_RWFromFile '"RWOPS_READ"'", rw != NULL )) | |
191 return; | |
192 if (rwops_testGeneric( rw, 0 )) | |
193 return; | |
194 SDL_FreeRW( rw ); | |
195 | |
196 /* Write test. */ | |
197 rw = SDL_RWFromFile( RWOPS_WRITE, "w+" ); | |
198 if (SDL_ATassert( "Opening memory with SDL_RWFromFile '"RWOPS_WRITE"'", rw != NULL )) | |
199 return; | |
200 if (rwops_testGeneric( rw, 1 )) | |
201 return; | |
202 SDL_FreeRW( rw ); | |
203 | |
204 /* End testcase. */ | |
205 SDL_ATend(); | |
206 } | |
207 | |
208 | |
209 /** | |
210 * @brief Tests opening from memory. | |
211 */ | |
212 static void rwops_testFP (void) | |
213 { | |
214 #ifdef HAVE_STDIO_H | |
215 FILE *fp; | |
216 SDL_RWops *rw; | |
217 | |
218 /* Begin testcase. */ | |
219 SDL_ATbegin( "SDL_RWFromFP" ); | |
220 | |
221 /* Run read tests. */ | |
222 fp = fopen( RWOPS_READ, "r" ); | |
223 if (SDL_ATassert( "Failed to open file '"RWOPS_READ"'", fp != NULL)) | |
224 return; | |
225 rw = SDL_RWFromFP( fp, 1 ); | |
226 if (SDL_ATassert( "Opening memory with SDL_RWFromFP", rw != NULL )) | |
227 return; | |
228 if (rwops_testGeneric( rw, 0 )) | |
229 return; | |
230 SDL_FreeRW( rw ); | |
231 | |
232 /* Run write tests. */ | |
233 fp = fopen( RWOPS_WRITE, "w+" ); | |
234 if (SDL_ATassert( "Failed to open file '"RWOPS_WRITE"'", fp != NULL)) | |
235 return; | |
236 rw = SDL_RWFromFP( fp, 1 ); | |
237 if (SDL_ATassert( "Opening memory with SDL_RWFromFP", rw != NULL )) | |
238 return; | |
239 if (rwops_testGeneric( rw, 1 )) | |
240 return; | |
241 SDL_FreeRW( rw ); | |
242 | |
243 /* End testcase. */ | |
244 SDL_ATend(); | |
245 #endif /* HAVE_STDIO_H */ | |
246 } | |
247 | |
248 | |
249 /** | |
250 * @brief Entry point. | |
251 */ | |
252 #ifdef TEST_STANDALONE | |
253 int main( int argc, const char *argv[] ) | |
254 { | |
255 (void) argc; | |
256 (void) argv; | |
257 #else /* TEST_STANDALONE */ | |
258 int test_rwops (void) | |
259 { | |
260 #endif /* TEST_STANDALONE */ | |
261 | |
262 SDL_ATinit( "SDL_RWops" ); | |
263 | |
264 rwops_testParam(); | |
265 rwops_testMem(); | |
266 rwops_testConstMem(); | |
267 rwops_testFile(); | |
268 rwops_testFP(); | |
269 | |
270 return SDL_ATfinish(); | |
271 } |