1 /* GLib testing framework examples and tests
2 * Copyright (C) 2008 Red Hat, Inc.
3 * Authors: Matthias Clasen <mclasen@redhat.com>
4 *
5 * This work is provided "as is"; redistribution and modification
6 * in whole or in part, in any medium, physical or electronic is
7 * permitted without restriction.
8 *
9 * This work is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 *
13 * In no event shall the authors or contributors be liable for any
14 * direct, indirect, incidental, special, exemplary, or consequential
15 * damages (including, but not limited to, procurement of substitute
16 * goods or services; loss of use, data, or profits; or business
17 * interruption) however caused and on any theory of liability, whether
18 * in contract, strict liability, or tort (including negligence or
19 * otherwise) arising in any way out of the use of this software, even
20 * if advised of the possibility of such damage.
21 */
22
23 #include <glib/glib.h>
24 #include <gio/gio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 static void
test_peek(void)29 test_peek (void)
30 {
31 GInputStream *base;
32 GInputStream *in;
33 gssize npeek;
34 char *buffer;
35
36 base = g_memory_input_stream_new_from_data ("abcdefghijk", -1, NULL);
37 in = g_buffered_input_stream_new_sized (base, 64);
38
39 g_buffered_input_stream_fill (G_BUFFERED_INPUT_STREAM (in), 5, NULL, NULL);
40 g_assert_cmpint (g_buffered_input_stream_get_available (G_BUFFERED_INPUT_STREAM (in)), ==, 5);
41 g_buffered_input_stream_fill (G_BUFFERED_INPUT_STREAM (in), -1, NULL, NULL);
42 g_assert_cmpint (g_buffered_input_stream_get_available (G_BUFFERED_INPUT_STREAM (in)), ==, strlen ("abcdefjhijk"));
43
44 buffer = g_new0 (char, 64);
45 npeek = g_buffered_input_stream_peek (G_BUFFERED_INPUT_STREAM (in), buffer, 2, 3);
46 g_assert_cmpint (npeek, ==, 3);
47 g_assert_cmpstr ("cde", ==, buffer);
48 g_free (buffer);
49
50 buffer = g_new0 (char, 64);
51 npeek = g_buffered_input_stream_peek (G_BUFFERED_INPUT_STREAM (in), buffer, 9, 5);
52 g_assert_cmpint (npeek, ==, 2);
53 g_assert_cmpstr ("jk", ==, buffer);
54 g_free (buffer);
55
56 buffer = g_new0 (char, 64);
57 npeek = g_buffered_input_stream_peek (G_BUFFERED_INPUT_STREAM (in), buffer, 75, 3);
58 g_assert_cmpint (npeek, ==, 0);
59 g_free (buffer);
60
61 g_object_unref (in);
62 g_object_unref (base);
63 }
64
65 static void
test_peek_buffer(void)66 test_peek_buffer (void)
67 {
68 GInputStream *base;
69 GInputStream *in;
70 gssize nfill;
71 gsize bufsize;
72 char *buffer;
73
74 base = g_memory_input_stream_new_from_data ("abcdefghijk", -1, NULL);
75 in = g_buffered_input_stream_new (base);
76
77 nfill = g_buffered_input_stream_fill (G_BUFFERED_INPUT_STREAM (in), strlen ("abcdefghijk"), NULL, NULL);
78 buffer = (char *) g_buffered_input_stream_peek_buffer (G_BUFFERED_INPUT_STREAM (in), &bufsize);
79 g_assert_cmpint (nfill, ==, bufsize);
80 g_assert (0 == strncmp ("abcdefghijk", buffer, bufsize));
81
82 g_object_unref (in);
83 g_object_unref (base);
84 }
85
86 static void
test_set_buffer_size(void)87 test_set_buffer_size (void)
88 {
89 GInputStream *base;
90 GInputStream *in;
91 guint bufsize_prop;
92 gsize size, bufsize;
93
94 base = g_memory_input_stream_new_from_data ("abcdefghijk", -1, NULL);
95 in = g_buffered_input_stream_new (base);
96 size = g_buffered_input_stream_get_buffer_size (G_BUFFERED_INPUT_STREAM (in));
97 g_assert_cmpint (size, ==, 4096);
98
99 g_buffered_input_stream_set_buffer_size (G_BUFFERED_INPUT_STREAM (in), 64);
100 size = g_buffered_input_stream_get_buffer_size (G_BUFFERED_INPUT_STREAM (in));
101 g_assert_cmpint (size, ==, 64);
102
103 /* size cannot shrink below current content len */
104 g_buffered_input_stream_fill (G_BUFFERED_INPUT_STREAM (in), strlen ("abcdefghijk"), NULL, NULL);
105 g_buffered_input_stream_peek_buffer (G_BUFFERED_INPUT_STREAM (in), &bufsize);
106 g_buffered_input_stream_set_buffer_size (G_BUFFERED_INPUT_STREAM (in), 2);
107 size = g_buffered_input_stream_get_buffer_size (G_BUFFERED_INPUT_STREAM (in));
108 g_assert_cmpint (size, ==, bufsize);
109 g_object_get (in, "buffer-size", &bufsize_prop, NULL);
110 g_assert_cmpint (bufsize_prop, ==, bufsize);
111
112 g_object_unref (in);
113
114 in = g_buffered_input_stream_new_sized (base, 64);
115 size = g_buffered_input_stream_get_buffer_size (G_BUFFERED_INPUT_STREAM (in));
116 g_assert_cmpint (size, ==, 64);
117
118 g_object_unref (in);
119 g_object_unref (base);
120 }
121
122 static void
test_read_byte(void)123 test_read_byte (void)
124 {
125 GInputStream *base;
126 GInputStream *in;
127 GError *error;
128
129 g_test_bug ("562393");
130
131 base = g_memory_input_stream_new_from_data ("abcdefgh", -1, NULL);
132 in = g_buffered_input_stream_new (base);
133
134 error = NULL;
135 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error), ==, 'a');
136 g_assert_no_error (error);
137 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error), ==, 'b');
138 g_assert_no_error (error);
139 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error), ==, 'c');
140 g_assert_no_error (error);
141
142 g_assert_cmpint (g_input_stream_skip (in, 3, NULL, &error), ==, 3);
143 g_assert_no_error (error);
144
145 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error), ==, 'g');
146 g_assert_no_error (error);
147 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error), ==, 'h');
148 g_assert_no_error (error);
149 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error), ==, -1);
150 g_assert_no_error (error);
151
152 g_assert (g_input_stream_close (in, NULL, &error));
153 g_assert_no_error (error);
154 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error), ==, -1);
155 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED);
156 g_error_free (error);
157
158 g_object_unref (in);
159 g_object_unref (base);
160 }
161
162 static void
test_read(void)163 test_read (void)
164 {
165 GInputStream *base;
166 GInputStream *in;
167 gchar buffer[20];
168 GError *error;
169
170 base = g_memory_input_stream_new_from_data ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", -1, NULL);
171 in = g_buffered_input_stream_new_sized (base, 8);
172
173 g_assert_cmpint (g_buffered_input_stream_get_available (G_BUFFERED_INPUT_STREAM (in)), ==, 0);
174
175 error = NULL;
176 g_assert_cmpint (g_buffered_input_stream_fill (G_BUFFERED_INPUT_STREAM (in), 8, NULL, &error), ==, 8);
177 g_assert_no_error (error);
178
179 g_assert_cmpint (g_buffered_input_stream_get_available (G_BUFFERED_INPUT_STREAM (in)), ==, 8);
180
181 memset (buffer, 0, 20);
182 g_assert_cmpint (g_input_stream_read (in, &buffer, 16, NULL, &error), ==, 16);
183 g_assert_cmpstr (buffer, ==, "abcdefghijklmnop");
184 g_assert_no_error (error);
185
186 g_assert_cmpint (g_buffered_input_stream_get_available (G_BUFFERED_INPUT_STREAM (in)), ==, 0);
187
188 memset (buffer, 0, 20);
189 g_assert_cmpint (g_input_stream_read (in, &buffer, 16, NULL, &error), ==, 16);
190 g_assert_cmpstr (buffer, ==, "qrstuvwxyzABCDEF");
191 g_assert_no_error (error);
192
193 memset (buffer, 0, 20);
194 g_assert_cmpint (g_input_stream_read (in, &buffer, 16, NULL, &error), ==, 16);
195 g_assert_cmpstr (buffer, ==, "GHIJKLMNOPQRSTUV");
196 g_assert_no_error (error);
197
198 memset (buffer, 0, 20);
199 g_assert_cmpint (g_input_stream_read (in, &buffer, 16, NULL, &error), ==, 4);
200 g_assert_cmpstr (buffer, ==, "WXYZ");
201 g_assert_no_error (error);
202
203 memset (buffer, 0, 20);
204 g_assert_cmpint (g_input_stream_read (in, &buffer, 16, NULL, &error), ==, 0);
205 g_assert_no_error (error);
206
207 g_object_unref (in);
208 g_object_unref (base);
209 }
210
211 static void
return_result_cb(GObject * object,GAsyncResult * result,gpointer user_data)212 return_result_cb (GObject *object,
213 GAsyncResult *result,
214 gpointer user_data)
215 {
216 GAsyncResult **ret = user_data;
217
218 *ret = g_object_ref (result);
219 }
220
221 static void
test_read_async(void)222 test_read_async (void)
223 {
224 GInputStream *base;
225 GInputStream *in;
226 gchar buffer[20];
227 GError *error;
228 GAsyncResult *result;
229
230 base = g_memory_input_stream_new_from_data ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", -1, NULL);
231 in = g_buffered_input_stream_new_sized (base, 8);
232
233 g_assert_cmpint (g_buffered_input_stream_get_available (G_BUFFERED_INPUT_STREAM (in)), ==, 0);
234
235 error = NULL;
236 result = NULL;
237 g_buffered_input_stream_fill_async (G_BUFFERED_INPUT_STREAM (in), 8,
238 G_PRIORITY_DEFAULT, NULL,
239 return_result_cb, &result);
240 while (!result)
241 g_main_context_iteration (NULL, TRUE);
242 g_assert_cmpint (g_buffered_input_stream_fill_finish (G_BUFFERED_INPUT_STREAM (in), result, &error), ==, 8);
243 g_assert_no_error (error);
244 g_clear_object (&result);
245
246 g_assert_cmpint (g_buffered_input_stream_get_available (G_BUFFERED_INPUT_STREAM (in)), ==, 8);
247
248 memset (buffer, 0, 20);
249 g_input_stream_read_async (in, &buffer, 16, G_PRIORITY_DEFAULT,
250 NULL, return_result_cb, &result);
251 while (!result)
252 g_main_context_iteration (NULL, TRUE);
253 g_assert_cmpint (g_input_stream_read_finish (in, result, &error), ==, 16);
254 g_assert_cmpstr (buffer, ==, "abcdefghijklmnop");
255 g_assert_no_error (error);
256 g_clear_object (&result);
257
258 g_assert_cmpint (g_buffered_input_stream_get_available (G_BUFFERED_INPUT_STREAM (in)), ==, 0);
259
260 memset (buffer, 0, 20);
261 g_input_stream_read_async (in, &buffer, 16, G_PRIORITY_DEFAULT,
262 NULL, return_result_cb, &result);
263 while (!result)
264 g_main_context_iteration (NULL, TRUE);
265 g_assert_cmpint (g_input_stream_read_finish (in, result, &error), ==, 16);
266 g_assert_cmpstr (buffer, ==, "qrstuvwxyzABCDEF");
267 g_assert_no_error (error);
268 g_clear_object (&result);
269
270 memset (buffer, 0, 20);
271 g_input_stream_read_async (in, &buffer, 16, G_PRIORITY_DEFAULT,
272 NULL, return_result_cb, &result);
273 while (!result)
274 g_main_context_iteration (NULL, TRUE);
275 g_assert_cmpint (g_input_stream_read_finish (in, result, &error), ==, 16);
276 g_assert_cmpstr (buffer, ==, "GHIJKLMNOPQRSTUV");
277 g_assert_no_error (error);
278 g_clear_object (&result);
279
280 memset (buffer, 0, 20);
281 g_input_stream_read_async (in, &buffer, 16, G_PRIORITY_DEFAULT,
282 NULL, return_result_cb, &result);
283 while (!result)
284 g_main_context_iteration (NULL, TRUE);
285 g_assert_cmpint (g_input_stream_read_finish (in, result, &error), ==, 4);
286 g_assert_cmpstr (buffer, ==, "WXYZ");
287 g_assert_no_error (error);
288 g_clear_object (&result);
289
290 memset (buffer, 0, 20);
291 g_input_stream_read_async (in, &buffer, 16, G_PRIORITY_DEFAULT,
292 NULL, return_result_cb, &result);
293 while (!result)
294 g_main_context_iteration (NULL, TRUE);
295 g_assert_cmpint (g_input_stream_read_finish (in, result, &error), ==, 0);
296 g_assert_no_error (error);
297 g_clear_object (&result);
298
299 g_object_unref (in);
300 g_object_unref (base);
301 }
302
303 static void
test_skip(void)304 test_skip (void)
305 {
306 GInputStream *base;
307 GInputStream *in;
308 GError *error;
309
310 base = g_memory_input_stream_new_from_data ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVXYZ", -1, NULL);
311 in = g_buffered_input_stream_new_sized (base, 5);
312
313 error = NULL;
314 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error), ==, 'a');
315 g_assert_no_error (error);
316 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error), ==, 'b');
317 g_assert_no_error (error);
318 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error), ==, 'c');
319 g_assert_no_error (error);
320
321 g_assert_cmpint (g_input_stream_skip (in, 7, NULL, &error), ==, 7);
322 g_assert_no_error (error);
323 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error), ==, 'k');
324 g_assert_no_error (error);
325
326 g_assert_cmpint (g_input_stream_skip (in, 10, NULL, &error), ==, 10);
327 g_assert_no_error (error);
328 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error), ==, 'v');
329 g_assert_no_error (error);
330
331 g_assert_cmpint (g_input_stream_skip (in, 20, NULL, &error), ==, 20);
332 g_assert_no_error (error);
333 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error), ==, 'Q');
334 g_assert_no_error (error);
335
336 g_assert_cmpint (g_input_stream_skip (in, 10, NULL, &error), ==, 8);
337 g_assert_no_error (error);
338 g_assert_cmpint (g_input_stream_skip (in, 10, NULL, &error), ==, 0);
339 g_assert_no_error (error);
340
341 g_object_unref (in);
342 g_object_unref (base);
343 }
344
345 static void
test_skip_async(void)346 test_skip_async (void)
347 {
348 GInputStream *base;
349 GInputStream *in;
350 GError *error;
351 GAsyncResult *result;
352
353 base = g_memory_input_stream_new_from_data ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVXYZ", -1, NULL);
354 in = g_buffered_input_stream_new_sized (base, 5);
355
356 error = NULL;
357 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error), ==, 'a');
358 g_assert_no_error (error);
359 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error), ==, 'b');
360 g_assert_no_error (error);
361 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error), ==, 'c');
362 g_assert_no_error (error);
363
364 result = NULL;
365 g_input_stream_skip_async (in, 7, G_PRIORITY_DEFAULT,
366 NULL, return_result_cb, &result);
367 while (!result)
368 g_main_context_iteration (NULL, TRUE);
369 g_assert_cmpint (g_input_stream_skip_finish (in, result, &error), ==, 7);
370 g_assert_no_error (error);
371 g_clear_object (&result);
372 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error), ==, 'k');
373 g_assert_no_error (error);
374
375 g_input_stream_skip_async (in, 10, G_PRIORITY_DEFAULT,
376 NULL, return_result_cb, &result);
377 while (!result)
378 g_main_context_iteration (NULL, TRUE);
379 g_assert_cmpint (g_input_stream_skip_finish (in, result, &error), ==, 10);
380 g_assert_no_error (error);
381 g_clear_object (&result);
382 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error), ==, 'v');
383 g_assert_no_error (error);
384
385 g_input_stream_skip_async (in, 20, G_PRIORITY_DEFAULT,
386 NULL, return_result_cb, &result);
387 while (!result)
388 g_main_context_iteration (NULL, TRUE);
389 g_assert_cmpint (g_input_stream_skip_finish (in, result, &error), ==, 20);
390 g_assert_no_error (error);
391 g_clear_object (&result);
392 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error), ==, 'Q');
393 g_assert_no_error (error);
394
395 g_input_stream_skip_async (in, 10, G_PRIORITY_DEFAULT,
396 NULL, return_result_cb, &result);
397 while (!result)
398 g_main_context_iteration (NULL, TRUE);
399 g_assert_cmpint (g_input_stream_skip_finish (in, result, &error), ==, 8);
400 g_clear_object (&result);
401 g_assert_no_error (error);
402
403 g_input_stream_skip_async (in, 10, G_PRIORITY_DEFAULT,
404 NULL, return_result_cb, &result);
405 while (!result)
406 g_main_context_iteration (NULL, TRUE);
407 g_assert_cmpint (g_input_stream_skip_finish (in, result, &error), ==, 0);
408 g_clear_object (&result);
409 g_assert_no_error (error);
410
411 g_object_unref (in);
412 g_object_unref (base);
413 }
414
415 static void
test_close(void)416 test_close (void)
417 {
418 GInputStream *base;
419 GInputStream *in;
420 GError *error;
421
422 base = g_memory_input_stream_new_from_data ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVXYZ", -1, NULL);
423 in = g_buffered_input_stream_new (base);
424
425 g_assert (g_filter_input_stream_get_close_base_stream (G_FILTER_INPUT_STREAM (in)));
426
427 error = NULL;
428 g_assert (g_input_stream_close (in, NULL, &error));
429 g_assert_no_error (error);
430 g_assert (g_input_stream_is_closed (base));
431
432 g_object_unref (in);
433 g_object_unref (base);
434
435 base = g_memory_input_stream_new_from_data ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVXYZ", -1, NULL);
436 in = g_buffered_input_stream_new (base);
437
438 g_filter_input_stream_set_close_base_stream (G_FILTER_INPUT_STREAM (in), FALSE);
439
440 error = NULL;
441 g_assert (g_input_stream_close (in, NULL, &error));
442 g_assert_no_error (error);
443 g_assert (!g_input_stream_is_closed (base));
444
445 g_object_unref (in);
446 g_object_unref (base);
447 }
448
449 static void
test_seek(void)450 test_seek (void)
451 {
452 GInputStream *base;
453 GInputStream *in;
454 GError *error;
455 gint byte;
456 gboolean ret;
457
458 base = g_memory_input_stream_new_from_data ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVXYZ", -1, NULL);
459 in = g_buffered_input_stream_new_sized (base, 4);
460 error = NULL;
461
462 /* Seek by read */
463 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (in)), ==, 0);
464 byte = g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error);
465 g_assert_no_error (error);
466 g_assert_cmpint (byte, ==, 'a');
467 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (in)), ==, 1);
468
469 /* Seek forward (in buffer) */
470 ret = g_seekable_seek (G_SEEKABLE (in), 1, G_SEEK_CUR, NULL, &error);
471 g_assert_no_error (error);
472 g_assert (ret);
473 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (in)), ==, 2);
474 byte = g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error);
475 g_assert_no_error (error);
476 g_assert_cmpint (byte, ==, 'c');
477 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (in)), ==, 3);
478
479 /* Seek backward (in buffer) */
480 ret = g_seekable_seek (G_SEEKABLE (in), -2, G_SEEK_CUR, NULL, &error);
481 g_assert_no_error (error);
482 g_assert (ret);
483 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (in)), ==, 1);
484 byte = g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error);
485 g_assert_no_error (error);
486 g_assert_cmpint (byte, ==, 'b');
487 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (in)), ==, 2);
488
489 /* Seek forward (outside buffer) */
490 ret = g_seekable_seek (G_SEEKABLE (in), 6, G_SEEK_CUR, NULL, &error);
491 g_assert_no_error (error);
492 g_assert (ret);
493 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (in)), ==, 8);
494 byte = g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error);
495 g_assert_no_error (error);
496 g_assert_cmpint (byte, ==, 'i');
497 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (in)), ==, 9);
498
499 /* Seek backward (outside buffer) */
500 ret = g_seekable_seek (G_SEEKABLE (in), -6, G_SEEK_CUR, NULL, &error);
501 g_assert_no_error (error);
502 g_assert (ret);
503 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (in)), ==, 3);
504 byte = g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error);
505 g_assert_no_error (error);
506 g_assert_cmpint (byte, ==, 'd');
507 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (in)), ==, 4);
508
509 /* Seek from beginning */
510 ret = g_seekable_seek (G_SEEKABLE (in), 8, G_SEEK_SET, NULL, &error);
511 g_assert_no_error (error);
512 g_assert (ret);
513 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (in)), ==, 8);
514 byte = g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error);
515 g_assert_no_error (error);
516 g_assert_cmpint (byte, ==, 'i');
517 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (in)), ==, 9);
518
519 /* Seek from end */
520 ret = g_seekable_seek (G_SEEKABLE (in), -1, G_SEEK_END, NULL, &error);
521 g_assert_no_error (error);
522 g_assert (ret);
523 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (in)), ==, 50);
524 byte = g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error);
525 g_assert_no_error (error);
526 g_assert_cmpint (byte, ==, 'Z');
527 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (in)), ==, 51);
528
529 /* Cleanup */
530 g_object_unref (in);
531 g_object_unref (base);
532 }
533
534 int
main(int argc,char * argv[])535 main (int argc,
536 char *argv[])
537 {
538 g_test_init (&argc, &argv, NULL);
539 g_test_bug_base ("http://bugzilla.gnome.org/");
540
541 g_test_add_func ("/buffered-input-stream/peek", test_peek);
542 g_test_add_func ("/buffered-input-stream/peek-buffer", test_peek_buffer);
543 g_test_add_func ("/buffered-input-stream/set-buffer-size", test_set_buffer_size);
544 g_test_add_func ("/buffered-input-stream/read-byte", test_read_byte);
545 g_test_add_func ("/buffered-input-stream/read", test_read);
546 g_test_add_func ("/buffered-input-stream/read-async", test_read_async);
547 g_test_add_func ("/buffered-input-stream/skip", test_skip);
548 g_test_add_func ("/buffered-input-stream/skip-async", test_skip_async);
549 g_test_add_func ("/buffered-input-stream/seek", test_seek);
550 g_test_add_func ("/filter-input-stream/close", test_close);
551
552 return g_test_run();
553 }
554