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>
38 test_copy_chunks_splice_cb (GObject
*source_object
,
42 TestCopyChunksData
*data
= user_data
;
43 GMemoryOutputStream
*ostream
;
47 g_io_stream_splice_finish (res
, &error
);
48 g_assert_no_error (error
);
50 ostream
= G_MEMORY_OUTPUT_STREAM (g_io_stream_get_output_stream (data
->iostream1
));
51 received_data
= g_memory_output_stream_get_data (ostream
);
52 g_assert_cmpstr (received_data
, ==, data
->data2
);
54 ostream
= G_MEMORY_OUTPUT_STREAM (g_io_stream_get_output_stream (data
->iostream2
));
55 received_data
= g_memory_output_stream_get_data (ostream
);
56 g_assert_cmpstr (received_data
, ==, data
->data1
);
58 g_assert (g_io_stream_is_closed (data
->iostream1
));
59 g_assert (g_io_stream_is_closed (data
->iostream2
));
61 g_main_loop_quit (data
->main_loop
);
65 test_copy_chunks (void)
67 TestCopyChunksData data
;
68 GInputStream
*istream
;
69 GOutputStream
*ostream
;
71 data
.main_loop
= g_main_loop_new (NULL
, FALSE
);
72 data
.data1
= "abcdefghijklmnopqrstuvwxyz";
73 data
.data2
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
75 istream
= g_memory_input_stream_new_from_data (data
.data1
, -1, NULL
);
76 ostream
= g_memory_output_stream_new (NULL
, 0, g_realloc
, g_free
);
77 data
.iostream1
= g_simple_io_stream_new (istream
, ostream
);
78 g_object_unref (istream
);
79 g_object_unref (ostream
);
81 istream
= g_memory_input_stream_new_from_data (data
.data2
, -1, NULL
);
82 ostream
= g_memory_output_stream_new (NULL
, 0, g_realloc
, g_free
);
83 data
.iostream2
= g_simple_io_stream_new (istream
, ostream
);
84 g_object_unref (istream
);
85 g_object_unref (ostream
);
87 g_io_stream_splice_async (data
.iostream1
, data
.iostream2
,
88 G_IO_STREAM_SPLICE_CLOSE_STREAM1
| G_IO_STREAM_SPLICE_CLOSE_STREAM2
|
89 G_IO_STREAM_SPLICE_WAIT_FOR_BOTH
, G_PRIORITY_DEFAULT
,
90 NULL
, test_copy_chunks_splice_cb
, &data
);
92 /* We do not hold a ref in data struct, this is to make sure the operation
93 * keeps the iostream objects alive until it finishes */
94 g_object_unref (data
.iostream1
);
95 g_object_unref (data
.iostream2
);
97 g_main_loop_run (data
.main_loop
);
98 g_main_loop_unref (data
.main_loop
);
102 close_async_done (GObject
*source
,
103 GAsyncResult
*result
,
106 gboolean
*done
= user_data
;
112 test_close_file (void)
120 file
= g_file_new_for_path ("/dev/null");
121 fios
= g_file_open_readwrite (file
, NULL
, NULL
);
122 g_object_unref (file
);
125 io
= g_simple_io_stream_new (g_io_stream_get_input_stream (G_IO_STREAM (fios
)),
126 g_io_stream_get_output_stream (G_IO_STREAM (fios
)));
127 g_object_unref (fios
);
129 g_io_stream_close_async (io
, 0, NULL
, close_async_done
, &done
);
134 g_main_context_iteration (NULL
, TRUE
);
139 test_close_memory (void)
146 in
= g_memory_input_stream_new ();
147 out
= g_memory_output_stream_new_resizable ();
148 io
= g_simple_io_stream_new (in
, out
);
149 g_object_unref (out
);
152 g_io_stream_close_async (io
, 0, NULL
, close_async_done
, &done
);
157 g_main_context_iteration (NULL
, TRUE
);
164 g_test_init (&argc
, &argv
, NULL
);
166 g_test_add_func ("/io-stream/copy-chunks", test_copy_chunks
);
167 g_test_add_func ("/io-stream/close/async/memory", test_close_memory
);
168 g_test_add_func ("/io-stream/close/async/file", test_close_file
);