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;
84 in_cb (GObject *object,
90 g_assert (object == expected_obj);
91 g_assert (user_data == expected_data);
92 g_assert (callback_happened == FALSE);
94 g_input_stream_close_finish (expected_obj, result, &error);
95 g_assert (error == NULL);
97 callback_happened = TRUE;
101 test_input_async (void)
103 GInputStream *base, *f1, *f2;
105 base = g_memory_input_stream_new_from_data ("abcdefghijk", -1, NULL);
106 f1 = g_buffered_input_stream_new (base);
107 f2 = g_buffered_input_stream_new (base);
109 g_filter_input_stream_set_close_base_stream (G_FILTER_INPUT_STREAM (f1), FALSE);
111 g_assert (g_filter_input_stream_get_base_stream (G_FILTER_INPUT_STREAM (f1)) == base);
112 g_assert (g_filter_input_stream_get_base_stream (G_FILTER_INPUT_STREAM (f2)) == base);
114 g_assert (!g_input_stream_is_closed (base));
115 g_assert (!g_input_stream_is_closed (f1));
116 g_assert (!g_input_stream_is_closed (f2));
119 expected_data = g_malloc (20);
120 callback_happened = FALSE;
121 g_input_stream_close_async (f1, 0, NULL, in_cb, expected_data);
123 g_assert (callback_happened == FALSE);
124 while (g_main_context_pending (NULL))
125 g_main_context_iteration (NULL, FALSE);
126 g_assert (callback_happened == TRUE);
128 g_assert (!g_input_stream_is_closed (base));
129 g_assert (!g_input_stream_is_closed (f2));
130 g_free (expected_data);
132 g_assert (!g_input_stream_is_closed (base));
133 g_assert (!g_input_stream_is_closed (f2));
136 expected_data = g_malloc (20);
137 callback_happened = FALSE;
138 g_input_stream_close_async (f2, 0, NULL, in_cb, expected_data);
140 g_assert (callback_happened == FALSE);
141 while (g_main_context_pending (NULL))
142 g_main_context_iteration (NULL, FALSE);
143 g_assert (callback_happened == TRUE);
145 g_assert (g_input_stream_is_closed (base));
146 g_assert (g_input_stream_is_closed (f2));
147 g_free (expected_data);
150 g_assert (g_input_stream_is_closed (base));
151 g_object_unref (base);
155 out_cb (GObject *object,
156 GAsyncResult *result,
159 GError *error = NULL;
161 g_assert (object == expected_obj);
162 g_assert (user_data == expected_data);
163 g_assert (callback_happened == FALSE);
165 g_output_stream_close_finish (expected_obj, result, &error);
166 g_assert (error == NULL);
168 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);
236 g_test_add_func ("/filter-stream/async-input", test_input_async);
237 g_test_add_func ("/filter-stream/async-output", test_output_async);