test/testfile.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 15 Jun 2007 07:19:05 +0000
branchSDL-1.2
changeset 3965 19d708301fab
parent 3927 f86ced1837de
permissions -rw-r--r--
Make sure __inline__ isn't defined before #defining it, since some compilers
(like Sun Studio) have this as a predefined macro.

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