Merge branch '976-disable-assert-checks' into 'master'
[glib.git] / gio / tests / buffered-output-stream.c
blobfc740b742c5340ffc867d8a04bce8a2599777ffd
1 #include <gio/gio.h>
3 static void
4 test_write (void)
6 GOutputStream *base;
7 GOutputStream *out;
8 GError *error;
9 const gchar buffer[] = "abcdefghijklmnopqrstuvwxyz";
11 base = g_memory_output_stream_new (g_malloc0 (20), 20, NULL, g_free);
12 out = g_buffered_output_stream_new (base);
14 g_assert_cmpint (g_buffered_output_stream_get_buffer_size (G_BUFFERED_OUTPUT_STREAM (out)), ==, 4096);
15 g_assert (!g_buffered_output_stream_get_auto_grow (G_BUFFERED_OUTPUT_STREAM (out)));
16 g_object_set (out, "auto-grow", TRUE, NULL);
17 g_assert (g_buffered_output_stream_get_auto_grow (G_BUFFERED_OUTPUT_STREAM (out)));
18 g_object_set (out, "auto-grow", FALSE, NULL);
20 g_buffered_output_stream_set_buffer_size (G_BUFFERED_OUTPUT_STREAM (out), 16);
21 g_assert_cmpint (g_buffered_output_stream_get_buffer_size (G_BUFFERED_OUTPUT_STREAM (out)), ==, 16);
23 error = NULL;
24 g_assert_cmpint (g_output_stream_write (out, buffer, 10, NULL, &error), ==, 10);
25 g_assert_no_error (error);
27 g_assert_cmpint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (base)), ==, 0);
29 g_assert_cmpint (g_output_stream_write (out, buffer + 10, 10, NULL, &error), ==, 6);
30 g_assert_no_error (error);
32 g_assert_cmpint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (base)), ==, 0);
33 g_assert (g_output_stream_flush (out, NULL, &error));
34 g_assert_no_error (error);
35 g_assert_cmpint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (base)), ==, 16);
37 g_assert_cmpstr (g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (base)), ==, "abcdefghijklmnop");
39 g_object_unref (out);
40 g_object_unref (base);
43 static void
44 test_grow (void)
46 GOutputStream *base;
47 GOutputStream *out;
48 GError *error;
49 const gchar buffer[] = "abcdefghijklmnopqrstuvwxyz";
50 gint size;
51 gboolean grow;
53 base = g_memory_output_stream_new (g_malloc0 (30), 30, g_realloc, g_free);
54 out = g_buffered_output_stream_new_sized (base, 16);
56 g_buffered_output_stream_set_auto_grow (G_BUFFERED_OUTPUT_STREAM (out), TRUE);
58 g_object_get (out, "buffer-size", &size, "auto-grow", &grow, NULL);
59 g_assert_cmpint (size, ==, 16);
60 g_assert (grow);
62 g_assert (g_seekable_can_seek (G_SEEKABLE (out)));
64 error = NULL;
65 g_assert_cmpint (g_output_stream_write (out, buffer, 10, NULL, &error), ==, 10);
66 g_assert_no_error (error);
68 g_assert_cmpint (g_buffered_output_stream_get_buffer_size (G_BUFFERED_OUTPUT_STREAM (out)), ==, 16);
69 g_assert_cmpint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (base)), ==, 0);
71 g_assert_cmpint (g_output_stream_write (out, buffer + 10, 10, NULL, &error), ==, 10);
72 g_assert_no_error (error);
74 g_assert_cmpint (g_buffered_output_stream_get_buffer_size (G_BUFFERED_OUTPUT_STREAM (out)), >=, 20);
75 g_assert_cmpint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (base)), ==, 0);
77 g_assert (g_output_stream_flush (out, NULL, &error));
78 g_assert_no_error (error);
80 g_assert_cmpstr (g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (base)), ==, "abcdefghijklmnopqrst");
82 g_object_unref (out);
83 g_object_unref (base);
86 static void
87 test_close (void)
89 GOutputStream *base;
90 GOutputStream *out;
91 GError *error;
93 base = g_memory_output_stream_new (g_malloc0 (30), 30, g_realloc, g_free);
94 out = g_buffered_output_stream_new (base);
96 g_assert (g_filter_output_stream_get_close_base_stream (G_FILTER_OUTPUT_STREAM (out)));
98 error = NULL;
99 g_assert (g_output_stream_close (out, NULL, &error));
100 g_assert_no_error (error);
101 g_assert (g_output_stream_is_closed (base));
103 g_object_unref (out);
104 g_object_unref (base);
106 base = g_memory_output_stream_new (g_malloc0 (30), 30, g_realloc, g_free);
107 out = g_buffered_output_stream_new (base);
109 g_filter_output_stream_set_close_base_stream (G_FILTER_OUTPUT_STREAM (out), FALSE);
111 error = NULL;
112 g_assert (g_output_stream_close (out, NULL, &error));
113 g_assert_no_error (error);
114 g_assert (!g_output_stream_is_closed (base));
116 g_object_unref (out);
117 g_object_unref (base);
120 static void
121 test_seek (void)
123 GMemoryOutputStream *base;
124 GOutputStream *out;
125 GSeekable *seekable;
126 GError *error;
127 gsize bytes_written;
128 gboolean ret;
129 const gchar buffer[] = "abcdefghijklmnopqrstuvwxyz";
131 base = G_MEMORY_OUTPUT_STREAM (g_memory_output_stream_new (g_malloc0 (30), 30, NULL, g_free));
132 out = g_buffered_output_stream_new_sized (G_OUTPUT_STREAM (base), 8);
133 seekable = G_SEEKABLE (out);
134 error = NULL;
136 /* Write data */
137 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (out)), ==, 0);
138 ret = g_output_stream_write_all (out, buffer, 4, &bytes_written, NULL, &error);
139 g_assert_no_error (error);
140 g_assert_cmpint (bytes_written, ==, 4);
141 g_assert (ret);
142 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (out)), ==, 4);
143 g_assert_cmpint (g_memory_output_stream_get_data_size (base), ==, 0);
145 /* Forward relative seek */
146 ret = g_seekable_seek (seekable, 2, G_SEEK_CUR, NULL, &error);
147 g_assert_no_error (error);
148 g_assert (ret);
149 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (out)), ==, 6);
150 g_assert_cmpint ('a', ==, ((gchar *)g_memory_output_stream_get_data (base))[0]);
151 g_assert_cmpint ('b', ==, ((gchar *)g_memory_output_stream_get_data (base))[1]);
152 g_assert_cmpint ('c', ==, ((gchar *)g_memory_output_stream_get_data (base))[2]);
153 g_assert_cmpint ('d', ==, ((gchar *)g_memory_output_stream_get_data (base))[3]);
154 ret = g_output_stream_write_all (out, buffer, 2, &bytes_written, NULL, &error);
155 g_assert_no_error (error);
156 g_assert (ret);
157 g_assert_cmpint (bytes_written, ==, 2);
158 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (out)), ==, 8);
160 /* Backward relative seek */
161 ret = g_seekable_seek (seekable, -4, G_SEEK_CUR, NULL, &error);
162 g_assert_no_error (error);
163 g_assert (ret);
164 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (out)), ==, 4);
165 g_assert_cmpint ('a', ==, ((gchar *)g_memory_output_stream_get_data (base))[0]);
166 g_assert_cmpint ('b', ==, ((gchar *)g_memory_output_stream_get_data (base))[1]);
167 g_assert_cmpint ('c', ==, ((gchar *)g_memory_output_stream_get_data (base))[2]);
168 g_assert_cmpint ('d', ==, ((gchar *)g_memory_output_stream_get_data (base))[3]);
169 g_assert_cmpint ('a', ==, ((gchar *)g_memory_output_stream_get_data (base))[6]);
170 g_assert_cmpint ('b', ==, ((gchar *)g_memory_output_stream_get_data (base))[7]);
171 ret = g_output_stream_write_all (out, buffer, 2, &bytes_written, NULL, &error);
172 g_assert_no_error (error);
173 g_assert (ret);
174 g_assert_cmpint (bytes_written, ==, 2);
175 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (out)), ==, 6);
177 /* From start */
178 ret = g_seekable_seek (seekable, 2, G_SEEK_SET, NULL, &error);
179 g_assert_no_error (error);
180 g_assert (ret);
181 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (out)), ==, 2);
182 g_assert_cmpint ('a', ==, ((gchar *)g_memory_output_stream_get_data (base))[0]);
183 g_assert_cmpint ('b', ==, ((gchar *)g_memory_output_stream_get_data (base))[1]);
184 g_assert_cmpint ('c', ==, ((gchar *)g_memory_output_stream_get_data (base))[2]);
185 g_assert_cmpint ('d', ==, ((gchar *)g_memory_output_stream_get_data (base))[3]);
186 g_assert_cmpint ('a', ==, ((gchar *)g_memory_output_stream_get_data (base))[4]);
187 g_assert_cmpint ('b', ==, ((gchar *)g_memory_output_stream_get_data (base))[5]);
188 g_assert_cmpint ('a', ==, ((gchar *)g_memory_output_stream_get_data (base))[6]);
189 g_assert_cmpint ('b', ==, ((gchar *)g_memory_output_stream_get_data (base))[7]);
190 ret = g_output_stream_write_all (out, buffer, 2, &bytes_written, NULL, &error);
191 g_assert_no_error (error);
192 g_assert (ret);
193 g_assert_cmpint (bytes_written, ==, 2);
194 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (out)), ==, 4);
196 /* From end */
197 ret = g_seekable_seek (seekable, 6 - 30, G_SEEK_END, NULL, &error);
198 g_assert_no_error (error);
199 g_assert (ret);
200 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (out)), ==, 6);
201 g_assert_cmpint ('a', ==, ((gchar *)g_memory_output_stream_get_data (base))[0]);
202 g_assert_cmpint ('b', ==, ((gchar *)g_memory_output_stream_get_data (base))[1]);
203 g_assert_cmpint ('a', ==, ((gchar *)g_memory_output_stream_get_data (base))[2]);
204 g_assert_cmpint ('b', ==, ((gchar *)g_memory_output_stream_get_data (base))[3]);
205 g_assert_cmpint ('a', ==, ((gchar *)g_memory_output_stream_get_data (base))[4]);
206 g_assert_cmpint ('b', ==, ((gchar *)g_memory_output_stream_get_data (base))[5]);
207 g_assert_cmpint ('a', ==, ((gchar *)g_memory_output_stream_get_data (base))[6]);
208 g_assert_cmpint ('b', ==, ((gchar *)g_memory_output_stream_get_data (base))[7]);
209 ret = g_output_stream_write_all (out, buffer + 2, 2, &bytes_written, NULL, &error);
210 g_assert_no_error (error);
211 g_assert (ret);
212 g_assert_cmpint (bytes_written, ==, 2);
213 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (out)), ==, 8);
215 /* Check flush */
216 ret = g_output_stream_flush (out, NULL, &error);
217 g_assert_no_error (error);
218 g_assert (ret);
219 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (out)), ==, 8);
220 g_assert_cmpint ('a', ==, ((gchar *)g_memory_output_stream_get_data (base))[0]);
221 g_assert_cmpint ('b', ==, ((gchar *)g_memory_output_stream_get_data (base))[1]);
222 g_assert_cmpint ('a', ==, ((gchar *)g_memory_output_stream_get_data (base))[2]);
223 g_assert_cmpint ('b', ==, ((gchar *)g_memory_output_stream_get_data (base))[3]);
224 g_assert_cmpint ('a', ==, ((gchar *)g_memory_output_stream_get_data (base))[4]);
225 g_assert_cmpint ('b', ==, ((gchar *)g_memory_output_stream_get_data (base))[5]);
226 g_assert_cmpint ('c', ==, ((gchar *)g_memory_output_stream_get_data (base))[6]);
227 g_assert_cmpint ('d', ==, ((gchar *)g_memory_output_stream_get_data (base))[7]);
229 g_object_unref (out);
230 g_object_unref (base);
233 static void
234 test_truncate(void)
236 GMemoryOutputStream *base_stream;
237 GOutputStream *stream;
238 GSeekable *seekable;
239 GError *error;
240 gsize bytes_written;
241 guchar *stream_data;
242 gsize len;
243 gboolean res;
245 len = 8;
247 /* Create objects */
248 stream_data = g_malloc0 (len);
249 base_stream = G_MEMORY_OUTPUT_STREAM (g_memory_output_stream_new (stream_data, len, g_realloc, g_free));
250 stream = g_buffered_output_stream_new_sized (G_OUTPUT_STREAM (base_stream), 8);
251 seekable = G_SEEKABLE (stream);
253 g_assert (g_seekable_can_truncate (seekable));
255 /* Write */
256 g_assert_cmpint (g_memory_output_stream_get_size (base_stream), ==, len);
257 g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 0);
259 error = NULL;
260 res = g_output_stream_write_all (stream, "ab", 2, &bytes_written, NULL, &error);
261 g_assert_no_error (error);
262 g_assert (res);
263 res = g_output_stream_write_all (stream, "cd", 2, &bytes_written, NULL, &error);
264 g_assert_no_error (error);
265 g_assert (res);
267 res = g_output_stream_flush (stream, NULL, &error);
268 g_assert_no_error (error);
269 g_assert (res);
271 g_assert_cmpint (g_memory_output_stream_get_size (base_stream), ==, len);
272 g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 4);
273 stream_data = g_memory_output_stream_get_data (base_stream);
274 g_assert_cmpint (stream_data[0], ==, 'a');
275 g_assert_cmpint (stream_data[1], ==, 'b');
276 g_assert_cmpint (stream_data[2], ==, 'c');
277 g_assert_cmpint (stream_data[3], ==, 'd');
279 /* Truncate at position */
280 res = g_seekable_truncate (seekable, 4, NULL, &error);
281 g_assert_no_error (error);
282 g_assert (res);
283 g_assert_cmpint (g_memory_output_stream_get_size (base_stream), ==, 4);
284 g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 4);
285 stream_data = g_memory_output_stream_get_data (base_stream);
286 g_assert_cmpint (stream_data[0], ==, 'a');
287 g_assert_cmpint (stream_data[1], ==, 'b');
288 g_assert_cmpint (stream_data[2], ==, 'c');
289 g_assert_cmpint (stream_data[3], ==, 'd');
291 /* Truncate beyond position */
292 res = g_seekable_truncate (seekable, 6, NULL, &error);
293 g_assert_no_error (error);
294 g_assert (res);
295 g_assert_cmpint (g_memory_output_stream_get_size (base_stream), ==, 6);
296 g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 6);
297 stream_data = g_memory_output_stream_get_data (base_stream);
298 g_assert_cmpint (stream_data[0], ==, 'a');
299 g_assert_cmpint (stream_data[1], ==, 'b');
300 g_assert_cmpint (stream_data[2], ==, 'c');
301 g_assert_cmpint (stream_data[3], ==, 'd');
303 /* Truncate before position */
304 res = g_seekable_truncate (seekable, 2, NULL, &error);
305 g_assert_no_error (error);
306 g_assert (res);
307 g_assert_cmpint (g_memory_output_stream_get_size (base_stream), ==, 2);
308 g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 2);
309 stream_data = g_memory_output_stream_get_data (base_stream);
310 g_assert_cmpint (stream_data[0], ==, 'a');
311 g_assert_cmpint (stream_data[1], ==, 'b');
313 g_object_unref (stream);
314 g_object_unref (base_stream);
318 main (int argc, char *argv[])
320 g_test_init (&argc, &argv, NULL);
322 g_test_add_func ("/buffered-output-stream/write", test_write);
323 g_test_add_func ("/buffered-output-stream/grow", test_grow);
324 g_test_add_func ("/buffered-output-stream/seek", test_seek);
325 g_test_add_func ("/buffered-output-stream/truncate", test_truncate);
326 g_test_add_func ("/filter-output-stream/close", test_close);
328 return g_test_run ();