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);
62 test_peek_buffer (void)
70 base = g_memory_input_stream_new_from_data ("abcdefghijk", -1, NULL);
71 in = g_buffered_input_stream_new (base);
73 nfill = g_buffered_input_stream_fill (G_BUFFERED_INPUT_STREAM (in), strlen ("abcdefghijk"), NULL, NULL);
74 buffer = (char *) g_buffered_input_stream_peek_buffer (G_BUFFERED_INPUT_STREAM (in), &bufsize);
75 g_assert_cmpint (nfill, ==, bufsize);
76 g_assert (0 == strncmp ("abcdefghijk", buffer, bufsize));
82 test_set_buffer_size (void)
88 base = g_memory_input_stream_new_from_data ("abcdefghijk", -1, NULL);
89 in = g_buffered_input_stream_new (base);
91 g_buffered_input_stream_set_buffer_size (G_BUFFERED_INPUT_STREAM (in), 64);
92 size = g_buffered_input_stream_get_buffer_size (G_BUFFERED_INPUT_STREAM (in));
93 g_assert_cmpint (size, ==, 64);
95 /* size cannot shrink below current content len */
96 g_buffered_input_stream_fill (G_BUFFERED_INPUT_STREAM (in), strlen ("abcdefghijk"), NULL, NULL);
97 g_buffered_input_stream_peek_buffer (G_BUFFERED_INPUT_STREAM (in), &bufsize);
98 g_buffered_input_stream_set_buffer_size (G_BUFFERED_INPUT_STREAM (in), 2);
99 size = g_buffered_input_stream_get_buffer_size (G_BUFFERED_INPUT_STREAM (in));
100 g_assert_cmpint (size, ==, bufsize);
106 test_read_byte (void)
111 g_test_bug ("562393");
113 base = g_memory_input_stream_new_from_data ("abcdefghijk", -1, NULL);
114 in = g_buffered_input_stream_new (base);
116 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, NULL), ==, 'a');
117 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, NULL), ==, 'b');
118 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, NULL), ==, 'c');
120 g_assert_cmpint (g_input_stream_skip (in, 3, NULL, NULL), ==, 3);
122 g_assert_cmpint (g_buffered_input_stream_read_byte (G_BUFFERED_INPUT_STREAM (in), NULL, NULL), ==, 'g');
132 g_test_init (&argc, &argv, NULL);
133 g_test_bug_base ("http://bugzilla.gnome.org/");
135 g_test_add_func ("/buffered-input-stream/peek", test_peek);
136 g_test_add_func ("/buffered-input-stream/peek-buffer", test_peek_buffer);
137 g_test_add_func ("/buffered-input-stream/set-buffer-size", test_set_buffer_size);
138 g_test_add_func ("/buffered-input-stream/read-byte", test_read_byte);