Install all test data
[platform/upstream/glib.git] / gio / tests / memory-output-stream.c
1 /* GLib testing framework examples and tests
2  * Copyright (C) 2008 Red Hat, Inc.
3  * Author: Matthias Clasen
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 #include <glib/glib.h>
23 #include <gio/gio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 static void
28 test_truncate (void)
29 {
30   GOutputStream *mo;
31   GDataOutputStream *o;
32   int i;
33   GError *error = NULL;
34
35   g_test_bug ("540423");
36
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++)
41     {
42       g_data_output_stream_put_byte (o, 1, NULL, &error);
43       g_assert_no_error (error);
44     }
45   g_seekable_truncate (G_SEEKABLE (mo), 0, NULL, &error);
46   g_assert_no_error (error);
47   for (i = 0; i < 2000; i++)
48     {
49       g_data_output_stream_put_byte (o, 1, NULL, &error);
50       g_assert_no_error (error);
51     }
52
53   g_object_unref (o);
54   g_object_unref (mo);
55 }
56
57 static void
58 test_seek_fixed (void)
59 {
60   GOutputStream *mo;
61   GError *error;
62
63   mo = g_memory_output_stream_new (g_new (gchar, 100), 100, NULL, g_free);
64
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);
68
69   error = NULL;
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);
74
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);
78
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);
82
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);
87
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);
92
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);
96
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);
100
101   g_object_unref (mo);
102 }
103
104 static void
105 test_seek_resizable_stream (GOutputStream *mo)
106 {
107   GError *error;
108
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);
112
113   error = NULL;
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);
117
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);
121
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);
125
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);
129
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);
133
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);
137
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);
143 }
144
145 static void
146 test_seek_resizable (void)
147 {
148   GOutputStream *mo;
149   gint i;
150
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.
155    */
156   for (i = 0; i < 1024; i++)
157     {
158       mo = g_memory_output_stream_new (g_malloc (i), i, g_realloc, g_free);
159
160       test_seek_resizable_stream (mo);
161
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);
165
166       g_object_unref (mo);
167     }
168 }
169
170 static void
171 test_data_size (void)
172 {
173   GOutputStream *mo;
174   GDataOutputStream *o;
175   int pos;
176
177   g_test_bug ("540459");
178
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);
184
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);
188
189   g_test_bug ("540461");
190   
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);
194   
195   pos = g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo));
196   g_assert_cmpint (pos, ==, 1);
197
198   g_assert_cmpint (g_memory_output_stream_get_size (G_MEMORY_OUTPUT_STREAM (mo)), ==, 16);
199
200   g_object_unref (o);
201   g_object_unref (mo);
202 }
203
204 static void
205 test_properties (void)
206 {
207   GOutputStream *mo;
208   GDataOutputStream *o;
209   int i;
210   GError *error = NULL;
211   gsize data_size_fun;
212   gsize data_size_prop = 0;
213   gpointer data_fun;
214   gpointer data_prop;
215   gpointer func;
216
217   g_test_bug ("605733");
218
219   mo = (GOutputStream*) g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
220                                       "realloc-function", g_realloc,
221                                       "destroy-function", g_free,
222                                       NULL);
223   o = g_data_output_stream_new (mo);
224
225   for (i = 0; i < 1000; i++)
226     {
227       g_data_output_stream_put_byte (o, 1, NULL, &error);
228       g_assert_no_error (error);
229     }
230
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);
234
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));
238
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);
243
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);
247
248   g_object_unref (o);
249   g_object_unref (mo);
250 }
251
252 static void
253 test_write_bytes (void)
254 {
255   GOutputStream *mo;
256   GBytes *bytes, *bytes2;
257   GError *error = NULL;
258
259   mo = (GOutputStream*) g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
260                                       "realloc-function", g_realloc,
261                                       "destroy-function", g_free,
262                                       NULL);
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);
266
267   g_output_stream_close (mo, NULL, &error);
268   g_assert_no_error (error);
269
270   bytes2 = g_memory_output_stream_steal_as_bytes (G_MEMORY_OUTPUT_STREAM (mo));
271   g_object_unref (mo);
272   g_assert (g_bytes_equal (bytes, bytes2));
273
274   g_bytes_unref (bytes);
275   g_bytes_unref (bytes2);
276 }
277
278 static void
279 test_steal_as_bytes (void)
280 {
281   GOutputStream *mo;
282   GDataOutputStream *o;
283   GError *error = NULL;
284   GBytes *bytes;
285   gsize size;
286
287   mo = (GOutputStream*) g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
288                                       "realloc-function", g_realloc,
289                                       "destroy-function", g_free,
290                                       NULL);
291   o = g_data_output_stream_new (mo);
292
293   g_data_output_stream_put_string (o, "hello ", NULL, &error);
294   g_assert_no_error (error);
295
296   g_data_output_stream_put_string (o, "world!", NULL, &error);
297   g_assert_no_error (error);
298
299   g_data_output_stream_put_byte (o, '\0', NULL, &error);
300   g_assert_no_error (error);
301
302   g_output_stream_close ((GOutputStream*) o, NULL, &error);
303   g_assert_no_error (error);
304
305   bytes = g_memory_output_stream_steal_as_bytes ((GMemoryOutputStream*)mo);
306   g_object_unref (mo);
307
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!");
310
311   g_bytes_unref (bytes);
312   g_object_unref (o);
313 }
314
315 int
316 main (int   argc,
317       char *argv[])
318 {
319   g_test_init (&argc, &argv, NULL);
320   g_test_bug_base ("http://bugzilla.gnome.org/");
321
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);
329
330   return g_test_run();
331 }