SDL  2.0
testautomation_rwops.c
Go to the documentation of this file.
1 
2 /**
3  * Automated SDL_RWops 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 #define _CRT_SECURE_NO_WARNINGS
14 
15 #include <stdio.h>
16 
17 #include "SDL.h"
18 #include "SDL_test.h"
19 
20 /* ================= Test Case Implementation ================== */
21 
22 const char* RWopsReadTestFilename = "rwops_read";
23 const char* RWopsWriteTestFilename = "rwops_write";
24 const char* RWopsAlphabetFilename = "rwops_alphabet";
25 
26 static const char RWopsHelloWorldTestString[] = "Hello World!";
27 static const char RWopsHelloWorldCompString[] = "Hello World!";
28 static const char RWopsAlphabetString[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
29 
30 /* Fixture */
31 
32 void
33 RWopsSetUp(void *arg)
34 {
35  size_t fileLen;
36  FILE *handle;
37  size_t writtenLen;
38  int result;
39 
40  /* Clean up from previous runs (if any); ignore errors */
41  remove(RWopsReadTestFilename);
42  remove(RWopsWriteTestFilename);
43  remove(RWopsAlphabetFilename);
44 
45  /* Create a test file */
46  handle = fopen(RWopsReadTestFilename, "w");
47  SDLTest_AssertCheck(handle != NULL, "Verify creation of file '%s' returned non NULL handle", RWopsReadTestFilename);
48  if (handle == NULL) return;
49 
50  /* Write some known text into it */
52  writtenLen = fwrite(RWopsHelloWorldTestString, 1, fileLen, handle);
53  SDLTest_AssertCheck(fileLen == writtenLen, "Verify number of written bytes, expected %i, got %i", (int) fileLen, (int) writtenLen);
54  result = fclose(handle);
55  SDLTest_AssertCheck(result == 0, "Verify result from fclose, expected 0, got %i", result);
56 
57  /* Create a second test file */
58  handle = fopen(RWopsAlphabetFilename, "w");
59  SDLTest_AssertCheck(handle != NULL, "Verify creation of file '%s' returned non NULL handle", RWopsAlphabetFilename);
60  if (handle == NULL) return;
61 
62  /* Write alphabet text into it */
64  writtenLen = fwrite(RWopsAlphabetString, 1, fileLen, handle);
65  SDLTest_AssertCheck(fileLen == writtenLen, "Verify number of written bytes, expected %i, got %i", (int) fileLen, (int) writtenLen);
66  result = fclose(handle);
67  SDLTest_AssertCheck(result == 0, "Verify result from fclose, expected 0, got %i", result);
68 
69  SDLTest_AssertPass("Creation of test file completed");
70 }
71 
72 void
73 RWopsTearDown(void *arg)
74 {
75  int result;
76 
77  /* Remove the created files to clean up; ignore errors for write filename */
78  result = remove(RWopsReadTestFilename);
79  SDLTest_AssertCheck(result == 0, "Verify result from remove(%s), expected 0, got %i", RWopsReadTestFilename, result);
80  remove(RWopsWriteTestFilename);
81  result = remove(RWopsAlphabetFilename);
82  SDLTest_AssertCheck(result == 0, "Verify result from remove(%s), expected 0, got %i", RWopsAlphabetFilename, result);
83 
84  SDLTest_AssertPass("Cleanup of test files completed");
85 }
86 
87 /**
88  * @brief Makes sure parameters work properly. Local helper function.
89  *
90  * \sa
91  * http://wiki.libsdl.org/moin.cgi/SDL_RWseek
92  * http://wiki.libsdl.org/moin.cgi/SDL_RWread
93  */
94 void
96 {
97  char buf[sizeof(RWopsHelloWorldTestString)];
98  Sint64 i;
99  size_t s;
100  int seekPos = SDLTest_RandomIntegerInRange(4, 8);
101 
102  /* Clear buffer */
103  SDL_zero(buf);
104 
105  /* Set to start. */
106  i = SDL_RWseek(rw, 0, RW_SEEK_SET );
107  SDLTest_AssertPass("Call to SDL_RWseek succeeded");
108  SDLTest_AssertCheck(i == (Sint64)0, "Verify seek to 0 with SDL_RWseek (RW_SEEK_SET), expected 0, got %"SDL_PRIs64, i);
109 
110  /* Test write. */
112  SDLTest_AssertPass("Call to SDL_RWwrite succeeded");
113  if (write) {
114  SDLTest_AssertCheck(s == (size_t)1, "Verify result of writing one byte with SDL_RWwrite, expected 1, got %i", (int) s);
115  }
116  else {
117  SDLTest_AssertCheck(s == (size_t)0, "Verify result of writing with SDL_RWwrite, expected: 0, got %i", (int) s);
118  }
119 
120  /* Test seek to random position */
121  i = SDL_RWseek( rw, seekPos, RW_SEEK_SET );
122  SDLTest_AssertPass("Call to SDL_RWseek succeeded");
123  SDLTest_AssertCheck(i == (Sint64)seekPos, "Verify seek to %i with SDL_RWseek (RW_SEEK_SET), expected %i, got %"SDL_PRIs64, seekPos, seekPos, i);
124 
125  /* Test seek back to start */
126  i = SDL_RWseek(rw, 0, RW_SEEK_SET );
127  SDLTest_AssertPass("Call to SDL_RWseek succeeded");
128  SDLTest_AssertCheck(i == (Sint64)0, "Verify seek to 0 with SDL_RWseek (RW_SEEK_SET), expected 0, got %"SDL_PRIs64, i);
129 
130  /* Test read */
131  s = SDL_RWread( rw, buf, 1, sizeof(RWopsHelloWorldTestString)-1 );
132  SDLTest_AssertPass("Call to SDL_RWread succeeded");
134  s == (size_t)(sizeof(RWopsHelloWorldTestString)-1),
135  "Verify result from SDL_RWread, expected %i, got %i",
136  (int) (sizeof(RWopsHelloWorldTestString)-1),
137  (int) s);
140  "Verify read bytes match expected string, expected '%s', got '%s'", RWopsHelloWorldTestString, buf);
141 
142  /* More seek tests. */
143  i = SDL_RWseek( rw, -4, RW_SEEK_CUR );
144  SDLTest_AssertPass("Call to SDL_RWseek(...,-4,RW_SEEK_CUR) succeeded");
146  i == (Sint64)(sizeof(RWopsHelloWorldTestString)-5),
147  "Verify seek to -4 with SDL_RWseek (RW_SEEK_CUR), expected %i, got %i",
148  (int) (sizeof(RWopsHelloWorldTestString)-5),
149  (int) i);
150 
151  i = SDL_RWseek( rw, -1, RW_SEEK_END );
152  SDLTest_AssertPass("Call to SDL_RWseek(...,-1,RW_SEEK_END) succeeded");
154  i == (Sint64)(sizeof(RWopsHelloWorldTestString)-2),
155  "Verify seek to -1 with SDL_RWseek (RW_SEEK_END), expected %i, got %i",
156  (int) (sizeof(RWopsHelloWorldTestString)-2),
157  (int) i);
158 
159  /* Invalid whence seek */
160  i = SDL_RWseek( rw, 0, 999 );
161  SDLTest_AssertPass("Call to SDL_RWseek(...,0,invalid_whence) succeeded");
163  i == (Sint64)(-1),
164  "Verify seek with SDL_RWseek (invalid_whence); expected: -1, got %i",
165  (int) i);
166 }
167 
168 /* !
169  * Negative test for SDL_RWFromFile parameters
170  *
171  * \sa http://wiki.libsdl.org/moin.cgi/SDL_RWFromFile
172  *
173  */
174 int
176 {
177  SDL_RWops *rwops;
178 
179  /* These should all fail. */
180  rwops = SDL_RWFromFile(NULL, NULL);
181  SDLTest_AssertPass("Call to SDL_RWFromFile(NULL, NULL) succeeded");
182  SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromFile(NULL, NULL) returns NULL");
183 
184  rwops = SDL_RWFromFile(NULL, "ab+");
185  SDLTest_AssertPass("Call to SDL_RWFromFile(NULL, \"ab+\") succeeded");
186  SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromFile(NULL, \"ab+\") returns NULL");
187 
188  rwops = SDL_RWFromFile(NULL, "sldfkjsldkfj");
189  SDLTest_AssertPass("Call to SDL_RWFromFile(NULL, \"sldfkjsldkfj\") succeeded");
190  SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromFile(NULL, \"sldfkjsldkfj\") returns NULL");
191 
192  rwops = SDL_RWFromFile("something", "");
193  SDLTest_AssertPass("Call to SDL_RWFromFile(\"something\", \"\") succeeded");
194  SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromFile(\"something\", \"\") returns NULL");
195 
196  rwops = SDL_RWFromFile("something", NULL);
197  SDLTest_AssertPass("Call to SDL_RWFromFile(\"something\", NULL) succeeded");
198  SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromFile(\"something\", NULL) returns NULL");
199 
200  rwops = SDL_RWFromMem((void *)NULL, 10);
201  SDLTest_AssertPass("Call to SDL_RWFromMem(NULL, 10) succeeded");
202  SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromMem(NULL, 10) returns NULL");
203 
204  rwops = SDL_RWFromMem((void *)RWopsAlphabetString, 0);
205  SDLTest_AssertPass("Call to SDL_RWFromMem(data, 0) succeeded");
206  SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromMem(data, 0) returns NULL");
207 
208  rwops = SDL_RWFromConstMem((const void *)RWopsAlphabetString, 0);
209  SDLTest_AssertPass("Call to SDL_RWFromConstMem(data, 0) succeeded");
210  SDLTest_AssertCheck(rwops == NULL, "Verify SDL_RWFromConstMem(data, 0) returns NULL");
211 
212  return TEST_COMPLETED;
213 }
214 
215 /**
216  * @brief Tests opening from memory.
217  *
218  * \sa http://wiki.libsdl.org/moin.cgi/SDL_RWFromMem
219  * \sa http://wiki.libsdl.org/moin.cgi/SDL_RWClose
220  */
221 int
223 {
224  char mem[sizeof(RWopsHelloWorldTestString)];
225  SDL_RWops *rw;
226  int result;
227 
228  /* Clear buffer */
229  SDL_zero(mem);
230 
231  /* Open */
232  rw = SDL_RWFromMem(mem, sizeof(RWopsHelloWorldTestString)-1);
233  SDLTest_AssertPass("Call to SDL_RWFromMem() succeeded");
234  SDLTest_AssertCheck(rw != NULL, "Verify opening memory with SDL_RWFromMem does not return NULL");
235 
236  /* Bail out if NULL */
237  if (rw == NULL) return TEST_ABORTED;
238 
239  /* Check type */
240  SDLTest_AssertCheck(rw->type == SDL_RWOPS_MEMORY, "Verify RWops type is SDL_RWOPS_MEMORY; expected: %d, got: %d", SDL_RWOPS_MEMORY, rw->type);
241 
242  /* Run generic tests */
244 
245  /* Close */
246  result = SDL_RWclose(rw);
247  SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
248  SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
249 
250  return TEST_COMPLETED;
251 }
252 
253 
254 /**
255  * @brief Tests opening from memory.
256  *
257  * \sa
258  * http://wiki.libsdl.org/moin.cgi/SDL_RWFromConstMem
259  * http://wiki.libsdl.org/moin.cgi/SDL_RWClose
260  */
261 int
263 {
264  SDL_RWops *rw;
265  int result;
266 
267  /* Open handle */
269  SDLTest_AssertPass("Call to SDL_RWFromConstMem() succeeded");
270  SDLTest_AssertCheck(rw != NULL, "Verify opening memory with SDL_RWFromConstMem does not return NULL");
271 
272  /* Bail out if NULL */
273  if (rw == NULL) return TEST_ABORTED;
274 
275  /* Check type */
276  SDLTest_AssertCheck(rw->type == SDL_RWOPS_MEMORY_RO, "Verify RWops type is SDL_RWOPS_MEMORY_RO; expected: %d, got: %d", SDL_RWOPS_MEMORY_RO, rw->type);
277 
278  /* Run generic tests */
280 
281  /* Close handle */
282  result = SDL_RWclose(rw);
283  SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
284  SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
285 
286  return TEST_COMPLETED;
287 }
288 
289 
290 /**
291  * @brief Tests reading from file.
292  *
293  * \sa
294  * http://wiki.libsdl.org/moin.cgi/SDL_RWFromFile
295  * http://wiki.libsdl.org/moin.cgi/SDL_RWClose
296  */
297 int
299 {
300  SDL_RWops *rw;
301  int result;
302 
303  /* Read test. */
305  SDLTest_AssertPass("Call to SDL_RWFromFile(..,\"r\") succeeded");
306  SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_RWFromFile in read mode does not return NULL");
307 
308  /* Bail out if NULL */
309  if (rw == NULL) return TEST_ABORTED;
310 
311  /* Check type */
312 #if defined(__ANDROID__)
314  rw->type == SDL_RWOPS_STDFILE || rw->type == SDL_RWOPS_JNIFILE,
315  "Verify RWops type is SDL_RWOPS_STDFILE or SDL_RWOPS_JNIFILE; expected: %d|%d, got: %d", SDL_RWOPS_STDFILE, SDL_RWOPS_JNIFILE, rw->type);
316 #elif defined(__WIN32__)
318  rw->type == SDL_RWOPS_WINFILE,
319  "Verify RWops type is SDL_RWOPS_WINFILE; expected: %d, got: %d", SDL_RWOPS_WINFILE, rw->type);
320 #else
322  rw->type == SDL_RWOPS_STDFILE,
323  "Verify RWops type is SDL_RWOPS_STDFILE; expected: %d, got: %d", SDL_RWOPS_STDFILE, rw->type);
324 #endif
325 
326  /* Run generic tests */
328 
329  /* Close handle */
330  result = SDL_RWclose(rw);
331  SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
332  SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
333 
334  return TEST_COMPLETED;
335 }
336 
337 /**
338  * @brief Tests writing from file.
339  *
340  * \sa
341  * http://wiki.libsdl.org/moin.cgi/SDL_RWFromFile
342  * http://wiki.libsdl.org/moin.cgi/SDL_RWClose
343  */
344 int
346 {
347  SDL_RWops *rw;
348  int result;
349 
350  /* Write test. */
352  SDLTest_AssertPass("Call to SDL_RWFromFile(..,\"w+\") succeeded");
353  SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_RWFromFile in write mode does not return NULL");
354 
355  /* Bail out if NULL */
356  if (rw == NULL) return TEST_ABORTED;
357 
358  /* Check type */
359 #if defined(__ANDROID__)
361  rw->type == SDL_RWOPS_STDFILE || rw->type == SDL_RWOPS_JNIFILE,
362  "Verify RWops type is SDL_RWOPS_STDFILE or SDL_RWOPS_JNIFILE; expected: %d|%d, got: %d", SDL_RWOPS_STDFILE, SDL_RWOPS_JNIFILE, rw->type);
363 #elif defined(__WIN32__)
365  rw->type == SDL_RWOPS_WINFILE,
366  "Verify RWops type is SDL_RWOPS_WINFILE; expected: %d, got: %d", SDL_RWOPS_WINFILE, rw->type);
367 #else
369  rw->type == SDL_RWOPS_STDFILE,
370  "Verify RWops type is SDL_RWOPS_STDFILE; expected: %d, got: %d", SDL_RWOPS_STDFILE, rw->type);
371 #endif
372 
373  /* Run generic tests */
375 
376  /* Close handle */
377  result = SDL_RWclose(rw);
378  SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
379  SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
380 
381  return TEST_COMPLETED;
382 }
383 
384 
385 /**
386  * @brief Tests reading from file handle
387  *
388  * \sa
389  * http://wiki.libsdl.org/moin.cgi/SDL_RWFromFP
390  * http://wiki.libsdl.org/moin.cgi/SDL_RWClose
391  *
392  */
393 int
395 {
396  FILE *fp;
397  SDL_RWops *rw;
398  int result;
399 
400  /* Run read tests. */
401  fp = fopen(RWopsReadTestFilename, "r");
402  SDLTest_AssertCheck(fp != NULL, "Verify handle from opening file '%s' in read mode is not NULL", RWopsReadTestFilename);
403 
404  /* Bail out if NULL */
405  if (fp == NULL) return TEST_ABORTED;
406 
407  /* Open */
408  rw = SDL_RWFromFP( fp, SDL_TRUE );
409  SDLTest_AssertPass("Call to SDL_RWFromFP() succeeded");
410  SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_RWFromFP in read mode does not return NULL");
411 
412  /* Bail out if NULL */
413  if (rw == NULL) {
414  fclose(fp);
415  return TEST_ABORTED;
416  }
417 
418  /* Check type */
420  rw->type == SDL_RWOPS_STDFILE,
421  "Verify RWops type is SDL_RWOPS_STDFILE; expected: %d, got: %d", SDL_RWOPS_STDFILE, rw->type);
422 
423  /* Run generic tests */
425 
426  /* Close handle - does fclose() */
427  result = SDL_RWclose(rw);
428  SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
429  SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
430 
431  return TEST_COMPLETED;
432 }
433 
434 
435 /**
436  * @brief Tests writing to file handle
437  *
438  * \sa
439  * http://wiki.libsdl.org/moin.cgi/SDL_RWFromFP
440  * http://wiki.libsdl.org/moin.cgi/SDL_RWClose
441  *
442  */
443 int
445 {
446  FILE *fp;
447  SDL_RWops *rw;
448  int result;
449 
450  /* Run write tests. */
451  fp = fopen(RWopsWriteTestFilename, "w+");
452  SDLTest_AssertCheck(fp != NULL, "Verify handle from opening file '%s' in write mode is not NULL", RWopsWriteTestFilename);
453 
454  /* Bail out if NULL */
455  if (fp == NULL) return TEST_ABORTED;
456 
457  /* Open */
458  rw = SDL_RWFromFP( fp, SDL_TRUE );
459  SDLTest_AssertPass("Call to SDL_RWFromFP() succeeded");
460  SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_RWFromFP in write mode does not return NULL");
461 
462  /* Bail out if NULL */
463  if (rw == NULL) {
464  fclose(fp);
465  return TEST_ABORTED;
466  }
467 
468  /* Check type */
470  rw->type == SDL_RWOPS_STDFILE,
471  "Verify RWops type is SDL_RWOPS_STDFILE; expected: %d, got: %d", SDL_RWOPS_STDFILE, rw->type);
472 
473  /* Run generic tests */
475 
476  /* Close handle - does fclose() */
477  result = SDL_RWclose(rw);
478  SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
479  SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
480 
481  return TEST_COMPLETED;
482 }
483 
484 /**
485  * @brief Tests alloc and free RW context.
486  *
487  * \sa http://wiki.libsdl.org/moin.cgi/SDL_AllocRW
488  * \sa http://wiki.libsdl.org/moin.cgi/SDL_FreeRW
489  */
490 int
492 {
493  /* Allocate context */
494  SDL_RWops *rw = SDL_AllocRW();
495  SDLTest_AssertPass("Call to SDL_AllocRW() succeeded");
496  SDLTest_AssertCheck(rw != NULL, "Validate result from SDL_AllocRW() is not NULL");
497  if (rw==NULL) return TEST_ABORTED;
498 
499  /* Check type */
501  rw->type == SDL_RWOPS_UNKNOWN,
502  "Verify RWops type is SDL_RWOPS_UNKNOWN; expected: %d, got: %d", SDL_RWOPS_UNKNOWN, rw->type);
503 
504  /* Free context again */
505  SDL_FreeRW(rw);
506  SDLTest_AssertPass("Call to SDL_FreeRW() succeeded");
507 
508  return TEST_COMPLETED;
509 }
510 
511 /**
512  * @brief Compare memory and file reads
513  *
514  * \sa http://wiki.libsdl.org/moin.cgi/SDL_RWFromMem
515  * \sa http://wiki.libsdl.org/moin.cgi/SDL_RWFromFile
516  */
517 int
519 {
520  int slen = 26;
521  char buffer_file[27];
522  char buffer_mem[27];
523  size_t rv_file;
524  size_t rv_mem;
525  Uint64 sv_file;
526  Uint64 sv_mem;
527  SDL_RWops* rwops_file;
528  SDL_RWops* rwops_mem;
529  int size;
530  int result;
531 
532 
533  for (size=5; size<10; size++)
534  {
535  /* Terminate buffer */
536  buffer_file[slen] = 0;
537  buffer_mem[slen] = 0;
538 
539  /* Read/seek from memory */
540  rwops_mem = SDL_RWFromMem((void *)RWopsAlphabetString, slen);
541  SDLTest_AssertPass("Call to SDL_RWFromMem()");
542  rv_mem = SDL_RWread(rwops_mem, buffer_mem, size, 6);
543  SDLTest_AssertPass("Call to SDL_RWread(mem, size=%d)", size);
544  sv_mem = SDL_RWseek(rwops_mem, 0, SEEK_END);
545  SDLTest_AssertPass("Call to SDL_RWseek(mem,SEEK_END)");
546  result = SDL_RWclose(rwops_mem);
547  SDLTest_AssertPass("Call to SDL_RWclose(mem)");
548  SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
549 
550  /* Read/see from file */
551  rwops_file = SDL_RWFromFile(RWopsAlphabetFilename, "r");
552  SDLTest_AssertPass("Call to SDL_RWFromFile()");
553  rv_file = SDL_RWread(rwops_file, buffer_file, size, 6);
554  SDLTest_AssertPass("Call to SDL_RWread(file, size=%d)", size);
555  sv_file = SDL_RWseek(rwops_file, 0, SEEK_END);
556  SDLTest_AssertPass("Call to SDL_RWseek(file,SEEK_END)");
557  result = SDL_RWclose(rwops_file);
558  SDLTest_AssertPass("Call to SDL_RWclose(file)");
559  SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
560 
561  /* Compare */
562  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);
563  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);
564  SDLTest_AssertCheck(buffer_mem[slen] == 0, "Verify mem buffer termination; expected: 0, got: %d", buffer_mem[slen]);
565  SDLTest_AssertCheck(buffer_file[slen] == 0, "Verify file buffer termination; expected: 0, got: %d", buffer_file[slen]);
567  SDL_strncmp(buffer_mem, RWopsAlphabetString, slen) == 0,
568  "Verify mem buffer contain alphabet string; expected: %s, got: %s", RWopsAlphabetString, buffer_mem);
570  SDL_strncmp(buffer_file, RWopsAlphabetString, slen) == 0,
571  "Verify file buffer contain alphabet string; expected: %s, got: %s", RWopsAlphabetString, buffer_file);
572  }
573 
574  return TEST_COMPLETED;
575 }
576 
577 /**
578  * @brief Tests writing and reading from file using endian aware functions.
579  *
580  * \sa
581  * http://wiki.libsdl.org/moin.cgi/SDL_RWFromFile
582  * http://wiki.libsdl.org/moin.cgi/SDL_RWClose
583  * http://wiki.libsdl.org/moin.cgi/SDL_ReadBE16
584  * http://wiki.libsdl.org/moin.cgi/SDL_WriteBE16
585  */
586 int
588 {
589  SDL_RWops *rw;
590  Sint64 result;
591  int mode;
592  size_t objectsWritten;
593  Uint16 BE16value;
594  Uint32 BE32value;
595  Uint64 BE64value;
596  Uint16 LE16value;
597  Uint32 LE32value;
598  Uint64 LE64value;
599  Uint16 BE16test;
600  Uint32 BE32test;
601  Uint64 BE64test;
602  Uint16 LE16test;
603  Uint32 LE32test;
604  Uint64 LE64test;
605  int cresult;
606 
607  for (mode = 0; mode < 3; mode++) {
608 
609  /* Create test data */
610  switch (mode) {
611  case 0:
612  SDLTest_Log("All 0 values");
613  BE16value = 0;
614  BE32value = 0;
615  BE64value = 0;
616  LE16value = 0;
617  LE32value = 0;
618  LE64value = 0;
619  break;
620  case 1:
621  SDLTest_Log("All 1 values");
622  BE16value = 1;
623  BE32value = 1;
624  BE64value = 1;
625  LE16value = 1;
626  LE32value = 1;
627  LE64value = 1;
628  break;
629  case 2:
630  SDLTest_Log("Random values");
631  BE16value = SDLTest_RandomUint16();
632  BE32value = SDLTest_RandomUint32();
633  BE64value = SDLTest_RandomUint64();
634  LE16value = SDLTest_RandomUint16();
635  LE32value = SDLTest_RandomUint32();
636  LE64value = SDLTest_RandomUint64();
637  break;
638  }
639 
640  /* Write test. */
642  SDLTest_AssertPass("Call to SDL_RWFromFile(..,\"w+\")");
643  SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_RWFromFile in write mode does not return NULL");
644 
645  /* Bail out if NULL */
646  if (rw == NULL) return TEST_ABORTED;
647 
648  /* Write test data */
649  objectsWritten = SDL_WriteBE16(rw, BE16value);
650  SDLTest_AssertPass("Call to SDL_WriteBE16()");
651  SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", (int) objectsWritten);
652  objectsWritten = SDL_WriteBE32(rw, BE32value);
653  SDLTest_AssertPass("Call to SDL_WriteBE32()");
654  SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", (int) objectsWritten);
655  objectsWritten = SDL_WriteBE64(rw, BE64value);
656  SDLTest_AssertPass("Call to SDL_WriteBE64()");
657  SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", (int) objectsWritten);
658  objectsWritten = SDL_WriteLE16(rw, LE16value);
659  SDLTest_AssertPass("Call to SDL_WriteLE16()");
660  SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", (int) objectsWritten);
661  objectsWritten = SDL_WriteLE32(rw, LE32value);
662  SDLTest_AssertPass("Call to SDL_WriteLE32()");
663  SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", (int) objectsWritten);
664  objectsWritten = SDL_WriteLE64(rw, LE64value);
665  SDLTest_AssertPass("Call to SDL_WriteLE64()");
666  SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", (int) objectsWritten);
667 
668  /* Test seek to start */
669  result = SDL_RWseek( rw, 0, RW_SEEK_SET );
670  SDLTest_AssertPass("Call to SDL_RWseek succeeded");
671  SDLTest_AssertCheck(result == 0, "Verify result from position 0 with SDL_RWseek, expected 0, got %i", (int) result);
672 
673  /* Read test data */
674  BE16test = SDL_ReadBE16(rw);
675  SDLTest_AssertPass("Call to SDL_ReadBE16()");
676  SDLTest_AssertCheck(BE16test == BE16value, "Validate return value from SDL_ReadBE16, expected: %hu, got: %hu", BE16value, BE16test);
677  BE32test = SDL_ReadBE32(rw);
678  SDLTest_AssertPass("Call to SDL_ReadBE32()");
679  SDLTest_AssertCheck(BE32test == BE32value, "Validate return value from SDL_ReadBE32, expected: %u, got: %u", BE32value, BE32test);
680  BE64test = SDL_ReadBE64(rw);
681  SDLTest_AssertPass("Call to SDL_ReadBE64()");
682  SDLTest_AssertCheck(BE64test == BE64value, "Validate return value from SDL_ReadBE64, expected: %"SDL_PRIu64", got: %"SDL_PRIu64, BE64value, BE64test);
683  LE16test = SDL_ReadLE16(rw);
684  SDLTest_AssertPass("Call to SDL_ReadLE16()");
685  SDLTest_AssertCheck(LE16test == LE16value, "Validate return value from SDL_ReadLE16, expected: %hu, got: %hu", LE16value, LE16test);
686  LE32test = SDL_ReadLE32(rw);
687  SDLTest_AssertPass("Call to SDL_ReadLE32()");
688  SDLTest_AssertCheck(LE32test == LE32value, "Validate return value from SDL_ReadLE32, expected: %u, got: %u", LE32value, LE32test);
689  LE64test = SDL_ReadLE64(rw);
690  SDLTest_AssertPass("Call to SDL_ReadLE64()");
691  SDLTest_AssertCheck(LE64test == LE64value, "Validate return value from SDL_ReadLE64, expected: %"SDL_PRIu64", got: %"SDL_PRIu64, LE64value, LE64test);
692 
693  /* Close handle */
694  cresult = SDL_RWclose(rw);
695  SDLTest_AssertPass("Call to SDL_RWclose() succeeded");
696  SDLTest_AssertCheck(cresult == 0, "Verify result value is 0; got: %d", cresult);
697  }
698 
699  return TEST_COMPLETED;
700 }
701 
702 
703 /* ================= Test References ================== */
704 
705 /* RWops test cases */
707  { (SDLTest_TestCaseFp)rwops_testParamNegative, "rwops_testParamNegative", "Negative test for SDL_RWFromFile parameters", TEST_ENABLED };
708 
710  { (SDLTest_TestCaseFp)rwops_testMem, "rwops_testMem", "Tests opening from memory", TEST_ENABLED };
711 
713  { (SDLTest_TestCaseFp)rwops_testConstMem, "rwops_testConstMem", "Tests opening from (const) memory", TEST_ENABLED };
714 
716  { (SDLTest_TestCaseFp)rwops_testFileRead, "rwops_testFileRead", "Tests reading from a file", TEST_ENABLED };
717 
719  { (SDLTest_TestCaseFp)rwops_testFileWrite, "rwops_testFileWrite", "Test writing to a file", TEST_ENABLED };
720 
722  { (SDLTest_TestCaseFp)rwops_testFPRead, "rwops_testFPRead", "Test reading from file pointer", TEST_ENABLED };
723 
725  { (SDLTest_TestCaseFp)rwops_testFPWrite, "rwops_testFPWrite", "Test writing to file pointer", TEST_ENABLED };
726 
728  { (SDLTest_TestCaseFp)rwops_testAllocFree, "rwops_testAllocFree", "Test alloc and free of RW context", TEST_ENABLED };
729 
731  { (SDLTest_TestCaseFp)rwops_testFileWriteReadEndian, "rwops_testFileWriteReadEndian", "Test writing and reading via the Endian aware functions", TEST_ENABLED };
732 
734  { (SDLTest_TestCaseFp)rwops_testCompareRWFromMemWithRWFromFile, "rwops_testCompareRWFromMemWithRWFromFile", "Compare RWFromMem and RWFromFile RWops for read and seek", TEST_ENABLED };
735 
736 /* Sequence of RWops test cases */
740 };
741 
742 /* RWops test suite (global) */
744  "RWops",
745  RWopsSetUp,
746  rwopsTests,
748 };
int rwops_testFPWrite(void)
Tests writing to file handle.
SDLTest_TestSuiteReference rwopsTestSuite
#define TEST_ABORTED
GLuint64EXT * result
GLdouble s
Definition: SDL_opengl.h:2063
static const SDLTest_TestCaseReference rwopsTest10
static const SDLTest_TestCaseReference rwopsTest3
Sint32 SDLTest_RandomIntegerInRange(Sint32 min, Sint32 max)
#define SDL_RWwrite(ctx, ptr, size, n)
Definition: SDL_rwops.h:188
const char * RWopsAlphabetFilename
void SDLTest_AssertPass(SDL_PRINTF_FORMAT_STRING const char *assertDescription,...) SDL_PRINTF_VARARG_FUNC(1)
Explicitly pass without checking an assertion condition. Updates assertion counter.
static const SDLTest_TestCaseReference * rwopsTests[]
#define SDL_RWOPS_WINFILE
Definition: SDL_rwops.h:43
#define SDL_WriteBE16
#define SDL_WriteLE16
#define SDL_ReadLE32
#define SDL_RWread(ctx, ptr, size, n)
Definition: SDL_rwops.h:187
#define SDL_RWOPS_UNKNOWN
Definition: SDL_rwops.h:42
Uint32 SDLTest_RandomUint32(void)
#define RW_SEEK_END
Definition: SDL_rwops.h:176
uint32_t Uint32
Definition: SDL_stdinc.h:181
#define SDL_AllocRW
#define SDL_strncmp
uint64_t Uint64
Definition: SDL_stdinc.h:194
static const SDLTest_TestCaseReference rwopsTest2
Uint32 type
Definition: SDL_rwops.h:93
static const SDLTest_TestCaseReference rwopsTest9
#define SDL_ReadBE32
static const SDLTest_TestCaseReference rwopsTest4
#define SDL_RWseek(ctx, offset, whence)
Definition: SDL_rwops.h:185
#define SDL_ReadLE16
int rwops_testParamNegative(void)
#define SDL_WriteBE64
int(* SDLTest_TestCaseFp)(void *arg)
#define SDL_RWFromFile
static const SDLTest_TestCaseReference rwopsTest1
#define SDL_PRIs64
Definition: SDL_stdinc.h:205
EGLImageKHR EGLint EGLint * handle
Definition: eglext.h:937
static const char RWopsHelloWorldTestString[]
void RWopsSetUp(void *arg)
int SDLTest_AssertCheck(int assertCondition, SDL_PRINTF_FORMAT_STRING const char *assertDescription,...) SDL_PRINTF_VARARG_FUNC(2)
Assert for test cases that logs but does not break execution flow on failures. Updates assertion coun...
Uint64 SDLTest_RandomUint64(void)
static const SDLTest_TestCaseReference rwopsTest6
#define SDL_memcmp
int rwops_testMem(void)
Tests opening from memory.
#define SDL_ReadLE64
GLenum mode
#define SDL_RWFromMem
static const SDLTest_TestCaseReference rwopsTest7
#define SDL_zero(x)
Definition: SDL_stdinc.h:416
const char * RWopsWriteTestFilename
GLenum GLuint GLenum GLsizei const GLchar * buf
#define TEST_COMPLETED
static const char RWopsAlphabetString[]
GLsizeiptr size
#define TEST_ENABLED
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
const char * RWopsReadTestFilename
#define NULL
Definition: begin_code.h:164
#define SDL_ReadBE16
#define SDL_RWclose(ctx)
Definition: SDL_rwops.h:189
int rwops_testFPRead(void)
Tests reading from file handle.
#define SDL_RWOPS_MEMORY
Definition: SDL_rwops.h:46
void RWopsTearDown(void *arg)
#define SDL_RWOPS_STDFILE
Definition: SDL_rwops.h:44
#define SDL_RWOPS_MEMORY_RO
Definition: SDL_rwops.h:47
#define SDL_strlen
int rwops_testConstMem(void)
Tests opening from memory.
Uint16 SDLTest_RandomUint16(void)
#define SDL_FreeRW
static const SDLTest_TestCaseReference rwopsTest5
#define SDL_PRIu64
Definition: SDL_stdinc.h:216
int rwops_testFileRead(void)
Tests reading from file.
#define SDL_WriteLE32
#define RW_SEEK_SET
Definition: SDL_rwops.h:174
uint16_t Uint16
Definition: SDL_stdinc.h:169
void SDLTest_Log(SDL_PRINTF_FORMAT_STRING const char *fmt,...) SDL_PRINTF_VARARG_FUNC(1)
Prints given message with a timestamp in the TEST category and INFO priority.
Definition: SDL_test_log.c:85
#define SDL_RWFromFP
int rwops_testAllocFree(void)
Tests alloc and free RW context.
#define SDL_RWFromConstMem
#define RW_SEEK_CUR
Definition: SDL_rwops.h:175
void _testGenericRWopsValidations(SDL_RWops *rw, int write)
Makes sure parameters work properly. Local helper function.
int64_t Sint64
Definition: SDL_stdinc.h:188
int rwops_testCompareRWFromMemWithRWFromFile(void)
Compare memory and file reads.
#define SDL_ReadBE64
#define SDL_WriteLE64
#define SDL_WriteBE32
static const char RWopsHelloWorldCompString[]
int rwops_testFileWrite(void)
Tests writing from file.
static const SDLTest_TestCaseReference rwopsTest8
int rwops_testFileWriteReadEndian(void)
Tests writing and reading from file using endian aware functions.
#define SDL_RWOPS_JNIFILE
Definition: SDL_rwops.h:45