1 /* GLib testing framework examples and tests
2 * Copyright (C) 2010 Collabora Ltd.
3 * Authors: Xavier Claessens <xclaesse@gmail.com>
5 * This work is provided "as is"; redistribution and modification
6 * in whole or in part, in any medium, physical or electronic is
7 * permitted without restriction.
9 * This work is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * In no event shall the authors or contributors be liable for any
14 * direct, indirect, incidental, special, exemplary, or consequential
15 * damages (including, but not limited to, procurement of substitute
16 * goods or services; loss of use, data, or profits; or business
17 * interruption) however caused and on any theory of liability, whether
18 * in contract, strict liability, or tort (including negligence or
19 * otherwise) arising in any way out of the use of this software, even
20 * if advised of the possibility of such damage.
23 #include <glib/glib.h>
31 GInputStream *input_stream;
32 GOutputStream *output_stream;
37 GIOStreamClass parent_class;
40 G_DEFINE_TYPE (GTestIOStream, g_test_io_stream, G_TYPE_IO_STREAM);
44 get_input_stream (GIOStream *io_stream)
46 GTestIOStream *self = (GTestIOStream *) io_stream;
48 return self->input_stream;
51 static GOutputStream *
52 get_output_stream (GIOStream *io_stream)
54 GTestIOStream *self = (GTestIOStream *) io_stream;
56 return self->output_stream;
60 finalize (GObject *object)
62 GTestIOStream *self = (GTestIOStream *) object;
64 if (self->input_stream != NULL)
65 g_object_unref (self->input_stream);
67 if (self->output_stream != NULL)
68 g_object_unref (self->output_stream);
70 G_OBJECT_CLASS (g_test_io_stream_parent_class)->finalize (object);
74 g_test_io_stream_class_init (GTestIOStreamClass *klass)
76 GObjectClass *object_class = G_OBJECT_CLASS (klass);
77 GIOStreamClass *io_class = G_IO_STREAM_CLASS (klass);
79 object_class->finalize = finalize;
81 io_class->get_input_stream = get_input_stream;
82 io_class->get_output_stream = get_output_stream;
86 g_test_io_stream_init (GTestIOStream *self)
91 g_test_io_stream_new (GInputStream *input, GOutputStream *output)
95 self = g_object_new (g_test_io_stream_get_type (), NULL);
96 self->input_stream = g_object_ref (input);
97 self->output_stream = g_object_ref (output);
99 return G_IO_STREAM (self);
104 GMainLoop *main_loop;
107 GIOStream *iostream1;
108 GIOStream *iostream2;
109 } TestCopyChunksData;
112 test_copy_chunks_splice_cb (GObject *source_object,
116 TestCopyChunksData *data = user_data;
117 GMemoryOutputStream *ostream;
118 gchar *received_data;
119 GError *error = NULL;
121 g_io_stream_splice_finish (res, &error);
122 g_assert_no_error (error);
124 ostream = G_MEMORY_OUTPUT_STREAM (((GTestIOStream *) data->iostream1)->output_stream);
125 received_data = g_memory_output_stream_get_data (ostream);
126 g_assert_cmpstr (received_data, ==, data->data2);
128 ostream = G_MEMORY_OUTPUT_STREAM (((GTestIOStream *) data->iostream2)->output_stream);
129 received_data = g_memory_output_stream_get_data (ostream);
130 g_assert_cmpstr (received_data, ==, data->data1);
132 g_assert (g_io_stream_is_closed (data->iostream1));
133 g_assert (g_io_stream_is_closed (data->iostream2));
135 g_main_loop_quit (data->main_loop);
139 test_copy_chunks (void)
141 TestCopyChunksData data;
142 GInputStream *istream;
143 GOutputStream *ostream;
145 data.main_loop = g_main_loop_new (NULL, FALSE);
146 data.data1 = "abcdefghijklmnopqrstuvwxyz";
147 data.data2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
149 istream = g_memory_input_stream_new_from_data (data.data1, -1, NULL);
150 ostream = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
151 data.iostream1 = g_test_io_stream_new (istream, ostream);
152 g_object_unref (istream);
153 g_object_unref (ostream);
155 istream = g_memory_input_stream_new_from_data (data.data2, -1, NULL);
156 ostream = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
157 data.iostream2 = g_test_io_stream_new (istream, ostream);
158 g_object_unref (istream);
159 g_object_unref (ostream);
161 g_io_stream_splice_async (data.iostream1, data.iostream2,
162 G_IO_STREAM_SPLICE_CLOSE_STREAM1 | G_IO_STREAM_SPLICE_CLOSE_STREAM2 |
163 G_IO_STREAM_SPLICE_WAIT_FOR_BOTH, G_PRIORITY_DEFAULT,
164 NULL, test_copy_chunks_splice_cb, &data);
166 /* We do not hold a ref in data struct, this is to make sure the operation
167 * keeps the iostream objects alive until it finishes */
168 g_object_unref (data.iostream1);
169 g_object_unref (data.iostream2);
171 g_main_loop_run (data.main_loop);
172 g_main_loop_unref (data.main_loop);
180 g_test_init (&argc, &argv, NULL);
182 g_test_add_func ("/io-stream/copy-chunks", test_copy_chunks);