GMemoryOutputStream: Add new _resizable() constructor usable from bindings
[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 (void)
59 {
60   GOutputStream *mo;
61   GError *error;
62
63   mo = g_memory_output_stream_new (g_new (gchar, 100), 100, g_realloc, g_free);
64
65   g_assert (G_IS_SEEKABLE (mo));
66   g_assert (g_seekable_can_seek (G_SEEKABLE (mo)));
67
68   error = NULL;
69   g_assert (g_seekable_seek (G_SEEKABLE (mo), 26, G_SEEK_SET, NULL, &error));
70   g_assert_no_error (error);
71   g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 26);
72
73   g_assert (!g_seekable_seek (G_SEEKABLE (mo), 200, G_SEEK_CUR, NULL, &error));
74   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
75   g_error_free (error);
76
77   g_object_unref (mo);
78 }
79
80 static void
81 test_data_size (void)
82 {
83   GOutputStream *mo;
84   GDataOutputStream *o;
85   int pos;
86
87   g_test_bug ("540459");
88
89   mo = g_memory_output_stream_new_resizable ();
90   o = g_data_output_stream_new (mo);
91   g_data_output_stream_put_byte (o, 1, NULL, NULL);
92   pos = g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo));
93   g_assert_cmpint (pos, ==, 1);
94
95   g_seekable_seek (G_SEEKABLE (mo), 0, G_SEEK_CUR, NULL, NULL);
96   pos = g_seekable_tell (G_SEEKABLE (mo));
97   g_assert_cmpint (pos, ==, 1);
98
99   g_test_bug ("540461");
100   
101   g_seekable_seek (G_SEEKABLE (mo), 0, G_SEEK_SET, NULL, NULL);
102   pos = g_seekable_tell (G_SEEKABLE (mo));
103   g_assert_cmpint (pos, ==, 0);
104   
105   pos = g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo));
106   g_assert_cmpint (pos, ==, 1);
107
108   g_assert_cmpint (g_memory_output_stream_get_size (G_MEMORY_OUTPUT_STREAM (mo)), ==, 16);
109
110   g_object_unref (o);
111   g_object_unref (mo);
112 }
113
114 static void
115 test_properties (void)
116 {
117   GOutputStream *mo;
118   GDataOutputStream *o;
119   int i;
120   GError *error = NULL;
121   gsize data_size_fun;
122   gsize data_size_prop;
123   gpointer data_fun;
124   gpointer data_prop;
125
126   g_test_bug ("605733");
127
128   mo = (GOutputStream*) g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
129                                       "realloc-function", g_realloc,
130                                       "destroy-function", g_free,
131                                       NULL);
132   o = g_data_output_stream_new (mo);
133
134   for (i = 0; i < 1000; i++)
135     {
136       g_data_output_stream_put_byte (o, 1, NULL, &error);
137       g_assert_no_error (error);
138     }
139
140   data_size_fun = g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo));
141   g_object_get (mo, "data-size", &data_size_prop, NULL);
142   g_assert_cmpint (data_size_fun, ==, data_size_prop);
143
144   data_fun = g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (mo));
145   g_object_get (mo, "data", &data_prop, NULL);
146   g_assert_cmphex (GPOINTER_TO_SIZE (data_fun), ==, GPOINTER_TO_SIZE (data_prop));
147
148   g_object_unref (o);
149   g_object_unref (mo);
150 }
151
152 static void
153 test_write_bytes (void)
154 {
155   GOutputStream *mo;
156   GBytes *bytes, *bytes2;
157   GError *error = NULL;
158
159   mo = (GOutputStream*) g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
160                                       "realloc-function", g_realloc,
161                                       "destroy-function", g_free,
162                                       NULL);
163   bytes = g_bytes_new_static ("hello world!", strlen ("hello world!") + 1);
164   g_output_stream_write_bytes (mo, bytes, NULL, &error);
165   g_assert_no_error (error);
166
167   g_output_stream_close (mo, NULL, &error);
168   g_assert_no_error (error);
169
170   bytes2 = g_memory_output_stream_steal_as_bytes (G_MEMORY_OUTPUT_STREAM (mo));
171   g_object_unref (mo);
172   g_assert (g_bytes_equal (bytes, bytes2));
173
174   g_bytes_unref (bytes);
175   g_bytes_unref (bytes2);
176 }
177
178 static void
179 test_steal_as_bytes (void)
180 {
181   GOutputStream *mo;
182   GDataOutputStream *o;
183   GError *error = NULL;
184   GBytes *bytes;
185   gsize size;
186
187   mo = (GOutputStream*) g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
188                                       "realloc-function", g_realloc,
189                                       "destroy-function", g_free,
190                                       NULL);
191   o = g_data_output_stream_new (mo);
192
193   g_data_output_stream_put_string (o, "hello ", NULL, &error);
194   g_assert_no_error (error);
195
196   g_data_output_stream_put_string (o, "world!", NULL, &error);
197   g_assert_no_error (error);
198
199   g_data_output_stream_put_byte (o, '\0', NULL, &error);
200   g_assert_no_error (error);
201
202   g_output_stream_close ((GOutputStream*) o, NULL, &error);
203   g_assert_no_error (error);
204
205   bytes = g_memory_output_stream_steal_as_bytes ((GMemoryOutputStream*)mo);
206   g_object_unref (mo);
207
208   g_assert_cmpint (g_bytes_get_size (bytes), ==, strlen ("hello world!") + 1);
209   g_assert_cmpstr (g_bytes_get_data (bytes, &size), ==, "hello world!");
210
211   g_bytes_unref (bytes);
212   g_object_unref (o);
213 }
214
215 int
216 main (int   argc,
217       char *argv[])
218 {
219   g_test_init (&argc, &argv, NULL);
220   g_test_bug_base ("http://bugzilla.gnome.org/");
221
222   g_test_add_func ("/memory-output-stream/truncate", test_truncate);
223   g_test_add_func ("/memory-output-stream/seek", test_seek);
224   g_test_add_func ("/memory-output-stream/get-data-size", test_data_size);
225   g_test_add_func ("/memory-output-stream/properties", test_properties);
226   g_test_add_func ("/memory-output-stream/write-bytes", test_write_bytes);
227   g_test_add_func ("/memory-output-stream/steal_as_bytes", test_steal_as_bytes);
228
229   return g_test_run();
230 }