comparison test/automated/rwops/rwops.c @ 3713:a34bab848c7e gsoc2009_unit_tests

Better output. Newline handling. rwops test handles ConstMem, FP and File.
author Edgar Simo <bobbens@gmail.com>
date Sat, 20 Jun 2009 16:46:58 +0000
parents
children d8772964e402
comparison
equal deleted inserted replaced
3712:916469fbdc29 3713:a34bab848c7e
1 /**
2 * Automated SDL_RWops test.
3 *
4 * Written by Edgar Simo "bobbens"
5 *
6 * Released under Public Domain.
7 */
8
9
10 #include "SDL.h"
11 #include "SDL_at.h"
12
13
14 static const char hello_world[] = "Hello World!";
15
16
17 /**
18 * @brief Does a generic rwops test.
19 *
20 * RWops should have "Hello World!" in it already if write is disabled.
21 *
22 * @param write Test writing also.
23 * @return 1 if an assert is failed.
24 */
25 static int rwops_testGeneric( SDL_RWops *rw, int write )
26 {
27 char buf[sizeof(hello_world)];
28 int i;
29
30 if (write) {
31 /* Test write. */
32 i = SDL_RWwrite( rw, hello_world, sizeof(hello_world)-1, 1 );
33 if (SDL_ATassert( "Writing with SDL_RWwrite", i == 1 ))
34 return 1;
35 }
36
37 /* Test seek. */
38 i = SDL_RWseek( rw, 6, RW_SEEK_SET );
39 if (SDL_ATassert( "Seeking with SDL_RWseek", i == 6 ))
40 return 1;
41
42 /* Test seek. */
43 i = SDL_RWseek( rw, 0, RW_SEEK_SET );
44 if (SDL_ATassert( "Seeking with SDL_RWseek", i == 0 ))
45 return 1;
46
47 /* Test read. */
48 i = SDL_RWread( rw, buf, 1, sizeof(hello_world)-1 );
49 if (i != sizeof(hello_world)-1)
50 printf("%s\n", SDL_GetError());
51 if (SDL_ATassert( "Reading with SDL_RWread", i == sizeof(hello_world)-1 ))
52 return 1;
53 if (SDL_ATassert( "Memory read does not match memory written",
54 memcmp( buf, hello_world, sizeof(hello_world)-1 ) == 0 ))
55 return 1;
56
57 return 0;
58 }
59
60
61 /**
62 * @brief Tests opening from memory.
63 */
64 static void rwops_testMem (void)
65 {
66 char mem[sizeof(hello_world)];
67 SDL_RWops *rw;
68
69 /* Begin testcase. */
70 SDL_ATbegin( "SDL_RWFromMem" );
71
72 /* Open. */
73 rw = SDL_RWFromMem( mem, sizeof(mem) );
74 if (SDL_ATassert( "Opening memory with SDL_RWFromMem", rw != NULL ))
75 return;
76
77 /* Run generic tests. */
78 if (rwops_testGeneric( rw, 1 ))
79 return;
80
81 /* Close. */
82 SDL_FreeRW( rw );
83
84 /* End testcase. */
85 SDL_ATend();
86 }
87
88
89 static const char const_mem[] = "Hello World!";
90 /**
91 * @brief Tests opening from memory.
92 */
93 static void rwops_testConstMem (void)
94 {
95 SDL_RWops *rw;
96
97 /* Begin testcase. */
98 SDL_ATbegin( "SDL_RWFromConstMem" );
99
100 /* Open. */
101 rw = SDL_RWFromConstMem( const_mem, sizeof(const_mem) );
102 if (SDL_ATassert( "Opening memory with SDL_RWFromConstMem", rw != NULL ))
103 return;
104
105 /* Run generic tests. */
106 if (rwops_testGeneric( rw, 0 ))
107 return;
108
109 /* Close. */
110 SDL_FreeRW( rw );
111
112 /* End testcase. */
113 SDL_ATend();
114 }
115
116
117 /**
118 * @brief Tests opening from memory.
119 */
120 static void rwops_testFile (void)
121 {
122 SDL_RWops *rw;
123 int i;
124
125 /* Begin testcase. */
126 SDL_ATbegin( "SDL_RWFromFile" );
127
128 /* Open. */
129 rw = SDL_RWFromFile( "rwops/read", "r" );
130 if (SDL_ATassert( "Opening memory with SDL_RWFromFile", rw != NULL ))
131 return;
132
133 /* Test writing. */
134 i = SDL_RWwrite( rw, hello_world, sizeof(hello_world), 1 );
135 if (SDL_ATassert( "Writing with SDL_RWwrite", i == 0 ))
136 return;
137
138 /* Run generic tests. */
139 if (rwops_testGeneric( rw, 0 ))
140 return;
141
142 /* Close. */
143 SDL_FreeRW( rw );
144
145 /* End testcase. */
146 SDL_ATend();
147 }
148
149
150 /**
151 * @brief Tests opening from memory.
152 */
153 static void rwops_testFP (void)
154 {
155 #ifdef HAVE_STDIO_H
156 FILE *fp;
157 SDL_RWops *rw;
158 int i;
159
160 /* Begin testcase. */
161 SDL_ATbegin( "SDL_RWFromFP" );
162
163 /* Open. */
164 fp = fopen( "rwops/write", "w+" );
165 if (fp == NULL) {
166 SDL_ATprint("Failed to open file rwops/write");
167 SDL_ATend();
168 return;
169 }
170 rw = SDL_RWFromFP( fp, 1 );
171 if (SDL_ATassert( "Opening memory with SDL_RWFromFP", rw != NULL ))
172 return;
173
174 /* Run generic tests. */
175 if (rwops_testGeneric( rw, 1 ))
176 return;
177
178 /* Close. */
179 SDL_FreeRW( rw );
180
181 /* End testcase. */
182 SDL_ATend();
183 #endif /* HAVE_STDIO_H */
184 }
185
186
187 /**
188 * @brief Entry point.
189 */
190 int main( int argc, const char *argv[] )
191 {
192 SDL_ATinit( "SDL_RWops" );
193
194 rwops_testMem();
195 rwops_testConstMem();
196 rwops_testFile();
197 rwops_testFP();
198
199 return SDL_ATfinish(1);
200 }