1 /* GLib testing framework examples and tests
2 * Copyright (C) 2008 Red Hat, Inc.
3 * Author: Matthias Clasen
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.
22 #include <glib/glib.h>
35 g_test_bug ("540423");
37 mo = g_memory_output_stream_new_resizable ();
38 g_assert (g_seekable_can_truncate (G_SEEKABLE (mo)));
39 o = g_data_output_stream_new (mo);
40 for (i = 0; i < 1000; i++)
42 g_data_output_stream_put_byte (o, 1, NULL, &error);
43 g_assert_no_error (error);
45 g_seekable_truncate (G_SEEKABLE (mo), 0, NULL, &error);
46 g_assert_no_error (error);
47 for (i = 0; i < 2000; i++)
49 g_data_output_stream_put_byte (o, 1, NULL, &error);
50 g_assert_no_error (error);
53 g_test_bug ("720080");
55 g_seekable_truncate (G_SEEKABLE (mo), 8192, NULL, &error);
56 g_assert_cmpint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo)), ==, 8192);
63 test_seek_fixed (void)
68 mo = g_memory_output_stream_new (g_new (gchar, 100), 100, NULL, g_free);
70 g_assert (G_IS_SEEKABLE (mo));
71 g_assert (g_seekable_can_seek (G_SEEKABLE (mo)));
72 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 0);
75 g_assert (!g_seekable_seek (G_SEEKABLE (mo), 222, G_SEEK_CUR, NULL, &error));
76 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
77 g_clear_error (&error);
78 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 0);
80 g_assert (g_seekable_seek (G_SEEKABLE (mo), 26, G_SEEK_SET, NULL, &error));
81 g_assert_no_error (error);
82 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 26);
84 g_assert (g_seekable_seek (G_SEEKABLE (mo), 20, G_SEEK_CUR, NULL, &error));
85 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 46);
86 g_assert_no_error (error);
88 g_assert (!g_seekable_seek (G_SEEKABLE (mo), 200, G_SEEK_CUR, NULL, &error));
89 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
90 g_clear_error (&error);
91 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 46);
93 g_assert (!g_seekable_seek (G_SEEKABLE (mo), 1, G_SEEK_END, NULL, &error));
94 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
95 g_clear_error (&error);
96 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 46);
98 g_assert (g_seekable_seek (G_SEEKABLE (mo), 0, G_SEEK_END, NULL, &error));
99 g_assert_no_error (error);
100 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 100);
102 g_assert (g_seekable_seek (G_SEEKABLE (mo), -1, G_SEEK_END, NULL, &error));
103 g_assert_no_error (error);
104 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 99);
110 test_seek_resizable_stream (GOutputStream *mo)
114 g_assert (G_IS_SEEKABLE (mo));
115 g_assert (g_seekable_can_seek (G_SEEKABLE (mo)));
116 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 0);
119 g_assert (g_seekable_seek (G_SEEKABLE (mo), 222, G_SEEK_CUR, NULL, &error));
120 g_assert_no_error (error);
121 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 222);
123 g_assert (g_seekable_seek (G_SEEKABLE (mo), 26, G_SEEK_SET, NULL, &error));
124 g_assert_no_error (error);
125 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 26);
127 g_assert (g_seekable_seek (G_SEEKABLE (mo), 20, G_SEEK_CUR, NULL, &error));
128 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 46);
129 g_assert_no_error (error);
131 g_assert (g_seekable_seek (G_SEEKABLE (mo), 200, G_SEEK_CUR, NULL, &error));
132 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 246);
133 g_assert_no_error (error);
135 g_assert (g_seekable_seek (G_SEEKABLE (mo), 1, G_SEEK_END, NULL, &error));
136 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 1);
137 g_assert_no_error (error);
139 g_assert (g_seekable_seek (G_SEEKABLE (mo), 0, G_SEEK_END, NULL, &error));
140 g_assert_no_error (error);
141 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 0);
143 /* The 'end' is still zero, so this should fail */
144 g_assert (!g_seekable_seek (G_SEEKABLE (mo), -1, G_SEEK_END, NULL, &error));
145 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
146 g_clear_error (&error);
147 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 0);
151 test_seek_resizable (void)
156 /* For resizable streams, the initially allocated size is purely an
157 * implementation detail. We should not be able to tell the
158 * difference based on the seek API, so make a bunch of streams with
159 * different sizes and subject them to the same test.
161 for (i = 0; i < 1024; i++)
163 mo = g_memory_output_stream_new (g_malloc (i), i, g_realloc, g_free);
165 test_seek_resizable_stream (mo);
167 g_assert_cmpint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo)), ==, 0);
168 /* No writes = no resizes */
169 g_assert_cmpint (g_memory_output_stream_get_size (G_MEMORY_OUTPUT_STREAM (mo)), ==, i);
176 test_data_size (void)
179 GDataOutputStream *o;
182 g_test_bug ("540459");
184 mo = g_memory_output_stream_new_resizable ();
185 o = g_data_output_stream_new (mo);
186 g_data_output_stream_put_byte (o, 1, NULL, NULL);
187 pos = g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo));
188 g_assert_cmpint (pos, ==, 1);
190 g_seekable_seek (G_SEEKABLE (mo), 0, G_SEEK_CUR, NULL, NULL);
191 pos = g_seekable_tell (G_SEEKABLE (mo));
192 g_assert_cmpint (pos, ==, 1);
194 g_test_bug ("540461");
196 g_seekable_seek (G_SEEKABLE (mo), 0, G_SEEK_SET, NULL, NULL);
197 pos = g_seekable_tell (G_SEEKABLE (mo));
198 g_assert_cmpint (pos, ==, 0);
200 pos = g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo));
201 g_assert_cmpint (pos, ==, 1);
203 g_assert_cmpint (g_memory_output_stream_get_size (G_MEMORY_OUTPUT_STREAM (mo)), ==, 16);
210 test_properties (void)
213 GDataOutputStream *o;
215 GError *error = NULL;
217 gsize data_size_prop = 0;
222 g_test_bug ("605733");
224 mo = (GOutputStream*) g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
225 "realloc-function", g_realloc,
226 "destroy-function", g_free,
228 o = g_data_output_stream_new (mo);
230 for (i = 0; i < 1000; i++)
232 g_data_output_stream_put_byte (o, 1, NULL, &error);
233 g_assert_no_error (error);
236 data_size_fun = g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo));
237 g_object_get (mo, "data-size", &data_size_prop, NULL);
238 g_assert_cmpint (data_size_fun, ==, data_size_prop);
240 data_fun = g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (mo));
241 g_object_get (mo, "data", &data_prop, NULL);
242 g_assert_cmphex (GPOINTER_TO_SIZE (data_fun), ==, GPOINTER_TO_SIZE (data_prop));
244 g_object_get (mo, "realloc-function", &func, NULL);
245 g_assert (func == g_realloc);
246 g_object_get (mo, "destroy-function", &func, NULL);
247 g_assert (func == g_free);
249 data_size_fun = g_memory_output_stream_get_size (G_MEMORY_OUTPUT_STREAM (mo));
250 g_object_get (mo, "size", &data_size_prop, NULL);
251 g_assert_cmpint (data_size_fun, ==, data_size_prop);
258 test_write_bytes (void)
261 GBytes *bytes, *bytes2;
262 GError *error = NULL;
264 mo = (GOutputStream*) g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
265 "realloc-function", g_realloc,
266 "destroy-function", g_free,
268 bytes = g_bytes_new_static ("hello world!", strlen ("hello world!") + 1);
269 g_output_stream_write_bytes (mo, bytes, NULL, &error);
270 g_assert_no_error (error);
272 g_output_stream_close (mo, NULL, &error);
273 g_assert_no_error (error);
275 bytes2 = g_memory_output_stream_steal_as_bytes (G_MEMORY_OUTPUT_STREAM (mo));
277 g_assert (g_bytes_equal (bytes, bytes2));
279 g_bytes_unref (bytes);
280 g_bytes_unref (bytes2);
284 test_steal_as_bytes (void)
287 GDataOutputStream *o;
288 GError *error = NULL;
292 mo = (GOutputStream*) g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
293 "realloc-function", g_realloc,
294 "destroy-function", g_free,
296 o = g_data_output_stream_new (mo);
298 g_data_output_stream_put_string (o, "hello ", NULL, &error);
299 g_assert_no_error (error);
301 g_data_output_stream_put_string (o, "world!", NULL, &error);
302 g_assert_no_error (error);
304 g_data_output_stream_put_byte (o, '\0', NULL, &error);
305 g_assert_no_error (error);
307 g_output_stream_close ((GOutputStream*) o, NULL, &error);
308 g_assert_no_error (error);
310 bytes = g_memory_output_stream_steal_as_bytes ((GMemoryOutputStream*)mo);
313 g_assert_cmpint (g_bytes_get_size (bytes), ==, strlen ("hello world!") + 1);
314 g_assert_cmpstr (g_bytes_get_data (bytes, &size), ==, "hello world!");
316 g_bytes_unref (bytes);
324 g_test_init (&argc, &argv, NULL);
325 g_test_bug_base ("http://bugzilla.gnome.org/");
327 g_test_add_func ("/memory-output-stream/truncate", test_truncate);
328 g_test_add_func ("/memory-output-stream/seek/fixed", test_seek_fixed);
329 g_test_add_func ("/memory-output-stream/seek/resizable", test_seek_resizable);
330 g_test_add_func ("/memory-output-stream/get-data-size", test_data_size);
331 g_test_add_func ("/memory-output-stream/properties", test_properties);
332 g_test_add_func ("/memory-output-stream/write-bytes", test_write_bytes);
333 g_test_add_func ("/memory-output-stream/steal_as_bytes", test_steal_as_bytes);