2015-06-21 23:33:46 +08:00
/**
* Automated SDL_RWops 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 */
2022-10-06 07:09:32 +08:00
# if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
2022-12-01 04:51:59 +08:00
# define _CRT_SECURE_NO_WARNINGS
2022-10-06 07:09:32 +08:00
# endif
2015-06-21 23:33:46 +08:00
# include <stdio.h>
2022-11-27 12:43:38 +08:00
# include <SDL3/SDL.h>
# include <SDL3/SDL_test.h>
2015-06-21 23:33:46 +08:00
/* ================= Test Case Implementation ================== */
2022-12-01 04:51:59 +08:00
const char * RWopsReadTestFilename = " rwops_read " ;
const char * RWopsWriteTestFilename = " rwops_write " ;
const char * RWopsAlphabetFilename = " rwops_alphabet " ;
2015-06-21 23:33:46 +08:00
static const char RWopsHelloWorldTestString [ ] = " Hello World! " ;
static const char RWopsHelloWorldCompString [ ] = " Hello World! " ;
static const char RWopsAlphabetString [ ] = " ABCDEFGHIJKLMNOPQRSTUVWXYZ " ;
/* Fixture */
2022-12-01 04:51:59 +08:00
void RWopsSetUp ( void * arg )
2015-06-21 23:33:46 +08:00
{
2017-08-30 03:52:49 +08:00
size_t fileLen ;
2015-06-21 23:33:46 +08:00
FILE * handle ;
2017-08-30 03:52:49 +08:00
size_t writtenLen ;
2015-06-21 23:33:46 +08:00
int result ;
/* Clean up from previous runs (if any); ignore errors */
2022-12-02 05:07:03 +08:00
( void ) remove ( RWopsReadTestFilename ) ;
( void ) remove ( RWopsWriteTestFilename ) ;
( void ) remove ( RWopsAlphabetFilename ) ;
2015-06-21 23:33:46 +08:00
/* Create a test file */
handle = fopen ( RWopsReadTestFilename , " w " ) ;
SDLTest_AssertCheck ( handle ! = NULL , " Verify creation of file '%s' returned non NULL handle " , RWopsReadTestFilename ) ;
2022-12-01 04:51:59 +08:00
if ( handle = = NULL ) {
return ;
2022-11-28 00:38:43 +08:00
}
2015-06-21 23:33:46 +08:00
/* Write some known text into it */
fileLen = SDL_strlen ( RWopsHelloWorldTestString ) ;
2017-08-30 03:52:49 +08:00
writtenLen = fwrite ( RWopsHelloWorldTestString , 1 , fileLen , handle ) ;
2022-12-01 04:51:59 +08:00
SDLTest_AssertCheck ( fileLen = = writtenLen , " Verify number of written bytes, expected %i, got %i " , ( int ) fileLen , ( int ) writtenLen ) ;
2015-06-21 23:33:46 +08:00
result = fclose ( handle ) ;
SDLTest_AssertCheck ( result = = 0 , " Verify result from fclose, expected 0, got %i " , result ) ;
/* Create a second test file */
handle = fopen ( RWopsAlphabetFilename , " w " ) ;
SDLTest_AssertCheck ( handle ! = NULL , " Verify creation of file '%s' returned non NULL handle " , RWopsAlphabetFilename ) ;
2022-12-01 04:51:59 +08:00
if ( handle = = NULL ) {
return ;
2022-11-28 00:38:43 +08:00
}
2015-06-21 23:33:46 +08:00
/* Write alphabet text into it */
fileLen = SDL_strlen ( RWopsAlphabetString ) ;
2017-08-30 03:52:49 +08:00
writtenLen = fwrite ( RWopsAlphabetString , 1 , fileLen , handle ) ;
2022-12-01 04:51:59 +08:00
SDLTest_AssertCheck ( fileLen = = writtenLen , " Verify number of written bytes, expected %i, got %i " , ( int ) fileLen , ( int ) writtenLen ) ;
2015-06-21 23:33:46 +08:00
result = fclose ( handle ) ;
SDLTest_AssertCheck ( result = = 0 , " Verify result from fclose, expected 0, got %i " , result ) ;
SDLTest_AssertPass ( " Creation of test file completed " ) ;
}
2022-12-01 04:51:59 +08:00
void RWopsTearDown ( void * arg )
2015-06-21 23:33:46 +08:00
{
int result ;
/* Remove the created files to clean up; ignore errors for write filename */
result = remove ( RWopsReadTestFilename ) ;
SDLTest_AssertCheck ( result = = 0 , " Verify result from remove(%s), expected 0, got %i " , RWopsReadTestFilename , result ) ;
2022-12-02 05:07:03 +08:00
( void ) remove ( RWopsWriteTestFilename ) ;
2015-06-21 23:33:46 +08:00
result = remove ( RWopsAlphabetFilename ) ;
SDLTest_AssertCheck ( result = = 0 , " Verify result from remove(%s), expected 0, got %i " , RWopsAlphabetFilename , result ) ;
SDLTest_AssertPass ( " Cleanup of test files completed " ) ;
}
/**
2023-02-02 07:21:53 +08:00
* \ brief Makes sure parameters work properly . Local helper function .
2015-06-21 23:33:46 +08:00
*
2023-02-02 07:21:53 +08:00
* \ sa SDL_RWseek
* \ sa SDL_RWread
2015-06-21 23:33:46 +08:00
*/
2022-12-30 05:58:16 +08:00
static void testGenericRWopsValidations ( SDL_RWops * rw , int write )
2015-06-21 23:33:46 +08:00
{
2022-12-01 04:51:59 +08:00
char buf [ sizeof ( RWopsHelloWorldTestString ) ] ;
Sint64 i ;
2022-12-15 04:42:54 +08:00
Sint64 s ;
2022-12-01 04:51:59 +08:00
int seekPos = SDLTest_RandomIntegerInRange ( 4 , 8 ) ;
/* Clear buffer */
SDL_zeroa ( buf ) ;
/* Set to start. */
2022-12-23 17:16:11 +08:00
i = SDL_RWseek ( rw , 0 , SDL_RW_SEEK_SET ) ;
2022-12-01 04:51:59 +08:00
SDLTest_AssertPass ( " Call to SDL_RWseek succeeded " ) ;
2022-12-23 17:16:11 +08:00
SDLTest_AssertCheck ( i = = ( Sint64 ) 0 , " Verify seek to 0 with SDL_RWseek (SDL_RW_SEEK_SET), expected 0, got % " SDL_PRIs64 , i ) ;
2022-12-01 04:51:59 +08:00
/* Test write. */
2022-12-15 04:42:54 +08:00
s = SDL_RWwrite ( rw , RWopsHelloWorldTestString , sizeof ( RWopsHelloWorldTestString ) - 1 ) ;
2022-12-01 04:51:59 +08:00
SDLTest_AssertPass ( " Call to SDL_RWwrite succeeded " ) ;
if ( write ) {
2022-12-15 04:42:54 +08:00
SDLTest_AssertCheck ( s = = sizeof ( RWopsHelloWorldTestString ) - 1 , " Verify result of writing one byte with SDL_RWwrite, expected 1, got %i " , ( int ) s ) ;
2022-12-01 04:51:59 +08:00
} else {
2022-12-16 17:24:19 +08:00
SDLTest_AssertCheck ( s = = - 1 , " Verify result of writing with SDL_RWwrite, expected: 0, got %i " , ( int ) s ) ;
2022-12-01 04:51:59 +08:00
}
/* Test seek to random position */
2022-12-23 17:16:11 +08:00
i = SDL_RWseek ( rw , seekPos , SDL_RW_SEEK_SET ) ;
2022-12-01 04:51:59 +08:00
SDLTest_AssertPass ( " Call to SDL_RWseek succeeded " ) ;
2022-12-23 17:16:11 +08:00
SDLTest_AssertCheck ( i = = ( Sint64 ) seekPos , " Verify seek to %i with SDL_RWseek (SDL_RW_SEEK_SET), expected %i, got % " SDL_PRIs64 , seekPos , seekPos , i ) ;
2022-12-01 04:51:59 +08:00
/* Test seek back to start */
2022-12-23 17:16:11 +08:00
i = SDL_RWseek ( rw , 0 , SDL_RW_SEEK_SET ) ;
2022-12-01 04:51:59 +08:00
SDLTest_AssertPass ( " Call to SDL_RWseek succeeded " ) ;
2022-12-23 17:16:11 +08:00
SDLTest_AssertCheck ( i = = ( Sint64 ) 0 , " Verify seek to 0 with SDL_RWseek (SDL_RW_SEEK_SET), expected 0, got % " SDL_PRIs64 , i ) ;
2022-12-01 04:51:59 +08:00
/* Test read */
2022-12-15 04:42:54 +08:00
s = SDL_RWread ( rw , buf , sizeof ( RWopsHelloWorldTestString ) - 1 ) ;
2022-12-01 04:51:59 +08:00
SDLTest_AssertPass ( " Call to SDL_RWread succeeded " ) ;
SDLTest_AssertCheck (
s = = ( size_t ) ( sizeof ( RWopsHelloWorldTestString ) - 1 ) ,
" Verify result from SDL_RWread, expected %i, got %i " ,
( int ) ( sizeof ( RWopsHelloWorldTestString ) - 1 ) ,
( int ) s ) ;
SDLTest_AssertCheck (
SDL_memcmp ( buf , RWopsHelloWorldTestString , sizeof ( RWopsHelloWorldTestString ) - 1 ) = = 0 ,
" Verify read bytes match expected string, expected '%s', got '%s' " , RWopsHelloWorldTestString , buf ) ;
/* More seek tests. */
2022-12-23 17:16:11 +08:00
i = SDL_RWseek ( rw , - 4 , SDL_RW_SEEK_CUR ) ;
SDLTest_AssertPass ( " Call to SDL_RWseek(...,-4,SDL_RW_SEEK_CUR) succeeded " ) ;
2022-12-01 04:51:59 +08:00
SDLTest_AssertCheck (
i = = ( Sint64 ) ( sizeof ( RWopsHelloWorldTestString ) - 5 ) ,
2022-12-23 17:16:11 +08:00
" Verify seek to -4 with SDL_RWseek (SDL_RW_SEEK_CUR), expected %i, got %i " ,
2022-12-01 04:51:59 +08:00
( int ) ( sizeof ( RWopsHelloWorldTestString ) - 5 ) ,
( int ) i ) ;
2022-12-23 17:16:11 +08:00
i = SDL_RWseek ( rw , - 1 , SDL_RW_SEEK_END ) ;
SDLTest_AssertPass ( " Call to SDL_RWseek(...,-1,SDL_RW_SEEK_END) succeeded " ) ;
2022-12-01 04:51:59 +08:00
SDLTest_AssertCheck (
i = = ( Sint64 ) ( sizeof ( RWopsHelloWorldTestString ) - 2 ) ,
2022-12-23 17:16:11 +08:00
" Verify seek to -1 with SDL_RWseek (SDL_RW_SEEK_END), expected %i, got %i " ,
2022-12-01 04:51:59 +08:00
( int ) ( sizeof ( RWopsHelloWorldTestString ) - 2 ) ,
( int ) i ) ;
/* Invalid whence seek */
i = SDL_RWseek ( rw , 0 , 999 ) ;
SDLTest_AssertPass ( " Call to SDL_RWseek(...,0,invalid_whence) succeeded " ) ;
SDLTest_AssertCheck (
i = = ( Sint64 ) ( - 1 ) ,
" Verify seek with SDL_RWseek (invalid_whence); expected: -1, got %i " ,
( int ) i ) ;
2015-06-21 23:33:46 +08:00
}
2023-02-02 07:21:53 +08:00
/**
2015-06-21 23:33:46 +08:00
* Negative test for SDL_RWFromFile parameters
*
2023-02-02 07:21:53 +08:00
* \ sa SDL_RWFromFile
2015-06-21 23:33:46 +08:00
*
*/
2022-12-01 04:51:59 +08:00
int rwops_testParamNegative ( void )
2015-06-21 23:33:46 +08:00
{
2022-12-01 04:51:59 +08:00
SDL_RWops * rwops ;
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
/* These should all fail. */
rwops = SDL_RWFromFile ( NULL , NULL ) ;
SDLTest_AssertPass ( " Call to SDL_RWFromFile(NULL, NULL) succeeded " ) ;
SDLTest_AssertCheck ( rwops = = NULL , " Verify SDL_RWFromFile(NULL, NULL) returns NULL " ) ;
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
rwops = SDL_RWFromFile ( NULL , " ab+ " ) ;
SDLTest_AssertPass ( " Call to SDL_RWFromFile(NULL, \" ab+ \" ) succeeded " ) ;
SDLTest_AssertCheck ( rwops = = NULL , " Verify SDL_RWFromFile(NULL, \" ab+ \" ) returns NULL " ) ;
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
rwops = SDL_RWFromFile ( NULL , " sldfkjsldkfj " ) ;
SDLTest_AssertPass ( " Call to SDL_RWFromFile(NULL, \" sldfkjsldkfj \" ) succeeded " ) ;
SDLTest_AssertCheck ( rwops = = NULL , " Verify SDL_RWFromFile(NULL, \" sldfkjsldkfj \" ) returns NULL " ) ;
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
rwops = SDL_RWFromFile ( " something " , " " ) ;
SDLTest_AssertPass ( " Call to SDL_RWFromFile( \" something \" , \" \" ) succeeded " ) ;
SDLTest_AssertCheck ( rwops = = NULL , " Verify SDL_RWFromFile( \" something \" , \" \" ) returns NULL " ) ;
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
rwops = SDL_RWFromFile ( " something " , NULL ) ;
SDLTest_AssertPass ( " Call to SDL_RWFromFile( \" something \" , NULL) succeeded " ) ;
SDLTest_AssertCheck ( rwops = = NULL , " Verify SDL_RWFromFile( \" something \" , NULL) returns NULL " ) ;
2015-06-21 23:33:46 +08:00
2022-12-02 05:07:03 +08:00
rwops = SDL_RWFromMem ( NULL , 10 ) ;
2022-12-01 04:51:59 +08:00
SDLTest_AssertPass ( " Call to SDL_RWFromMem(NULL, 10) succeeded " ) ;
SDLTest_AssertCheck ( rwops = = NULL , " Verify SDL_RWFromMem(NULL, 10) returns NULL " ) ;
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
rwops = SDL_RWFromMem ( ( void * ) RWopsAlphabetString , 0 ) ;
SDLTest_AssertPass ( " Call to SDL_RWFromMem(data, 0) succeeded " ) ;
SDLTest_AssertCheck ( rwops = = NULL , " Verify SDL_RWFromMem(data, 0) returns NULL " ) ;
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
rwops = SDL_RWFromConstMem ( ( const void * ) RWopsAlphabetString , 0 ) ;
SDLTest_AssertPass ( " Call to SDL_RWFromConstMem(data, 0) succeeded " ) ;
SDLTest_AssertCheck ( rwops = = NULL , " Verify SDL_RWFromConstMem(data, 0) returns NULL " ) ;
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
return TEST_COMPLETED ;
2015-06-21 23:33:46 +08:00
}
/**
2023-02-02 07:21:53 +08:00
* \ brief Tests opening from memory .
2015-06-21 23:33:46 +08:00
*
2023-02-02 07:21:53 +08:00
* \ sa SDL_RWFromMem
* \ sa SDL_RWClose
2015-06-21 23:33:46 +08:00
*/
2022-12-01 04:51:59 +08:00
int rwops_testMem ( void )
2015-06-21 23:33:46 +08:00
{
2022-12-01 04:51:59 +08:00
char mem [ sizeof ( RWopsHelloWorldTestString ) ] ;
SDL_RWops * rw ;
int result ;
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
/* Clear buffer */
SDL_zeroa ( mem ) ;
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
/* Open */
rw = SDL_RWFromMem ( mem , sizeof ( RWopsHelloWorldTestString ) - 1 ) ;
SDLTest_AssertPass ( " Call to SDL_RWFromMem() succeeded " ) ;
SDLTest_AssertCheck ( rw ! = NULL , " Verify opening memory with SDL_RWFromMem does not return NULL " ) ;
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
/* Bail out if NULL */
if ( rw = = NULL ) {
return TEST_ABORTED ;
}
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
/* Check type */
SDLTest_AssertCheck ( rw - > type = = SDL_RWOPS_MEMORY , " Verify RWops type is SDL_RWOPS_MEMORY; expected: %d, got: % " SDL_PRIu32 , SDL_RWOPS_MEMORY , rw - > type ) ;
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
/* Run generic tests */
2022-12-30 05:58:16 +08:00
testGenericRWopsValidations ( rw , 1 ) ;
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
/* Close */
result = SDL_RWclose ( rw ) ;
SDLTest_AssertPass ( " Call to SDL_RWclose() succeeded " ) ;
SDLTest_AssertCheck ( result = = 0 , " Verify result value is 0; got: %d " , result ) ;
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
return TEST_COMPLETED ;
2015-06-21 23:33:46 +08:00
}
/**
2023-02-02 07:21:53 +08:00
* \ brief Tests opening from memory .
2015-06-21 23:33:46 +08:00
*
2023-02-02 07:21:53 +08:00
* \ sa SDL_RWFromConstMem
* \ sa SDL_RWClose
2015-06-21 23:33:46 +08:00
*/
2022-12-01 04:51:59 +08:00
int rwops_testConstMem ( void )
2015-06-21 23:33:46 +08:00
{
2022-12-01 04:51:59 +08:00
SDL_RWops * rw ;
int result ;
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
/* Open handle */
rw = SDL_RWFromConstMem ( RWopsHelloWorldCompString , sizeof ( RWopsHelloWorldCompString ) - 1 ) ;
SDLTest_AssertPass ( " Call to SDL_RWFromConstMem() succeeded " ) ;
SDLTest_AssertCheck ( rw ! = NULL , " Verify opening memory with SDL_RWFromConstMem does not return NULL " ) ;
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
/* Bail out if NULL */
if ( rw = = NULL ) {
return TEST_ABORTED ;
}
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
/* Check type */
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 ) ;
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
/* Run generic tests */
2022-12-30 05:58:16 +08:00
testGenericRWopsValidations ( rw , 0 ) ;
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
/* Close handle */
result = SDL_RWclose ( rw ) ;
SDLTest_AssertPass ( " Call to SDL_RWclose() succeeded " ) ;
SDLTest_AssertCheck ( result = = 0 , " Verify result value is 0; got: %d " , result ) ;
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
return TEST_COMPLETED ;
2015-06-21 23:33:46 +08:00
}
/**
2023-02-02 07:21:53 +08:00
* \ brief Tests reading from file .
2015-06-21 23:33:46 +08:00
*
2023-02-02 07:21:53 +08:00
* \ sa SDL_RWFromFile
* \ sa SDL_RWClose
2015-06-21 23:33:46 +08:00
*/
2022-12-01 04:51:59 +08:00
int rwops_testFileRead ( void )
2015-06-21 23:33:46 +08:00
{
2022-12-01 04:51:59 +08:00
SDL_RWops * rw ;
int result ;
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
/* Read test. */
rw = SDL_RWFromFile ( RWopsReadTestFilename , " r " ) ;
SDLTest_AssertPass ( " Call to SDL_RWFromFile(.., \" r \" ) succeeded " ) ;
SDLTest_AssertCheck ( rw ! = NULL , " Verify opening file with SDL_RWFromFile in read mode does not return NULL " ) ;
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
/* Bail out if NULL */
if ( rw = = NULL ) {
return TEST_ABORTED ;
}
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
/* Check type */
2015-06-21 23:33:46 +08:00
# if defined(__ANDROID__)
2022-12-01 04:51:59 +08:00
SDLTest_AssertCheck (
rw - > type = = SDL_RWOPS_STDFILE | | rw - > type = = SDL_RWOPS_JNIFILE ,
" Verify RWops type is SDL_RWOPS_STDFILE or SDL_RWOPS_JNIFILE; expected: %d|%d, got: %d " , SDL_RWOPS_STDFILE , SDL_RWOPS_JNIFILE , rw - > type ) ;
2015-06-21 23:33:46 +08:00
# elif defined(__WIN32__)
2022-12-01 04:51:59 +08:00
SDLTest_AssertCheck (
rw - > type = = SDL_RWOPS_WINFILE ,
" Verify RWops type is SDL_RWOPS_WINFILE; expected: %d, got: %d " , SDL_RWOPS_WINFILE , rw - > type ) ;
2015-06-21 23:33:46 +08:00
# else
2022-12-01 04:51:59 +08:00
SDLTest_AssertCheck (
rw - > type = = SDL_RWOPS_STDFILE ,
" Verify RWops type is SDL_RWOPS_STDFILE; expected: %d, got: % " SDL_PRIu32 , SDL_RWOPS_STDFILE , rw - > type ) ;
2015-06-21 23:33:46 +08:00
# endif
2022-12-01 04:51:59 +08:00
/* Run generic tests */
2022-12-30 05:58:16 +08:00
testGenericRWopsValidations ( rw , 0 ) ;
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
/* Close handle */
result = SDL_RWclose ( rw ) ;
SDLTest_AssertPass ( " Call to SDL_RWclose() succeeded " ) ;
SDLTest_AssertCheck ( result = = 0 , " Verify result value is 0; got: %d " , result ) ;
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
return TEST_COMPLETED ;
2015-06-21 23:33:46 +08:00
}
/**
2023-02-02 07:21:53 +08:00
* \ brief Tests writing from file .
2015-06-21 23:33:46 +08:00
*
2023-02-02 07:21:53 +08:00
* \ sa SDL_RWFromFile
* \ sa SDL_RWClose
2015-06-21 23:33:46 +08:00
*/
2022-12-01 04:51:59 +08:00
int rwops_testFileWrite ( void )
2015-06-21 23:33:46 +08:00
{
2022-12-01 04:51:59 +08:00
SDL_RWops * rw ;
int result ;
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
/* Write test. */
rw = SDL_RWFromFile ( RWopsWriteTestFilename , " w+ " ) ;
SDLTest_AssertPass ( " Call to SDL_RWFromFile(.., \" w+ \" ) succeeded " ) ;
SDLTest_AssertCheck ( rw ! = NULL , " Verify opening file with SDL_RWFromFile in write mode does not return NULL " ) ;
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
/* Bail out if NULL */
if ( rw = = NULL ) {
return TEST_ABORTED ;
}
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
/* Check type */
2015-06-21 23:33:46 +08:00
# if defined(__ANDROID__)
2022-12-01 04:51:59 +08:00
SDLTest_AssertCheck (
rw - > type = = SDL_RWOPS_STDFILE | | rw - > type = = SDL_RWOPS_JNIFILE ,
" Verify RWops type is SDL_RWOPS_STDFILE or SDL_RWOPS_JNIFILE; expected: %d|%d, got: %d " , SDL_RWOPS_STDFILE , SDL_RWOPS_JNIFILE , rw - > type ) ;
2015-06-21 23:33:46 +08:00
# elif defined(__WIN32__)
2022-12-01 04:51:59 +08:00
SDLTest_AssertCheck (
rw - > type = = SDL_RWOPS_WINFILE ,
" Verify RWops type is SDL_RWOPS_WINFILE; expected: %d, got: %d " , SDL_RWOPS_WINFILE , rw - > type ) ;
2015-06-21 23:33:46 +08:00
# else
2022-12-01 04:51:59 +08:00
SDLTest_AssertCheck (
rw - > type = = SDL_RWOPS_STDFILE ,
" Verify RWops type is SDL_RWOPS_STDFILE; expected: %d, got: % " SDL_PRIu32 , SDL_RWOPS_STDFILE , rw - > type ) ;
2015-06-21 23:33:46 +08:00
# endif
2022-12-01 04:51:59 +08:00
/* Run generic tests */
2022-12-30 05:58:16 +08:00
testGenericRWopsValidations ( rw , 1 ) ;
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
/* Close handle */
result = SDL_RWclose ( rw ) ;
SDLTest_AssertPass ( " Call to SDL_RWclose() succeeded " ) ;
SDLTest_AssertCheck ( result = = 0 , " Verify result value is 0; got: %d " , result ) ;
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
return TEST_COMPLETED ;
2015-06-21 23:33:46 +08:00
}
/**
2023-02-02 07:21:53 +08:00
* \ brief Tests alloc and free RW context .
2015-06-21 23:33:46 +08:00
*
2023-02-02 07:21:53 +08:00
* \ sa SDL_CreateRW
* \ sa SDL_DestroyRW
2015-06-21 23:33:46 +08:00
*/
2022-12-01 04:51:59 +08:00
int rwops_testAllocFree ( void )
2015-06-21 23:33:46 +08:00
{
2022-12-01 04:51:59 +08:00
/* Allocate context */
2022-12-30 07:07:59 +08:00
SDL_RWops * rw = SDL_CreateRW ( ) ;
SDLTest_AssertPass ( " Call to SDL_CreateRW() succeeded " ) ;
SDLTest_AssertCheck ( rw ! = NULL , " Validate result from SDL_CreateRW() is not NULL " ) ;
2022-12-01 04:51:59 +08:00
if ( rw = = NULL ) {
return TEST_ABORTED ;
}
/* Check type */
SDLTest_AssertCheck (
rw - > type = = SDL_RWOPS_UNKNOWN ,
" Verify RWops type is SDL_RWOPS_UNKNOWN; expected: %d, got: % " SDL_PRIu32 , SDL_RWOPS_UNKNOWN , rw - > type ) ;
/* Free context again */
2022-12-30 07:07:59 +08:00
SDL_DestroyRW ( rw ) ;
SDLTest_AssertPass ( " Call to SDL_DestroyRW() succeeded " ) ;
2022-12-01 04:51:59 +08:00
return TEST_COMPLETED ;
2015-06-21 23:33:46 +08:00
}
/**
2023-02-02 07:21:53 +08:00
* \ brief Compare memory and file reads
2015-06-21 23:33:46 +08:00
*
2023-02-02 07:21:53 +08:00
* \ sa SDL_RWFromMem
* \ sa SDL_RWFromFile
2015-06-21 23:33:46 +08:00
*/
2022-12-01 04:51:59 +08:00
int rwops_testCompareRWFromMemWithRWFromFile ( void )
2015-06-21 23:33:46 +08:00
{
2022-12-01 04:51:59 +08:00
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_RWops * rwops_file ;
SDL_RWops * rwops_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 */
rwops_mem = SDL_RWFromMem ( ( void * ) RWopsAlphabetString , slen ) ;
SDLTest_AssertPass ( " Call to SDL_RWFromMem() " ) ;
2022-12-15 17:06:15 +08:00
rv_mem = ( size_t ) SDL_RWread ( rwops_mem , buffer_mem , size * 6 ) ;
2022-12-15 04:42:54 +08:00
SDLTest_AssertPass ( " Call to SDL_RWread(mem, size=%d) " , size * 6 ) ;
2022-12-01 04:51:59 +08:00
sv_mem = SDL_RWseek ( rwops_mem , 0 , SEEK_END ) ;
SDLTest_AssertPass ( " Call to SDL_RWseek(mem,SEEK_END) " ) ;
result = SDL_RWclose ( rwops_mem ) ;
SDLTest_AssertPass ( " Call to SDL_RWclose(mem) " ) ;
SDLTest_AssertCheck ( result = = 0 , " Verify result value is 0; got: %d " , result ) ;
/* Read/see from file */
rwops_file = SDL_RWFromFile ( RWopsAlphabetFilename , " r " ) ;
SDLTest_AssertPass ( " Call to SDL_RWFromFile() " ) ;
2022-12-15 17:06:15 +08:00
rv_file = ( size_t ) SDL_RWread ( rwops_file , buffer_file , size * 6 ) ;
2022-12-15 04:42:54 +08:00
SDLTest_AssertPass ( " Call to SDL_RWread(file, size=%d) " , size * 6 ) ;
2022-12-01 04:51:59 +08:00
sv_file = SDL_RWseek ( rwops_file , 0 , SEEK_END ) ;
SDLTest_AssertPass ( " Call to SDL_RWseek(file,SEEK_END) " ) ;
result = SDL_RWclose ( rwops_file ) ;
SDLTest_AssertPass ( " Call to SDL_RWclose(file) " ) ;
SDLTest_AssertCheck ( result = = 0 , " Verify result value is 0; 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 , RWopsAlphabetString , slen ) = = 0 ,
" Verify mem buffer contain alphabet string; expected: %s, got: %s " , RWopsAlphabetString , buffer_mem ) ;
SDLTest_AssertCheck (
SDL_strncmp ( buffer_file , RWopsAlphabetString , slen ) = = 0 ,
" Verify file buffer contain alphabet string; expected: %s, got: %s " , RWopsAlphabetString , buffer_file ) ;
}
return TEST_COMPLETED ;
2015-06-21 23:33:46 +08:00
}
/**
2023-02-02 07:21:53 +08:00
* \ brief Tests writing and reading from file using endian aware functions .
2015-06-21 23:33:46 +08:00
*
2023-02-02 07:21:53 +08:00
* \ sa SDL_RWFromFile
* \ sa SDL_RWClose
* \ sa SDL_ReadBE16
* \ sa SDL_WriteBE16
2015-06-21 23:33:46 +08:00
*/
2022-12-01 04:51:59 +08:00
int rwops_testFileWriteReadEndian ( void )
2015-06-21 23:33:46 +08:00
{
2022-12-01 04:51:59 +08:00
SDL_RWops * rw ;
Sint64 result ;
int mode ;
size_t objectsWritten ;
Uint16 BE16value ;
Uint32 BE32value ;
Uint64 BE64value ;
Uint16 LE16value ;
Uint32 LE32value ;
Uint64 LE64value ;
Uint16 BE16test ;
Uint32 BE32test ;
Uint64 BE64test ;
Uint16 LE16test ;
Uint32 LE32test ;
Uint64 LE64test ;
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_RWFromFile ( RWopsWriteTestFilename , " w+ " ) ;
SDLTest_AssertPass ( " Call to SDL_RWFromFile(.., \" w+ \" ) " ) ;
SDLTest_AssertCheck ( rw ! = NULL , " Verify opening file with SDL_RWFromFile in write mode does not return NULL " ) ;
/* Bail out if NULL */
if ( rw = = NULL ) {
return TEST_ABORTED ;
}
/* Write test data */
objectsWritten = SDL_WriteBE16 ( rw , BE16value ) ;
SDLTest_AssertPass ( " Call to SDL_WriteBE16() " ) ;
SDLTest_AssertCheck ( objectsWritten = = 1 , " Validate number of objects written, expected: 1, got: %i " , ( int ) objectsWritten ) ;
objectsWritten = SDL_WriteBE32 ( rw , BE32value ) ;
SDLTest_AssertPass ( " Call to SDL_WriteBE32() " ) ;
SDLTest_AssertCheck ( objectsWritten = = 1 , " Validate number of objects written, expected: 1, got: %i " , ( int ) objectsWritten ) ;
objectsWritten = SDL_WriteBE64 ( rw , BE64value ) ;
SDLTest_AssertPass ( " Call to SDL_WriteBE64() " ) ;
SDLTest_AssertCheck ( objectsWritten = = 1 , " Validate number of objects written, expected: 1, got: %i " , ( int ) objectsWritten ) ;
objectsWritten = SDL_WriteLE16 ( rw , LE16value ) ;
SDLTest_AssertPass ( " Call to SDL_WriteLE16() " ) ;
SDLTest_AssertCheck ( objectsWritten = = 1 , " Validate number of objects written, expected: 1, got: %i " , ( int ) objectsWritten ) ;
objectsWritten = SDL_WriteLE32 ( rw , LE32value ) ;
SDLTest_AssertPass ( " Call to SDL_WriteLE32() " ) ;
SDLTest_AssertCheck ( objectsWritten = = 1 , " Validate number of objects written, expected: 1, got: %i " , ( int ) objectsWritten ) ;
objectsWritten = SDL_WriteLE64 ( rw , LE64value ) ;
SDLTest_AssertPass ( " Call to SDL_WriteLE64() " ) ;
SDLTest_AssertCheck ( objectsWritten = = 1 , " Validate number of objects written, expected: 1, got: %i " , ( int ) objectsWritten ) ;
/* Test seek to start */
2022-12-23 17:16:11 +08:00
result = SDL_RWseek ( rw , 0 , SDL_RW_SEEK_SET ) ;
2022-12-01 04:51:59 +08:00
SDLTest_AssertPass ( " Call to SDL_RWseek succeeded " ) ;
SDLTest_AssertCheck ( result = = 0 , " Verify result from position 0 with SDL_RWseek, expected 0, got %i " , ( int ) result ) ;
/* Read test data */
BE16test = SDL_ReadBE16 ( rw ) ;
SDLTest_AssertPass ( " Call to SDL_ReadBE16() " ) ;
SDLTest_AssertCheck ( BE16test = = BE16value , " Validate return value from SDL_ReadBE16, expected: %hu, got: %hu " , BE16value , BE16test ) ;
BE32test = SDL_ReadBE32 ( rw ) ;
SDLTest_AssertPass ( " Call to SDL_ReadBE32() " ) ;
SDLTest_AssertCheck ( BE32test = = BE32value , " Validate return value from SDL_ReadBE32, expected: % " SDL_PRIu32 " , got: % " SDL_PRIu32 , BE32value , BE32test ) ;
BE64test = SDL_ReadBE64 ( rw ) ;
SDLTest_AssertPass ( " Call to SDL_ReadBE64() " ) ;
SDLTest_AssertCheck ( BE64test = = BE64value , " Validate return value from SDL_ReadBE64, expected: % " SDL_PRIu64 " , got: % " SDL_PRIu64 , BE64value , BE64test ) ;
LE16test = SDL_ReadLE16 ( rw ) ;
SDLTest_AssertPass ( " Call to SDL_ReadLE16() " ) ;
SDLTest_AssertCheck ( LE16test = = LE16value , " Validate return value from SDL_ReadLE16, expected: %hu, got: %hu " , LE16value , LE16test ) ;
LE32test = SDL_ReadLE32 ( rw ) ;
SDLTest_AssertPass ( " Call to SDL_ReadLE32() " ) ;
SDLTest_AssertCheck ( LE32test = = LE32value , " Validate return value from SDL_ReadLE32, expected: % " SDL_PRIu32 " , got: % " SDL_PRIu32 , LE32value , LE32test ) ;
LE64test = SDL_ReadLE64 ( rw ) ;
SDLTest_AssertPass ( " Call to SDL_ReadLE64() " ) ;
SDLTest_AssertCheck ( LE64test = = LE64value , " Validate return value from SDL_ReadLE64, expected: % " SDL_PRIu64 " , got: % " SDL_PRIu64 , LE64value , LE64test ) ;
/* Close handle */
cresult = SDL_RWclose ( rw ) ;
SDLTest_AssertPass ( " Call to SDL_RWclose() succeeded " ) ;
SDLTest_AssertCheck ( cresult = = 0 , " Verify result value is 0; got: %d " , cresult ) ;
}
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
return TEST_COMPLETED ;
}
2015-06-21 23:33:46 +08:00
/* ================= Test References ================== */
/* RWops test cases */
2022-12-01 04:51:59 +08:00
static const SDLTest_TestCaseReference rwopsTest1 = {
( SDLTest_TestCaseFp ) rwops_testParamNegative , " rwops_testParamNegative " , " Negative test for SDL_RWFromFile parameters " , TEST_ENABLED
} ;
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
static const SDLTest_TestCaseReference rwopsTest2 = {
( SDLTest_TestCaseFp ) rwops_testMem , " rwops_testMem " , " Tests opening from memory " , TEST_ENABLED
} ;
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
static const SDLTest_TestCaseReference rwopsTest3 = {
( SDLTest_TestCaseFp ) rwops_testConstMem , " rwops_testConstMem " , " Tests opening from (const) memory " , TEST_ENABLED
} ;
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
static const SDLTest_TestCaseReference rwopsTest4 = {
( SDLTest_TestCaseFp ) rwops_testFileRead , " rwops_testFileRead " , " Tests reading from a file " , TEST_ENABLED
} ;
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
static const SDLTest_TestCaseReference rwopsTest5 = {
( SDLTest_TestCaseFp ) rwops_testFileWrite , " rwops_testFileWrite " , " Test writing to a file " , TEST_ENABLED
} ;
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
static const SDLTest_TestCaseReference rwopsTest6 = {
( SDLTest_TestCaseFp ) rwops_testAllocFree , " rwops_testAllocFree " , " Test alloc and free of RW context " , TEST_ENABLED
} ;
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
static const SDLTest_TestCaseReference rwopsTest7 = {
( SDLTest_TestCaseFp ) rwops_testFileWriteReadEndian , " rwops_testFileWriteReadEndian " , " Test writing and reading via the Endian aware functions " , TEST_ENABLED
} ;
2015-06-21 23:33:46 +08:00
2022-12-01 04:51:59 +08:00
static const SDLTest_TestCaseReference rwopsTest8 = {
( SDLTest_TestCaseFp ) rwops_testCompareRWFromMemWithRWFromFile , " rwops_testCompareRWFromMemWithRWFromFile " , " Compare RWFromMem and RWFromFile RWops for read and seek " , TEST_ENABLED
} ;
2015-06-21 23:33:46 +08:00
/* Sequence of RWops test cases */
2022-12-01 04:51:59 +08:00
static const SDLTest_TestCaseReference * rwopsTests [ ] = {
2015-06-21 23:33:46 +08:00
& rwopsTest1 , & rwopsTest2 , & rwopsTest3 , & rwopsTest4 , & rwopsTest5 , & rwopsTest6 ,
2022-11-26 02:46:26 +08:00
& rwopsTest7 , & rwopsTest8 , NULL
2015-06-21 23:33:46 +08:00
} ;
/* RWops test suite (global) */
SDLTest_TestSuiteReference rwopsTestSuite = {
" RWops " ,
RWopsSetUp ,
rwopsTests ,
RWopsTearDown
} ;