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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
|
/**
* Automated SDL_IOStream test.
*
* Original code written by Edgar Simo "bobbens"
* Ported by Markus Kauppila (markus.kauppila@gmail.com)
* Updated and extended for SDL_test by aschiffler at ferzkopp dot net
*
* Released under Public Domain.
*/
/* quiet windows compiler warnings */
#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <stdio.h>
#include <SDL3/SDL.h>
#include <SDL3/SDL_test.h>
#include "testautomation_suites.h"
/* ================= Test Case Implementation ================== */
static const char *IOStreamReadTestFilename = "iostrm_read";
static const char *IOStreamWriteTestFilename = "iostrm_write";
static const char *IOStreamAlphabetFilename = "iostrm_alphabet";
static const char IOStreamHelloWorldTestString[] = "Hello World!";
static const char IOStreamHelloWorldCompString[] = "Hello World!";
static const char IOStreamAlphabetString[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
/* Fixture */
static void SDLCALL IOStreamSetUp(void **arg)
{
size_t fileLen;
FILE *handle;
size_t writtenLen;
int result;
/* Clean up from previous runs (if any); ignore errors */
(void)remove(IOStreamReadTestFilename);
(void)remove(IOStreamWriteTestFilename);
(void)remove(IOStreamAlphabetFilename);
/* Create a test file */
handle = fopen(IOStreamReadTestFilename, "w");
SDLTest_AssertCheck(handle != NULL, "Verify creation of file '%s' returned non NULL handle", IOStreamReadTestFilename);
if (handle == NULL) {
return;
}
/* Write some known text into it */
fileLen = SDL_strlen(IOStreamHelloWorldTestString);
writtenLen = fwrite(IOStreamHelloWorldTestString, 1, fileLen, handle);
SDLTest_AssertCheck(fileLen == writtenLen, "Verify number of written bytes, expected %i, got %i", (int)fileLen, (int)writtenLen);
result = fclose(handle);
SDLTest_AssertCheck(result == 0, "Verify result from fclose, expected 0, got %i", result);
/* Create a second test file */
handle = fopen(IOStreamAlphabetFilename, "w");
SDLTest_AssertCheck(handle != NULL, "Verify creation of file '%s' returned non NULL handle", IOStreamAlphabetFilename);
if (handle == NULL) {
return;
}
/* Write alphabet text into it */
fileLen = SDL_strlen(IOStreamAlphabetString);
writtenLen = fwrite(IOStreamAlphabetString, 1, fileLen, handle);
SDLTest_AssertCheck(fileLen == writtenLen, "Verify number of written bytes, expected %i, got %i", (int)fileLen, (int)writtenLen);
result = fclose(handle);
SDLTest_AssertCheck(result == 0, "Verify result from fclose, expected 0, got %i", result);
SDLTest_AssertPass("Creation of test file completed");
}
static void SDLCALL IOStreamTearDown(void *arg)
{
int result;
/* Remove the created files to clean up; ignore errors for write filename */
result = remove(IOStreamReadTestFilename);
SDLTest_AssertCheck(result == 0, "Verify result from remove(%s), expected 0, got %i", IOStreamReadTestFilename, result);
(void)remove(IOStreamWriteTestFilename);
result = remove(IOStreamAlphabetFilename);
SDLTest_AssertCheck(result == 0, "Verify result from remove(%s), expected 0, got %i", IOStreamAlphabetFilename, result);
SDLTest_AssertPass("Cleanup of test files completed");
}
/**
* Makes sure parameters work properly. Local helper function.
*
* \sa SDL_SeekIO
* \sa SDL_ReadIO
*/
static void testGenericIOStreamValidations(SDL_IOStream *rw, bool write)
{
char buf[sizeof(IOStreamHelloWorldTestString)];
Sint64 i;
size_t s;
int seekPos = SDLTest_RandomIntegerInRange(4, 8);
/* Clear buffer */
SDL_zeroa(buf);
/* Set to start. */
i = SDL_SeekIO(rw, 0, SDL_IO_SEEK_SET);
SDLTest_AssertPass("Call to SDL_SeekIO succeeded");
SDLTest_AssertCheck(i == (Sint64)0, "Verify seek to 0 with SDL_SeekIO (SDL_IO_SEEK_SET), expected 0, got %" SDL_PRIs64, i);
/* Test write */
s = SDL_WriteIO(rw, IOStreamHelloWorldTestString, sizeof(IOStreamHelloWorldTestString) - 1);
SDLTest_AssertPass("Call to SDL_WriteIO succeeded");
if (write) {
SDLTest_AssertCheck(s == sizeof(IOStreamHelloWorldTestString) - 1, "Verify result of writing with SDL_WriteIO, expected %i, got %i", (int)sizeof(IOStreamHelloWorldTestString) - 1, (int)s);
} else {
SDLTest_AssertCheck(s == 0, "Verify result of writing with SDL_WriteIO, expected: 0, got %i", (int)s);
}
/* Test seek to random position */
i = SDL_SeekIO(rw, seekPos, SDL_IO_SEEK_SET);
SDLTest_AssertPass("Call to SDL_SeekIO succeeded");
SDLTest_AssertCheck(i == (Sint64)seekPos, "Verify seek to %i with SDL_SeekIO (SDL_IO_SEEK_SET), expected %i, got %" SDL_PRIs64, seekPos, seekPos, i);
/* Test seek back to start */
i = SDL_SeekIO(rw, 0, SDL_IO_SEEK_SET);
SDLTest_AssertPass("Call to SDL_SeekIO succeeded");
SDLTest_AssertCheck(i == (Sint64)0, "Verify seek to 0 with SDL_SeekIO (SDL_IO_SEEK_SET), expected 0, got %" SDL_PRIs64, i);
/* Test read */
s = SDL_ReadIO(rw, buf, sizeof(IOStreamHelloWorldTestString) - 1);
SDLTest_AssertPass("Call to SDL_ReadIO succeeded");
SDLTest_AssertCheck(
s == (sizeof(IOStreamHelloWorldTestString) - 1),
"Verify result from SDL_ReadIO, expected %i, got %i",
(int)(sizeof(IOStreamHelloWorldTestString) - 1),
(int)s);
SDLTest_AssertCheck(
SDL_memcmp(buf, IOStreamHelloWorldTestString, sizeof(IOStreamHelloWorldTestString) - 1) == 0,
"Verify read bytes match expected string, expected '%s', got '%s'", IOStreamHelloWorldTestString, buf);
/* Test seek back to start */
i = SDL_SeekIO(rw, 0, SDL_IO_SEEK_SET);
SDLTest_AssertPass("Call to SDL_SeekIO succeeded");
SDLTest_AssertCheck(i == (Sint64)0, "Verify seek to 0 with SDL_SeekIO (SDL_IO_SEEK_SET), expected 0, got %" SDL_PRIs64, i);
/* Test printf */
s = SDL_IOprintf(rw, "%s", IOStreamHelloWorldTestString);
SDLTest_AssertPass("Call to SDL_IOprintf succeeded");
if (write) {
SDLTest_AssertCheck(s == sizeof(IOStreamHelloWorldTestString) - 1, "Verify result of writing with SDL_IOprintf, expected %i, got %i", (int)sizeof(IOStreamHelloWorldTestString) - 1, (int)s);
} else {
SDLTest_AssertCheck(s == 0, "Verify result of writing with SDL_WriteIO, expected: 0, got %i", (int)s);
}
/* Test seek back to start */
i = SDL_SeekIO(rw, 0, SDL_IO_SEEK_SET);
SDLTest_AssertPass("Call to SDL_SeekIO succeeded");
SDLTest_AssertCheck(i == (Sint64)0, "Verify seek to 0 with SDL_SeekIO (SDL_IO_SEEK_SET), expected 0, got %" SDL_PRIs64, i);
/* Test read */
s = SDL_ReadIO(rw, buf, sizeof(IOStreamHelloWorldTestString) - 1);
SDLTest_AssertPass("Call to SDL_ReadIO succeeded");
SDLTest_AssertCheck(
s == (sizeof(IOStreamHelloWorldTestString) - 1),
"Verify result from SDL_ReadIO, expected %i, got %i",
(int)(sizeof(IOStreamHelloWorldTestString) - 1),
(int)s);
SDLTest_AssertCheck(
SDL_memcmp(buf, IOStreamHelloWorldTestString, sizeof(IOStreamHelloWorldTestString) - 1) == 0,
"Verify read bytes match expected string, expected '%s', got '%s'", IOStreamHelloWorldTestString, buf);
/* More seek tests. */
i = SDL_SeekIO(rw, -4, SDL_IO_SEEK_CUR);
SDLTest_AssertPass("Call to SDL_SeekIO(...,-4,SDL_IO_SEEK_CUR) succeeded");
SDLTest_AssertCheck(
i == (Sint64)(sizeof(IOStreamHelloWorldTestString) - 5),
"Verify seek to -4 with SDL_SeekIO (SDL_IO_SEEK_CUR), expected %i, got %i",
(int)(sizeof(IOStreamHelloWorldTestString) - 5),
(int)i);
i = SDL_SeekIO(rw, -1, SDL_IO_SEEK_END);
SDLTest_AssertPass("Call to SDL_SeekIO(...,-1,SDL_IO_SEEK_END) succeeded");
SDLTest_AssertCheck(
i == (Sint64)(sizeof(IOStreamHelloWorldTestString) - 2),
"Verify seek to -1 with SDL_SeekIO (SDL_IO_SEEK_END), expected %i, got %i",
(int)(sizeof(IOStreamHelloWorldTestString) - 2),
(int)i);
/* Invalid whence seek */
i = SDL_SeekIO(rw, 0, (SDL_IOWhence)999);
SDLTest_AssertPass("Call to SDL_SeekIO(...,0,invalid_whence) succeeded");
SDLTest_AssertCheck(
i == (Sint64)(-1),
"Verify seek with SDL_SeekIO (invalid_whence); expected: -1, got %i",
(int)i);
}
/**
* Negative test for SDL_IOFromFile parameters
*
* \sa SDL_IOFromFile
*
*/
static int SDLCALL iostrm_testParamNegative(void *arg)
{
SDL_IOStream *iostrm;
/* These should all fail. */
iostrm = SDL_IOFromFile(NULL, NULL);
SDLTest_AssertPass("Call to SDL_IOFromFile(NULL, NULL) succeeded");
SDLTest_AssertCheck(iostrm == NULL, "Verify SDL_IOFromFile(NULL, NULL) returns NULL");
iostrm = SDL_IOFromFile(NULL, "ab+");
SDLTest_AssertPass("Call to SDL_IOFromFile(NULL, \"ab+\") succeeded");
SDLTest_AssertCheck(iostrm == NULL, "Verify SDL_IOFromFile(NULL, \"ab+\") returns NULL");
iostrm = SDL_IOFromFile(NULL, "sldfkjsldkfj");
SDLTest_AssertPass("Call to SDL_IOFromFile(NULL, \"sldfkjsldkfj\") succeeded");
SDLTest_AssertCheck(iostrm == NULL, "Verify SDL_IOFromFile(NULL, \"sldfkjsldkfj\") returns NULL");
iostrm = SDL_IOFromFile("something", "");
SDLTest_AssertPass("Call to SDL_IOFromFile(\"something\", \"\") succeeded");
SDLTest_AssertCheck(iostrm == NULL, "Verify SDL_IOFromFile(\"something\", \"\") returns NULL");
iostrm = SDL_IOFromFile("something", NULL);
SDLTest_AssertPass("Call to SDL_IOFromFile(\"something\", NULL) succeeded");
SDLTest_AssertCheck(iostrm == NULL, "Verify SDL_IOFromFile(\"something\", NULL) returns NULL");
iostrm = SDL_IOFromMem(NULL, 10);
SDLTest_AssertPass("Call to SDL_IOFromMem(NULL, 10) succeeded");
SDLTest_AssertCheck(iostrm == NULL, "Verify SDL_IOFromMem(NULL, 10) returns NULL");
iostrm = SDL_IOFromMem((void *)IOStreamAlphabetString, 0);
SDLTest_AssertPass("Call to SDL_IOFromMem(data, 0) succeeded");
SDLTest_AssertCheck(iostrm == NULL, "Verify SDL_IOFromMem(data, 0) returns NULL");
iostrm = SDL_IOFromConstMem((const void *)IOStreamAlphabetString, 0);
SDLTest_AssertPass("Call to SDL_IOFromConstMem(data, 0) succeeded");
SDLTest_AssertCheck(iostrm == NULL, "Verify SDL_IOFromConstMem(data, 0) returns NULL");
return TEST_COMPLETED;
}
/**
* Tests opening from memory.
*
* \sa SDL_IOFromMem
* \sa SDL_CloseIO
*/
static int SDLCALL iostrm_testMem(void *arg)
{
char mem[sizeof(IOStreamHelloWorldTestString)];
SDL_IOStream *rw;
int result;
/* Clear buffer */
SDL_zeroa(mem);
/* Open */
rw = SDL_IOFromMem(mem, sizeof(IOStreamHelloWorldTestString) - 1);
SDLTest_AssertPass("Call to SDL_IOFromMem() succeeded");
SDLTest_AssertCheck(rw != NULL, "Verify opening memory with SDL_IOFromMem does not return NULL");
/* Bail out if NULL */
if (rw == NULL) {
return TEST_ABORTED;
}
/* Run generic tests */
testGenericIOStreamValidations(rw, true);
/* Close */
result = SDL_CloseIO(rw);
SDLTest_AssertPass("Call to SDL_CloseIO() succeeded");
SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result);
return TEST_COMPLETED;
}
/**
* Tests opening from memory.
*
* \sa SDL_IOFromConstMem
* \sa SDL_CloseIO
*/
static int SDLCALL iostrm_testConstMem(void *arg)
{
SDL_IOStream *rw;
int result;
/* Open handle */
rw = SDL_IOFromConstMem(IOStreamHelloWorldCompString, sizeof(IOStreamHelloWorldCompString) - 1);
SDLTest_AssertPass("Call to SDL_IOFromConstMem() succeeded");
SDLTest_AssertCheck(rw != NULL, "Verify opening memory with SDL_IOFromConstMem does not return NULL");
/* Bail out if NULL */
if (rw == NULL) {
return TEST_ABORTED;
}
/* Run generic tests */
testGenericIOStreamValidations(rw, false);
/* Close handle */
result = SDL_CloseIO(rw);
SDLTest_AssertPass("Call to SDL_CloseIO() succeeded");
SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result);
return TEST_COMPLETED;
}
/**
* Tests dynamic memory
*
* \sa SDL_IOFromDynamicMem
* \sa SDL_CloseIO
*/
static int SDLCALL iostrm_testDynamicMem(void *arg)
{
SDL_IOStream *rw;
SDL_PropertiesID props;
char *mem;
int result;
/* Open */
rw = SDL_IOFromDynamicMem();
SDLTest_AssertPass("Call to SDL_IOFromDynamicMem() succeeded");
SDLTest_AssertCheck(rw != NULL, "Verify opening memory with SDL_IOFromDynamicMem does not return NULL");
/* Bail out if NULL */
if (rw == NULL) {
return TEST_ABORTED;
}
/* Set the chunk size to 1 byte */
props = SDL_GetIOProperties(rw);
SDL_SetNumberProperty(props, SDL_PROP_IOSTREAM_DYNAMIC_CHUNKSIZE_NUMBER, 1);
/* Run generic tests */
testGenericIOStreamValidations(rw, true);
/* Get the dynamic memory and verify it */
mem = (char *)SDL_GetPointerProperty(props, SDL_PROP_IOSTREAM_DYNAMIC_MEMORY_POINTER, NULL);
SDLTest_AssertPass("Call to SDL_GetPointerProperty(props, SDL_PROP_IOSTREAM_DYNAMIC_MEMORY_POINTER, NULL) succeeded");
SDLTest_AssertCheck(mem != NULL, "Verify memory value is not NULL");
mem[SDL_GetIOSize(rw)] = '\0';
SDLTest_AssertCheck(SDL_strcmp(mem, IOStreamHelloWorldTestString) == 0, "Verify memory value is correct");
/* Take the memory and free it ourselves */
SDL_SetPointerProperty(props, SDL_PROP_IOSTREAM_DYNAMIC_MEMORY_POINTER, NULL);
SDL_free(mem);
/* Close */
result = SDL_CloseIO(rw);
SDLTest_AssertPass("Call to SDL_CloseIO() succeeded");
SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result);
return TEST_COMPLETED;
}
/**
* Tests reading from file.
*
* \sa SDL_IOFromFile
* \sa SDL_CloseIO
*/
static int SDLCALL iostrm_testFileRead(void *arg)
{
SDL_IOStream *rw;
int result;
/* Read test. */
rw = SDL_IOFromFile(IOStreamReadTestFilename, "r");
SDLTest_AssertPass("Call to SDL_IOFromFile(..,\"r\") succeeded");
SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_IOFromFile in read mode does not return NULL");
/* Bail out if NULL */
if (rw == NULL) {
return TEST_ABORTED;
}
/* Run generic tests */
testGenericIOStreamValidations(rw, false);
/* Close handle */
result = SDL_CloseIO(rw);
SDLTest_AssertPass("Call to SDL_CloseIO() succeeded");
SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result);
return TEST_COMPLETED;
}
/**
* Tests writing from file.
*
* \sa SDL_IOFromFile
* \sa SDL_CloseIO
*/
static int SDLCALL iostrm_testFileWrite(void *arg)
{
SDL_IOStream *rw;
int result;
/* Write test. */
rw = SDL_IOFromFile(IOStreamWriteTestFilename, "w+");
SDLTest_AssertPass("Call to SDL_IOFromFile(..,\"w+\") succeeded");
SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_IOFromFile in write mode does not return NULL");
/* Bail out if NULL */
if (rw == NULL) {
return TEST_ABORTED;
}
/* Run generic tests */
testGenericIOStreamValidations(rw, true);
/* Close handle */
result = SDL_CloseIO(rw);
SDLTest_AssertPass("Call to SDL_CloseIO() succeeded");
SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result);
return TEST_COMPLETED;
}
/**
* Tests alloc and free RW context.
*
* \sa SDL_OpenIO
* \sa SDL_CloseIO
*/
static int SDLCALL iostrm_testAllocFree(void *arg)
{
/* Allocate context */
SDL_IOStreamInterface iface;
SDL_IOStream *rw;
SDL_INIT_INTERFACE(&iface);
rw = SDL_OpenIO(&iface, NULL);
SDLTest_AssertPass("Call to SDL_OpenIO() succeeded");
SDLTest_AssertCheck(rw != NULL, "Validate result from SDL_OpenIO() is not NULL");
if (rw == NULL) {
return TEST_ABORTED;
}
/* Free context again */
SDL_CloseIO(rw);
SDLTest_AssertPass("Call to SDL_CloseIO() succeeded");
return TEST_COMPLETED;
}
/**
* Compare memory and file reads
*
* \sa SDL_IOFromMem
* \sa SDL_IOFromFile
*/
static int SDLCALL iostrm_testCompareRWFromMemWithRWFromFile(void *arg)
{
int slen = 26;
char buffer_file[27];
char buffer_mem[27];
size_t rv_file;
size_t rv_mem;
Uint64 sv_file;
Uint64 sv_mem;
SDL_IOStream *iostrm_file;
SDL_IOStream *iostrm_mem;
int size;
int result;
for (size = 5; size < 10; size++) {
/* Terminate buffer */
buffer_file[slen] = 0;
buffer_mem[slen] = 0;
/* Read/seek from memory */
iostrm_mem = SDL_IOFromMem((void *)IOStreamAlphabetString, slen);
SDLTest_AssertPass("Call to SDL_IOFromMem()");
rv_mem = SDL_ReadIO(iostrm_mem, buffer_mem, size * 6);
SDLTest_AssertPass("Call to SDL_ReadIO(mem, size=%d)", size * 6);
sv_mem = SDL_SeekIO(iostrm_mem, 0, SEEK_END);
SDLTest_AssertPass("Call to SDL_SeekIO(mem,SEEK_END)");
result = SDL_CloseIO(iostrm_mem);
SDLTest_AssertPass("Call to SDL_CloseIO(mem)");
SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result);
/* Read/see from file */
iostrm_file = SDL_IOFromFile(IOStreamAlphabetFilename, "r");
SDLTest_AssertPass("Call to SDL_IOFromFile()");
rv_file = SDL_ReadIO(iostrm_file, buffer_file, size * 6);
SDLTest_AssertPass("Call to SDL_ReadIO(file, size=%d)", size * 6);
sv_file = SDL_SeekIO(iostrm_file, 0, SEEK_END);
SDLTest_AssertPass("Call to SDL_SeekIO(file,SEEK_END)");
result = SDL_CloseIO(iostrm_file);
SDLTest_AssertPass("Call to SDL_CloseIO(file)");
SDLTest_AssertCheck(result == true, "Verify result value is true; got: %d", result);
/* Compare */
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);
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);
SDLTest_AssertCheck(buffer_mem[slen] == 0, "Verify mem buffer termination; expected: 0, got: %d", buffer_mem[slen]);
SDLTest_AssertCheck(buffer_file[slen] == 0, "Verify file buffer termination; expected: 0, got: %d", buffer_file[slen]);
SDLTest_AssertCheck(
SDL_strncmp(buffer_mem, IOStreamAlphabetString, slen) == 0,
"Verify mem buffer contain alphabet string; expected: %s, got: %s", IOStreamAlphabetString, buffer_mem);
SDLTest_AssertCheck(
SDL_strncmp(buffer_file, IOStreamAlphabetString, slen) == 0,
"Verify file buffer contain alphabet string; expected: %s, got: %s", IOStreamAlphabetString, buffer_file);
}
return TEST_COMPLETED;
}
/**
* Tests writing and reading from file using endian aware functions.
*
* \sa SDL_IOFromFile
* \sa SDL_CloseIO
* \sa SDL_ReadU16BE
* \sa SDL_WriteU16BE
*/
static int SDLCALL iostrm_testFileWriteReadEndian(void *arg)
{
SDL_IOStream *rw;
Sint64 result;
int mode;
Uint16 BE16value;
Uint32 BE32value;
Uint64 BE64value;
Uint16 LE16value;
Uint32 LE32value;
Uint64 LE64value;
Uint16 BE16test;
Uint32 BE32test;
Uint64 BE64test;
Uint16 LE16test;
Uint32 LE32test;
Uint64 LE64test;
bool bresult;
int cresult;
for (mode = 0; mode < 3; mode++) {
/* Create test data */
switch (mode) {
default:
case 0:
SDLTest_Log("All 0 values");
BE16value = 0;
BE32value = 0;
BE64value = 0;
LE16value = 0;
LE32value = 0;
LE64value = 0;
break;
case 1:
SDLTest_Log("All 1 values");
BE16value = 1;
BE32value = 1;
BE64value = 1;
LE16value = 1;
LE32value = 1;
LE64value = 1;
break;
case 2:
SDLTest_Log("Random values");
BE16value = SDLTest_RandomUint16();
BE32value = SDLTest_RandomUint32();
BE64value = SDLTest_RandomUint64();
LE16value = SDLTest_RandomUint16();
LE32value = SDLTest_RandomUint32();
LE64value = SDLTest_RandomUint64();
break;
}
/* Write test. */
rw = SDL_IOFromFile(IOStreamWriteTestFilename, "w+");
SDLTest_AssertPass("Call to SDL_IOFromFile(..,\"w+\")");
SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_IOFromFile in write mode does not return NULL");
/* Bail out if NULL */
if (rw == NULL) {
return TEST_ABORTED;
}
/* Write test data */
bresult = SDL_WriteU16BE(rw, BE16value);
SDLTest_AssertPass("Call to SDL_WriteU16BE()");
SDLTest_AssertCheck(bresult == true, "Validate object written, expected: true, got: false");
bresult = SDL_WriteU32BE(rw, BE32value);
SDLTest_AssertPass("Call to SDL_WriteU32BE()");
SDLTest_AssertCheck(bresult == true, "Validate object written, expected: true, got: false");
bresult = SDL_WriteU64BE(rw, BE64value);
SDLTest_AssertPass("Call to SDL_WriteU64BE()");
SDLTest_AssertCheck(bresult == true, "Validate object written, expected: true, got: false");
bresult = SDL_WriteU16LE(rw, LE16value);
SDLTest_AssertPass("Call to SDL_WriteU16LE()");
SDLTest_AssertCheck(bresult == true, "Validate object written, expected: true, got: false");
bresult = SDL_WriteU32LE(rw, LE32value);
SDLTest_AssertPass("Call to SDL_WriteU32LE()");
SDLTest_AssertCheck(bresult == true, "Validate object written, expected: true, got: false");
bresult = SDL_WriteU64LE(rw, LE64value);
SDLTest_AssertPass("Call to SDL_WriteU64LE()");
SDLTest_AssertCheck(bresult == true, "Validate object written, expected: true, got: false");
/* Test seek to start */
result = SDL_SeekIO(rw, 0, SDL_IO_SEEK_SET);
SDLTest_AssertPass("Call to SDL_SeekIO succeeded");
SDLTest_AssertCheck(result == 0, "Verify result from position 0 with SDL_SeekIO, expected 0, got %i", (int)result);
/* Read test data */
bresult = SDL_ReadU16BE(rw, &BE16test);
SDLTest_AssertPass("Call to SDL_ReadU16BE()");
SDLTest_AssertCheck(bresult == true, "Validate object read, expected: true, got: false");
SDLTest_AssertCheck(BE16test == BE16value, "Validate object read from SDL_ReadU16BE, expected: %hu, got: %hu", BE16value, BE16test);
bresult = SDL_ReadU32BE(rw, &BE32test);
SDLTest_AssertPass("Call to SDL_ReadU32BE()");
SDLTest_AssertCheck(bresult == true, "Validate object read, expected: true, got: false");
SDLTest_AssertCheck(BE32test == BE32value, "Validate object read from SDL_ReadU32BE, expected: %" SDL_PRIu32 ", got: %" SDL_PRIu32, BE32value, BE32test);
bresult = SDL_ReadU64BE(rw, &BE64test);
SDLTest_AssertPass("Call to SDL_ReadU64BE()");
SDLTest_AssertCheck(bresult == true, "Validate object read, expected: true, got: false");
SDLTest_AssertCheck(BE64test == BE64value, "Validate object read from SDL_ReadU64BE, expected: %" SDL_PRIu64 ", got: %" SDL_PRIu64, BE64value, BE64test);
bresult = SDL_ReadU16LE(rw, &LE16test);
SDLTest_AssertPass("Call to SDL_ReadU16LE()");
SDLTest_AssertCheck(bresult == true, "Validate object read, expected: true, got: false");
SDLTest_AssertCheck(LE16test == LE16value, "Validate object read from SDL_ReadU16LE, expected: %hu, got: %hu", LE16value, LE16test);
bresult = SDL_ReadU32LE(rw, &LE32test);
SDLTest_AssertPass("Call to SDL_ReadU32LE()");
SDLTest_AssertCheck(bresult == true, "Validate object read, expected: true, got: false");
SDLTest_AssertCheck(LE32test == LE32value, "Validate object read from SDL_ReadU32LE, expected: %" SDL_PRIu32 ", got: %" SDL_PRIu32, LE32value, LE32test);
bresult = SDL_ReadU64LE(rw, &LE64test);
SDLTest_AssertPass("Call to SDL_ReadU64LE()");
SDLTest_AssertCheck(bresult == true, "Validate object read, expected: true, got: false");
SDLTest_AssertCheck(LE64test == LE64value, "Validate object read from SDL_ReadU64LE, expected: %" SDL_PRIu64 ", got: %" SDL_PRIu64, LE64value, LE64test);
/* Close handle */
cresult = SDL_CloseIO(rw);
SDLTest_AssertPass("Call to SDL_CloseIO() succeeded");
SDLTest_AssertCheck(cresult == true, "Verify result value is true; got: %d", cresult);
}
return TEST_COMPLETED;
}
/* ================= Test References ================== */
/* IOStream test cases */
static const SDLTest_TestCaseReference iostrmTest1 = {
iostrm_testParamNegative, "iostrm_testParamNegative", "Negative test for SDL_IOFromFile parameters", TEST_ENABLED
};
static const SDLTest_TestCaseReference iostrmTest2 = {
iostrm_testMem, "iostrm_testMem", "Tests opening from memory", TEST_ENABLED
};
static const SDLTest_TestCaseReference iostrmTest3 = {
iostrm_testConstMem, "iostrm_testConstMem", "Tests opening from (const) memory", TEST_ENABLED
};
static const SDLTest_TestCaseReference iostrmTest4 = {
iostrm_testDynamicMem, "iostrm_testDynamicMem", "Tests opening dynamic memory", TEST_ENABLED
};
static const SDLTest_TestCaseReference iostrmTest5 = {
iostrm_testFileRead, "iostrm_testFileRead", "Tests reading from a file", TEST_ENABLED
};
static const SDLTest_TestCaseReference iostrmTest6 = {
iostrm_testFileWrite, "iostrm_testFileWrite", "Test writing to a file", TEST_ENABLED
};
static const SDLTest_TestCaseReference iostrmTest7 = {
iostrm_testAllocFree, "iostrm_testAllocFree", "Test alloc and free of RW context", TEST_ENABLED
};
static const SDLTest_TestCaseReference iostrmTest8 = {
iostrm_testFileWriteReadEndian, "iostrm_testFileWriteReadEndian", "Test writing and reading via the Endian aware functions", TEST_ENABLED
};
static const SDLTest_TestCaseReference iostrmTest9 = {
iostrm_testCompareRWFromMemWithRWFromFile, "iostrm_testCompareRWFromMemWithRWFromFile", "Compare RWFromMem and RWFromFile IOStream for read and seek", TEST_ENABLED
};
/* Sequence of IOStream test cases */
static const SDLTest_TestCaseReference *iostrmTests[] = {
&iostrmTest1, &iostrmTest2, &iostrmTest3, &iostrmTest4, &iostrmTest5, &iostrmTest6,
&iostrmTest7, &iostrmTest8, &iostrmTest9, NULL
};
/* IOStream test suite (global) */
SDLTest_TestSuiteReference iostrmTestSuite = {
"IOStream",
IOStreamSetUp,
iostrmTests,
IOStreamTearDown
};
|