1 /* GLib testing framework examples and tests
2 * Copyright (C) 2008 Red Hat, Inc.
3 * Author: Matthias Clasen
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.
24 #include <glib/glib.h>
38 g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=540423");
40 mo = g_memory_output_stream_new_resizable ();
41 g_assert (g_seekable_can_truncate (G_SEEKABLE (mo)));
42 o = g_data_output_stream_new (mo);
43 for (i = 0; i < 1000; i++)
45 g_data_output_stream_put_byte (o, 1, NULL, &error);
46 g_assert_no_error (error);
48 g_assert_cmpint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo)), ==, 1000);
49 g_seekable_truncate (G_SEEKABLE (mo), 0, NULL, &error);
50 g_assert_cmpuint (g_seekable_tell (G_SEEKABLE (mo)), ==, 1000);
52 g_assert_no_error (error);
53 g_assert_cmpint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo)), ==, 0);
54 for (i = 0; i < 2000; i++)
56 g_data_output_stream_put_byte (o, 2, NULL, &error);
57 g_assert_no_error (error);
59 g_assert_cmpint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo)), ==, 3000);
61 data = (guint8 *)g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (mo));
63 /* The 1's written initially were lost when we truncated to 0
64 * and then started writing at position 1000.
66 for (i = 0; i < 1000; i++)
67 g_assert_cmpuint (data[i], ==, 0);
68 for (i = 1000; i < 3000; i++)
69 g_assert_cmpuint (data[i], ==, 2);
71 g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=720080");
73 g_seekable_truncate (G_SEEKABLE (mo), 8192, NULL, &error);
74 g_assert_cmpint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo)), ==, 8192);
76 data = (guint8 *)g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (mo));
77 for (i = 3000; i < 8192; i++)
78 g_assert_cmpuint (data[i], ==, 0);
85 test_seek_fixed (void)
90 mo = g_memory_output_stream_new (g_new (gchar, 100), 100, NULL, g_free);
92 g_assert (G_IS_SEEKABLE (mo));
93 g_assert (g_seekable_can_seek (G_SEEKABLE (mo)));
94 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 0);
97 g_assert (!g_seekable_seek (G_SEEKABLE (mo), 222, G_SEEK_CUR, NULL, &error));
98 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
99 g_clear_error (&error);
100 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 0);
102 g_assert (g_seekable_seek (G_SEEKABLE (mo), 26, G_SEEK_SET, NULL, &error));
103 g_assert_no_error (error);
104 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 26);
106 g_assert (g_seekable_seek (G_SEEKABLE (mo), 20, G_SEEK_CUR, NULL, &error));
107 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 46);
108 g_assert_no_error (error);
110 g_assert (!g_seekable_seek (G_SEEKABLE (mo), 200, G_SEEK_CUR, NULL, &error));
111 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
112 g_clear_error (&error);
113 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 46);
115 g_assert (!g_seekable_seek (G_SEEKABLE (mo), 1, G_SEEK_END, NULL, &error));
116 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
117 g_clear_error (&error);
118 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 46);
120 g_assert (g_seekable_seek (G_SEEKABLE (mo), 0, G_SEEK_END, NULL, &error));
121 g_assert_no_error (error);
122 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 100);
124 g_assert (g_seekable_seek (G_SEEKABLE (mo), -1, G_SEEK_END, NULL, &error));
125 g_assert_no_error (error);
126 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 99);
132 test_seek_resizable_stream (GOutputStream *mo)
136 g_assert (G_IS_SEEKABLE (mo));
137 g_assert (g_seekable_can_seek (G_SEEKABLE (mo)));
138 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 0);
141 g_assert (g_seekable_seek (G_SEEKABLE (mo), 222, G_SEEK_CUR, NULL, &error));
142 g_assert_no_error (error);
143 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 222);
145 g_assert (g_seekable_seek (G_SEEKABLE (mo), 26, G_SEEK_SET, NULL, &error));
146 g_assert_no_error (error);
147 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 26);
149 g_assert (g_seekable_seek (G_SEEKABLE (mo), 20, G_SEEK_CUR, NULL, &error));
150 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 46);
151 g_assert_no_error (error);
153 g_assert (g_seekable_seek (G_SEEKABLE (mo), 200, G_SEEK_CUR, NULL, &error));
154 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 246);
155 g_assert_no_error (error);
157 g_assert (g_seekable_seek (G_SEEKABLE (mo), 1, G_SEEK_END, NULL, &error));
158 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 1);
159 g_assert_no_error (error);
161 g_assert (g_seekable_seek (G_SEEKABLE (mo), 0, G_SEEK_END, NULL, &error));
162 g_assert_no_error (error);
163 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 0);
165 /* The 'end' is still zero, so this should fail */
166 g_assert (!g_seekable_seek (G_SEEKABLE (mo), -1, G_SEEK_END, NULL, &error));
167 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
168 g_clear_error (&error);
169 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 0);
173 test_seek_resizable (void)
178 /* For resizable streams, the initially allocated size is purely an
179 * implementation detail. We should not be able to tell the
180 * difference based on the seek API, so make a bunch of streams with
181 * different sizes and subject them to the same test.
183 for (i = 0; i < 1024; i++)
185 mo = g_memory_output_stream_new (g_malloc (i), i, g_realloc, g_free);
187 test_seek_resizable_stream (mo);
189 g_assert_cmpint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo)), ==, 0);
190 /* No writes = no resizes */
191 g_assert_cmpint (g_memory_output_stream_get_size (G_MEMORY_OUTPUT_STREAM (mo)), ==, i);
198 test_data_size (void)
201 GDataOutputStream *o;
204 g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=540459");
206 mo = g_memory_output_stream_new_resizable ();
207 o = g_data_output_stream_new (mo);
208 g_data_output_stream_put_byte (o, 1, NULL, NULL);
209 pos = g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo));
210 g_assert_cmpint (pos, ==, 1);
212 g_seekable_seek (G_SEEKABLE (mo), 0, G_SEEK_CUR, NULL, NULL);
213 pos = g_seekable_tell (G_SEEKABLE (mo));
214 g_assert_cmpint (pos, ==, 1);
216 g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=540461");
218 g_seekable_seek (G_SEEKABLE (mo), 0, G_SEEK_SET, NULL, NULL);
219 pos = g_seekable_tell (G_SEEKABLE (mo));
220 g_assert_cmpint (pos, ==, 0);
222 pos = g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo));
223 g_assert_cmpint (pos, ==, 1);
225 g_assert_cmpint (g_memory_output_stream_get_size (G_MEMORY_OUTPUT_STREAM (mo)), ==, 16);
232 test_properties (void)
235 GDataOutputStream *o;
237 GError *error = NULL;
239 gsize data_size_prop = 0;
244 g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=605733");
246 mo = (GOutputStream*) g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
247 "realloc-function", g_realloc,
248 "destroy-function", g_free,
250 o = g_data_output_stream_new (mo);
252 for (i = 0; i < 1000; i++)
254 g_data_output_stream_put_byte (o, 1, NULL, &error);
255 g_assert_no_error (error);
258 data_size_fun = g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo));
259 g_object_get (mo, "data-size", &data_size_prop, NULL);
260 g_assert_cmpint (data_size_fun, ==, data_size_prop);
262 data_fun = g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (mo));
263 g_object_get (mo, "data", &data_prop, NULL);
264 g_assert_cmphex (GPOINTER_TO_SIZE (data_fun), ==, GPOINTER_TO_SIZE (data_prop));
266 g_object_get (mo, "realloc-function", &func, NULL);
267 g_assert (func == g_realloc);
268 g_object_get (mo, "destroy-function", &func, NULL);
269 g_assert (func == g_free);
271 data_size_fun = g_memory_output_stream_get_size (G_MEMORY_OUTPUT_STREAM (mo));
272 g_object_get (mo, "size", &data_size_prop, NULL);
273 g_assert_cmpint (data_size_fun, ==, data_size_prop);
280 test_write_bytes (void)
283 GBytes *bytes, *bytes2;
284 GError *error = NULL;
286 mo = (GOutputStream*) g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
287 "realloc-function", g_realloc,
288 "destroy-function", g_free,
290 bytes = g_bytes_new_static ("hello world!", strlen ("hello world!") + 1);
291 g_output_stream_write_bytes (mo, bytes, NULL, &error);
292 g_assert_no_error (error);
294 g_output_stream_close (mo, NULL, &error);
295 g_assert_no_error (error);
297 bytes2 = g_memory_output_stream_steal_as_bytes (G_MEMORY_OUTPUT_STREAM (mo));
299 g_assert (g_bytes_equal (bytes, bytes2));
301 g_bytes_unref (bytes);
302 g_bytes_unref (bytes2);
306 test_write_null (void)
309 GError *error = NULL;
311 g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/2471");
313 mo = g_memory_output_stream_new_resizable ();
314 g_output_stream_write_all (mo, NULL, 0, NULL, NULL, &error);
315 g_assert_no_error (error);
317 g_assert_cmpint (0, ==, g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo)));
319 g_output_stream_close (mo, NULL, &error);
320 g_assert_no_error (error);
324 /* Test that writev() works on #GMemoryOutputStream with a non-empty set of vectors. This
325 * covers the default writev() implementation around write(). */
330 GError *error = NULL;
333 GOutputVector vectors[3];
334 const guint8 buffer[] = {1, 2, 3, 4, 5,
335 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
337 guint8 *output_buffer;
339 vectors[0].buffer = buffer;
342 vectors[1].buffer = buffer + vectors[0].size;
343 vectors[1].size = 12;
345 vectors[2].buffer = buffer + vectors[0].size + vectors[1].size;
348 mo = (GOutputStream*) g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
349 "realloc-function", g_realloc,
350 "destroy-function", g_free,
352 res = g_output_stream_writev_all (mo, vectors, G_N_ELEMENTS (vectors), &bytes_written, NULL, &error);
353 g_assert_no_error (error);
355 g_assert_cmpuint (bytes_written, ==, sizeof buffer);
357 g_output_stream_close (mo, NULL, &error);
358 g_assert_no_error (error);
360 g_assert_cmpuint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo)), ==, sizeof buffer);
361 output_buffer = g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (mo));
362 g_assert_cmpmem (output_buffer, sizeof buffer, buffer, sizeof buffer);
367 /* Test that writev_nonblocking() works on #GMemoryOutputStream with a non-empty set of vectors. This
368 * covers the default writev_nonblocking() implementation around write_nonblocking(). */
370 test_writev_nonblocking (void)
373 GError *error = NULL;
376 GOutputVector vectors[3];
377 const guint8 buffer[] = {1, 2, 3, 4, 5,
378 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
380 guint8 *output_buffer;
382 vectors[0].buffer = buffer;
385 vectors[1].buffer = buffer + vectors[0].size;
386 vectors[1].size = 12;
388 vectors[2].buffer = buffer + vectors[0].size + vectors[1].size;
391 mo = (GOutputStream*) g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
392 "realloc-function", g_realloc,
393 "destroy-function", g_free,
395 res = g_pollable_output_stream_writev_nonblocking (G_POLLABLE_OUTPUT_STREAM (mo),
396 vectors, G_N_ELEMENTS (vectors),
397 &bytes_written, NULL, &error);
398 g_assert_no_error (error);
399 g_assert_cmpint (res, ==, G_POLLABLE_RETURN_OK);
400 g_assert_cmpuint (bytes_written, ==, sizeof buffer);
402 g_output_stream_close (mo, NULL, &error);
403 g_assert_no_error (error);
405 g_assert_cmpuint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo)), ==, sizeof buffer);
406 output_buffer = g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (mo));
407 g_assert_cmpmem (output_buffer, sizeof buffer, buffer, sizeof buffer);
413 test_steal_as_bytes (void)
416 GDataOutputStream *o;
417 GError *error = NULL;
421 mo = (GOutputStream*) g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
422 "realloc-function", g_realloc,
423 "destroy-function", g_free,
425 o = g_data_output_stream_new (mo);
427 g_data_output_stream_put_string (o, "hello ", NULL, &error);
428 g_assert_no_error (error);
430 g_data_output_stream_put_string (o, "world!", NULL, &error);
431 g_assert_no_error (error);
433 g_data_output_stream_put_byte (o, '\0', NULL, &error);
434 g_assert_no_error (error);
436 g_output_stream_close ((GOutputStream*) o, NULL, &error);
437 g_assert_no_error (error);
439 bytes = g_memory_output_stream_steal_as_bytes ((GMemoryOutputStream*)mo);
442 g_assert_cmpint (g_bytes_get_size (bytes), ==, strlen ("hello world!") + 1);
443 g_assert_cmpstr (g_bytes_get_data (bytes, &size), ==, "hello world!");
445 g_bytes_unref (bytes);
453 g_test_init (&argc, &argv, NULL);
455 g_test_add_func ("/memory-output-stream/truncate", test_truncate);
456 g_test_add_func ("/memory-output-stream/seek/fixed", test_seek_fixed);
457 g_test_add_func ("/memory-output-stream/seek/resizable", test_seek_resizable);
458 g_test_add_func ("/memory-output-stream/get-data-size", test_data_size);
459 g_test_add_func ("/memory-output-stream/properties", test_properties);
460 g_test_add_func ("/memory-output-stream/write-bytes", test_write_bytes);
461 g_test_add_func ("/memory-output-stream/write-null", test_write_null);
462 g_test_add_func ("/memory-output-stream/writev", test_writev);
463 g_test_add_func ("/memory-output-stream/writev_nonblocking", test_writev_nonblocking);
464 g_test_add_func ("/memory-output-stream/steal_as_bytes", test_steal_as_bytes);