1 /* GLib testing framework examples and tests
2 * Copyright (C) 2010-2012 Collabora Ltd.
3 * Authors: Xavier Claessens <xclaesse@gmail.com>
4 * Mike Ruprecht <mike.ruprecht@collabora.co.uk>
6 * This work is provided "as is"; redistribution and modification
7 * in whole or in part, in any medium, physical or electronic is
8 * permitted without restriction.
10 * This work is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * In no event shall the authors or contributors be liable for any
15 * direct, indirect, incidental, special, exemplary, or consequential
16 * damages (including, but not limited to, procurement of substitute
17 * goods or services; loss of use, data, or profits; or business
18 * interruption) however caused and on any theory of liability, whether
19 * in contract, strict liability, or tort (including negligence or
20 * otherwise) arising in any way out of the use of this software, even
21 * if advised of the possibility of such damage.
24 #include <glib/glib.h>
25 #include <glib/gstdio.h>
32 TEST_THREADED_NONE = 0,
33 TEST_THREADED_ISTREAM = 1,
34 TEST_THREADED_OSTREAM = 2,
35 TEST_THREADED_BOTH = TEST_THREADED_ISTREAM | TEST_THREADED_OSTREAM,
42 GInputStream *istream;
43 GOutputStream *ostream;
44 TestThreadedFlags flags;
50 test_copy_chunks_splice_cb (GObject *source,
54 TestCopyChunksData *data = user_data;
59 bytes_spliced = g_output_stream_splice_finish (G_OUTPUT_STREAM (source),
61 g_assert_no_error (error);
62 g_assert_cmpint (bytes_spliced, ==, strlen (data->data));
64 if (data->flags & TEST_THREADED_OSTREAM)
68 g_file_get_contents (data->output_path, &received_data,
70 g_assert_no_error (error);
71 g_assert_cmpstr (received_data, ==, data->data);
72 g_free (received_data);
76 received_data = g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (data->ostream));
77 g_assert_cmpstr (received_data, ==, data->data);
80 g_assert (g_input_stream_is_closed (data->istream));
81 g_assert (g_output_stream_is_closed (data->ostream));
83 if (data->flags & TEST_THREADED_ISTREAM)
85 g_unlink (data->input_path);
86 g_free (data->input_path);
89 if (data->flags & TEST_THREADED_OSTREAM)
91 g_unlink (data->output_path);
92 g_free (data->output_path);
95 g_main_loop_quit (data->main_loop);
99 test_copy_chunks_start (TestThreadedFlags flags)
101 TestCopyChunksData data;
102 GError *error = NULL;
104 data.main_loop = g_main_loop_new (NULL, FALSE);
105 data.data = "abcdefghijklmnopqrstuvwxyz";
108 if (data.flags & TEST_THREADED_ISTREAM)
111 GFileIOStream *stream;
113 file = g_file_new_tmp ("test-inputXXXXXX", &stream, &error);
114 g_assert_no_error (error);
115 g_object_unref (stream);
116 data.input_path = g_file_get_path (file);
117 g_file_set_contents (data.input_path,
118 data.data, strlen (data.data),
120 g_assert_no_error (error);
121 data.istream = G_INPUT_STREAM (g_file_read (file, NULL, &error));
122 g_assert_no_error (error);
123 g_object_unref (file);
127 data.istream = g_memory_input_stream_new_from_data (data.data, -1, NULL);
130 if (data.flags & TEST_THREADED_OSTREAM)
133 GFileIOStream *stream;
135 file = g_file_new_tmp ("test-outputXXXXXX", &stream, &error);
136 g_assert_no_error (error);
137 g_object_unref (stream);
138 data.output_path = g_file_get_path (file);
139 data.ostream = G_OUTPUT_STREAM (g_file_replace (file, NULL, FALSE,
142 g_assert_no_error (error);
143 g_object_unref (file);
147 data.ostream = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
150 g_output_stream_splice_async (data.ostream, data.istream,
151 G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE |
152 G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
153 G_PRIORITY_DEFAULT, NULL,
154 test_copy_chunks_splice_cb, &data);
156 /* We do not hold a ref in data struct, this is to make sure the operation
157 * keeps the iostream objects alive until it finishes
159 g_object_unref (data.istream);
160 g_object_unref (data.ostream);
162 g_main_loop_run (data.main_loop);
163 g_main_loop_unref (data.main_loop);
167 test_copy_chunks (void)
169 test_copy_chunks_start (TEST_THREADED_NONE);
173 test_copy_chunks_threaded_input (void)
175 test_copy_chunks_start (TEST_THREADED_ISTREAM);
179 test_copy_chunks_threaded_output (void)
181 test_copy_chunks_start (TEST_THREADED_OSTREAM);
185 test_copy_chunks_threaded (void)
187 test_copy_chunks_start (TEST_THREADED_BOTH);
194 g_test_init (&argc, &argv, NULL);
196 g_test_add_func ("/async-splice/copy-chunks", test_copy_chunks);
197 g_test_add_func ("/async-splice/copy-chunks-threaded-input",
198 test_copy_chunks_threaded_input);
199 g_test_add_func ("/async-splice/copy-chunks-threaded-output",
200 test_copy_chunks_threaded_output);
201 g_test_add_func ("/async-splice/copy-chunks-threaded",
202 test_copy_chunks_threaded);