diff options
Diffstat (limited to 'src/contrib/SDL-3.2.20/test/testautomation_iostream.c')
-rw-r--r-- | src/contrib/SDL-3.2.20/test/testautomation_iostream.c | 701 |
1 files changed, 701 insertions, 0 deletions
diff --git a/src/contrib/SDL-3.2.20/test/testautomation_iostream.c b/src/contrib/SDL-3.2.20/test/testautomation_iostream.c new file mode 100644 index 0000000..d1e5877 --- /dev/null +++ b/src/contrib/SDL-3.2.20/test/testautomation_iostream.c | |||
@@ -0,0 +1,701 @@ | |||
1 | |||
2 | /** | ||
3 | * Automated SDL_IOStream test. | ||
4 | * | ||
5 | * Original code written by Edgar Simo "bobbens" | ||
6 | * Ported by Markus Kauppila (markus.kauppila@gmail.com) | ||
7 | * Updated and extended for SDL_test by aschiffler at ferzkopp dot net | ||
8 | * | ||
9 | * Released under Public Domain. | ||
10 | */ | ||
11 | |||
12 | /* quiet windows compiler warnings */ | ||
13 | #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS) | ||
14 | #define _CRT_SECURE_NO_WARNINGS | ||
15 | #endif | ||
16 | |||
17 | #include <stdio.h> | ||
18 | |||
19 | #include <SDL3/SDL.h> | ||
20 | #include <SDL3/SDL_test.h> | ||
21 | #include "testautomation_suites.h" | ||
22 | |||
23 | /* ================= Test Case Implementation ================== */ | ||
24 | |||
25 | static const char *IOStreamReadTestFilename = "iostrm_read"; | ||
26 | static const char *IOStreamWriteTestFilename = "iostrm_write"; | ||
27 | static const char *IOStreamAlphabetFilename = "iostrm_alphabet"; | ||
28 | |||
29 | static const char IOStreamHelloWorldTestString[] = "Hello World!"; | ||
30 | static const char IOStreamHelloWorldCompString[] = "Hello World!"; | ||
31 | static const char IOStreamAlphabetString[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | ||
32 | |||
33 | /* Fixture */ | ||
34 | |||
35 | static void SDLCALL IOStreamSetUp(void **arg) | ||
36 | { | ||
37 | size_t fileLen; | ||
38 | FILE *handle; | ||
39 | size_t writtenLen; | ||
40 | int result; | ||
41 | |||
42 | /* Clean up from previous runs (if any); ignore errors */ | ||
43 | (void)remove(IOStreamReadTestFilename); | ||
44 | (void)remove(IOStreamWriteTestFilename); | ||
45 | (void)remove(IOStreamAlphabetFilename); | ||
46 | |||
47 | /* Create a test file */ | ||
48 | handle = fopen(IOStreamReadTestFilename, "w"); | ||
49 | SDLTest_AssertCheck(handle != NULL, "Verify creation of file '%s' returned non NULL handle", IOStreamReadTestFilename); | ||
50 | if (handle == NULL) { | ||
51 | return; | ||
52 | } | ||
53 | |||
54 | /* Write some known text into it */ | ||
55 | fileLen = SDL_strlen(IOStreamHelloWorldTestString); | ||
56 | writtenLen = fwrite(IOStreamHelloWorldTestString, 1, fileLen, handle); | ||
57 | SDLTest_AssertCheck(fileLen == writtenLen, "Verify number of written bytes, expected %i, got %i", (int)fileLen, (int)writtenLen); | ||
58 | result = fclose(handle); | ||
59 | SDLTest_AssertCheck(result == 0, "Verify result from fclose, expected 0, got %i", result); | ||
60 | |||
61 | /* Create a second test file */ | ||
62 | handle = fopen(IOStreamAlphabetFilename, "w"); | ||
63 | SDLTest_AssertCheck(handle != NULL, "Verify creation of file '%s' returned non NULL handle", IOStreamAlphabetFilename); | ||
64 | if (handle == NULL) { | ||
65 | return; | ||
66 | } | ||
67 | |||
68 | /* Write alphabet text into it */ | ||
69 | fileLen = SDL_strlen(IOStreamAlphabetString); | ||
70 | writtenLen = fwrite(IOStreamAlphabetString, 1, fileLen, handle); | ||
71 | SDLTest_AssertCheck(fileLen == writtenLen, "Verify number of written bytes, expected %i, got %i", (int)fileLen, (int)writtenLen); | ||
72 | result = fclose(handle); | ||
73 | SDLTest_AssertCheck(result == 0, "Verify result from fclose, expected 0, got %i", result); | ||
74 | |||
75 | SDLTest_AssertPass("Creation of test file completed"); | ||
76 | } | ||
77 | |||
78 | static void SDLCALL IOStreamTearDown(void *arg) | ||
79 | { | ||
80 | int result; | ||
81 | |||
82 | /* Remove the created files to clean up; ignore errors for write filename */ | ||
83 | result = remove(IOStreamReadTestFilename); | ||
84 | SDLTest_AssertCheck(result == 0, "Verify result from remove(%s), expected 0, got %i", IOStreamReadTestFilename, result); | ||
85 | (void)remove(IOStreamWriteTestFilename); | ||
86 | result = remove(IOStreamAlphabetFilename); | ||
87 | SDLTest_AssertCheck(result == 0, "Verify result from remove(%s), expected 0, got %i", IOStreamAlphabetFilename, result); | ||
88 | |||
89 | SDLTest_AssertPass("Cleanup of test files completed"); | ||
90 | } | ||
91 | |||
92 | /** | ||
93 | * Makes sure parameters work properly. Local helper function. | ||
94 | * | ||
95 | * \sa SDL_SeekIO | ||
96 | * \sa SDL_ReadIO | ||
97 | */ | ||
98 | static void testGenericIOStreamValidations(SDL_IOStream *rw, bool write) | ||
99 | { | ||
100 | char buf[sizeof(IOStreamHelloWorldTestString)]; | ||
101 | Sint64 i; | ||
102 | size_t s; | ||
103 | int seekPos = SDLTest_RandomIntegerInRange(4, 8); | ||
104 | |||
105 | /* Clear buffer */ | ||
106 | SDL_zeroa(buf); | ||
107 | |||
108 | /* Set to start. */ | ||
109 | i = SDL_SeekIO(rw, 0, SDL_IO_SEEK_SET); | ||
110 | SDLTest_AssertPass("Call to SDL_SeekIO succeeded"); | ||
111 | SDLTest_AssertCheck(i == (Sint64)0, "Verify seek to 0 with SDL_SeekIO (SDL_IO_SEEK_SET), expected 0, got %" SDL_PRIs64, i); | ||
112 | |||
113 | /* Test write */ | ||
114 | s = SDL_WriteIO(rw, IOStreamHelloWorldTestString, sizeof(IOStreamHelloWorldTestString) - 1); | ||
115 | SDLTest_AssertPass("Call to SDL_WriteIO succeeded"); | ||
116 | if (write) { | ||
117 | SDLTest_AssertCheck(s == sizeof(IOStreamHelloWorldTestString) - 1, "Verify result of writing with SDL_WriteIO, expected %i, got %i", (int)sizeof(IOStreamHelloWorldTestString) - 1, (int)s); | ||
118 | } else { | ||
119 | SDLTest_AssertCheck(s == 0, "Verify result of writing with SDL_WriteIO, expected: 0, got %i", (int)s); | ||
120 | } | ||
121 | |||
122 | /* Test seek to random position */ | ||
123 | i = SDL_SeekIO(rw, seekPos, SDL_IO_SEEK_SET); | ||
124 | SDLTest_AssertPass("Call to SDL_SeekIO succeeded"); | ||
125 | SDLTest_AssertCheck(i == (Sint64)seekPos, "Verify seek to %i with SDL_SeekIO (SDL_IO_SEEK_SET), expected %i, got %" SDL_PRIs64, seekPos, seekPos, i); | ||
126 | |||
127 | /* Test seek back to start */ | ||
128 | i = SDL_SeekIO(rw, 0, SDL_IO_SEEK_SET); | ||
129 | SDLTest_AssertPass("Call to SDL_SeekIO succeeded"); | ||
130 | SDLTest_AssertCheck(i == (Sint64)0, "Verify seek to 0 with SDL_SeekIO (SDL_IO_SEEK_SET), expected 0, got %" SDL_PRIs64, i); | ||
131 | |||
132 | /* Test read */ | ||
133 | s = SDL_ReadIO(rw, buf, sizeof(IOStreamHelloWorldTestString) - 1); | ||
134 | SDLTest_AssertPass("Call to SDL_ReadIO succeeded"); | ||
135 | SDLTest_AssertCheck( | ||
136 | s == (sizeof(IOStreamHelloWorldTestString) - 1), | ||
137 | "Verify result from SDL_ReadIO, expected %i, got %i", | ||
138 | (int)(sizeof(IOStreamHelloWorldTestString) - 1), | ||
139 | (int)s); | ||
140 | SDLTest_AssertCheck( | ||
141 | SDL_memcmp(buf, IOStreamHelloWorldTestString, sizeof(IOStreamHelloWorldTestString) - 1) == 0, | ||
142 | "Verify read bytes match expected string, expected '%s', got '%s'", IOStreamHelloWorldTestString, buf); | ||
143 | |||
144 | /* Test seek back to start */ | ||
145 | i = SDL_SeekIO(rw, 0, SDL_IO_SEEK_SET); | ||
146 | SDLTest_AssertPass("Call to SDL_SeekIO succeeded"); | ||
147 | SDLTest_AssertCheck(i == (Sint64)0, "Verify seek to 0 with SDL_SeekIO (SDL_IO_SEEK_SET), expected 0, got %" SDL_PRIs64, i); | ||
148 | |||
149 | /* Test printf */ | ||
150 | s = SDL_IOprintf(rw, "%s", IOStreamHelloWorldTestString); | ||
151 | SDLTest_AssertPass("Call to SDL_IOprintf succeeded"); | ||
152 | if (write) { | ||
153 | SDLTest_AssertCheck(s == sizeof(IOStreamHelloWorldTestString) - 1, "Verify result of writing with SDL_IOprintf, expected %i, got %i", (int)sizeof(IOStreamHelloWorldTestString) - 1, (int)s); | ||
154 | } else { | ||
155 | SDLTest_AssertCheck(s == 0, "Verify result of writing with SDL_WriteIO, expected: 0, got %i", (int)s); | ||
156 | } | ||
157 | |||
158 | /* Test seek back to start */ | ||
159 | i = SDL_SeekIO(rw, 0, SDL_IO_SEEK_SET); | ||
160 | SDLTest_AssertPass("Call to SDL_SeekIO succeeded"); | ||
161 | SDLTest_AssertCheck(i == (Sint64)0, "Verify seek to 0 with SDL_SeekIO (SDL_IO_SEEK_SET), expected 0, got %" SDL_PRIs64, i); | ||
162 | |||
163 | /* Test read */ | ||
164 | s = SDL_ReadIO(rw, buf, sizeof(IOStreamHelloWorldTestString) - 1); | ||
165 | SDLTest_AssertPass("Call to SDL_ReadIO succeeded"); | ||
166 | SDLTest_AssertCheck( | ||
167 | s == (sizeof(IOStreamHelloWorldTestString) - 1), | ||
168 | "Verify result from SDL_ReadIO, expected %i, got %i", | ||
169 | (int)(sizeof(IOStreamHelloWorldTestString) - 1), | ||
170 | (int)s); | ||
171 | SDLTest_AssertCheck( | ||
172 | SDL_memcmp(buf, IOStreamHelloWorldTestString, sizeof(IOStreamHelloWorldTestString) - 1) == 0, | ||
173 | "Verify read bytes match expected string, expected '%s', got '%s'", IOStreamHelloWorldTestString, buf); | ||
174 | |||
175 | /* More seek tests. */ | ||
176 | i = SDL_SeekIO(rw, -4, SDL_IO_SEEK_CUR); | ||
177 | SDLTest_AssertPass("Call to SDL_SeekIO(...,-4,SDL_IO_SEEK_CUR) succeeded"); | ||
178 | SDLTest_AssertCheck( | ||
179 | i == (Sint64)(sizeof(IOStreamHelloWorldTestString) - 5), | ||
180 | "Verify seek to -4 with SDL_SeekIO (SDL_IO_SEEK_CUR), expected %i, got %i", | ||
181 | (int)(sizeof(IOStreamHelloWorldTestString) - 5), | ||
182 | (int)i); | ||
183 | |||
184 | i = SDL_SeekIO(rw, -1, SDL_IO_SEEK_END); | ||
185 | SDLTest_AssertPass("Call to SDL_SeekIO(...,-1,SDL_IO_SEEK_END) succeeded"); | ||
186 | SDLTest_AssertCheck( | ||
187 | i == (Sint64)(sizeof(IOStreamHelloWorldTestString) - 2), | ||
188 | "Verify seek to -1 with SDL_SeekIO (SDL_IO_SEEK_END), expected %i, got %i", | ||
189 | (int)(sizeof(IOStreamHelloWorldTestString) - 2), | ||
190 | (int)i); | ||
191 | |||
192 | /* Invalid whence seek */ | ||
193 | i = SDL_SeekIO(rw, 0, (SDL_IOWhence)999); | ||
194 | SDLTest_AssertPass("Call to SDL_SeekIO(...,0,invalid_whence) succeeded"); | ||
195 | SDLTest_AssertCheck( | ||
196 | i == (Sint64)(-1), | ||
197 | "Verify seek with SDL_SeekIO (invalid_whence); expected: -1, got %i", | ||
198 | (int)i); | ||
199 | } | ||
200 | |||
201 | /** | ||
202 | * Negative test for SDL_IOFromFile parameters | ||
203 | * | ||
204 | * \sa SDL_IOFromFile | ||
205 | * | ||
206 | */ | ||
207 | static int SDLCALL iostrm_testParamNegative(void *arg) | ||
208 | { | ||
209 | SDL_IOStream *iostrm; | ||
210 | |||
211 | /* These should all fail. */ | ||
212 | iostrm = SDL_IOFromFile(NULL, NULL); | ||
213 | SDLTest_AssertPass("Call to SDL_IOFromFile(NULL, NULL) succeeded"); | ||
214 | SDLTest_AssertCheck(iostrm == NULL, "Verify SDL_IOFromFile(NULL, NULL) returns NULL"); | ||
215 | |||
216 | iostrm = SDL_IOFromFile(NULL, "ab+"); | ||
217 | SDLTest_AssertPass("Call to SDL_IOFromFile(NULL, \"ab+\") succeeded"); | ||
218 | SDLTest_AssertCheck(iostrm == NULL, "Verify SDL_IOFromFile(NULL, \"ab+\") returns NULL"); | ||
219 | |||
220 | iostrm = SDL_IOFromFile(NULL, "sldfkjsldkfj"); | ||
221 | SDLTest_AssertPass("Call to SDL_IOFromFile(NULL, \"sldfkjsldkfj\") succeeded"); | ||
222 | SDLTest_AssertCheck(iostrm == NULL, "Verify SDL_IOFromFile(NULL, \"sldfkjsldkfj\") returns NULL"); | ||
223 | |||
224 | iostrm = SDL_IOFromFile("something", ""); | ||
225 | SDLTest_AssertPass("Call to SDL_IOFromFile(\"something\", \"\") succeeded"); | ||
226 | SDLTest_AssertCheck(iostrm == NULL, "Verify SDL_IOFromFile(\"something\", \"\") returns NULL"); | ||
227 | |||
228 | iostrm = SDL_IOFromFile("something", NULL); | ||
229 | SDLTest_AssertPass("Call to SDL_IOFromFile(\"something\", NULL) succeeded"); | ||
230 | SDLTest_AssertCheck(iostrm == NULL, "Verify SDL_IOFromFile(\"something\", NULL) returns NULL"); | ||
231 | |||
232 | iostrm = SDL_IOFromMem(NULL, 10); | ||
233 | SDLTest_AssertPass("Call to SDL_IOFromMem(NULL, 10) succeeded"); | ||
234 | SDLTest_AssertCheck(iostrm == NULL, "Verify SDL_IOFromMem(NULL, 10) returns NULL"); | ||
235 | |||
236 | iostrm = SDL_IOFromMem((void *)IOStreamAlphabetString, 0); | ||
237 | SDLTest_AssertPass("Call to SDL_IOFromMem(data, 0) succeeded"); | ||
238 | SDLTest_AssertCheck(iostrm == NULL, "Verify SDL_IOFromMem(data, 0) returns NULL"); | ||
239 | |||
240 | iostrm = SDL_IOFromConstMem((const void *)IOStreamAlphabetString, 0); | ||
241 | SDLTest_AssertPass("Call to SDL_IOFromConstMem(data, 0) succeeded"); | ||
242 | SDLTest_AssertCheck(iostrm == NULL, "Verify SDL_IOFromConstMem(data, 0) returns NULL"); | ||
243 | |||
244 | return TEST_COMPLETED; | ||
245 | } | ||
246 | |||
247 | /** | ||
248 | * Tests opening from memory. | ||
249 | * | ||
250 | * \sa SDL_IOFromMem | ||
251 | * \sa SDL_CloseIO | ||
252 | */ | ||
253 | static int SDLCALL iostrm_testMem(void *arg) | ||
254 | { | ||
255 | char mem[sizeof(IOStreamHelloWorldTestString)]; | ||
256 | SDL_IOStream *rw; | ||
257 | int result; | ||
258 | |||
259 | /* Clear buffer */ | ||
260 | SDL_zeroa(mem); | ||
261 | |||
262 | /* Open */ | ||
263 | rw = SDL_IOFromMem(mem, sizeof(IOStreamHelloWorldTestString) - 1); | ||
264 | SDLTest_AssertPass("Call to SDL_IOFromMem() succeeded"); | ||
265 | SDLTest_AssertCheck(rw != NULL, "Verify opening memory with SDL_IOFromMem does not return NULL"); | ||
266 | |||
267 | /* Bail out if NULL */ | ||
268 | if (rw == NULL) { | ||
269 | return TEST_ABORTED; | ||
270 | } | ||
271 | |||
272 | /* Run generic tests */ | ||
273 | testGenericIOStreamValidations(rw, true); | ||
274 | |||
275 | /* Close */ | ||
276 | result = SDL_CloseIO(rw); | ||
277 | SDLTest_AssertPass("Call to SDL_CloseIO() succeeded"); | ||
278 | SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result); | ||
279 | |||
280 | return TEST_COMPLETED; | ||
281 | } | ||
282 | |||
283 | /** | ||
284 | * Tests opening from memory. | ||
285 | * | ||
286 | * \sa SDL_IOFromConstMem | ||
287 | * \sa SDL_CloseIO | ||
288 | */ | ||
289 | static int SDLCALL iostrm_testConstMem(void *arg) | ||
290 | { | ||
291 | SDL_IOStream *rw; | ||
292 | int result; | ||
293 | |||
294 | /* Open handle */ | ||
295 | rw = SDL_IOFromConstMem(IOStreamHelloWorldCompString, sizeof(IOStreamHelloWorldCompString) - 1); | ||
296 | SDLTest_AssertPass("Call to SDL_IOFromConstMem() succeeded"); | ||
297 | SDLTest_AssertCheck(rw != NULL, "Verify opening memory with SDL_IOFromConstMem does not return NULL"); | ||
298 | |||
299 | /* Bail out if NULL */ | ||
300 | if (rw == NULL) { | ||
301 | return TEST_ABORTED; | ||
302 | } | ||
303 | |||
304 | /* Run generic tests */ | ||
305 | testGenericIOStreamValidations(rw, false); | ||
306 | |||
307 | /* Close handle */ | ||
308 | result = SDL_CloseIO(rw); | ||
309 | SDLTest_AssertPass("Call to SDL_CloseIO() succeeded"); | ||
310 | SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result); | ||
311 | |||
312 | return TEST_COMPLETED; | ||
313 | } | ||
314 | |||
315 | /** | ||
316 | * Tests dynamic memory | ||
317 | * | ||
318 | * \sa SDL_IOFromDynamicMem | ||
319 | * \sa SDL_CloseIO | ||
320 | */ | ||
321 | static int SDLCALL iostrm_testDynamicMem(void *arg) | ||
322 | { | ||
323 | SDL_IOStream *rw; | ||
324 | SDL_PropertiesID props; | ||
325 | char *mem; | ||
326 | int result; | ||
327 | |||
328 | /* Open */ | ||
329 | rw = SDL_IOFromDynamicMem(); | ||
330 | SDLTest_AssertPass("Call to SDL_IOFromDynamicMem() succeeded"); | ||
331 | SDLTest_AssertCheck(rw != NULL, "Verify opening memory with SDL_IOFromDynamicMem does not return NULL"); | ||
332 | |||
333 | /* Bail out if NULL */ | ||
334 | if (rw == NULL) { | ||
335 | return TEST_ABORTED; | ||
336 | } | ||
337 | |||
338 | /* Set the chunk size to 1 byte */ | ||
339 | props = SDL_GetIOProperties(rw); | ||
340 | SDL_SetNumberProperty(props, SDL_PROP_IOSTREAM_DYNAMIC_CHUNKSIZE_NUMBER, 1); | ||
341 | |||
342 | /* Run generic tests */ | ||
343 | testGenericIOStreamValidations(rw, true); | ||
344 | |||
345 | /* Get the dynamic memory and verify it */ | ||
346 | mem = (char *)SDL_GetPointerProperty(props, SDL_PROP_IOSTREAM_DYNAMIC_MEMORY_POINTER, NULL); | ||
347 | SDLTest_AssertPass("Call to SDL_GetPointerProperty(props, SDL_PROP_IOSTREAM_DYNAMIC_MEMORY_POINTER, NULL) succeeded"); | ||
348 | SDLTest_AssertCheck(mem != NULL, "Verify memory value is not NULL"); | ||
349 | mem[SDL_GetIOSize(rw)] = '\0'; | ||
350 | SDLTest_AssertCheck(SDL_strcmp(mem, IOStreamHelloWorldTestString) == 0, "Verify memory value is correct"); | ||
351 | |||
352 | /* Take the memory and free it ourselves */ | ||
353 | SDL_SetPointerProperty(props, SDL_PROP_IOSTREAM_DYNAMIC_MEMORY_POINTER, NULL); | ||
354 | SDL_free(mem); | ||
355 | |||
356 | /* Close */ | ||
357 | result = SDL_CloseIO(rw); | ||
358 | SDLTest_AssertPass("Call to SDL_CloseIO() succeeded"); | ||
359 | SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result); | ||
360 | |||
361 | return TEST_COMPLETED; | ||
362 | } | ||
363 | |||
364 | /** | ||
365 | * Tests reading from file. | ||
366 | * | ||
367 | * \sa SDL_IOFromFile | ||
368 | * \sa SDL_CloseIO | ||
369 | */ | ||
370 | static int SDLCALL iostrm_testFileRead(void *arg) | ||
371 | { | ||
372 | SDL_IOStream *rw; | ||
373 | int result; | ||
374 | |||
375 | /* Read test. */ | ||
376 | rw = SDL_IOFromFile(IOStreamReadTestFilename, "r"); | ||
377 | SDLTest_AssertPass("Call to SDL_IOFromFile(..,\"r\") succeeded"); | ||
378 | SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_IOFromFile in read mode does not return NULL"); | ||
379 | |||
380 | /* Bail out if NULL */ | ||
381 | if (rw == NULL) { | ||
382 | return TEST_ABORTED; | ||
383 | } | ||
384 | |||
385 | /* Run generic tests */ | ||
386 | testGenericIOStreamValidations(rw, false); | ||
387 | |||
388 | /* Close handle */ | ||
389 | result = SDL_CloseIO(rw); | ||
390 | SDLTest_AssertPass("Call to SDL_CloseIO() succeeded"); | ||
391 | SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result); | ||
392 | |||
393 | return TEST_COMPLETED; | ||
394 | } | ||
395 | |||
396 | /** | ||
397 | * Tests writing from file. | ||
398 | * | ||
399 | * \sa SDL_IOFromFile | ||
400 | * \sa SDL_CloseIO | ||
401 | */ | ||
402 | static int SDLCALL iostrm_testFileWrite(void *arg) | ||
403 | { | ||
404 | SDL_IOStream *rw; | ||
405 | int result; | ||
406 | |||
407 | /* Write test. */ | ||
408 | rw = SDL_IOFromFile(IOStreamWriteTestFilename, "w+"); | ||
409 | SDLTest_AssertPass("Call to SDL_IOFromFile(..,\"w+\") succeeded"); | ||
410 | SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_IOFromFile in write mode does not return NULL"); | ||
411 | |||
412 | /* Bail out if NULL */ | ||
413 | if (rw == NULL) { | ||
414 | return TEST_ABORTED; | ||
415 | } | ||
416 | |||
417 | /* Run generic tests */ | ||
418 | testGenericIOStreamValidations(rw, true); | ||
419 | |||
420 | /* Close handle */ | ||
421 | result = SDL_CloseIO(rw); | ||
422 | SDLTest_AssertPass("Call to SDL_CloseIO() succeeded"); | ||
423 | SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result); | ||
424 | |||
425 | return TEST_COMPLETED; | ||
426 | } | ||
427 | |||
428 | /** | ||
429 | * Tests alloc and free RW context. | ||
430 | * | ||
431 | * \sa SDL_OpenIO | ||
432 | * \sa SDL_CloseIO | ||
433 | */ | ||
434 | static int SDLCALL iostrm_testAllocFree(void *arg) | ||
435 | { | ||
436 | /* Allocate context */ | ||
437 | SDL_IOStreamInterface iface; | ||
438 | SDL_IOStream *rw; | ||
439 | |||
440 | SDL_INIT_INTERFACE(&iface); | ||
441 | rw = SDL_OpenIO(&iface, NULL); | ||
442 | SDLTest_AssertPass("Call to SDL_OpenIO() succeeded"); | ||
443 | SDLTest_AssertCheck(rw != NULL, "Validate result from SDL_OpenIO() is not NULL"); | ||
444 | if (rw == NULL) { | ||
445 | return TEST_ABORTED; | ||
446 | } | ||
447 | |||
448 | /* Free context again */ | ||
449 | SDL_CloseIO(rw); | ||
450 | SDLTest_AssertPass("Call to SDL_CloseIO() succeeded"); | ||
451 | |||
452 | return TEST_COMPLETED; | ||
453 | } | ||
454 | |||
455 | /** | ||
456 | * Compare memory and file reads | ||
457 | * | ||
458 | * \sa SDL_IOFromMem | ||
459 | * \sa SDL_IOFromFile | ||
460 | */ | ||
461 | static int SDLCALL iostrm_testCompareRWFromMemWithRWFromFile(void *arg) | ||
462 | { | ||
463 | int slen = 26; | ||
464 | char buffer_file[27]; | ||
465 | char buffer_mem[27]; | ||
466 | size_t rv_file; | ||
467 | size_t rv_mem; | ||
468 | Uint64 sv_file; | ||
469 | Uint64 sv_mem; | ||
470 | SDL_IOStream *iostrm_file; | ||
471 | SDL_IOStream *iostrm_mem; | ||
472 | int size; | ||
473 | int result; | ||
474 | |||
475 | for (size = 5; size < 10; size++) { | ||
476 | /* Terminate buffer */ | ||
477 | buffer_file[slen] = 0; | ||
478 | buffer_mem[slen] = 0; | ||
479 | |||
480 | /* Read/seek from memory */ | ||
481 | iostrm_mem = SDL_IOFromMem((void *)IOStreamAlphabetString, slen); | ||
482 | SDLTest_AssertPass("Call to SDL_IOFromMem()"); | ||
483 | rv_mem = SDL_ReadIO(iostrm_mem, buffer_mem, size * 6); | ||
484 | SDLTest_AssertPass("Call to SDL_ReadIO(mem, size=%d)", size * 6); | ||
485 | sv_mem = SDL_SeekIO(iostrm_mem, 0, SEEK_END); | ||
486 | SDLTest_AssertPass("Call to SDL_SeekIO(mem,SEEK_END)"); | ||
487 | result = SDL_CloseIO(iostrm_mem); | ||
488 | SDLTest_AssertPass("Call to SDL_CloseIO(mem)"); | ||
489 | SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result); | ||
490 | |||
491 | /* Read/see from file */ | ||
492 | iostrm_file = SDL_IOFromFile(IOStreamAlphabetFilename, "r"); | ||
493 | SDLTest_AssertPass("Call to SDL_IOFromFile()"); | ||
494 | rv_file = SDL_ReadIO(iostrm_file, buffer_file, size * 6); | ||
495 | SDLTest_AssertPass("Call to SDL_ReadIO(file, size=%d)", size * 6); | ||
496 | sv_file = SDL_SeekIO(iostrm_file, 0, SEEK_END); | ||
497 | SDLTest_AssertPass("Call to SDL_SeekIO(file,SEEK_END)"); | ||
498 | result = SDL_CloseIO(iostrm_file); | ||
499 | SDLTest_AssertPass("Call to SDL_CloseIO(file)"); | ||
500 | SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result); | ||
501 | |||
502 | /* Compare */ | ||
503 | SDLTest_AssertCheck(rv_mem == rv_file, "Verify returned read blocks matches for mem and file reads; got: rv_mem=%d rv_file=%d", (int)rv_mem, (int)rv_file); | ||
504 | SDLTest_AssertCheck(sv_mem == sv_file, "Verify SEEK_END position matches for mem and file seeks; got: sv_mem=%d sv_file=%d", (int)sv_mem, (int)sv_file); | ||
505 | SDLTest_AssertCheck(buffer_mem[slen] == 0, "Verify mem buffer termination; expected: 0, got: %d", buffer_mem[slen]); | ||
506 | SDLTest_AssertCheck(buffer_file[slen] == 0, "Verify file buffer termination; expected: 0, got: %d", buffer_file[slen]); | ||
507 | SDLTest_AssertCheck( | ||
508 | SDL_strncmp(buffer_mem, IOStreamAlphabetString, slen) == 0, | ||
509 | "Verify mem buffer contain alphabet string; expected: %s, got: %s", IOStreamAlphabetString, buffer_mem); | ||
510 | SDLTest_AssertCheck( | ||
511 | SDL_strncmp(buffer_file, IOStreamAlphabetString, slen) == 0, | ||
512 | "Verify file buffer contain alphabet string; expected: %s, got: %s", IOStreamAlphabetString, buffer_file); | ||
513 | } | ||
514 | |||
515 | return TEST_COMPLETED; | ||
516 | } | ||
517 | |||
518 | /** | ||
519 | * Tests writing and reading from file using endian aware functions. | ||
520 | * | ||
521 | * \sa SDL_IOFromFile | ||
522 | * \sa SDL_CloseIO | ||
523 | * \sa SDL_ReadU16BE | ||
524 | * \sa SDL_WriteU16BE | ||
525 | */ | ||
526 | static int SDLCALL iostrm_testFileWriteReadEndian(void *arg) | ||
527 | { | ||
528 | SDL_IOStream *rw; | ||
529 | Sint64 result; | ||
530 | int mode; | ||
531 | Uint16 BE16value; | ||
532 | Uint32 BE32value; | ||
533 | Uint64 BE64value; | ||
534 | Uint16 LE16value; | ||
535 | Uint32 LE32value; | ||
536 | Uint64 LE64value; | ||
537 | Uint16 BE16test; | ||
538 | Uint32 BE32test; | ||
539 | Uint64 BE64test; | ||
540 | Uint16 LE16test; | ||
541 | Uint32 LE32test; | ||
542 | Uint64 LE64test; | ||
543 | bool bresult; | ||
544 | int cresult; | ||
545 | |||
546 | for (mode = 0; mode < 3; mode++) { | ||
547 | |||
548 | /* Create test data */ | ||
549 | switch (mode) { | ||
550 | default: | ||
551 | case 0: | ||
552 | SDLTest_Log("All 0 values"); | ||
553 | BE16value = 0; | ||
554 | BE32value = 0; | ||
555 | BE64value = 0; | ||
556 | LE16value = 0; | ||
557 | LE32value = 0; | ||
558 | LE64value = 0; | ||
559 | break; | ||
560 | case 1: | ||
561 | SDLTest_Log("All 1 values"); | ||
562 | BE16value = 1; | ||
563 | BE32value = 1; | ||
564 | BE64value = 1; | ||
565 | LE16value = 1; | ||
566 | LE32value = 1; | ||
567 | LE64value = 1; | ||
568 | break; | ||
569 | case 2: | ||
570 | SDLTest_Log("Random values"); | ||
571 | BE16value = SDLTest_RandomUint16(); | ||
572 | BE32value = SDLTest_RandomUint32(); | ||
573 | BE64value = SDLTest_RandomUint64(); | ||
574 | LE16value = SDLTest_RandomUint16(); | ||
575 | LE32value = SDLTest_RandomUint32(); | ||
576 | LE64value = SDLTest_RandomUint64(); | ||
577 | break; | ||
578 | } | ||
579 | |||
580 | /* Write test. */ | ||
581 | rw = SDL_IOFromFile(IOStreamWriteTestFilename, "w+"); | ||
582 | SDLTest_AssertPass("Call to SDL_IOFromFile(..,\"w+\")"); | ||
583 | SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_IOFromFile in write mode does not return NULL"); | ||
584 | |||
585 | /* Bail out if NULL */ | ||
586 | if (rw == NULL) { | ||
587 | return TEST_ABORTED; | ||
588 | } | ||
589 | |||
590 | /* Write test data */ | ||
591 | bresult = SDL_WriteU16BE(rw, BE16value); | ||
592 | SDLTest_AssertPass("Call to SDL_WriteU16BE()"); | ||
593 | SDLTest_AssertCheck(bresult == true, "Validate object written, expected: true, got: false"); | ||
594 | bresult = SDL_WriteU32BE(rw, BE32value); | ||
595 | SDLTest_AssertPass("Call to SDL_WriteU32BE()"); | ||
596 | SDLTest_AssertCheck(bresult == true, "Validate object written, expected: true, got: false"); | ||
597 | bresult = SDL_WriteU64BE(rw, BE64value); | ||
598 | SDLTest_AssertPass("Call to SDL_WriteU64BE()"); | ||
599 | SDLTest_AssertCheck(bresult == true, "Validate object written, expected: true, got: false"); | ||
600 | bresult = SDL_WriteU16LE(rw, LE16value); | ||
601 | SDLTest_AssertPass("Call to SDL_WriteU16LE()"); | ||
602 | SDLTest_AssertCheck(bresult == true, "Validate object written, expected: true, got: false"); | ||
603 | bresult = SDL_WriteU32LE(rw, LE32value); | ||
604 | SDLTest_AssertPass("Call to SDL_WriteU32LE()"); | ||
605 | SDLTest_AssertCheck(bresult == true, "Validate object written, expected: true, got: false"); | ||
606 | bresult = SDL_WriteU64LE(rw, LE64value); | ||
607 | SDLTest_AssertPass("Call to SDL_WriteU64LE()"); | ||
608 | SDLTest_AssertCheck(bresult == true, "Validate object written, expected: true, got: false"); | ||
609 | |||
610 | /* Test seek to start */ | ||
611 | result = SDL_SeekIO(rw, 0, SDL_IO_SEEK_SET); | ||
612 | SDLTest_AssertPass("Call to SDL_SeekIO succeeded"); | ||
613 | SDLTest_AssertCheck(result == 0, "Verify result from position 0 with SDL_SeekIO, expected 0, got %i", (int)result); | ||
614 | |||
615 | /* Read test data */ | ||
616 | bresult = SDL_ReadU16BE(rw, &BE16test); | ||
617 | SDLTest_AssertPass("Call to SDL_ReadU16BE()"); | ||
618 | SDLTest_AssertCheck(bresult == true, "Validate object read, expected: true, got: false"); | ||
619 | SDLTest_AssertCheck(BE16test == BE16value, "Validate object read from SDL_ReadU16BE, expected: %hu, got: %hu", BE16value, BE16test); | ||
620 | bresult = SDL_ReadU32BE(rw, &BE32test); | ||
621 | SDLTest_AssertPass("Call to SDL_ReadU32BE()"); | ||
622 | SDLTest_AssertCheck(bresult == true, "Validate object read, expected: true, got: false"); | ||
623 | SDLTest_AssertCheck(BE32test == BE32value, "Validate object read from SDL_ReadU32BE, expected: %" SDL_PRIu32 ", got: %" SDL_PRIu32, BE32value, BE32test); | ||
624 | bresult = SDL_ReadU64BE(rw, &BE64test); | ||
625 | SDLTest_AssertPass("Call to SDL_ReadU64BE()"); | ||
626 | SDLTest_AssertCheck(bresult == true, "Validate object read, expected: true, got: false"); | ||
627 | SDLTest_AssertCheck(BE64test == BE64value, "Validate object read from SDL_ReadU64BE, expected: %" SDL_PRIu64 ", got: %" SDL_PRIu64, BE64value, BE64test); | ||
628 | bresult = SDL_ReadU16LE(rw, &LE16test); | ||
629 | SDLTest_AssertPass("Call to SDL_ReadU16LE()"); | ||
630 | SDLTest_AssertCheck(bresult == true, "Validate object read, expected: true, got: false"); | ||
631 | SDLTest_AssertCheck(LE16test == LE16value, "Validate object read from SDL_ReadU16LE, expected: %hu, got: %hu", LE16value, LE16test); | ||
632 | bresult = SDL_ReadU32LE(rw, &LE32test); | ||
633 | SDLTest_AssertPass("Call to SDL_ReadU32LE()"); | ||
634 | SDLTest_AssertCheck(bresult == true, "Validate object read, expected: true, got: false"); | ||
635 | SDLTest_AssertCheck(LE32test == LE32value, "Validate object read from SDL_ReadU32LE, expected: %" SDL_PRIu32 ", got: %" SDL_PRIu32, LE32value, LE32test); | ||
636 | bresult = SDL_ReadU64LE(rw, &LE64test); | ||
637 | SDLTest_AssertPass("Call to SDL_ReadU64LE()"); | ||
638 | SDLTest_AssertCheck(bresult == true, "Validate object read, expected: true, got: false"); | ||
639 | SDLTest_AssertCheck(LE64test == LE64value, "Validate object read from SDL_ReadU64LE, expected: %" SDL_PRIu64 ", got: %" SDL_PRIu64, LE64value, LE64test); | ||
640 | |||
641 | /* Close handle */ | ||
642 | cresult = SDL_CloseIO(rw); | ||
643 | SDLTest_AssertPass("Call to SDL_CloseIO() succeeded"); | ||
644 | SDLTest_AssertCheck(cresult == true, "Verify result value is true; got: %d", cresult); | ||
645 | } | ||
646 | |||
647 | return TEST_COMPLETED; | ||
648 | } | ||
649 | |||
650 | /* ================= Test References ================== */ | ||
651 | |||
652 | /* IOStream test cases */ | ||
653 | static const SDLTest_TestCaseReference iostrmTest1 = { | ||
654 | iostrm_testParamNegative, "iostrm_testParamNegative", "Negative test for SDL_IOFromFile parameters", TEST_ENABLED | ||
655 | }; | ||
656 | |||
657 | static const SDLTest_TestCaseReference iostrmTest2 = { | ||
658 | iostrm_testMem, "iostrm_testMem", "Tests opening from memory", TEST_ENABLED | ||
659 | }; | ||
660 | |||
661 | static const SDLTest_TestCaseReference iostrmTest3 = { | ||
662 | iostrm_testConstMem, "iostrm_testConstMem", "Tests opening from (const) memory", TEST_ENABLED | ||
663 | }; | ||
664 | |||
665 | static const SDLTest_TestCaseReference iostrmTest4 = { | ||
666 | iostrm_testDynamicMem, "iostrm_testDynamicMem", "Tests opening dynamic memory", TEST_ENABLED | ||
667 | }; | ||
668 | |||
669 | static const SDLTest_TestCaseReference iostrmTest5 = { | ||
670 | iostrm_testFileRead, "iostrm_testFileRead", "Tests reading from a file", TEST_ENABLED | ||
671 | }; | ||
672 | |||
673 | static const SDLTest_TestCaseReference iostrmTest6 = { | ||
674 | iostrm_testFileWrite, "iostrm_testFileWrite", "Test writing to a file", TEST_ENABLED | ||
675 | }; | ||
676 | |||
677 | static const SDLTest_TestCaseReference iostrmTest7 = { | ||
678 | iostrm_testAllocFree, "iostrm_testAllocFree", "Test alloc and free of RW context", TEST_ENABLED | ||
679 | }; | ||
680 | |||
681 | static const SDLTest_TestCaseReference iostrmTest8 = { | ||
682 | iostrm_testFileWriteReadEndian, "iostrm_testFileWriteReadEndian", "Test writing and reading via the Endian aware functions", TEST_ENABLED | ||
683 | }; | ||
684 | |||
685 | static const SDLTest_TestCaseReference iostrmTest9 = { | ||
686 | iostrm_testCompareRWFromMemWithRWFromFile, "iostrm_testCompareRWFromMemWithRWFromFile", "Compare RWFromMem and RWFromFile IOStream for read and seek", TEST_ENABLED | ||
687 | }; | ||
688 | |||
689 | /* Sequence of IOStream test cases */ | ||
690 | static const SDLTest_TestCaseReference *iostrmTests[] = { | ||
691 | &iostrmTest1, &iostrmTest2, &iostrmTest3, &iostrmTest4, &iostrmTest5, &iostrmTest6, | ||
692 | &iostrmTest7, &iostrmTest8, &iostrmTest9, NULL | ||
693 | }; | ||
694 | |||
695 | /* IOStream test suite (global) */ | ||
696 | SDLTest_TestSuiteReference iostrmTestSuite = { | ||
697 | "IOStream", | ||
698 | IOStreamSetUp, | ||
699 | iostrmTests, | ||
700 | IOStreamTearDown | ||
701 | }; | ||