test/testfile.c
changeset 1453 2eb75f8a32ca
child 1454 07d2b0085ecd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testfile.c	Mon Feb 27 19:19:22 2006 +0000
     1.3 @@ -0,0 +1,178 @@
     1.4 +
     1.5 +/* sanity tests on SDL_rwops.c (usefull for alternative implementations of stdio rwops) */
     1.6 +
     1.7 +
     1.8 +
     1.9 +#include "SDL.h"
    1.10 +#include "SDL_endian.h"
    1.11 +
    1.12 +
    1.13 +#include <stdio.h>	
    1.14 +
    1.15 +/* WARNING ! those 2 files will be destroyed by this test program */
    1.16 +#define FBASENAME1	"sdldata1"		/* this file will be created during tests */
    1.17 +#define FBASENAME2	"sdldata2"		/* this file should not exists before starting test */
    1.18 +
    1.19 +
    1.20 +#ifndef NULL
    1.21 +#define NULL ((void *)0)
    1.22 +#endif 
    1.23 +
    1.24 +static void cleanup( void ) {
    1.25 +
    1.26 +	unlink(FBASENAME1);
    1.27 +	unlink(FBASENAME2); 
    1.28 +}
    1.29 +
    1.30 +static void rwops_error_quit( unsigned line, SDL_RWops *rwops) {
    1.31 +	
    1.32 +	printf("testfile.c(%d): failed\n",line);
    1.33 +	if (rwops) {
    1.34 +		rwops->close(rwops); /* This calls SDL_FreeRW(rwops); */
    1.35 +	}
    1.36 +	cleanup();
    1.37 +	exit(1); /* quit with rwops error (test failed) */
    1.38 +}
    1.39 +
    1.40 +#define RWOP_ERR_QUIT(x)	rwops_error_quit( __LINE__, (x) )
    1.41 +
    1.42 +
    1.43 +
    1.44 +int main(int argc, char *argv[])
    1.45 +{
    1.46 +	SDL_RWops *rwops = NULL;
    1.47 +	char test_buf[30];
    1.48 +	
    1.49 +	cleanup();
    1.50 +
    1.51 +/* test 1 : basic argument test: all those calls to SDL_RWFromFile should fail */
    1.52 +	
    1.53 +	rwops = SDL_RWFromFile(NULL,NULL);
    1.54 +	if (rwops) RWOP_ERR_QUIT(rwops);
    1.55 +	rwops = SDL_RWFromFile(NULL,"a+" WIN32_FILE_MODE);
    1.56 +	if (rwops) RWOP_ERR_QUIT(rwops);
    1.57 +	rwops = SDL_RWFromFile(NULL,"sldfkjsldkfj");
    1.58 +	if (rwops) RWOP_ERR_QUIT(rwops);
    1.59 +	rwops = SDL_RWFromFile("something","");
    1.60 +	if (rwops) RWOP_ERR_QUIT(rwops);
    1.61 +	rwops = SDL_RWFromFile("something",NULL);
    1.62 +	if (rwops) RWOP_ERR_QUIT(rwops);	
    1.63 +	printf("test1 OK\n");
    1.64 +
    1.65 +/* test 2 : check that inexistant file is not successfully opened/created when required */
    1.66 +/* modes : r, r+ implie that file MUST exist 
    1.67 +   modes : a, a+, w, w+ checks that it succeeds (file may not exists)
    1.68 +   
    1.69 + */
    1.70 +	rwops = SDL_RWFromFile(FBASENAME2,"rb"); /* this file doesn't exist that call must fail */
    1.71 +	if (rwops) RWOP_ERR_QUIT(rwops);
    1.72 +	rwops = SDL_RWFromFile(FBASENAME2,"rb+"); /* this file doesn't exist that call must fail */
    1.73 +	if (rwops) RWOP_ERR_QUIT(rwops);
    1.74 +	rwops = SDL_RWFromFile(FBASENAME2,"wb");
    1.75 +	if (!rwops) RWOP_ERR_QUIT(rwops); 
    1.76 +	rwops->close(rwops); unlink(FBASENAME2);
    1.77 +	rwops = SDL_RWFromFile(FBASENAME2,"wb+");
    1.78 +	if (!rwops) RWOP_ERR_QUIT(rwops); 
    1.79 +	rwops->close(rwops); unlink(FBASENAME2);
    1.80 +	rwops = SDL_RWFromFile(FBASENAME2,"ab"); 
    1.81 +	if (!rwops) RWOP_ERR_QUIT(rwops); 
    1.82 +	rwops->close(rwops); unlink(FBASENAME2);
    1.83 +	rwops = SDL_RWFromFile(FBASENAME2,"ab+");
    1.84 +	if (!rwops) RWOP_ERR_QUIT(rwops); 
    1.85 +	rwops->close(rwops); unlink(FBASENAME2);
    1.86 +	printf("test2 OK\n");
    1.87 +
    1.88 +/* test 3 : creation, writing , reading, seeking, 
    1.89 +	        test : w mode, r mode, w+ mode
    1.90 + */
    1.91 +	rwops = SDL_RWFromFile(FBASENAME1,"wb"); /* write only */	
    1.92 +	if (!rwops)											RWOP_ERR_QUIT(rwops);
    1.93 +	if (1 != rwops->write(rwops,"1234567890",10,1) )	RWOP_ERR_QUIT(rwops);		
    1.94 +	if (10 != rwops->write(rwops,"1234567890",1,10) )	RWOP_ERR_QUIT(rwops);		
    1.95 +	if (7 != rwops->write(rwops,"1234567",1,7) )		RWOP_ERR_QUIT(rwops);		
    1.96 +	if (0!=rwops->seek(rwops,0L,RW_SEEK_SET))			RWOP_ERR_QUIT(rwops);					
    1.97 +	if (0!=rwops->read(rwops,test_buf,1,1))				RWOP_ERR_QUIT(rwops); /* we are in write only mode */
    1.98 +	rwops->close(rwops);
    1.99 +
   1.100 +	rwops = SDL_RWFromFile(FBASENAME1,"rb"); /* read mode, file must exists */
   1.101 +	if (!rwops)											RWOP_ERR_QUIT(rwops);
   1.102 +	if (0!=rwops->seek(rwops,0L,RW_SEEK_SET))			RWOP_ERR_QUIT(rwops);					
   1.103 +	if (20!=rwops->seek(rwops,-7,RW_SEEK_END))			RWOP_ERR_QUIT(rwops);					
   1.104 +	if (7!=rwops->read(rwops,test_buf,1,7))				RWOP_ERR_QUIT(rwops);		
   1.105 +	if (SDL_memcmp(test_buf,"1234567",7))				RWOP_ERR_QUIT(rwops);		
   1.106 +	if (0!=rwops->read(rwops,test_buf,1,1))				RWOP_ERR_QUIT(rwops);		
   1.107 +	if (0!=rwops->read(rwops,test_buf,10,100))			RWOP_ERR_QUIT(rwops);		
   1.108 +	if (0!=rwops->seek(rwops,-27,RW_SEEK_CUR))			RWOP_ERR_QUIT(rwops);		
   1.109 +	if (2!=rwops->read(rwops,test_buf,10,3))			RWOP_ERR_QUIT(rwops);				
   1.110 +	if (SDL_memcmp(test_buf,"12345678901234567890",20))	RWOP_ERR_QUIT(rwops);
   1.111 +	if (0!=rwops->write(rwops,test_buf,1,1))			RWOP_ERR_QUIT(rwops); /* readonly mode */
   1.112 +	rwops->close(rwops);
   1.113 +
   1.114 +/* test 3: same with w+ mode */
   1.115 +	rwops = SDL_RWFromFile(FBASENAME1,"wb+"); /* write + read + truncation */		
   1.116 +	if (!rwops)											RWOP_ERR_QUIT(rwops);
   1.117 +	if (1 != rwops->write(rwops,"1234567890",10,1) )	RWOP_ERR_QUIT(rwops);		
   1.118 +	if (10 != rwops->write(rwops,"1234567890",1,10) )	RWOP_ERR_QUIT(rwops);		
   1.119 +	if (7 != rwops->write(rwops,"1234567",1,7) )		RWOP_ERR_QUIT(rwops);		
   1.120 +	if (0!=rwops->seek(rwops,0L,RW_SEEK_SET))			RWOP_ERR_QUIT(rwops);					
   1.121 +	if (1!=rwops->read(rwops,test_buf,1,1))				RWOP_ERR_QUIT(rwops); /* we are in read/write mode */
   1.122 +	if (0!=rwops->seek(rwops,0L,RW_SEEK_SET))			RWOP_ERR_QUIT(rwops);					
   1.123 +	if (20!=rwops->seek(rwops,-7,RW_SEEK_END))			RWOP_ERR_QUIT(rwops);					
   1.124 +	if (7!=rwops->read(rwops,test_buf,1,7))				RWOP_ERR_QUIT(rwops);		
   1.125 +	if (SDL_memcmp(test_buf,"1234567",7))				RWOP_ERR_QUIT(rwops);		
   1.126 +	if (0!=rwops->read(rwops,test_buf,1,1))				RWOP_ERR_QUIT(rwops);		
   1.127 +	if (0!=rwops->read(rwops,test_buf,10,100))			RWOP_ERR_QUIT(rwops);		
   1.128 +	if (0!=rwops->seek(rwops,-27,RW_SEEK_CUR))			RWOP_ERR_QUIT(rwops);		
   1.129 +	if (2!=rwops->read(rwops,test_buf,10,3))			RWOP_ERR_QUIT(rwops);				
   1.130 +	if (SDL_memcmp(test_buf,"12345678901234567890",20))	RWOP_ERR_QUIT(rwops);
   1.131 +	rwops->close(rwops);
   1.132 +	printf("test3 OK\n");
   1.133 +
   1.134 +/* test 4: same in r+ mode */
   1.135 +	rwops = SDL_RWFromFile(FBASENAME1,"rb+"); /* write + read + file must exists, no truncation */		
   1.136 +	if (!rwops)											RWOP_ERR_QUIT(rwops);
   1.137 +	if (1 != rwops->write(rwops,"1234567890",10,1) )	RWOP_ERR_QUIT(rwops);		
   1.138 +	if (10 != rwops->write(rwops,"1234567890",1,10) )	RWOP_ERR_QUIT(rwops);		
   1.139 +	if (7 != rwops->write(rwops,"1234567",1,7) )		RWOP_ERR_QUIT(rwops);		
   1.140 +	if (0!=rwops->seek(rwops,0L,RW_SEEK_SET))			RWOP_ERR_QUIT(rwops);					
   1.141 +	if (1!=rwops->read(rwops,test_buf,1,1))				RWOP_ERR_QUIT(rwops); /* we are in read/write mode */
   1.142 +	if (0!=rwops->seek(rwops,0L,RW_SEEK_SET))			RWOP_ERR_QUIT(rwops);					
   1.143 +	if (20!=rwops->seek(rwops,-7,RW_SEEK_END))			RWOP_ERR_QUIT(rwops);					
   1.144 +	if (7!=rwops->read(rwops,test_buf,1,7))				RWOP_ERR_QUIT(rwops);		
   1.145 +	if (SDL_memcmp(test_buf,"1234567",7))				RWOP_ERR_QUIT(rwops);		
   1.146 +	if (0!=rwops->read(rwops,test_buf,1,1))				RWOP_ERR_QUIT(rwops);		
   1.147 +	if (0!=rwops->read(rwops,test_buf,10,100))			RWOP_ERR_QUIT(rwops);		
   1.148 +	if (0!=rwops->seek(rwops,-27,RW_SEEK_CUR))			RWOP_ERR_QUIT(rwops);		
   1.149 +	if (2!=rwops->read(rwops,test_buf,10,3))			RWOP_ERR_QUIT(rwops);				
   1.150 +	if (SDL_memcmp(test_buf,"12345678901234567890",20))	RWOP_ERR_QUIT(rwops);
   1.151 +	rwops->close(rwops);
   1.152 +	printf("test4 OK\n");
   1.153 +
   1.154 +/* test5 : append mode */
   1.155 +	rwops = SDL_RWFromFile(FBASENAME1,"ab+"); /* write + read + append */		
   1.156 +	if (!rwops)											RWOP_ERR_QUIT(rwops);
   1.157 +	if (1 != rwops->write(rwops,"1234567890",10,1) )	RWOP_ERR_QUIT(rwops);		
   1.158 +	if (10 != rwops->write(rwops,"1234567890",1,10) )	RWOP_ERR_QUIT(rwops);		
   1.159 +	if (7 != rwops->write(rwops,"1234567",1,7) )		RWOP_ERR_QUIT(rwops); 	
   1.160 +	if (0!=rwops->seek(rwops,0L,RW_SEEK_SET))			RWOP_ERR_QUIT(rwops);					
   1.161 +	
   1.162 +	if (1!=rwops->read(rwops,test_buf,1,1))				RWOP_ERR_QUIT(rwops); 
   1.163 +	if (0!=rwops->seek(rwops,0L,RW_SEEK_SET))			RWOP_ERR_QUIT(rwops);					
   1.164 +	
   1.165 +	if (20+27!=rwops->seek(rwops,-7,RW_SEEK_END))		RWOP_ERR_QUIT(rwops);					
   1.166 +	if (7!=rwops->read(rwops,test_buf,1,7))				RWOP_ERR_QUIT(rwops);		
   1.167 +	if (SDL_memcmp(test_buf,"1234567",7))				RWOP_ERR_QUIT(rwops);		
   1.168 +	if (0!=rwops->read(rwops,test_buf,1,1))				RWOP_ERR_QUIT(rwops);		
   1.169 +	if (0!=rwops->read(rwops,test_buf,10,100))			RWOP_ERR_QUIT(rwops);		
   1.170 +	
   1.171 +	if (27!=rwops->seek(rwops,-27,RW_SEEK_CUR))			RWOP_ERR_QUIT(rwops);
   1.172 +	
   1.173 +	if (0!=rwops->seek(rwops,0L,RW_SEEK_SET))			RWOP_ERR_QUIT(rwops);
   1.174 +	if (3!=rwops->read(rwops,test_buf,10,3))			RWOP_ERR_QUIT(rwops);				
   1.175 +	if (SDL_memcmp(test_buf,"123456789012345678901234567123",30))	
   1.176 +														RWOP_ERR_QUIT(rwops);
   1.177 +	rwops->close(rwops);
   1.178 +	printf("test5 OK\n");
   1.179 +	cleanup();
   1.180 +	return 0; /* all ok */
   1.181 +}