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