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), strlen ("abcdefghijk"), NULL, NULL);
41 buffer = g_new0 (char, 64);
42 npeek = g_buffered_input_stream_peek (G_BUFFERED_INPUT_STREAM (in), buffer, 2, 3);
43 g_assert_cmpint (npeek, ==, 3);
44 g_assert_cmpstr ("cde", ==, buffer);
47 buffer = g_new0 (char, 64);
48 npeek = g_buffered_input_stream_peek (G_BUFFERED_INPUT_STREAM (in), buffer, 9, 5);
49 g_assert_cmpint (npeek, ==, 2);
50 g_assert_cmpstr ("jk", ==, buffer);
53 buffer = g_new0 (char, 64);
54 npeek = g_buffered_input_stream_peek (G_BUFFERED_INPUT_STREAM (in), buffer, 75, 3);
55 g_assert_cmpint (npeek, ==, 0);
59 g_object_unref (base);
63 test_peek_buffer (void)
71 base = g_memory_input_stream_new_from_data ("abcdefghijk", -1, NULL);
72 in = g_buffered_input_stream_new (base);
74 nfill = g_buffered_input_stream_fill (G_BUFFERED_INPUT_STREAM (in), strlen ("abcdefghijk"), NULL, NULL);
75 buffer = (char *) g_buffered_input_stream_peek_buffer (G_BUFFERED_INPUT_STREAM (in), &bufsize);
76 g_assert_cmpint (nfill, ==, bufsize);
77 g_assert (0 == strncmp ("abcdefghijk", buffer, bufsize));
80 g_object_unref (base);
84 test_set_buffer_size (void)
90 base = g_memory_input_stream_new_from_data ("abcdefghijk", -1, NULL);
91 in = g_buffered_input_stream_new (base);
92 size = g_buffered_input_stream_get_buffer_size (G_BUFFERED_INPUT_STREAM (in));
93 g_assert_cmpint (size, ==, 4096);
95 g_buffered_input_stream_set_buffer_size (G_BUFFERED_INPUT_STREAM (in), 64);
96 size = g_buffered_input_stream_get_buffer_size (G_BUFFERED_INPUT_STREAM (in));
97 g_assert_cmpint (size, ==, 64);
99 /* size cannot shrink below current content len */
100 g_buffered_input_stream_fill (G_BUFFERED_INPUT_STREAM (in), strlen ("abcdefghijk"), NULL, NULL);
101 g_buffered_input_stream_peek_buffer (G_BUFFERED_INPUT_STREAM (in), &bufsize);
102 g_buffered_input_stream_set_buffer_size (G_BUFFERED_INPUT_STREAM (in), 2);
103 size = g_buffered_input_stream_get_buffer_size (G_BUFFERED_INPUT_STREAM (in));
104 g_assert_cmpint (size, ==, bufsize);
108 in = g_buffered_input_stream_new_sized (base, 64);
109 size = g_buffered_input_stream_get_buffer_size (G_BUFFERED_INPUT_STREAM (in));
110 g_assert_cmpint (size, ==, 64);
113 g_object_unref (base);
117 test_read_byte (void)
122 g_test_bug ("562393");
124 base = g_memory_input_stream_new_from_data ("abcdefghijk", -1, NULL);
125 in = g_buffered_input_stream_new (base);
127 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, NULL), ==, 'a');
128 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, NULL), ==, 'b');
129 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, NULL), ==, 'c');
131 g_assert_cmpint (g_input_stream_skip (in, 3, NULL, NULL), ==, 3);
133 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, NULL), ==, 'g');
136 g_object_unref (base);
140 test_large_read (void)
146 base = g_memory_input_stream_new_from_data ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVXYZ", -1, NULL);
147 in = g_buffered_input_stream_new_sized (base, 8);
149 g_assert_cmpint (g_buffered_input_stream_get_available (G_BUFFERED_INPUT_STREAM (in)), ==, 0);
151 g_assert_cmpint (g_buffered_input_stream_fill (G_BUFFERED_INPUT_STREAM (in), 8, NULL, NULL), ==, 8);
153 g_assert_cmpint (g_buffered_input_stream_get_available (G_BUFFERED_INPUT_STREAM (in)), ==, 8);
155 memset (buffer, 0, 20);
156 g_assert_cmpint (g_input_stream_read (in, &buffer, 16, NULL, NULL), ==, 16);
157 g_assert_cmpstr (buffer, ==, "abcdefghijklmnop");
159 g_assert_cmpint (g_buffered_input_stream_get_available (G_BUFFERED_INPUT_STREAM (in)), ==, 0);
161 memset (buffer, 0, 20);
162 g_assert_cmpint (g_input_stream_read (in, &buffer, 16, NULL, NULL), ==, 16);
163 g_assert_cmpstr (buffer, ==, "qrstuvwxyzABCDEF");
166 g_object_unref (base);
175 base = g_memory_input_stream_new_from_data ("abcdefghijk", -1, NULL);
176 in = g_buffered_input_stream_new_sized (base, 5);
178 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, NULL), ==, 'a');
179 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, NULL), ==, 'b');
180 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, NULL), ==, 'c');
182 g_assert_cmpint (g_input_stream_skip (in, 7, NULL, NULL), ==, 7);
184 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, NULL), ==, 'k');
187 g_object_unref (base);
195 g_test_init (&argc, &argv, NULL);
196 g_test_bug_base ("http://bugzilla.gnome.org/");
198 g_test_add_func ("/buffered-input-stream/peek", test_peek);
199 g_test_add_func ("/buffered-input-stream/peek-buffer", test_peek_buffer);
200 g_test_add_func ("/buffered-input-stream/set-buffer-size", test_set_buffer_size);
201 g_test_add_func ("/buffered-input-stream/read-byte", test_read_byte);
202 g_test_add_func ("/buffered-input-stream/large-read", test_large_read);
203 g_test_add_func ("/buffered-input-stream/skip", test_skip);