1 /* GLib testing framework examples and tests
2 * Copyright (C) 2008 Red Hat, Inc.
3 * Authors: Matthias Clasen <mclasen@redhat.com>
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.
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.
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.
23 #include <glib/glib.h>
36 base = g_memory_input_stream_new_from_data ("abcdefghijk", -1, NULL);
37 in = g_buffered_input_stream_new_sized (base, 64);
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"));
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);
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);
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);
62 g_object_unref (base);
66 test_peek_buffer (void)
74 base = g_memory_input_stream_new_from_data ("abcdefghijk", -1, NULL);
75 in = g_buffered_input_stream_new (base);
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));
83 g_object_unref (base);
87 test_set_buffer_size (void)
93 base = g_memory_input_stream_new_from_data ("abcdefghijk", -1, NULL);
94 in = g_buffered_input_stream_new (base);
95 size = g_buffered_input_stream_get_buffer_size (G_BUFFERED_INPUT_STREAM (in));
96 g_assert_cmpint (size, ==, 4096);
98 g_buffered_input_stream_set_buffer_size (G_BUFFERED_INPUT_STREAM (in), 64);
99 size = g_buffered_input_stream_get_buffer_size (G_BUFFERED_INPUT_STREAM (in));
100 g_assert_cmpint (size, ==, 64);
102 /* size cannot shrink below current content len */
103 g_buffered_input_stream_fill (G_BUFFERED_INPUT_STREAM (in), strlen ("abcdefghijk"), NULL, NULL);
104 g_buffered_input_stream_peek_buffer (G_BUFFERED_INPUT_STREAM (in), &bufsize);
105 g_buffered_input_stream_set_buffer_size (G_BUFFERED_INPUT_STREAM (in), 2);
106 size = g_buffered_input_stream_get_buffer_size (G_BUFFERED_INPUT_STREAM (in));
107 g_assert_cmpint (size, ==, bufsize);
108 g_object_get (in, "buffer-size", &size, NULL);
109 g_assert_cmpint (size, ==, bufsize);
113 in = g_buffered_input_stream_new_sized (base, 64);
114 size = g_buffered_input_stream_get_buffer_size (G_BUFFERED_INPUT_STREAM (in));
115 g_assert_cmpint (size, ==, 64);
118 g_object_unref (base);
122 test_read_byte (void)
128 g_test_bug ("562393");
130 base = g_memory_input_stream_new_from_data ("abcdefgh", -1, NULL);
131 in = g_buffered_input_stream_new (base);
134 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error), ==, 'a');
135 g_assert_no_error (error);
136 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error), ==, 'b');
137 g_assert_no_error (error);
138 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error), ==, 'c');
139 g_assert_no_error (error);
141 g_assert_cmpint (g_input_stream_skip (in, 3, NULL, &error), ==, 3);
142 g_assert_no_error (error);
144 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error), ==, 'g');
145 g_assert_no_error (error);
146 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error), ==, 'h');
147 g_assert_no_error (error);
148 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error), ==, -1);
149 g_assert_no_error (error);
151 g_assert (g_input_stream_close (in, NULL, &error));
152 g_assert_no_error (error);
153 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error), ==, -1);
154 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED);
155 g_error_free (error);
158 g_object_unref (base);
169 base = g_memory_input_stream_new_from_data ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", -1, NULL);
170 in = g_buffered_input_stream_new_sized (base, 8);
172 g_assert_cmpint (g_buffered_input_stream_get_available (G_BUFFERED_INPUT_STREAM (in)), ==, 0);
175 g_assert_cmpint (g_buffered_input_stream_fill (G_BUFFERED_INPUT_STREAM (in), 8, NULL, &error), ==, 8);
176 g_assert_no_error (error);
178 g_assert_cmpint (g_buffered_input_stream_get_available (G_BUFFERED_INPUT_STREAM (in)), ==, 8);
180 memset (buffer, 0, 20);
181 g_assert_cmpint (g_input_stream_read (in, &buffer, 16, NULL, &error), ==, 16);
182 g_assert_cmpstr (buffer, ==, "abcdefghijklmnop");
183 g_assert_no_error (error);
185 g_assert_cmpint (g_buffered_input_stream_get_available (G_BUFFERED_INPUT_STREAM (in)), ==, 0);
187 memset (buffer, 0, 20);
188 g_assert_cmpint (g_input_stream_read (in, &buffer, 16, NULL, &error), ==, 16);
189 g_assert_cmpstr (buffer, ==, "qrstuvwxyzABCDEF");
190 g_assert_no_error (error);
192 memset (buffer, 0, 20);
193 g_assert_cmpint (g_input_stream_read (in, &buffer, 16, NULL, &error), ==, 16);
194 g_assert_cmpstr (buffer, ==, "GHIJKLMNOPQRSTUV");
195 g_assert_no_error (error);
197 memset (buffer, 0, 20);
198 g_assert_cmpint (g_input_stream_read (in, &buffer, 16, NULL, &error), ==, 4);
199 g_assert_cmpstr (buffer, ==, "WXYZ");
200 g_assert_no_error (error);
202 memset (buffer, 0, 20);
203 g_assert_cmpint (g_input_stream_read (in, &buffer, 16, NULL, &error), ==, 0);
204 g_assert_no_error (error);
207 g_object_unref (base);
217 base = g_memory_input_stream_new_from_data ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVXYZ", -1, NULL);
218 in = g_buffered_input_stream_new_sized (base, 5);
221 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error), ==, 'a');
222 g_assert_no_error (error);
223 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error), ==, 'b');
224 g_assert_no_error (error);
225 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error), ==, 'c');
226 g_assert_no_error (error);
228 g_assert_cmpint (g_input_stream_skip (in, 7, NULL, &error), ==, 7);
229 g_assert_no_error (error);
230 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error), ==, 'k');
231 g_assert_no_error (error);
233 g_assert_cmpint (g_input_stream_skip (in, 10, NULL, &error), ==, 10);
234 g_assert_no_error (error);
235 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error), ==, 'v');
236 g_assert_no_error (error);
238 g_assert_cmpint (g_input_stream_skip (in, 20, NULL, &error), ==, 20);
239 g_assert_no_error (error);
240 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error), ==, 'Q');
241 g_assert_no_error (error);
243 g_assert_cmpint (g_input_stream_skip (in, 10, NULL, &error), ==, 8);
244 g_assert_no_error (error);
245 g_assert_cmpint (g_input_stream_skip (in, 10, NULL, &error), ==, 0);
246 g_assert_no_error (error);
249 g_object_unref (base);
259 base = g_memory_input_stream_new_from_data ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVXYZ", -1, NULL);
260 in = g_buffered_input_stream_new (base);
262 g_assert (g_filter_input_stream_get_close_base_stream (G_FILTER_INPUT_STREAM (in)));
265 g_assert (g_input_stream_close (in, NULL, &error));
266 g_assert_no_error (error);
267 g_assert (g_input_stream_is_closed (base));
270 g_object_unref (base);
272 base = g_memory_input_stream_new_from_data ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVXYZ", -1, NULL);
273 in = g_buffered_input_stream_new (base);
275 g_filter_input_stream_set_close_base_stream (G_FILTER_INPUT_STREAM (in), FALSE);
278 g_assert (g_input_stream_close (in, NULL, &error));
279 g_assert_no_error (error);
280 g_assert (!g_input_stream_is_closed (base));
283 g_object_unref (base);
295 base = g_memory_input_stream_new_from_data ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVXYZ", -1, NULL);
296 in = g_buffered_input_stream_new_sized (base, 4);
300 g_assert_cmpstr (g_seekable_tell (G_SEEKABLE (in)), ==, 0);
301 byte = g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error);
302 g_assert_no_error (error);
303 g_assert_cmpint (byte, ==, 'a');
304 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (in)), ==, 1);
306 /* Seek forward (in buffer) */
307 ret = g_seekable_seek (G_SEEKABLE (in), 1, G_SEEK_CUR, NULL, &error);
308 g_assert_no_error (error);
310 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (in)), ==, 2);
311 byte = g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error);
312 g_assert_no_error (error);
313 g_assert_cmpint (byte, ==, 'c');
314 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (in)), ==, 3);
316 /* Seek backward (in buffer) */
317 ret = g_seekable_seek (G_SEEKABLE (in), -2, G_SEEK_CUR, NULL, &error);
318 g_assert_no_error (error);
320 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (in)), ==, 1);
321 byte = g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error);
322 g_assert_no_error (error);
323 g_assert_cmpint (byte, ==, 'b');
324 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (in)), ==, 2);
326 /* Seek forward (outside buffer) */
327 ret = g_seekable_seek (G_SEEKABLE (in), 6, G_SEEK_CUR, NULL, &error);
328 g_assert_no_error (error);
330 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (in)), ==, 8);
331 byte = g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error);
332 g_assert_no_error (error);
333 g_assert_cmpint (byte, ==, 'i');
334 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (in)), ==, 9);
336 /* Seek backward (outside buffer) */
337 ret = g_seekable_seek (G_SEEKABLE (in), -6, G_SEEK_CUR, NULL, &error);
338 g_assert_no_error (error);
340 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (in)), ==, 3);
341 byte = g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error);
342 g_assert_no_error (error);
343 g_assert_cmpint (byte, ==, 'd');
344 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (in)), ==, 4);
346 /* Seek from beginning */
347 ret = g_seekable_seek (G_SEEKABLE (in), 8, G_SEEK_SET, NULL, &error);
348 g_assert_no_error (error);
350 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (in)), ==, 8);
351 byte = g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error);
352 g_assert_no_error (error);
353 g_assert_cmpint (byte, ==, 'i');
354 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (in)), ==, 9);
357 ret = g_seekable_seek (G_SEEKABLE (in), -1, G_SEEK_END, NULL, &error);
358 g_assert_no_error (error);
360 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (in)), ==, 50);
361 byte = g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, &error);
362 g_assert_no_error (error);
363 g_assert_cmpint (byte, ==, 'Z');
364 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (in)), ==, 51);
368 g_object_unref (base);
376 g_test_init (&argc, &argv, NULL);
377 g_test_bug_base ("http://bugzilla.gnome.org/");
379 g_test_add_func ("/buffered-input-stream/peek", test_peek);
380 g_test_add_func ("/buffered-input-stream/peek-buffer", test_peek_buffer);
381 g_test_add_func ("/buffered-input-stream/set-buffer-size", test_set_buffer_size);
382 g_test_add_func ("/buffered-input-stream/read-byte", test_read_byte);
383 g_test_add_func ("/buffered-input-stream/read", test_read);
384 g_test_add_func ("/buffered-input-stream/skip", test_skip);
385 g_test_add_func ("/buffered-input-stream/seek", test_seek);
386 g_test_add_func ("/filter-input-stream/close", test_close);