1 /* GLib testing framework examples and tests
2 * Authors: Jesse van den Kieboom <jessevdk@gnome.org>
4 * This work is provided "as is"; redistribution and modification
5 * in whole or in part, in any medium, physical or electronic is
6 * permitted without restriction.
8 * This work is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * In no event shall the authors or contributors be liable for any
13 * direct, indirect, incidental, special, exemplary, or consequential
14 * damages (including, but not limited to, procurement of substitute
15 * goods or services; loss of use, data, or profits; or business
16 * interruption) however caused and on any theory of liability, whether
17 * in contract, strict liability, or tort (including negligence or
18 * otherwise) arising in any way out of the use of this software, even
19 * if advised of the possibility of such damage.
22 #include <glib/glib.h>
27 #define DATA_TO_WRITE "Hello world\n"
31 GOutputStream
*conv_stream
;
32 GOutputStream
*data_stream
;
33 gchar
*expected_output
;
39 create_streams (SetupData
*data
)
41 GConverter
*converter
;
43 converter
= G_CONVERTER (g_zlib_compressor_new (G_ZLIB_COMPRESSOR_FORMAT_GZIP
, -1));
45 data
->data_stream
= g_memory_output_stream_new (NULL
, 0, g_realloc
, g_free
);
46 data
->conv_stream
= g_converter_output_stream_new (data
->data_stream
,
49 g_object_unref (converter
);
53 destroy_streams (SetupData
*data
)
55 g_object_unref (data
->data_stream
);
56 g_object_unref (data
->conv_stream
);
60 write_data_to_stream (SetupData
*data
)
65 /* just write the data synchronously */
66 g_output_stream_write_all (data
->conv_stream
,
68 sizeof (DATA_TO_WRITE
),
73 g_assert_no_error (error
);
74 g_assert_cmpint (sizeof (DATA_TO_WRITE
), ==, bytes_written
);
78 setup_data (SetupData
*data
,
79 gconstpointer user_data
)
81 data
->main_loop
= g_main_loop_new (NULL
, FALSE
);
82 create_streams (data
);
86 teardown_data (SetupData
*data
,
87 gconstpointer user_data
)
90 g_main_loop_unref (data
->main_loop
);
92 destroy_streams (data
);
94 g_free (data
->expected_output
);
98 compare_output (SetupData
*data
)
103 written
= g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (data
->data_stream
));
104 size
= g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (data
->data_stream
));
106 g_assert_cmpmem (written
, size
, data
->expected_output
, data
->expected_size
);
110 async_close_ready (GOutputStream
*stream
,
111 GAsyncResult
*result
,
114 GError
*error
= NULL
;
116 /* finish the close */
117 g_output_stream_close_finish (stream
, result
, &error
);
119 g_assert_no_error (error
);
121 /* compare the output with the desired output */
122 compare_output (data
);
124 g_main_loop_quit (data
->main_loop
);
128 prepare_data (SetupData
*data
,
129 gboolean manual_flush
)
131 GError
*error
= NULL
;
134 write_data_to_stream (data
);
138 g_output_stream_flush (data
->conv_stream
, NULL
, &error
);
139 g_assert_no_error (error
);
142 g_output_stream_close (data
->conv_stream
, NULL
, &error
);
144 g_assert_no_error (error
);
146 written
= g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (data
->data_stream
));
148 data
->expected_size
= g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (data
->data_stream
));
150 g_assert_cmpint (data
->expected_size
, >, 0);
152 data
->expected_output
= g_memdup (written
, (guint
)data
->expected_size
);
154 /* then recreate the streams and prepare them for the asynchronous close */
155 destroy_streams (data
);
156 create_streams (data
);
158 write_data_to_stream (data
);
162 test_without_flush (SetupData
*data
,
163 gconstpointer user_data
)
165 prepare_data (data
, FALSE
);
167 g_test_bug ("617937");
169 /* just close asynchronously */
170 g_output_stream_close_async (data
->conv_stream
,
173 (GAsyncReadyCallback
)async_close_ready
,
176 g_main_loop_run (data
->main_loop
);
180 test_with_flush (SetupData
*data
, gconstpointer user_data
)
182 GError
*error
= NULL
;
184 g_test_bug ("617937");
186 prepare_data (data
, TRUE
);
188 g_output_stream_flush (data
->conv_stream
, NULL
, &error
);
190 g_assert_no_error (error
);
192 /* then close asynchronously */
193 g_output_stream_close_async (data
->conv_stream
,
196 (GAsyncReadyCallback
)async_close_ready
,
199 g_main_loop_run (data
->main_loop
);
203 async_flush_ready (GOutputStream
*stream
,
204 GAsyncResult
*result
,
207 GError
*error
= NULL
;
209 g_output_stream_flush_finish (stream
, result
, &error
);
211 g_assert_no_error (error
);
213 /* then close async after the flush */
214 g_output_stream_close_async (data
->conv_stream
,
217 (GAsyncReadyCallback
)async_close_ready
,
222 test_with_async_flush (SetupData
*data
,
223 gconstpointer user_data
)
225 g_test_bug ("617937");
227 prepare_data (data
, TRUE
);
229 /* first flush async */
230 g_output_stream_flush_async (data
->conv_stream
,
233 (GAsyncReadyCallback
)async_flush_ready
,
236 g_main_loop_run (data
->main_loop
);
245 g_test_init (&argc
, &argv
, NULL
);
247 g_test_bug_base ("http://bugzilla.gnome.org/");
249 data
= g_slice_new (SetupData
);
251 /* test closing asynchronously without flushing manually */
252 g_test_add ("/close-async/without-flush",
259 /* test closing asynchronously with a synchronous manually flush */
260 g_test_add ("/close-async/with-flush",
267 /* test closing asynchronously with an asynchronous manually flush */
268 g_test_add ("/close-async/with-async-flush",
272 test_with_async_flush
,
275 g_slice_free (SetupData
, data
);