1 /* GLib testing framework examples and tests
2 * Copyright (C) 2010 Collabora Ltd.
3 * Authors: Xavier Claessens <xclaesse@gmail.com>
5 * SPDX-License-Identifier: LicenseRef-old-glib-tests
7 * This work is provided "as is"; redistribution and modification
8 * in whole or in part, in any medium, physical or electronic is
9 * permitted without restriction.
11 * This work is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 * In no event shall the authors or contributors be liable for any
16 * direct, indirect, incidental, special, exemplary, or consequential
17 * damages (including, but not limited to, procurement of substitute
18 * goods or services; loss of use, data, or profits; or business
19 * interruption) however caused and on any theory of liability, whether
20 * in contract, strict liability, or tort (including negligence or
21 * otherwise) arising in any way out of the use of this software, even
22 * if advised of the possibility of such damage.
25 #include <glib/glib.h>
40 test_copy_chunks_splice_cb (GObject *source_object,
44 TestCopyChunksData *data = user_data;
45 GMemoryOutputStream *ostream;
49 g_io_stream_splice_finish (res, &error);
50 g_assert_no_error (error);
52 ostream = G_MEMORY_OUTPUT_STREAM (g_io_stream_get_output_stream (data->iostream1));
53 received_data = g_memory_output_stream_get_data (ostream);
54 g_assert_cmpstr (received_data, ==, data->data2);
56 ostream = G_MEMORY_OUTPUT_STREAM (g_io_stream_get_output_stream (data->iostream2));
57 received_data = g_memory_output_stream_get_data (ostream);
58 g_assert_cmpstr (received_data, ==, data->data1);
60 g_assert (g_io_stream_is_closed (data->iostream1));
61 g_assert (g_io_stream_is_closed (data->iostream2));
63 g_main_loop_quit (data->main_loop);
67 test_copy_chunks (void)
69 TestCopyChunksData data;
70 GInputStream *istream;
71 GOutputStream *ostream;
73 data.main_loop = g_main_loop_new (NULL, FALSE);
74 data.data1 = "abcdefghijklmnopqrstuvwxyz";
75 data.data2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
77 istream = g_memory_input_stream_new_from_data (data.data1, -1, NULL);
78 ostream = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
79 data.iostream1 = g_simple_io_stream_new (istream, ostream);
80 g_object_unref (istream);
81 g_object_unref (ostream);
83 istream = g_memory_input_stream_new_from_data (data.data2, -1, NULL);
84 ostream = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
85 data.iostream2 = g_simple_io_stream_new (istream, ostream);
86 g_object_unref (istream);
87 g_object_unref (ostream);
89 g_io_stream_splice_async (data.iostream1, data.iostream2,
90 G_IO_STREAM_SPLICE_CLOSE_STREAM1 | G_IO_STREAM_SPLICE_CLOSE_STREAM2 |
91 G_IO_STREAM_SPLICE_WAIT_FOR_BOTH, G_PRIORITY_DEFAULT,
92 NULL, test_copy_chunks_splice_cb, &data);
94 /* We do not hold a ref in data struct, this is to make sure the operation
95 * keeps the iostream objects alive until it finishes */
96 g_object_unref (data.iostream1);
97 g_object_unref (data.iostream2);
99 g_main_loop_run (data.main_loop);
100 g_main_loop_unref (data.main_loop);
104 close_async_done (GObject *source,
105 GAsyncResult *result,
108 gboolean *done = user_data;
114 test_close_file (void)
122 file = g_file_new_for_path ("/dev/null");
123 fios = g_file_open_readwrite (file, NULL, NULL);
124 g_object_unref (file);
127 io = g_simple_io_stream_new (g_io_stream_get_input_stream (G_IO_STREAM (fios)),
128 g_io_stream_get_output_stream (G_IO_STREAM (fios)));
129 g_object_unref (fios);
131 g_io_stream_close_async (io, 0, NULL, close_async_done, &done);
136 g_main_context_iteration (NULL, TRUE);
141 test_close_memory (void)
148 in = g_memory_input_stream_new ();
149 out = g_memory_output_stream_new_resizable ();
150 io = g_simple_io_stream_new (in, out);
151 g_object_unref (out);
154 g_io_stream_close_async (io, 0, NULL, close_async_done, &done);
159 g_main_context_iteration (NULL, TRUE);
166 g_test_init (&argc, &argv, NULL);
168 g_test_add_func ("/io-stream/copy-chunks", test_copy_chunks);
169 g_test_add_func ("/io-stream/close/async/memory", test_close_memory);
170 g_test_add_func ("/io-stream/close/async/file", test_close_file);