1/* GLib testing framework examples and tests
2 * Copyright (C) 2007 Imendio AB
3 * Authors: Tim Janik
4 *
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.
8 *
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.
12 *
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.
21 */
22
23#include <glib/glib.h>
24#include <gio/gio.h>
25#include <stdlib.h>
26#include <string.h>
27
28static void
29test_read_chunks (void)
30{
31 const char *data1 = "abcdefghijklmnopqrstuvwxyz";
32 const char *data2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
33 const char *result = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
34 char buffer[128];
35 gsize bytes_read, pos, len, chunk_size;
36 GError *error = NULL;
37 GInputStream *stream;
38 gboolean res;
39
40 stream = g_memory_input_stream_new ();
41
42 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
43 data: data1, len: -1, NULL);
44 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
45 data: data2, len: -1, NULL);
46 len = strlen (s: data1) + strlen (s: data2);
47
48 for (chunk_size = 1; chunk_size < len - 1; chunk_size++)
49 {
50 pos = 0;
51 while (pos < len)
52 {
53 bytes_read = g_input_stream_read (stream, buffer, count: chunk_size, NULL, error: &error);
54 g_assert_no_error (error);
55 g_assert_cmpint (bytes_read, ==, MIN (chunk_size, len - pos));
56 g_assert (strncmp (buffer, result + pos, bytes_read) == 0);
57
58 pos += bytes_read;
59 }
60
61 g_assert_cmpint (pos, ==, len);
62 res = g_seekable_seek (G_SEEKABLE (stream), offset: 0, type: G_SEEK_SET, NULL, error: &error);
63 g_assert_cmpint (res, ==, TRUE);
64 g_assert_no_error (error);
65 }
66
67 g_object_unref (object: stream);
68}
69
70GMainLoop *loop;
71
72static void
73async_read_chunk (GObject *object,
74 GAsyncResult *result,
75 gpointer user_data)
76{
77 gsize *bytes_read = user_data;
78 GError *error = NULL;
79
80 *bytes_read = g_input_stream_read_finish (G_INPUT_STREAM (object),
81 result, error: &error);
82 g_assert_no_error (error);
83
84 g_main_loop_quit (loop);
85}
86
87static void
88async_skipped_chunk (GObject *object,
89 GAsyncResult *result,
90 gpointer user_data)
91{
92 gsize *bytes_skipped = user_data;
93 GError *error = NULL;
94
95 *bytes_skipped = g_input_stream_skip_finish (G_INPUT_STREAM (object),
96 result, error: &error);
97 g_assert_no_error (error);
98
99 g_main_loop_quit (loop);
100}
101
102static void
103test_async (void)
104{
105 const char *data1 = "abcdefghijklmnopqrstuvwxyz";
106 const char *data2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
107 const char *result = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
108 char buffer[128];
109 gsize bytes_read, bytes_skipped;
110 gsize pos, len, chunk_size;
111 GError *error = NULL;
112 GInputStream *stream;
113 gboolean res;
114
115 loop = g_main_loop_new (NULL, FALSE);
116
117 stream = g_memory_input_stream_new ();
118
119 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
120 data: data1, len: -1, NULL);
121 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
122 data: data2, len: -1, NULL);
123 len = strlen (s: data1) + strlen (s: data2);
124
125 for (chunk_size = 1; chunk_size < len - 1; chunk_size++)
126 {
127 pos = 0;
128 while (pos < len)
129 {
130 g_input_stream_read_async (stream, buffer, count: chunk_size,
131 G_PRIORITY_DEFAULT, NULL,
132 callback: async_read_chunk, user_data: &bytes_read);
133 g_main_loop_run (loop);
134
135 g_assert_cmpint (bytes_read, ==, MIN (chunk_size, len - pos));
136 g_assert (strncmp (buffer, result + pos, bytes_read) == 0);
137
138 pos += bytes_read;
139 }
140
141 g_assert_cmpint (pos, ==, len);
142 res = g_seekable_seek (G_SEEKABLE (stream), offset: 0, type: G_SEEK_SET, NULL, error: &error);
143 g_assert_cmpint (res, ==, TRUE);
144 g_assert_no_error (error);
145
146 pos = 0;
147 while (pos + chunk_size + 1 < len)
148 {
149 g_input_stream_skip_async (stream, count: chunk_size,
150 G_PRIORITY_DEFAULT, NULL,
151 callback: async_skipped_chunk, user_data: &bytes_skipped);
152 g_main_loop_run (loop);
153
154 g_assert_cmpint (bytes_skipped, ==, MIN (chunk_size, len - pos));
155
156 pos += bytes_skipped;
157 }
158
159 g_input_stream_read_async (stream, buffer, count: len - pos,
160 G_PRIORITY_DEFAULT, NULL,
161 callback: async_read_chunk, user_data: &bytes_read);
162 g_main_loop_run (loop);
163
164 g_assert_cmpint (bytes_read, ==, len - pos);
165 g_assert (strncmp (buffer, result + pos, bytes_read) == 0);
166
167 res = g_seekable_seek (G_SEEKABLE (stream), offset: 0, type: G_SEEK_SET, NULL, error: &error);
168 g_assert_cmpint (res, ==, TRUE);
169 g_assert_no_error (error);
170 }
171
172 g_object_unref (object: stream);
173 g_main_loop_unref (loop);
174}
175
176static void
177test_seek (void)
178{
179 const char *data1 = "abcdefghijklmnopqrstuvwxyz";
180 const char *data2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
181 GInputStream *stream;
182 GError *error;
183 char buffer[10];
184
185 stream = g_memory_input_stream_new ();
186
187 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
188 data: data1, len: -1, NULL);
189 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
190 data: data2, len: -1, NULL);
191
192 g_assert (G_IS_SEEKABLE (stream));
193 g_assert (g_seekable_can_seek (G_SEEKABLE (stream)));
194
195 error = NULL;
196 g_assert (g_seekable_seek (G_SEEKABLE (stream), 26, G_SEEK_SET, NULL, &error));
197 g_assert_no_error (error);
198 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (stream)), ==, 26);
199
200 g_assert (g_input_stream_read (stream, buffer, 1, NULL, &error) == 1);
201 g_assert_no_error (error);
202
203 g_assert (buffer[0] == 'A');
204
205 g_assert (!g_seekable_seek (G_SEEKABLE (stream), 26, G_SEEK_CUR, NULL, &error));
206 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
207 g_error_free (error);
208
209 g_object_unref (object: stream);
210}
211
212static void
213test_truncate (void)
214{
215 const char *data1 = "abcdefghijklmnopqrstuvwxyz";
216 const char *data2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
217 GInputStream *stream;
218 GError *error;
219
220 stream = g_memory_input_stream_new ();
221
222 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
223 data: data1, len: -1, NULL);
224 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
225 data: data2, len: -1, NULL);
226
227 g_assert (G_IS_SEEKABLE (stream));
228 g_assert (!g_seekable_can_truncate (G_SEEKABLE (stream)));
229
230 error = NULL;
231 g_assert (!g_seekable_truncate (G_SEEKABLE (stream), 26, NULL, &error));
232 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
233 g_error_free (error);
234
235 g_object_unref (object: stream);
236}
237
238static void
239test_read_bytes (void)
240{
241 const char *data1 = "abcdefghijklmnopqrstuvwxyz";
242 const char *data2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
243 GInputStream *stream;
244 GError *error = NULL;
245 GBytes *bytes;
246 gsize size;
247 gconstpointer data;
248
249 stream = g_memory_input_stream_new ();
250 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
251 data: data1, len: -1, NULL);
252 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
253 data: data2, len: -1, NULL);
254
255 bytes = g_input_stream_read_bytes (stream, count: 26, NULL, error: &error);
256 g_assert_no_error (error);
257
258 data = g_bytes_get_data (bytes, size: &size);
259 g_assert_cmpint (size, ==, 26);
260 g_assert (strncmp (data, data1, 26) == 0);
261
262 g_bytes_unref (bytes);
263 g_object_unref (object: stream);
264}
265
266static void
267test_from_bytes (void)
268{
269 gchar data[4096], buffer[4096];
270 GBytes *bytes;
271 GError *error = NULL;
272 GInputStream *stream;
273 gint i;
274
275 for (i = 0; i < 4096; i++)
276 data[i] = 1 + i % 255;
277
278 bytes = g_bytes_new_static (data, size: 4096);
279 stream = g_memory_input_stream_new_from_bytes (bytes);
280 g_assert (g_input_stream_read (stream, buffer, 2048, NULL, &error) == 2048);
281 g_assert_no_error (error);
282 g_assert (strncmp (data, buffer, 2048) == 0);
283
284 g_object_unref (object: stream);
285 g_bytes_unref (bytes);
286}
287
288int
289main (int argc,
290 char *argv[])
291{
292 g_test_init (argc: &argc, argv: &argv, NULL);
293
294 g_test_add_func (testpath: "/memory-input-stream/read-chunks", test_func: test_read_chunks);
295 g_test_add_func (testpath: "/memory-input-stream/async", test_func: test_async);
296 g_test_add_func (testpath: "/memory-input-stream/seek", test_func: test_seek);
297 g_test_add_func (testpath: "/memory-input-stream/truncate", test_func: test_truncate);
298 g_test_add_func (testpath: "/memory-input-stream/read-bytes", test_func: test_read_bytes);
299 g_test_add_func (testpath: "/memory-input-stream/from-bytes", test_func: test_from_bytes);
300
301 return g_test_run();
302}
303

source code of gtk/subprojects/glib/gio/tests/memory-input-stream.c