SDL  2.0
testfile.c File Reference
#include <stdlib.h>
#include <unistd.h>
#include "SDL.h"
#include <stdio.h>
+ Include dependency graph for testfile.c:

Go to the source code of this file.

Macros

#define _CRT_NONSTDC_NO_WARNINGS
 
#define FBASENAME1   "sdldata1" /* this file will be created during tests */
 
#define FBASENAME2   "sdldata2" /* this file should not exist before starting test */
 
#define NULL   ((void *)0)
 
#define RWOP_ERR_QUIT(x)   rwops_error_quit( __LINE__, (x) )
 

Functions

static void cleanup (void)
 
static void rwops_error_quit (unsigned line, SDL_RWops *rwops)
 
int main (int argc, char *argv[])
 

Macro Definition Documentation

◆ _CRT_NONSTDC_NO_WARNINGS

#define _CRT_NONSTDC_NO_WARNINGS

Definition at line 16 of file testfile.c.

◆ FBASENAME1

#define FBASENAME1   "sdldata1" /* this file will be created during tests */

Definition at line 35 of file testfile.c.

Referenced by cleanup(), and main().

◆ FBASENAME2

#define FBASENAME2   "sdldata2" /* this file should not exist before starting test */

Definition at line 36 of file testfile.c.

Referenced by cleanup(), and main().

◆ NULL

#define NULL   ((void *)0)

Definition at line 40 of file testfile.c.

Referenced by main().

◆ RWOP_ERR_QUIT

#define RWOP_ERR_QUIT (   x)    rwops_error_quit( __LINE__, (x) )

Definition at line 61 of file testfile.c.

Referenced by main().

Function Documentation

◆ cleanup()

static void cleanup ( void  )
static

Definition at line 44 of file testfile.c.

References FBASENAME1, and FBASENAME2.

Referenced by main(), rwops_error_quit(), and video_getSetWindowData().

45 {
46  unlink(FBASENAME1);
47  unlink(FBASENAME2);
48 }
#define FBASENAME1
Definition: testfile.c:35
#define FBASENAME2
Definition: testfile.c:36

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 66 of file testfile.c.

References cleanup(), SDL_RWops::close, FBASENAME1, FBASENAME2, NULL, SDL_RWops::read, RW_SEEK_CUR, RW_SEEK_END, RW_SEEK_SET, RWOP_ERR_QUIT, SDL_Log, SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, SDL_LogSetPriority, SDL_memcmp, SDL_RWFromFile, SDL_RWops::seek, and SDL_RWops::write.

67 {
68  SDL_RWops *rwops = NULL;
69  char test_buf[30];
70 
71  /* Enable standard application logging */
73 
74  cleanup();
75 
76 /* test 1 : basic argument test: all those calls to SDL_RWFromFile should fail */
77 
78  rwops = SDL_RWFromFile(NULL, NULL);
79  if (rwops)
80  RWOP_ERR_QUIT(rwops);
81  rwops = SDL_RWFromFile(NULL, "ab+");
82  if (rwops)
83  RWOP_ERR_QUIT(rwops);
84  rwops = SDL_RWFromFile(NULL, "sldfkjsldkfj");
85  if (rwops)
86  RWOP_ERR_QUIT(rwops);
87  rwops = SDL_RWFromFile("something", "");
88  if (rwops)
89  RWOP_ERR_QUIT(rwops);
90  rwops = SDL_RWFromFile("something", NULL);
91  if (rwops)
92  RWOP_ERR_QUIT(rwops);
93  SDL_Log("test1 OK\n");
94 
95 /* test 2 : check that inexistent file is not successfully opened/created when required */
96 /* modes : r, r+ imply that file MUST exist
97  modes : a, a+, w, w+ checks that it succeeds (file may not exists)
98 
99  */
100  rwops = SDL_RWFromFile(FBASENAME2, "rb"); /* this file doesn't exist that call must fail */
101  if (rwops)
102  RWOP_ERR_QUIT(rwops);
103  rwops = SDL_RWFromFile(FBASENAME2, "rb+"); /* this file doesn't exist that call must fail */
104  if (rwops)
105  RWOP_ERR_QUIT(rwops);
106  rwops = SDL_RWFromFile(FBASENAME2, "wb");
107  if (!rwops)
108  RWOP_ERR_QUIT(rwops);
109  rwops->close(rwops);
110  unlink(FBASENAME2);
111  rwops = SDL_RWFromFile(FBASENAME2, "wb+");
112  if (!rwops)
113  RWOP_ERR_QUIT(rwops);
114  rwops->close(rwops);
115  unlink(FBASENAME2);
116  rwops = SDL_RWFromFile(FBASENAME2, "ab");
117  if (!rwops)
118  RWOP_ERR_QUIT(rwops);
119  rwops->close(rwops);
120  unlink(FBASENAME2);
121  rwops = SDL_RWFromFile(FBASENAME2, "ab+");
122  if (!rwops)
123  RWOP_ERR_QUIT(rwops);
124  rwops->close(rwops);
125  unlink(FBASENAME2);
126  SDL_Log("test2 OK\n");
127 
128 /* test 3 : creation, writing , reading, seeking,
129  test : w mode, r mode, w+ mode
130  */
131  rwops = SDL_RWFromFile(FBASENAME1, "wb"); /* write only */
132  if (!rwops)
133  RWOP_ERR_QUIT(rwops);
134  if (1 != rwops->write(rwops, "1234567890", 10, 1))
135  RWOP_ERR_QUIT(rwops);
136  if (10 != rwops->write(rwops, "1234567890", 1, 10))
137  RWOP_ERR_QUIT(rwops);
138  if (7 != rwops->write(rwops, "1234567", 1, 7))
139  RWOP_ERR_QUIT(rwops);
140  if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
141  RWOP_ERR_QUIT(rwops);
142  if (0 != rwops->read(rwops, test_buf, 1, 1))
143  RWOP_ERR_QUIT(rwops); /* we are in write only mode */
144  rwops->close(rwops);
145 
146  rwops = SDL_RWFromFile(FBASENAME1, "rb"); /* read mode, file must exists */
147  if (!rwops)
148  RWOP_ERR_QUIT(rwops);
149  if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
150  RWOP_ERR_QUIT(rwops);
151  if (20 != rwops->seek(rwops, -7, RW_SEEK_END))
152  RWOP_ERR_QUIT(rwops);
153  if (7 != rwops->read(rwops, test_buf, 1, 7))
154  RWOP_ERR_QUIT(rwops);
155  if (SDL_memcmp(test_buf, "1234567", 7))
156  RWOP_ERR_QUIT(rwops);
157  if (0 != rwops->read(rwops, test_buf, 1, 1))
158  RWOP_ERR_QUIT(rwops);
159  if (0 != rwops->read(rwops, test_buf, 10, 100))
160  RWOP_ERR_QUIT(rwops);
161  if (0 != rwops->seek(rwops, -27, RW_SEEK_CUR))
162  RWOP_ERR_QUIT(rwops);
163  if (2 != rwops->read(rwops, test_buf, 10, 3))
164  RWOP_ERR_QUIT(rwops);
165  if (SDL_memcmp(test_buf, "12345678901234567890", 20))
166  RWOP_ERR_QUIT(rwops);
167  if (0 != rwops->write(rwops, test_buf, 1, 1))
168  RWOP_ERR_QUIT(rwops); /* readonly mode */
169  rwops->close(rwops);
170 
171 /* test 3: same with w+ mode */
172  rwops = SDL_RWFromFile(FBASENAME1, "wb+"); /* write + read + truncation */
173  if (!rwops)
174  RWOP_ERR_QUIT(rwops);
175  if (1 != rwops->write(rwops, "1234567890", 10, 1))
176  RWOP_ERR_QUIT(rwops);
177  if (10 != rwops->write(rwops, "1234567890", 1, 10))
178  RWOP_ERR_QUIT(rwops);
179  if (7 != rwops->write(rwops, "1234567", 1, 7))
180  RWOP_ERR_QUIT(rwops);
181  if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
182  RWOP_ERR_QUIT(rwops);
183  if (1 != rwops->read(rwops, test_buf, 1, 1))
184  RWOP_ERR_QUIT(rwops); /* we are in read/write mode */
185  if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
186  RWOP_ERR_QUIT(rwops);
187  if (20 != rwops->seek(rwops, -7, RW_SEEK_END))
188  RWOP_ERR_QUIT(rwops);
189  if (7 != rwops->read(rwops, test_buf, 1, 7))
190  RWOP_ERR_QUIT(rwops);
191  if (SDL_memcmp(test_buf, "1234567", 7))
192  RWOP_ERR_QUIT(rwops);
193  if (0 != rwops->read(rwops, test_buf, 1, 1))
194  RWOP_ERR_QUIT(rwops);
195  if (0 != rwops->read(rwops, test_buf, 10, 100))
196  RWOP_ERR_QUIT(rwops);
197  if (0 != rwops->seek(rwops, -27, RW_SEEK_CUR))
198  RWOP_ERR_QUIT(rwops);
199  if (2 != rwops->read(rwops, test_buf, 10, 3))
200  RWOP_ERR_QUIT(rwops);
201  if (SDL_memcmp(test_buf, "12345678901234567890", 20))
202  RWOP_ERR_QUIT(rwops);
203  rwops->close(rwops);
204  SDL_Log("test3 OK\n");
205 
206 /* test 4: same in r+ mode */
207  rwops = SDL_RWFromFile(FBASENAME1, "rb+"); /* write + read + file must exists, no truncation */
208  if (!rwops)
209  RWOP_ERR_QUIT(rwops);
210  if (1 != rwops->write(rwops, "1234567890", 10, 1))
211  RWOP_ERR_QUIT(rwops);
212  if (10 != rwops->write(rwops, "1234567890", 1, 10))
213  RWOP_ERR_QUIT(rwops);
214  if (7 != rwops->write(rwops, "1234567", 1, 7))
215  RWOP_ERR_QUIT(rwops);
216  if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
217  RWOP_ERR_QUIT(rwops);
218  if (1 != rwops->read(rwops, test_buf, 1, 1))
219  RWOP_ERR_QUIT(rwops); /* we are in read/write mode */
220  if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
221  RWOP_ERR_QUIT(rwops);
222  if (20 != rwops->seek(rwops, -7, RW_SEEK_END))
223  RWOP_ERR_QUIT(rwops);
224  if (7 != rwops->read(rwops, test_buf, 1, 7))
225  RWOP_ERR_QUIT(rwops);
226  if (SDL_memcmp(test_buf, "1234567", 7))
227  RWOP_ERR_QUIT(rwops);
228  if (0 != rwops->read(rwops, test_buf, 1, 1))
229  RWOP_ERR_QUIT(rwops);
230  if (0 != rwops->read(rwops, test_buf, 10, 100))
231  RWOP_ERR_QUIT(rwops);
232  if (0 != rwops->seek(rwops, -27, RW_SEEK_CUR))
233  RWOP_ERR_QUIT(rwops);
234  if (2 != rwops->read(rwops, test_buf, 10, 3))
235  RWOP_ERR_QUIT(rwops);
236  if (SDL_memcmp(test_buf, "12345678901234567890", 20))
237  RWOP_ERR_QUIT(rwops);
238  rwops->close(rwops);
239  SDL_Log("test4 OK\n");
240 
241 /* test5 : append mode */
242  rwops = SDL_RWFromFile(FBASENAME1, "ab+"); /* write + read + append */
243  if (!rwops)
244  RWOP_ERR_QUIT(rwops);
245  if (1 != rwops->write(rwops, "1234567890", 10, 1))
246  RWOP_ERR_QUIT(rwops);
247  if (10 != rwops->write(rwops, "1234567890", 1, 10))
248  RWOP_ERR_QUIT(rwops);
249  if (7 != rwops->write(rwops, "1234567", 1, 7))
250  RWOP_ERR_QUIT(rwops);
251  if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
252  RWOP_ERR_QUIT(rwops);
253 
254  if (1 != rwops->read(rwops, test_buf, 1, 1))
255  RWOP_ERR_QUIT(rwops);
256  if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
257  RWOP_ERR_QUIT(rwops);
258 
259  if (20 + 27 != rwops->seek(rwops, -7, RW_SEEK_END))
260  RWOP_ERR_QUIT(rwops);
261  if (7 != rwops->read(rwops, test_buf, 1, 7))
262  RWOP_ERR_QUIT(rwops);
263  if (SDL_memcmp(test_buf, "1234567", 7))
264  RWOP_ERR_QUIT(rwops);
265  if (0 != rwops->read(rwops, test_buf, 1, 1))
266  RWOP_ERR_QUIT(rwops);
267  if (0 != rwops->read(rwops, test_buf, 10, 100))
268  RWOP_ERR_QUIT(rwops);
269 
270  if (27 != rwops->seek(rwops, -27, RW_SEEK_CUR))
271  RWOP_ERR_QUIT(rwops);
272 
273  if (0 != rwops->seek(rwops, 0L, RW_SEEK_SET))
274  RWOP_ERR_QUIT(rwops);
275  if (3 != rwops->read(rwops, test_buf, 10, 3))
276  RWOP_ERR_QUIT(rwops);
277  if (SDL_memcmp(test_buf, "123456789012345678901234567123", 30))
278  RWOP_ERR_QUIT(rwops);
279  rwops->close(rwops);
280  SDL_Log("test5 OK\n");
281  cleanup();
282  return 0; /* all ok */
283 }
#define NULL
Definition: testfile.c:40
size_t(* write)(struct SDL_RWops *context, const void *ptr, size_t size, size_t num)
Definition: SDL_rwops.h:83
#define RW_SEEK_END
Definition: SDL_rwops.h:176
Sint64(* seek)(struct SDL_RWops *context, Sint64 offset, int whence)
Definition: SDL_rwops.h:65
#define SDL_Log
#define SDL_RWFromFile
int(* close)(struct SDL_RWops *context)
Definition: SDL_rwops.h:91
#define SDL_memcmp
#define RWOP_ERR_QUIT(x)
Definition: testfile.c:61
#define SDL_LogSetPriority
size_t(* read)(struct SDL_RWops *context, void *ptr, size_t size, size_t maxnum)
Definition: SDL_rwops.h:74
#define RW_SEEK_SET
Definition: SDL_rwops.h:174
#define FBASENAME1
Definition: testfile.c:35
#define RW_SEEK_CUR
Definition: SDL_rwops.h:175
#define FBASENAME2
Definition: testfile.c:36
static void cleanup(void)
Definition: testfile.c:44

◆ rwops_error_quit()

static void rwops_error_quit ( unsigned  line,
SDL_RWops rwops 
)
static

Definition at line 51 of file testfile.c.

References cleanup(), SDL_RWops::close, SDL_LOG_CATEGORY_APPLICATION, and SDL_LogError.

52 {
53  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "testfile.c(%d): failed\n", line);
54  if (rwops) {
55  rwops->close(rwops); /* This calls SDL_FreeRW(rwops); */
56  }
57  cleanup();
58  exit(1); /* quit with rwops error (test failed) */
59 }
#define SDL_LogError
int(* close)(struct SDL_RWops *context)
Definition: SDL_rwops.h:91
static void cleanup(void)
Definition: testfile.c:44