Replace functions which called once with their bodies
[pidgin-git.git] / libpurple / tests / test_queued_output_stream.c
blob9ba11b04452a2d5427e3789df2019484957a7404
1 /*
2 * Purple
4 * Purple is the legal property of its developers, whose names are too
5 * numerous to list here. Please refer to the COPYRIGHT file distributed
6 * with this source distribution
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or (at
11 * your option) any later version.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
23 #include <glib.h>
24 #include <string.h>
26 #include <queuedoutputstream.h>
28 static const gsize test_bytes_data_len = 5;
29 static const guint8 test_bytes_data[] = "12345";
31 static const gsize test_bytes_data_len2 = 4;
32 static const guint8 test_bytes_data2[] = "6789";
34 static const gsize test_bytes_data_len3 = 12;
35 static const guint8 test_bytes_data3[] = "101112131415";
37 static void
38 test_queued_output_stream_new(void) {
39 GOutputStream *output;
40 PurpleQueuedOutputStream *queued;
41 GError *err = NULL;
43 output = g_memory_output_stream_new_resizable();
44 g_assert_nonnull(output);
46 queued = purple_queued_output_stream_new(output);
47 g_assert_true(PURPLE_IS_QUEUED_OUTPUT_STREAM(queued));
49 g_assert_true(g_output_stream_close(
50 G_OUTPUT_STREAM(queued), NULL, &err));
51 g_assert_no_error(err);
53 g_clear_object(&queued);
54 g_clear_object(&output);
57 static void
58 test_queued_output_stream_push_bytes_async_cb(GObject *source,
59 GAsyncResult *res, gpointer user_data)
61 PurpleQueuedOutputStream *queued = PURPLE_QUEUED_OUTPUT_STREAM(source);
62 gboolean *done = user_data;
63 GError *err = NULL;
65 g_assert_true(purple_queued_output_stream_push_bytes_finish(queued,
66 res, &err));
67 g_assert_no_error(err);
69 *done = TRUE;
72 static void
73 test_queued_output_stream_push_bytes_async(void) {
74 GMemoryOutputStream *output;
75 PurpleQueuedOutputStream *queued;
76 GBytes *bytes;
77 GError *err = NULL;
78 gboolean done = FALSE;
80 output = G_MEMORY_OUTPUT_STREAM(g_memory_output_stream_new_resizable());
81 g_assert_nonnull(output);
83 queued = purple_queued_output_stream_new(G_OUTPUT_STREAM(output));
84 g_assert_true(PURPLE_IS_QUEUED_OUTPUT_STREAM(queued));
86 bytes = g_bytes_new_static(test_bytes_data, test_bytes_data_len);
87 purple_queued_output_stream_push_bytes_async(queued, bytes,
88 G_PRIORITY_DEFAULT, NULL,
89 test_queued_output_stream_push_bytes_async_cb, &done);
90 g_bytes_unref(bytes);
92 while (!done) {
93 g_main_context_iteration(NULL, TRUE);
96 g_assert_cmpmem(g_memory_output_stream_get_data(output),
97 g_memory_output_stream_get_data_size(output),
98 test_bytes_data, test_bytes_data_len);
100 g_assert_true(g_output_stream_close(
101 G_OUTPUT_STREAM(queued), NULL, &err));
102 g_assert_no_error(err);
104 g_clear_object(&queued);
105 g_clear_object(&output);
108 static void
109 test_queued_output_stream_push_bytes_async_multiple_cb(GObject *source,
110 GAsyncResult *res, gpointer user_data)
112 PurpleQueuedOutputStream *queued = PURPLE_QUEUED_OUTPUT_STREAM(source);
113 gint *done = user_data;
114 GError *err = NULL;
116 g_assert_true(purple_queued_output_stream_push_bytes_finish(queued,
117 res, &err));
118 g_assert_no_error(err);
120 --*done;
123 static void
124 test_queued_output_stream_push_bytes_async_multiple(void) {
125 GMemoryOutputStream *output;
126 PurpleQueuedOutputStream *queued;
127 GBytes *bytes;
128 gchar *all_test_bytes_data;
129 GError *err = NULL;
130 int done = 3;
132 output = G_MEMORY_OUTPUT_STREAM(g_memory_output_stream_new_resizable());
133 g_assert_nonnull(output);
135 queued = purple_queued_output_stream_new(G_OUTPUT_STREAM(output));
136 g_assert_true(PURPLE_IS_QUEUED_OUTPUT_STREAM(queued));
138 bytes = g_bytes_new_static(test_bytes_data, test_bytes_data_len);
139 purple_queued_output_stream_push_bytes_async(queued, bytes,
140 G_PRIORITY_DEFAULT, NULL,
141 test_queued_output_stream_push_bytes_async_multiple_cb,
142 &done);
143 g_bytes_unref(bytes);
145 bytes = g_bytes_new_static(test_bytes_data2, test_bytes_data_len2);
146 purple_queued_output_stream_push_bytes_async(queued, bytes,
147 G_PRIORITY_DEFAULT, NULL,
148 test_queued_output_stream_push_bytes_async_multiple_cb,
149 &done);
150 g_bytes_unref(bytes);
152 bytes = g_bytes_new_static(test_bytes_data3, test_bytes_data_len3);
153 purple_queued_output_stream_push_bytes_async(queued, bytes,
154 G_PRIORITY_DEFAULT, NULL,
155 test_queued_output_stream_push_bytes_async_multiple_cb,
156 &done);
157 g_bytes_unref(bytes);
159 while (done > 0) {
160 g_main_context_iteration(NULL, TRUE);
163 g_assert_cmpint(done, ==, 0);
165 all_test_bytes_data = g_strconcat((const gchar *)test_bytes_data,
166 test_bytes_data2, test_bytes_data3, NULL);
168 g_assert_cmpmem(g_memory_output_stream_get_data(output),
169 g_memory_output_stream_get_data_size(output),
170 all_test_bytes_data, strlen(all_test_bytes_data));
172 g_free(all_test_bytes_data);
174 g_assert_true(g_output_stream_close(
175 G_OUTPUT_STREAM(queued), NULL, &err));
176 g_assert_no_error(err);
178 g_clear_object(&queued);
179 g_clear_object(&output);
182 static void
183 test_queued_output_stream_push_bytes_async_error_cb(GObject *source,
184 GAsyncResult *res, gpointer user_data)
186 PurpleQueuedOutputStream *queued = PURPLE_QUEUED_OUTPUT_STREAM(source);
187 gint *done = user_data;
188 GError *err = NULL;
190 g_assert_false(purple_queued_output_stream_push_bytes_finish(queued,
191 res, &err));
193 g_assert_error(err, G_IO_ERROR, G_IO_ERROR_CANCELLED);
194 g_clear_error(&err);
196 --*done;
199 static void
200 test_queued_output_stream_push_bytes_async_error(void) {
201 GMemoryOutputStream *output;
202 PurpleQueuedOutputStream *queued;
203 GBytes *bytes;
204 GCancellable *cancellable;
205 GError *err = NULL;
206 gint done = 3;
208 output = G_MEMORY_OUTPUT_STREAM(g_memory_output_stream_new_resizable());
209 g_assert_nonnull(output);
211 queued = purple_queued_output_stream_new(G_OUTPUT_STREAM(output));
212 g_assert_true(PURPLE_IS_QUEUED_OUTPUT_STREAM(queued));
214 cancellable = g_cancellable_new();
215 g_assert_nonnull(cancellable);
217 g_cancellable_cancel(cancellable);
218 g_assert_true(g_cancellable_is_cancelled(cancellable));
220 bytes = g_bytes_new_static(test_bytes_data, test_bytes_data_len);
222 purple_queued_output_stream_push_bytes_async(queued, bytes,
223 G_PRIORITY_DEFAULT, cancellable,
224 test_queued_output_stream_push_bytes_async_error_cb,
225 &done);
227 purple_queued_output_stream_push_bytes_async(queued, bytes,
228 G_PRIORITY_DEFAULT, cancellable,
229 test_queued_output_stream_push_bytes_async_error_cb,
230 &done);
232 purple_queued_output_stream_push_bytes_async(queued, bytes,
233 G_PRIORITY_DEFAULT, cancellable,
234 test_queued_output_stream_push_bytes_async_error_cb,
235 &done);
237 g_bytes_unref(bytes);
239 while (done > 0) {
240 g_main_context_iteration(NULL, TRUE);
243 g_assert_cmpint(done, ==, 0);
245 g_assert_cmpmem(g_memory_output_stream_get_data(output),
246 g_memory_output_stream_get_data_size(output),
247 NULL, 0);
249 g_assert_true(g_output_stream_close(
250 G_OUTPUT_STREAM(queued), NULL, &err));
251 g_assert_no_error(err);
253 g_clear_object(&queued);
254 g_clear_object(&output);
257 static void
258 test_queued_output_stream_clear_queue_on_error_cb(GObject *source,
259 GAsyncResult *res, gpointer user_data)
261 PurpleQueuedOutputStream *queued = PURPLE_QUEUED_OUTPUT_STREAM(source);
262 gint *done = user_data;
263 GError *err = NULL;
265 g_assert_false(purple_queued_output_stream_push_bytes_finish(queued,
266 res, &err));
268 g_assert_error(err, G_IO_ERROR, G_IO_ERROR_CANCELLED);
269 g_clear_error(&err);
271 purple_queued_output_stream_clear_queue(queued);
273 --*done;
276 static void
277 test_queued_output_stream_clear_queue_on_error(void) {
278 GMemoryOutputStream *output;
279 PurpleQueuedOutputStream *queued;
280 GBytes *bytes;
281 GCancellable *cancellable;
282 GError *err = NULL;
283 gint done = 3;
285 output = G_MEMORY_OUTPUT_STREAM(g_memory_output_stream_new_resizable());
286 g_assert_nonnull(output);
288 queued = purple_queued_output_stream_new(G_OUTPUT_STREAM(output));
289 g_assert_true(PURPLE_IS_QUEUED_OUTPUT_STREAM(queued));
291 cancellable = g_cancellable_new();
292 g_assert_nonnull(cancellable);
294 g_cancellable_cancel(cancellable);
295 g_assert_true(g_cancellable_is_cancelled(cancellable));
297 bytes = g_bytes_new_static(test_bytes_data, test_bytes_data_len);
299 purple_queued_output_stream_push_bytes_async(queued, bytes,
300 G_PRIORITY_DEFAULT, cancellable,
301 test_queued_output_stream_clear_queue_on_error_cb,
302 &done);
304 /* Don't set cancellable on these */
305 purple_queued_output_stream_push_bytes_async(queued, bytes,
306 G_PRIORITY_DEFAULT, NULL,
307 test_queued_output_stream_clear_queue_on_error_cb,
308 &done);
310 purple_queued_output_stream_push_bytes_async(queued, bytes,
311 G_PRIORITY_DEFAULT, NULL,
312 test_queued_output_stream_clear_queue_on_error_cb,
313 &done);
315 g_bytes_unref(bytes);
317 while (done > 0) {
318 g_main_context_iteration(NULL, TRUE);
321 g_assert_cmpint(done, ==, 0);
323 g_assert_cmpmem(g_memory_output_stream_get_data(output),
324 g_memory_output_stream_get_data_size(output),
325 NULL, 0);
327 g_assert_true(g_output_stream_close(
328 G_OUTPUT_STREAM(queued), NULL, &err));
329 g_assert_no_error(err);
331 g_clear_object(&queued);
332 g_clear_object(&output);
335 /******************************************************************************
336 * Main
337 *****************************************************************************/
338 gint
339 main(gint argc, gchar **argv) {
340 g_test_init(&argc, &argv, NULL);
342 g_test_set_nonfatal_assertions();
344 g_test_add_func("/queued-output-stream/new",
345 test_queued_output_stream_new);
346 g_test_add_func("/queued-output-stream/push-bytes-async",
347 test_queued_output_stream_push_bytes_async);
348 g_test_add_func("/queued-output-stream/push-bytes-async-multiple",
349 test_queued_output_stream_push_bytes_async_multiple);
350 g_test_add_func("/queued-output-stream/push-bytes-async-error",
351 test_queued_output_stream_push_bytes_async_error);
352 g_test_add_func("/queued-output-stream/clear-queue-on-error",
353 test_queued_output_stream_clear_queue_on_error);
355 return g_test_run();