GSettings: small internal refactor
[glib.git] / gio / tests / io-stream.c
blob59b1c2669584aaad0c280ed26eaa725ac4411d4c
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>
24 #include <gio/gio.h>
25 #include <stdlib.h>
26 #include <string.h>
28 typedef struct
30 GIOStream parent;
31 GInputStream *input_stream;
32 GOutputStream *output_stream;
33 } GTestIOStream;
35 typedef struct
37 GIOStreamClass parent_class;
38 } GTestIOStreamClass;
40 static GType g_test_io_stream_get_type (void);
41 G_DEFINE_TYPE (GTestIOStream, g_test_io_stream, G_TYPE_IO_STREAM);
44 static GInputStream *
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;
60 static void
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);
74 static void
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;
86 static void
87 g_test_io_stream_init (GTestIOStream *self)
91 static GIOStream *
92 g_test_io_stream_new (GInputStream *input, GOutputStream *output)
94 GTestIOStream *self;
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);
103 typedef struct
105 GMainLoop *main_loop;
106 const gchar *data1;
107 const gchar *data2;
108 GIOStream *iostream1;
109 GIOStream *iostream2;
110 } TestCopyChunksData;
112 static void
113 test_copy_chunks_splice_cb (GObject *source_object,
114 GAsyncResult *res,
115 gpointer user_data)
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);
139 static void
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);
177 main (int argc,
178 char *argv[])
180 g_test_init (&argc, &argv, NULL);
182 g_test_add_func ("/io-stream/copy-chunks", test_copy_chunks);
184 return g_test_run();