test/testfile.c
changeset 1453 2eb75f8a32ca
child 1454 07d2b0085ecd
equal deleted inserted replaced
1452:b7184889c1cb 1453:2eb75f8a32ca
       
     1 
       
     2 /* sanity tests on SDL_rwops.c (usefull for alternative implementations of stdio rwops) */
       
     3 
       
     4 
       
     5 
       
     6 #include "SDL.h"
       
     7 #include "SDL_endian.h"
       
     8 
       
     9 
       
    10 #include <stdio.h>	
       
    11 
       
    12 /* WARNING ! those 2 files will be destroyed by this test program */
       
    13 #define FBASENAME1	"sdldata1"		/* this file will be created during tests */
       
    14 #define FBASENAME2	"sdldata2"		/* this file should not exists before starting test */
       
    15 
       
    16 
       
    17 #ifndef NULL
       
    18 #define NULL ((void *)0)
       
    19 #endif 
       
    20 
       
    21 static void cleanup( void ) {
       
    22 
       
    23 	unlink(FBASENAME1);
       
    24 	unlink(FBASENAME2); 
       
    25 }
       
    26 
       
    27 static void rwops_error_quit( unsigned line, SDL_RWops *rwops) {
       
    28 	
       
    29 	printf("testfile.c(%d): failed\n",line);
       
    30 	if (rwops) {
       
    31 		rwops->close(rwops); /* This calls SDL_FreeRW(rwops); */
       
    32 	}
       
    33 	cleanup();
       
    34 	exit(1); /* quit with rwops error (test failed) */
       
    35 }
       
    36 
       
    37 #define RWOP_ERR_QUIT(x)	rwops_error_quit( __LINE__, (x) )
       
    38 
       
    39 
       
    40 
       
    41 int main(int argc, char *argv[])
       
    42 {
       
    43 	SDL_RWops *rwops = NULL;
       
    44 	char test_buf[30];
       
    45 	
       
    46 	cleanup();
       
    47 
       
    48 /* test 1 : basic argument test: all those calls to SDL_RWFromFile should fail */
       
    49 	
       
    50 	rwops = SDL_RWFromFile(NULL,NULL);
       
    51 	if (rwops) RWOP_ERR_QUIT(rwops);
       
    52 	rwops = SDL_RWFromFile(NULL,"a+" WIN32_FILE_MODE);
       
    53 	if (rwops) RWOP_ERR_QUIT(rwops);
       
    54 	rwops = SDL_RWFromFile(NULL,"sldfkjsldkfj");
       
    55 	if (rwops) RWOP_ERR_QUIT(rwops);
       
    56 	rwops = SDL_RWFromFile("something","");
       
    57 	if (rwops) RWOP_ERR_QUIT(rwops);
       
    58 	rwops = SDL_RWFromFile("something",NULL);
       
    59 	if (rwops) RWOP_ERR_QUIT(rwops);	
       
    60 	printf("test1 OK\n");
       
    61 
       
    62 /* test 2 : check that inexistant file is not successfully opened/created when required */
       
    63 /* modes : r, r+ implie that file MUST exist 
       
    64    modes : a, a+, w, w+ checks that it succeeds (file may not exists)
       
    65    
       
    66  */
       
    67 	rwops = SDL_RWFromFile(FBASENAME2,"rb"); /* this file doesn't exist that call must fail */
       
    68 	if (rwops) RWOP_ERR_QUIT(rwops);
       
    69 	rwops = SDL_RWFromFile(FBASENAME2,"rb+"); /* this file doesn't exist that call must fail */
       
    70 	if (rwops) RWOP_ERR_QUIT(rwops);
       
    71 	rwops = SDL_RWFromFile(FBASENAME2,"wb");
       
    72 	if (!rwops) RWOP_ERR_QUIT(rwops); 
       
    73 	rwops->close(rwops); unlink(FBASENAME2);
       
    74 	rwops = SDL_RWFromFile(FBASENAME2,"wb+");
       
    75 	if (!rwops) RWOP_ERR_QUIT(rwops); 
       
    76 	rwops->close(rwops); unlink(FBASENAME2);
       
    77 	rwops = SDL_RWFromFile(FBASENAME2,"ab"); 
       
    78 	if (!rwops) RWOP_ERR_QUIT(rwops); 
       
    79 	rwops->close(rwops); unlink(FBASENAME2);
       
    80 	rwops = SDL_RWFromFile(FBASENAME2,"ab+");
       
    81 	if (!rwops) RWOP_ERR_QUIT(rwops); 
       
    82 	rwops->close(rwops); unlink(FBASENAME2);
       
    83 	printf("test2 OK\n");
       
    84 
       
    85 /* test 3 : creation, writing , reading, seeking, 
       
    86 	        test : w mode, r mode, w+ mode
       
    87  */
       
    88 	rwops = SDL_RWFromFile(FBASENAME1,"wb"); /* write only */	
       
    89 	if (!rwops)											RWOP_ERR_QUIT(rwops);
       
    90 	if (1 != rwops->write(rwops,"1234567890",10,1) )	RWOP_ERR_QUIT(rwops);		
       
    91 	if (10 != rwops->write(rwops,"1234567890",1,10) )	RWOP_ERR_QUIT(rwops);		
       
    92 	if (7 != rwops->write(rwops,"1234567",1,7) )		RWOP_ERR_QUIT(rwops);		
       
    93 	if (0!=rwops->seek(rwops,0L,RW_SEEK_SET))			RWOP_ERR_QUIT(rwops);					
       
    94 	if (0!=rwops->read(rwops,test_buf,1,1))				RWOP_ERR_QUIT(rwops); /* we are in write only mode */
       
    95 	rwops->close(rwops);
       
    96 
       
    97 	rwops = SDL_RWFromFile(FBASENAME1,"rb"); /* read mode, file must exists */
       
    98 	if (!rwops)											RWOP_ERR_QUIT(rwops);
       
    99 	if (0!=rwops->seek(rwops,0L,RW_SEEK_SET))			RWOP_ERR_QUIT(rwops);					
       
   100 	if (20!=rwops->seek(rwops,-7,RW_SEEK_END))			RWOP_ERR_QUIT(rwops);					
       
   101 	if (7!=rwops->read(rwops,test_buf,1,7))				RWOP_ERR_QUIT(rwops);		
       
   102 	if (SDL_memcmp(test_buf,"1234567",7))				RWOP_ERR_QUIT(rwops);		
       
   103 	if (0!=rwops->read(rwops,test_buf,1,1))				RWOP_ERR_QUIT(rwops);		
       
   104 	if (0!=rwops->read(rwops,test_buf,10,100))			RWOP_ERR_QUIT(rwops);		
       
   105 	if (0!=rwops->seek(rwops,-27,RW_SEEK_CUR))			RWOP_ERR_QUIT(rwops);		
       
   106 	if (2!=rwops->read(rwops,test_buf,10,3))			RWOP_ERR_QUIT(rwops);				
       
   107 	if (SDL_memcmp(test_buf,"12345678901234567890",20))	RWOP_ERR_QUIT(rwops);
       
   108 	if (0!=rwops->write(rwops,test_buf,1,1))			RWOP_ERR_QUIT(rwops); /* readonly mode */
       
   109 	rwops->close(rwops);
       
   110 
       
   111 /* test 3: same with w+ mode */
       
   112 	rwops = SDL_RWFromFile(FBASENAME1,"wb+"); /* write + read + truncation */		
       
   113 	if (!rwops)											RWOP_ERR_QUIT(rwops);
       
   114 	if (1 != rwops->write(rwops,"1234567890",10,1) )	RWOP_ERR_QUIT(rwops);		
       
   115 	if (10 != rwops->write(rwops,"1234567890",1,10) )	RWOP_ERR_QUIT(rwops);		
       
   116 	if (7 != rwops->write(rwops,"1234567",1,7) )		RWOP_ERR_QUIT(rwops);		
       
   117 	if (0!=rwops->seek(rwops,0L,RW_SEEK_SET))			RWOP_ERR_QUIT(rwops);					
       
   118 	if (1!=rwops->read(rwops,test_buf,1,1))				RWOP_ERR_QUIT(rwops); /* we are in read/write mode */
       
   119 	if (0!=rwops->seek(rwops,0L,RW_SEEK_SET))			RWOP_ERR_QUIT(rwops);					
       
   120 	if (20!=rwops->seek(rwops,-7,RW_SEEK_END))			RWOP_ERR_QUIT(rwops);					
       
   121 	if (7!=rwops->read(rwops,test_buf,1,7))				RWOP_ERR_QUIT(rwops);		
       
   122 	if (SDL_memcmp(test_buf,"1234567",7))				RWOP_ERR_QUIT(rwops);		
       
   123 	if (0!=rwops->read(rwops,test_buf,1,1))				RWOP_ERR_QUIT(rwops);		
       
   124 	if (0!=rwops->read(rwops,test_buf,10,100))			RWOP_ERR_QUIT(rwops);		
       
   125 	if (0!=rwops->seek(rwops,-27,RW_SEEK_CUR))			RWOP_ERR_QUIT(rwops);		
       
   126 	if (2!=rwops->read(rwops,test_buf,10,3))			RWOP_ERR_QUIT(rwops);				
       
   127 	if (SDL_memcmp(test_buf,"12345678901234567890",20))	RWOP_ERR_QUIT(rwops);
       
   128 	rwops->close(rwops);
       
   129 	printf("test3 OK\n");
       
   130 
       
   131 /* test 4: same in r+ mode */
       
   132 	rwops = SDL_RWFromFile(FBASENAME1,"rb+"); /* write + read + file must exists, no truncation */		
       
   133 	if (!rwops)											RWOP_ERR_QUIT(rwops);
       
   134 	if (1 != rwops->write(rwops,"1234567890",10,1) )	RWOP_ERR_QUIT(rwops);		
       
   135 	if (10 != rwops->write(rwops,"1234567890",1,10) )	RWOP_ERR_QUIT(rwops);		
       
   136 	if (7 != rwops->write(rwops,"1234567",1,7) )		RWOP_ERR_QUIT(rwops);		
       
   137 	if (0!=rwops->seek(rwops,0L,RW_SEEK_SET))			RWOP_ERR_QUIT(rwops);					
       
   138 	if (1!=rwops->read(rwops,test_buf,1,1))				RWOP_ERR_QUIT(rwops); /* we are in read/write mode */
       
   139 	if (0!=rwops->seek(rwops,0L,RW_SEEK_SET))			RWOP_ERR_QUIT(rwops);					
       
   140 	if (20!=rwops->seek(rwops,-7,RW_SEEK_END))			RWOP_ERR_QUIT(rwops);					
       
   141 	if (7!=rwops->read(rwops,test_buf,1,7))				RWOP_ERR_QUIT(rwops);		
       
   142 	if (SDL_memcmp(test_buf,"1234567",7))				RWOP_ERR_QUIT(rwops);		
       
   143 	if (0!=rwops->read(rwops,test_buf,1,1))				RWOP_ERR_QUIT(rwops);		
       
   144 	if (0!=rwops->read(rwops,test_buf,10,100))			RWOP_ERR_QUIT(rwops);		
       
   145 	if (0!=rwops->seek(rwops,-27,RW_SEEK_CUR))			RWOP_ERR_QUIT(rwops);		
       
   146 	if (2!=rwops->read(rwops,test_buf,10,3))			RWOP_ERR_QUIT(rwops);				
       
   147 	if (SDL_memcmp(test_buf,"12345678901234567890",20))	RWOP_ERR_QUIT(rwops);
       
   148 	rwops->close(rwops);
       
   149 	printf("test4 OK\n");
       
   150 
       
   151 /* test5 : append mode */
       
   152 	rwops = SDL_RWFromFile(FBASENAME1,"ab+"); /* write + read + append */		
       
   153 	if (!rwops)											RWOP_ERR_QUIT(rwops);
       
   154 	if (1 != rwops->write(rwops,"1234567890",10,1) )	RWOP_ERR_QUIT(rwops);		
       
   155 	if (10 != rwops->write(rwops,"1234567890",1,10) )	RWOP_ERR_QUIT(rwops);		
       
   156 	if (7 != rwops->write(rwops,"1234567",1,7) )		RWOP_ERR_QUIT(rwops); 	
       
   157 	if (0!=rwops->seek(rwops,0L,RW_SEEK_SET))			RWOP_ERR_QUIT(rwops);					
       
   158 	
       
   159 	if (1!=rwops->read(rwops,test_buf,1,1))				RWOP_ERR_QUIT(rwops); 
       
   160 	if (0!=rwops->seek(rwops,0L,RW_SEEK_SET))			RWOP_ERR_QUIT(rwops);					
       
   161 	
       
   162 	if (20+27!=rwops->seek(rwops,-7,RW_SEEK_END))		RWOP_ERR_QUIT(rwops);					
       
   163 	if (7!=rwops->read(rwops,test_buf,1,7))				RWOP_ERR_QUIT(rwops);		
       
   164 	if (SDL_memcmp(test_buf,"1234567",7))				RWOP_ERR_QUIT(rwops);		
       
   165 	if (0!=rwops->read(rwops,test_buf,1,1))				RWOP_ERR_QUIT(rwops);		
       
   166 	if (0!=rwops->read(rwops,test_buf,10,100))			RWOP_ERR_QUIT(rwops);		
       
   167 	
       
   168 	if (27!=rwops->seek(rwops,-27,RW_SEEK_CUR))			RWOP_ERR_QUIT(rwops);
       
   169 	
       
   170 	if (0!=rwops->seek(rwops,0L,RW_SEEK_SET))			RWOP_ERR_QUIT(rwops);
       
   171 	if (3!=rwops->read(rwops,test_buf,10,3))			RWOP_ERR_QUIT(rwops);				
       
   172 	if (SDL_memcmp(test_buf,"123456789012345678901234567123",30))	
       
   173 														RWOP_ERR_QUIT(rwops);
       
   174 	rwops->close(rwops);
       
   175 	printf("test5 OK\n");
       
   176 	cleanup();
       
   177 	return 0; /* all ok */
       
   178 }