2 * Copyright © 2009 Codethink Limited
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation; either version 2 of the licence or (at
7 * your option) any later version.
9 * See the included COPYING file for more information.
11 * Author: Ryan Lortie <desrt@desrt.ca>
14 #include <glib/glib.h>
18 test_input_filter (void)
20 GInputStream *base, *f1, *f2;
22 g_test_bug ("568394");
23 base = g_memory_input_stream_new_from_data ("abcdefghijk", -1, NULL);
24 f1 = g_buffered_input_stream_new (base);
25 f2 = g_buffered_input_stream_new (base);
27 g_filter_input_stream_set_close_base_stream (G_FILTER_INPUT_STREAM (f1), FALSE);
29 g_assert (g_filter_input_stream_get_base_stream (G_FILTER_INPUT_STREAM (f1)) == base);
30 g_assert (g_filter_input_stream_get_base_stream (G_FILTER_INPUT_STREAM (f2)) == base);
32 g_assert (!g_input_stream_is_closed (base));
33 g_assert (!g_input_stream_is_closed (f1));
34 g_assert (!g_input_stream_is_closed (f2));
38 g_assert (!g_input_stream_is_closed (base));
39 g_assert (!g_input_stream_is_closed (f2));
43 g_assert (g_input_stream_is_closed (base));
45 g_object_unref (base);
49 test_output_filter (void)
51 GOutputStream *base, *f1, *f2;
53 base = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
54 f1 = g_buffered_output_stream_new (base);
55 f2 = g_buffered_output_stream_new (base);
57 g_filter_output_stream_set_close_base_stream (G_FILTER_OUTPUT_STREAM (f1), FALSE);
59 g_assert (g_filter_output_stream_get_base_stream (G_FILTER_OUTPUT_STREAM (f1)) == base);
60 g_assert (g_filter_output_stream_get_base_stream (G_FILTER_OUTPUT_STREAM (f2)) == base);
62 g_assert (!g_output_stream_is_closed (base));
63 g_assert (!g_output_stream_is_closed (f1));
64 g_assert (!g_output_stream_is_closed (f2));
68 g_assert (!g_output_stream_is_closed (base));
69 g_assert (!g_output_stream_is_closed (f2));
73 g_assert (g_output_stream_is_closed (base));
75 g_object_unref (base);
78 gpointer expected_obj;
79 gpointer expected_data;
80 gboolean callback_happened;
83 in_cb (GObject *object,
89 g_assert (object == expected_obj);
90 g_assert (user_data == expected_data);
91 g_assert (callback_happened == FALSE);
93 g_input_stream_close_finish (expected_obj, result, &error);
94 g_assert (error == NULL);
96 callback_happened = TRUE;
100 test_input_async (void)
102 GInputStream *base, *f1, *f2;
104 base = g_memory_input_stream_new_from_data ("abcdefghijk", -1, NULL);
105 f1 = g_buffered_input_stream_new (base);
106 f2 = g_buffered_input_stream_new (base);
108 g_filter_input_stream_set_close_base_stream (G_FILTER_INPUT_STREAM (f1), FALSE);
110 g_assert (g_filter_input_stream_get_base_stream (G_FILTER_INPUT_STREAM (f1)) == base);
111 g_assert (g_filter_input_stream_get_base_stream (G_FILTER_INPUT_STREAM (f2)) == base);
113 g_assert (!g_input_stream_is_closed (base));
114 g_assert (!g_input_stream_is_closed (f1));
115 g_assert (!g_input_stream_is_closed (f2));
118 expected_data = g_malloc (20);
119 callback_happened = FALSE;
120 g_input_stream_close_async (f1, 0, NULL, in_cb, expected_data);
122 g_assert (callback_happened == FALSE);
123 while (g_main_context_pending (NULL))
124 g_main_context_iteration (NULL, FALSE);
125 g_assert (callback_happened == TRUE);
127 g_assert (!g_input_stream_is_closed (base));
128 g_assert (!g_input_stream_is_closed (f2));
129 g_free (expected_data);
131 g_assert (!g_input_stream_is_closed (base));
132 g_assert (!g_input_stream_is_closed (f2));
135 expected_data = g_malloc (20);
136 callback_happened = FALSE;
137 g_input_stream_close_async (f2, 0, NULL, in_cb, expected_data);
139 g_assert (callback_happened == FALSE);
140 while (g_main_context_pending (NULL))
141 g_main_context_iteration (NULL, FALSE);
142 g_assert (callback_happened == TRUE);
144 g_assert (g_input_stream_is_closed (base));
145 g_assert (g_input_stream_is_closed (f2));
146 g_free (expected_data);
149 g_assert (g_input_stream_is_closed (base));
150 g_object_unref (base);
154 out_cb (GObject *object,
155 GAsyncResult *result,
158 GError *error = NULL;
160 g_assert (object == expected_obj);
161 g_assert (user_data == expected_data);
162 g_assert (callback_happened == FALSE);
164 g_output_stream_close_finish (expected_obj, result, &error);
165 g_assert (error == NULL);
167 callback_happened = TRUE;
172 test_output_async (void)
174 GOutputStream *base, *f1, *f2;
176 base = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
177 f1 = g_buffered_output_stream_new (base);
178 f2 = g_buffered_output_stream_new (base);
180 g_filter_output_stream_set_close_base_stream (G_FILTER_OUTPUT_STREAM (f1), FALSE);
182 g_assert (g_filter_output_stream_get_base_stream (G_FILTER_OUTPUT_STREAM (f1)) == base);
183 g_assert (g_filter_output_stream_get_base_stream (G_FILTER_OUTPUT_STREAM (f2)) == base);
185 g_assert (!g_output_stream_is_closed (base));
186 g_assert (!g_output_stream_is_closed (f1));
187 g_assert (!g_output_stream_is_closed (f2));
190 expected_data = g_malloc (20);
191 callback_happened = FALSE;
192 g_output_stream_close_async (f1, 0, NULL, out_cb, expected_data);
194 g_assert (callback_happened == FALSE);
195 while (g_main_context_pending (NULL))
196 g_main_context_iteration (NULL, FALSE);
197 g_assert (callback_happened == TRUE);
199 g_assert (!g_output_stream_is_closed (base));
200 g_assert (!g_output_stream_is_closed (f2));
201 g_free (expected_data);
203 g_assert (!g_output_stream_is_closed (base));
204 g_assert (!g_output_stream_is_closed (f2));
207 expected_data = g_malloc (20);
208 callback_happened = FALSE;
209 g_output_stream_close_async (f2, 0, NULL, out_cb, expected_data);
211 g_assert (callback_happened == FALSE);
212 while (g_main_context_pending (NULL))
213 g_main_context_iteration (NULL, FALSE);
214 g_assert (callback_happened == TRUE);
216 g_assert (g_output_stream_is_closed (base));
217 g_assert (g_output_stream_is_closed (f2));
218 g_free (expected_data);
221 g_assert (g_output_stream_is_closed (base));
222 g_object_unref (base);
227 main (int argc, char **argv)
229 g_test_init (&argc, &argv, NULL);
230 g_test_bug_base ("http://bugzilla.gnome.org/");
233 g_test_add_func ("/filter-stream/input", test_input_filter);
234 g_test_add_func ("/filter-stream/output", test_output_filter);
235 g_test_add_func ("/filter-stream/async-input", test_input_async);
236 g_test_add_func ("/filter-stream/async-output", test_output_async);