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