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 static GType
g_test_io_stream_get_type (void);
41 G_DEFINE_TYPE (GTestIOStream
, g_test_io_stream
, G_TYPE_IO_STREAM
);
45 get_input_stream (GIOStream
*io_stream
)
47 GTestIOStream
*self
= (GTestIOStream
*) io_stream
;
49 return self
->input_stream
;
52 static GOutputStream
*
53 get_output_stream (GIOStream
*io_stream
)
55 GTestIOStream
*self
= (GTestIOStream
*) io_stream
;
57 return self
->output_stream
;
61 finalize (GObject
*object
)
63 GTestIOStream
*self
= (GTestIOStream
*) object
;
65 if (self
->input_stream
!= NULL
)
66 g_object_unref (self
->input_stream
);
68 if (self
->output_stream
!= NULL
)
69 g_object_unref (self
->output_stream
);
71 G_OBJECT_CLASS (g_test_io_stream_parent_class
)->finalize (object
);
75 g_test_io_stream_class_init (GTestIOStreamClass
*klass
)
77 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
78 GIOStreamClass
*io_class
= G_IO_STREAM_CLASS (klass
);
80 object_class
->finalize
= finalize
;
82 io_class
->get_input_stream
= get_input_stream
;
83 io_class
->get_output_stream
= get_output_stream
;
87 g_test_io_stream_init (GTestIOStream
*self
)
92 g_test_io_stream_new (GInputStream
*input
, GOutputStream
*output
)
96 self
= g_object_new (g_test_io_stream_get_type (), NULL
);
97 self
->input_stream
= g_object_ref (input
);
98 self
->output_stream
= g_object_ref (output
);
100 return G_IO_STREAM (self
);
105 GMainLoop
*main_loop
;
108 GIOStream
*iostream1
;
109 GIOStream
*iostream2
;
110 } TestCopyChunksData
;
113 test_copy_chunks_splice_cb (GObject
*source_object
,
117 TestCopyChunksData
*data
= user_data
;
118 GMemoryOutputStream
*ostream
;
119 gchar
*received_data
;
120 GError
*error
= NULL
;
122 g_io_stream_splice_finish (res
, &error
);
123 g_assert_no_error (error
);
125 ostream
= G_MEMORY_OUTPUT_STREAM (((GTestIOStream
*) data
->iostream1
)->output_stream
);
126 received_data
= g_memory_output_stream_get_data (ostream
);
127 g_assert_cmpstr (received_data
, ==, data
->data2
);
129 ostream
= G_MEMORY_OUTPUT_STREAM (((GTestIOStream
*) data
->iostream2
)->output_stream
);
130 received_data
= g_memory_output_stream_get_data (ostream
);
131 g_assert_cmpstr (received_data
, ==, data
->data1
);
133 g_assert (g_io_stream_is_closed (data
->iostream1
));
134 g_assert (g_io_stream_is_closed (data
->iostream2
));
136 g_main_loop_quit (data
->main_loop
);
140 test_copy_chunks (void)
142 TestCopyChunksData data
;
143 GInputStream
*istream
;
144 GOutputStream
*ostream
;
146 data
.main_loop
= g_main_loop_new (NULL
, FALSE
);
147 data
.data1
= "abcdefghijklmnopqrstuvwxyz";
148 data
.data2
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
150 istream
= g_memory_input_stream_new_from_data (data
.data1
, -1, NULL
);
151 ostream
= g_memory_output_stream_new (NULL
, 0, g_realloc
, g_free
);
152 data
.iostream1
= g_test_io_stream_new (istream
, ostream
);
153 g_object_unref (istream
);
154 g_object_unref (ostream
);
156 istream
= g_memory_input_stream_new_from_data (data
.data2
, -1, NULL
);
157 ostream
= g_memory_output_stream_new (NULL
, 0, g_realloc
, g_free
);
158 data
.iostream2
= g_test_io_stream_new (istream
, ostream
);
159 g_object_unref (istream
);
160 g_object_unref (ostream
);
162 g_io_stream_splice_async (data
.iostream1
, data
.iostream2
,
163 G_IO_STREAM_SPLICE_CLOSE_STREAM1
| G_IO_STREAM_SPLICE_CLOSE_STREAM2
|
164 G_IO_STREAM_SPLICE_WAIT_FOR_BOTH
, G_PRIORITY_DEFAULT
,
165 NULL
, test_copy_chunks_splice_cb
, &data
);
167 /* We do not hold a ref in data struct, this is to make sure the operation
168 * keeps the iostream objects alive until it finishes */
169 g_object_unref (data
.iostream1
);
170 g_object_unref (data
.iostream2
);
172 g_main_loop_run (data
.main_loop
);
173 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
);