test/testfile.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 27 Feb 2006 19:19:22 +0000
changeset 1453 2eb75f8a32ca
child 1454 07d2b0085ecd
permissions -rw-r--r--
Date: Mon, 27 Feb 2006 17:01:43 +0100
From: "William Petiot [exoide]"
Subject: Re: [SDL] cvs version: standard win32 SDL.DLL file test [patch]

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