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