Skip to content

Commit

Permalink
Date: Mon, 27 Feb 2006 17:01:43 +0100
Browse files Browse the repository at this point in the history
From: "William Petiot [exoide]"
Subject: Re: [SDL] cvs version: standard win32 SDL.DLL file test [patch]

I found a bug or two in previous patch, so here is a patch to correct this.
  • Loading branch information
slouken committed Feb 27, 2006
1 parent f4eb805 commit 3102a5d
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 5 deletions.
Binary file modified VisualC.zip
Binary file not shown.
1 change: 0 additions & 1 deletion include/SDL_rwops.h
Expand Up @@ -64,7 +64,6 @@ typedef struct SDL_RWops {
union {
#ifdef __WIN32__
struct {
int autoclose;
int append;
void* h;
} win32io;
Expand Down
8 changes: 5 additions & 3 deletions src/file/SDL_rwops.c
Expand Up @@ -59,8 +59,8 @@ static int win32_file_open(SDL_RWops *context, const char *filename, const char
must_exist = ( SDL_strchr(mode,'r') != NULL ) ? OPEN_EXISTING : 0;
truncate = ( SDL_strchr(mode,'w') != NULL ) ? CREATE_ALWAYS : 0;
r_right = ( SDL_strchr(mode,'+') != NULL || must_exist ) ? GENERIC_READ : 0;
a_mode = ( SDL_strchr(mode,'a') != NULL );
w_right = ( a_mode || SDL_strchr(mode,'w') || truncate ) ? GENERIC_WRITE : 0;
a_mode = ( SDL_strchr(mode,'a') != NULL ) ? OPEN_ALWAYS : 0;
w_right = ( a_mode || SDL_strchr(mode,'+') || truncate ) ? GENERIC_WRITE : 0;

if (!r_right && !w_right) /* inconsistent mode */
return -1; /* failed (invalid call)*/
Expand All @@ -69,7 +69,7 @@ static int win32_file_open(SDL_RWops *context, const char *filename, const char
old_error_mode = SetErrorMode(SEM_NOOPENFILEERRORBOX|SEM_FAILCRITICALERRORS);

h = CreateFile(filename, (w_right|r_right), (w_right)? 0 : FILE_SHARE_READ,
NULL, (must_exist|truncate), FILE_ATTRIBUTE_NORMAL,NULL);
NULL, (must_exist|truncate|a_mode), FILE_ATTRIBUTE_NORMAL,NULL);

/* restore old behaviour */
SetErrorMode(old_error_mode);
Expand Down Expand Up @@ -338,6 +338,8 @@ SDL_RWops *SDL_RWFromFile(const char *file, const char *mode)

#ifdef __WIN32__
rwops = SDL_AllocRW();
if (!rwops)
return NULL; /* SDL_SetError already setup by SDL_AllocRW() */
rwops->hidden.win32io.h = INVALID_HANDLE_VALUE;
if (win32_file_open(rwops,file,mode)) {
SDL_FreeRW(rwops);
Expand Down
5 changes: 4 additions & 1 deletion test/Makefile.in
Expand Up @@ -7,7 +7,7 @@ EXE = @EXE@
CFLAGS = @CFLAGS@
LIBS = @LIBS@

TARGETS = checkkeys$(EXE) graywin$(EXE) loopwave$(EXE) testalpha$(EXE) testbitmap$(EXE) testblitspeed$(EXE) testcdrom$(EXE) testdyngl$(EXE) testerror$(EXE) testgamma$(EXE) testgl$(EXE) testhread$(EXE) testjoystick$(EXE) testkeys$(EXE) testlock$(EXE) testoverlay2$(EXE) testoverlay$(EXE) testpalette$(EXE) testplatform$(EXE) testsem$(EXE) testsprite$(EXE) testtimer$(EXE) testver$(EXE) testvidinfo$(EXE) testwin$(EXE) testwm$(EXE) threadwin$(EXE) torturethread$(EXE)
TARGETS = checkkeys$(EXE) graywin$(EXE) loopwave$(EXE) testalpha$(EXE) testbitmap$(EXE) testblitspeed$(EXE) testcdrom$(EXE) testdyngl$(EXE) testerror$(EXE) testfile$(EXE) testgamma$(EXE) testgl$(EXE) testhread$(EXE) testjoystick$(EXE) testkeys$(EXE) testlock$(EXE) testoverlay2$(EXE) testoverlay$(EXE) testpalette$(EXE) testplatform$(EXE) testsem$(EXE) testsprite$(EXE) testtimer$(EXE) testver$(EXE) testvidinfo$(EXE) testwin$(EXE) testwm$(EXE) threadwin$(EXE) torturethread$(EXE)

all: $(TARGETS)

Expand Down Expand Up @@ -38,6 +38,9 @@ testdyngl$(EXE): $(srcdir)/testdyngl.c
testerror$(EXE): $(srcdir)/testerror.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS)

testfile$(EXE): $(srcdir)/testfile.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS)

testgamma$(EXE): $(srcdir)/testgamma.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS) @MATHLIB@

Expand Down
178 changes: 178 additions & 0 deletions test/testfile.c
@@ -0,0 +1,178 @@

/* sanity tests on SDL_rwops.c (usefull for alternative implementations of stdio rwops) */



#include "SDL.h"
#include "SDL_endian.h"


#include <stdio.h>

/* WARNING ! those 2 files will be destroyed by this test program */
#define FBASENAME1 "sdldata1" /* this file will be created during tests */
#define FBASENAME2 "sdldata2" /* this file should not exists before starting test */


#ifndef NULL
#define NULL ((void *)0)
#endif

static void cleanup( void ) {

unlink(FBASENAME1);
unlink(FBASENAME2);
}

static void rwops_error_quit( unsigned line, SDL_RWops *rwops) {

printf("testfile.c(%d): failed\n",line);
if (rwops) {
rwops->close(rwops); /* This calls SDL_FreeRW(rwops); */
}
cleanup();
exit(1); /* quit with rwops error (test failed) */
}

#define RWOP_ERR_QUIT(x) rwops_error_quit( __LINE__, (x) )



int main(int argc, char *argv[])
{
SDL_RWops *rwops = NULL;
char test_buf[30];

cleanup();

/* test 1 : basic argument test: all those calls to SDL_RWFromFile should fail */

rwops = SDL_RWFromFile(NULL,NULL);
if (rwops) RWOP_ERR_QUIT(rwops);
rwops = SDL_RWFromFile(NULL,"a+" WIN32_FILE_MODE);
if (rwops) RWOP_ERR_QUIT(rwops);
rwops = SDL_RWFromFile(NULL,"sldfkjsldkfj");
if (rwops) RWOP_ERR_QUIT(rwops);
rwops = SDL_RWFromFile("something","");
if (rwops) RWOP_ERR_QUIT(rwops);
rwops = SDL_RWFromFile("something",NULL);
if (rwops) RWOP_ERR_QUIT(rwops);
printf("test1 OK\n");

/* test 2 : check that inexistant file is not successfully opened/created when required */
/* modes : r, r+ implie that file MUST exist
modes : a, a+, w, w+ checks that it succeeds (file may not exists)
*/
rwops = SDL_RWFromFile(FBASENAME2,"rb"); /* this file doesn't exist that call must fail */
if (rwops) RWOP_ERR_QUIT(rwops);
rwops = SDL_RWFromFile(FBASENAME2,"rb+"); /* this file doesn't exist that call must fail */
if (rwops) RWOP_ERR_QUIT(rwops);
rwops = SDL_RWFromFile(FBASENAME2,"wb");
if (!rwops) RWOP_ERR_QUIT(rwops);
rwops->close(rwops); unlink(FBASENAME2);
rwops = SDL_RWFromFile(FBASENAME2,"wb+");
if (!rwops) RWOP_ERR_QUIT(rwops);
rwops->close(rwops); unlink(FBASENAME2);
rwops = SDL_RWFromFile(FBASENAME2,"ab");
if (!rwops) RWOP_ERR_QUIT(rwops);
rwops->close(rwops); unlink(FBASENAME2);
rwops = SDL_RWFromFile(FBASENAME2,"ab+");
if (!rwops) RWOP_ERR_QUIT(rwops);
rwops->close(rwops); unlink(FBASENAME2);
printf("test2 OK\n");

/* test 3 : creation, writing , reading, seeking,
test : w mode, r mode, w+ mode
*/
rwops = SDL_RWFromFile(FBASENAME1,"wb"); /* write only */
if (!rwops) RWOP_ERR_QUIT(rwops);
if (1 != rwops->write(rwops,"1234567890",10,1) ) RWOP_ERR_QUIT(rwops);
if (10 != rwops->write(rwops,"1234567890",1,10) ) RWOP_ERR_QUIT(rwops);
if (7 != rwops->write(rwops,"1234567",1,7) ) RWOP_ERR_QUIT(rwops);
if (0!=rwops->seek(rwops,0L,RW_SEEK_SET)) RWOP_ERR_QUIT(rwops);
if (0!=rwops->read(rwops,test_buf,1,1)) RWOP_ERR_QUIT(rwops); /* we are in write only mode */
rwops->close(rwops);

rwops = SDL_RWFromFile(FBASENAME1,"rb"); /* read mode, file must exists */
if (!rwops) RWOP_ERR_QUIT(rwops);
if (0!=rwops->seek(rwops,0L,RW_SEEK_SET)) RWOP_ERR_QUIT(rwops);
if (20!=rwops->seek(rwops,-7,RW_SEEK_END)) RWOP_ERR_QUIT(rwops);
if (7!=rwops->read(rwops,test_buf,1,7)) RWOP_ERR_QUIT(rwops);
if (SDL_memcmp(test_buf,"1234567",7)) RWOP_ERR_QUIT(rwops);
if (0!=rwops->read(rwops,test_buf,1,1)) RWOP_ERR_QUIT(rwops);
if (0!=rwops->read(rwops,test_buf,10,100)) RWOP_ERR_QUIT(rwops);
if (0!=rwops->seek(rwops,-27,RW_SEEK_CUR)) RWOP_ERR_QUIT(rwops);
if (2!=rwops->read(rwops,test_buf,10,3)) RWOP_ERR_QUIT(rwops);
if (SDL_memcmp(test_buf,"12345678901234567890",20)) RWOP_ERR_QUIT(rwops);
if (0!=rwops->write(rwops,test_buf,1,1)) RWOP_ERR_QUIT(rwops); /* readonly mode */
rwops->close(rwops);

/* test 3: same with w+ mode */
rwops = SDL_RWFromFile(FBASENAME1,"wb+"); /* write + read + truncation */
if (!rwops) RWOP_ERR_QUIT(rwops);
if (1 != rwops->write(rwops,"1234567890",10,1) ) RWOP_ERR_QUIT(rwops);
if (10 != rwops->write(rwops,"1234567890",1,10) ) RWOP_ERR_QUIT(rwops);
if (7 != rwops->write(rwops,"1234567",1,7) ) RWOP_ERR_QUIT(rwops);
if (0!=rwops->seek(rwops,0L,RW_SEEK_SET)) RWOP_ERR_QUIT(rwops);
if (1!=rwops->read(rwops,test_buf,1,1)) RWOP_ERR_QUIT(rwops); /* we are in read/write mode */
if (0!=rwops->seek(rwops,0L,RW_SEEK_SET)) RWOP_ERR_QUIT(rwops);
if (20!=rwops->seek(rwops,-7,RW_SEEK_END)) RWOP_ERR_QUIT(rwops);
if (7!=rwops->read(rwops,test_buf,1,7)) RWOP_ERR_QUIT(rwops);
if (SDL_memcmp(test_buf,"1234567",7)) RWOP_ERR_QUIT(rwops);
if (0!=rwops->read(rwops,test_buf,1,1)) RWOP_ERR_QUIT(rwops);
if (0!=rwops->read(rwops,test_buf,10,100)) RWOP_ERR_QUIT(rwops);
if (0!=rwops->seek(rwops,-27,RW_SEEK_CUR)) RWOP_ERR_QUIT(rwops);
if (2!=rwops->read(rwops,test_buf,10,3)) RWOP_ERR_QUIT(rwops);
if (SDL_memcmp(test_buf,"12345678901234567890",20)) RWOP_ERR_QUIT(rwops);
rwops->close(rwops);
printf("test3 OK\n");

/* test 4: same in r+ mode */
rwops = SDL_RWFromFile(FBASENAME1,"rb+"); /* write + read + file must exists, no truncation */
if (!rwops) RWOP_ERR_QUIT(rwops);
if (1 != rwops->write(rwops,"1234567890",10,1) ) RWOP_ERR_QUIT(rwops);
if (10 != rwops->write(rwops,"1234567890",1,10) ) RWOP_ERR_QUIT(rwops);
if (7 != rwops->write(rwops,"1234567",1,7) ) RWOP_ERR_QUIT(rwops);
if (0!=rwops->seek(rwops,0L,RW_SEEK_SET)) RWOP_ERR_QUIT(rwops);
if (1!=rwops->read(rwops,test_buf,1,1)) RWOP_ERR_QUIT(rwops); /* we are in read/write mode */
if (0!=rwops->seek(rwops,0L,RW_SEEK_SET)) RWOP_ERR_QUIT(rwops);
if (20!=rwops->seek(rwops,-7,RW_SEEK_END)) RWOP_ERR_QUIT(rwops);
if (7!=rwops->read(rwops,test_buf,1,7)) RWOP_ERR_QUIT(rwops);
if (SDL_memcmp(test_buf,"1234567",7)) RWOP_ERR_QUIT(rwops);
if (0!=rwops->read(rwops,test_buf,1,1)) RWOP_ERR_QUIT(rwops);
if (0!=rwops->read(rwops,test_buf,10,100)) RWOP_ERR_QUIT(rwops);
if (0!=rwops->seek(rwops,-27,RW_SEEK_CUR)) RWOP_ERR_QUIT(rwops);
if (2!=rwops->read(rwops,test_buf,10,3)) RWOP_ERR_QUIT(rwops);
if (SDL_memcmp(test_buf,"12345678901234567890",20)) RWOP_ERR_QUIT(rwops);
rwops->close(rwops);
printf("test4 OK\n");

/* test5 : append mode */
rwops = SDL_RWFromFile(FBASENAME1,"ab+"); /* write + read + append */
if (!rwops) RWOP_ERR_QUIT(rwops);
if (1 != rwops->write(rwops,"1234567890",10,1) ) RWOP_ERR_QUIT(rwops);
if (10 != rwops->write(rwops,"1234567890",1,10) ) RWOP_ERR_QUIT(rwops);
if (7 != rwops->write(rwops,"1234567",1,7) ) RWOP_ERR_QUIT(rwops);
if (0!=rwops->seek(rwops,0L,RW_SEEK_SET)) RWOP_ERR_QUIT(rwops);

if (1!=rwops->read(rwops,test_buf,1,1)) RWOP_ERR_QUIT(rwops);
if (0!=rwops->seek(rwops,0L,RW_SEEK_SET)) RWOP_ERR_QUIT(rwops);

if (20+27!=rwops->seek(rwops,-7,RW_SEEK_END)) RWOP_ERR_QUIT(rwops);
if (7!=rwops->read(rwops,test_buf,1,7)) RWOP_ERR_QUIT(rwops);
if (SDL_memcmp(test_buf,"1234567",7)) RWOP_ERR_QUIT(rwops);
if (0!=rwops->read(rwops,test_buf,1,1)) RWOP_ERR_QUIT(rwops);
if (0!=rwops->read(rwops,test_buf,10,100)) RWOP_ERR_QUIT(rwops);

if (27!=rwops->seek(rwops,-27,RW_SEEK_CUR)) RWOP_ERR_QUIT(rwops);

if (0!=rwops->seek(rwops,0L,RW_SEEK_SET)) RWOP_ERR_QUIT(rwops);
if (3!=rwops->read(rwops,test_buf,10,3)) RWOP_ERR_QUIT(rwops);
if (SDL_memcmp(test_buf,"123456789012345678901234567123",30))
RWOP_ERR_QUIT(rwops);
rwops->close(rwops);
printf("test5 OK\n");
cleanup();
return 0; /* all ok */
}

0 comments on commit 3102a5d

Please sign in to comment.