gio: Update GMemoryOutputStream length after truncate
[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_test_bug ("720080");
54
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);
57
58   g_object_unref (o);
59   g_object_unref (mo);
60 }
61
62 static void
63 test_seek_fixed (void)
64 {
65   GOutputStream *mo;
66   GError *error;
67
68   mo = g_memory_output_stream_new (g_new (gchar, 100), 100, NULL, g_free);
69
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);
73
74   error = NULL;
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);
79
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);
83
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);
87
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);
92
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);
97
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);
101
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);
105
106   g_object_unref (mo);
107 }
108
109 static void
110 test_seek_resizable_stream (GOutputStream *mo)
111 {
112   GError *error;
113
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);
117
118   error = NULL;
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);
122
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);
126
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);
130
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);
134
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);
138
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);
142
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);
148 }
149
150 static void
151 test_seek_resizable (void)
152 {
153   GOutputStream *mo;
154   gint i;
155
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.
160    */
161   for (i = 0; i < 1024; i++)
162     {
163       mo = g_memory_output_stream_new (g_malloc (i), i, g_realloc, g_free);
164
165       test_seek_resizable_stream (mo);
166
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);
170
171       g_object_unref (mo);
172     }
173 }
174
175 static void
176 test_data_size (void)
177 {
178   GOutputStream *mo;
179   GDataOutputStream *o;
180   int pos;
181
182   g_test_bug ("540459");
183
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);
189
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);
193
194   g_test_bug ("540461");
195   
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);
199   
200   pos = g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo));
201   g_assert_cmpint (pos, ==, 1);
202
203   g_assert_cmpint (g_memory_output_stream_get_size (G_MEMORY_OUTPUT_STREAM (mo)), ==, 16);
204
205   g_object_unref (o);
206   g_object_unref (mo);
207 }
208
209 static void
210 test_properties (void)
211 {
212   GOutputStream *mo;
213   GDataOutputStream *o;
214   int i;
215   GError *error = NULL;
216   gsize data_size_fun;
217   gsize data_size_prop = 0;
218   gpointer data_fun;
219   gpointer data_prop;
220   gpointer func;
221
222   g_test_bug ("605733");
223
224   mo = (GOutputStream*) g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
225                                       "realloc-function", g_realloc,
226                                       "destroy-function", g_free,
227                                       NULL);
228   o = g_data_output_stream_new (mo);
229
230   for (i = 0; i < 1000; i++)
231     {
232       g_data_output_stream_put_byte (o, 1, NULL, &error);
233       g_assert_no_error (error);
234     }
235
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);
239
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));
243
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);
248
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);
252
253   g_object_unref (o);
254   g_object_unref (mo);
255 }
256
257 static void
258 test_write_bytes (void)
259 {
260   GOutputStream *mo;
261   GBytes *bytes, *bytes2;
262   GError *error = NULL;
263
264   mo = (GOutputStream*) g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
265                                       "realloc-function", g_realloc,
266                                       "destroy-function", g_free,
267                                       NULL);
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);
271
272   g_output_stream_close (mo, NULL, &error);
273   g_assert_no_error (error);
274
275   bytes2 = g_memory_output_stream_steal_as_bytes (G_MEMORY_OUTPUT_STREAM (mo));
276   g_object_unref (mo);
277   g_assert (g_bytes_equal (bytes, bytes2));
278
279   g_bytes_unref (bytes);
280   g_bytes_unref (bytes2);
281 }
282
283 static void
284 test_steal_as_bytes (void)
285 {
286   GOutputStream *mo;
287   GDataOutputStream *o;
288   GError *error = NULL;
289   GBytes *bytes;
290   gsize size;
291
292   mo = (GOutputStream*) g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
293                                       "realloc-function", g_realloc,
294                                       "destroy-function", g_free,
295                                       NULL);
296   o = g_data_output_stream_new (mo);
297
298   g_data_output_stream_put_string (o, "hello ", NULL, &error);
299   g_assert_no_error (error);
300
301   g_data_output_stream_put_string (o, "world!", NULL, &error);
302   g_assert_no_error (error);
303
304   g_data_output_stream_put_byte (o, '\0', NULL, &error);
305   g_assert_no_error (error);
306
307   g_output_stream_close ((GOutputStream*) o, NULL, &error);
308   g_assert_no_error (error);
309
310   bytes = g_memory_output_stream_steal_as_bytes ((GMemoryOutputStream*)mo);
311   g_object_unref (mo);
312
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!");
315
316   g_bytes_unref (bytes);
317   g_object_unref (o);
318 }
319
320 int
321 main (int   argc,
322       char *argv[])
323 {
324   g_test_init (&argc, &argv, NULL);
325   g_test_bug_base ("http://bugzilla.gnome.org/");
326
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);
334
335   return g_test_run();
336 }