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);
291 g_test_init (&argc, &argv, NULL);
292 g_test_bug_base ("http://bugzilla.gnome.org/");
294 g_test_add_func ("/buffered-input-stream/peek", test_peek);
295 g_test_add_func ("/buffered-input-stream/peek-buffer", test_peek_buffer);
296 g_test_add_func ("/buffered-input-stream/set-buffer-size", test_set_buffer_size);
297 g_test_add_func ("/buffered-input-stream/read-byte", test_read_byte);
298 g_test_add_func ("/buffered-input-stream/read", test_read);
299 g_test_add_func ("/buffered-input-stream/skip", test_skip);
300 g_test_add_func ("/filter-input-stream/close", test_close);