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);
58 test_seek_fixed (void)
63 mo = g_memory_output_stream_new (g_new (gchar, 100), 100, NULL, g_free);
65 g_assert (G_IS_SEEKABLE (mo));
66 g_assert (g_seekable_can_seek (G_SEEKABLE (mo)));
67 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 0);
70 g_assert (!g_seekable_seek (G_SEEKABLE (mo), 222, G_SEEK_CUR, NULL, &error));
71 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
72 g_clear_error (&error);
73 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 0);
75 g_assert (g_seekable_seek (G_SEEKABLE (mo), 26, G_SEEK_SET, NULL, &error));
76 g_assert_no_error (error);
77 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 26);
79 g_assert (g_seekable_seek (G_SEEKABLE (mo), 20, G_SEEK_CUR, NULL, &error));
80 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 46);
81 g_assert_no_error (error);
83 g_assert (!g_seekable_seek (G_SEEKABLE (mo), 200, G_SEEK_CUR, NULL, &error));
84 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
85 g_clear_error (&error);
86 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 46);
88 g_assert (!g_seekable_seek (G_SEEKABLE (mo), 1, G_SEEK_END, 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), 0, G_SEEK_END, NULL, &error));
94 g_assert_no_error (error);
95 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 100);
97 g_assert (g_seekable_seek (G_SEEKABLE (mo), -1, G_SEEK_END, NULL, &error));
98 g_assert_no_error (error);
99 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 99);
105 test_seek_resizable_stream (GOutputStream *mo)
109 g_assert (G_IS_SEEKABLE (mo));
110 g_assert (g_seekable_can_seek (G_SEEKABLE (mo)));
111 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 0);
114 g_assert (g_seekable_seek (G_SEEKABLE (mo), 222, G_SEEK_CUR, NULL, &error));
115 g_assert_no_error (error);
116 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 222);
118 g_assert (g_seekable_seek (G_SEEKABLE (mo), 26, G_SEEK_SET, NULL, &error));
119 g_assert_no_error (error);
120 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 26);
122 g_assert (g_seekable_seek (G_SEEKABLE (mo), 20, G_SEEK_CUR, NULL, &error));
123 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 46);
124 g_assert_no_error (error);
126 g_assert (g_seekable_seek (G_SEEKABLE (mo), 200, G_SEEK_CUR, NULL, &error));
127 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 246);
128 g_assert_no_error (error);
130 g_assert (g_seekable_seek (G_SEEKABLE (mo), 1, G_SEEK_END, NULL, &error));
131 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 1);
132 g_assert_no_error (error);
134 g_assert (g_seekable_seek (G_SEEKABLE (mo), 0, G_SEEK_END, NULL, &error));
135 g_assert_no_error (error);
136 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 0);
138 /* The 'end' is still zero, so this should fail */
139 g_assert (!g_seekable_seek (G_SEEKABLE (mo), -1, G_SEEK_END, NULL, &error));
140 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
141 g_clear_error (&error);
142 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 0);
146 test_seek_resizable (void)
151 /* For resizable streams, the initially allocated size is purely an
152 * implementation detail. We should not be able to tell the
153 * difference based on the seek API, so make a bunch of streams with
154 * different sizes and subject them to the same test.
156 for (i = 0; i < 1024; i++)
158 mo = g_memory_output_stream_new (g_malloc (i), i, g_realloc, g_free);
160 test_seek_resizable_stream (mo);
162 g_assert_cmpint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo)), ==, 0);
163 /* No writes = no resizes */
164 g_assert_cmpint (g_memory_output_stream_get_size (G_MEMORY_OUTPUT_STREAM (mo)), ==, i);
171 test_data_size (void)
174 GDataOutputStream *o;
177 g_test_bug ("540459");
179 mo = g_memory_output_stream_new_resizable ();
180 o = g_data_output_stream_new (mo);
181 g_data_output_stream_put_byte (o, 1, NULL, NULL);
182 pos = g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo));
183 g_assert_cmpint (pos, ==, 1);
185 g_seekable_seek (G_SEEKABLE (mo), 0, G_SEEK_CUR, NULL, NULL);
186 pos = g_seekable_tell (G_SEEKABLE (mo));
187 g_assert_cmpint (pos, ==, 1);
189 g_test_bug ("540461");
191 g_seekable_seek (G_SEEKABLE (mo), 0, G_SEEK_SET, NULL, NULL);
192 pos = g_seekable_tell (G_SEEKABLE (mo));
193 g_assert_cmpint (pos, ==, 0);
195 pos = g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo));
196 g_assert_cmpint (pos, ==, 1);
198 g_assert_cmpint (g_memory_output_stream_get_size (G_MEMORY_OUTPUT_STREAM (mo)), ==, 16);
205 test_properties (void)
208 GDataOutputStream *o;
210 GError *error = NULL;
212 gsize data_size_prop = 0;
217 g_test_bug ("605733");
219 mo = (GOutputStream*) g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
220 "realloc-function", g_realloc,
221 "destroy-function", g_free,
223 o = g_data_output_stream_new (mo);
225 for (i = 0; i < 1000; i++)
227 g_data_output_stream_put_byte (o, 1, NULL, &error);
228 g_assert_no_error (error);
231 data_size_fun = g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo));
232 g_object_get (mo, "data-size", &data_size_prop, NULL);
233 g_assert_cmpint (data_size_fun, ==, data_size_prop);
235 data_fun = g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (mo));
236 g_object_get (mo, "data", &data_prop, NULL);
237 g_assert_cmphex (GPOINTER_TO_SIZE (data_fun), ==, GPOINTER_TO_SIZE (data_prop));
239 g_object_get (mo, "realloc-function", &func, NULL);
240 g_assert (func == g_realloc);
241 g_object_get (mo, "destroy-function", &func, NULL);
242 g_assert (func == g_free);
244 data_size_fun = g_memory_output_stream_get_size (G_MEMORY_OUTPUT_STREAM (mo));
245 g_object_get (mo, "size", &data_size_prop, NULL);
246 g_assert_cmpint (data_size_fun, ==, data_size_prop);
253 test_write_bytes (void)
256 GBytes *bytes, *bytes2;
257 GError *error = NULL;
259 mo = (GOutputStream*) g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
260 "realloc-function", g_realloc,
261 "destroy-function", g_free,
263 bytes = g_bytes_new_static ("hello world!", strlen ("hello world!") + 1);
264 g_output_stream_write_bytes (mo, bytes, NULL, &error);
265 g_assert_no_error (error);
267 g_output_stream_close (mo, NULL, &error);
268 g_assert_no_error (error);
270 bytes2 = g_memory_output_stream_steal_as_bytes (G_MEMORY_OUTPUT_STREAM (mo));
272 g_assert (g_bytes_equal (bytes, bytes2));
274 g_bytes_unref (bytes);
275 g_bytes_unref (bytes2);
279 test_steal_as_bytes (void)
282 GDataOutputStream *o;
283 GError *error = NULL;
287 mo = (GOutputStream*) g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
288 "realloc-function", g_realloc,
289 "destroy-function", g_free,
291 o = g_data_output_stream_new (mo);
293 g_data_output_stream_put_string (o, "hello ", NULL, &error);
294 g_assert_no_error (error);
296 g_data_output_stream_put_string (o, "world!", NULL, &error);
297 g_assert_no_error (error);
299 g_data_output_stream_put_byte (o, '\0', NULL, &error);
300 g_assert_no_error (error);
302 g_output_stream_close ((GOutputStream*) o, NULL, &error);
303 g_assert_no_error (error);
305 bytes = g_memory_output_stream_steal_as_bytes ((GMemoryOutputStream*)mo);
308 g_assert_cmpint (g_bytes_get_size (bytes), ==, strlen ("hello world!") + 1);
309 g_assert_cmpstr (g_bytes_get_data (bytes, &size), ==, "hello world!");
311 g_bytes_unref (bytes);
319 g_test_init (&argc, &argv, NULL);
320 g_test_bug_base ("http://bugzilla.gnome.org/");
322 g_test_add_func ("/memory-output-stream/truncate", test_truncate);
323 g_test_add_func ("/memory-output-stream/seek/fixed", test_seek_fixed);
324 g_test_add_func ("/memory-output-stream/seek/resizable", test_seek_resizable);
325 g_test_add_func ("/memory-output-stream/get-data-size", test_data_size);
326 g_test_add_func ("/memory-output-stream/properties", test_properties);
327 g_test_add_func ("/memory-output-stream/write-bytes", test_write_bytes);
328 g_test_add_func ("/memory-output-stream/steal_as_bytes", test_steal_as_bytes);