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