9 const gchar buffer[] = "abcdefghijklmnopqrstuvwxyz";
11 base = g_memory_output_stream_new (g_malloc0 (20), 20, g_realloc, 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_buffered_output_stream_set_buffer_size (G_BUFFERED_OUTPUT_STREAM (out), 16);
17 g_assert_cmpint (g_buffered_output_stream_get_buffer_size (G_BUFFERED_OUTPUT_STREAM (out)), ==, 16);
20 g_assert_cmpint (g_output_stream_write (out, buffer, 10, NULL, &error), ==, 10);
21 g_assert_no_error (error);
23 g_assert_cmpint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (base)), ==, 0);
25 g_assert_cmpint (g_output_stream_write (out, buffer + 10, 10, NULL, &error), ==, 6);
26 g_assert_no_error (error);
28 g_assert_cmpint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (base)), ==, 0);
29 g_assert (g_output_stream_flush (out, NULL, &error));
30 g_assert_no_error (error);
31 g_assert_cmpint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (base)), ==, 16);
33 g_assert_cmpstr (g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (base)), ==, "abcdefghijklmnop");
36 g_object_unref (base);
45 const gchar buffer[] = "abcdefghijklmnopqrstuvwxyz";
49 base = g_memory_output_stream_new (g_malloc0 (30), 30, g_realloc, g_free);
50 out = g_buffered_output_stream_new_sized (base, 16);
52 g_buffered_output_stream_set_auto_grow (G_BUFFERED_OUTPUT_STREAM (out), TRUE);
54 g_object_get (out, "buffer-size", &size, "auto-grow", &grow, NULL);
55 g_assert_cmpint (size, ==, 16);
59 g_assert_cmpint (g_output_stream_write (out, buffer, 10, NULL, &error), ==, 10);
60 g_assert_no_error (error);
62 g_assert_cmpint (g_buffered_output_stream_get_buffer_size (G_BUFFERED_OUTPUT_STREAM (out)), ==, 16);
63 g_assert_cmpint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (base)), ==, 0);
65 g_assert_cmpint (g_output_stream_write (out, buffer + 10, 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)), >=, 20);
69 g_assert_cmpint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (base)), ==, 0);
71 g_assert (g_output_stream_flush (out, NULL, &error));
72 g_assert_no_error (error);
74 g_assert_cmpstr (g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (base)), ==, "abcdefghijklmnopqrst");
77 g_object_unref (base);
87 base = g_memory_output_stream_new (g_malloc0 (30), 30, g_realloc, g_free);
88 out = g_buffered_output_stream_new (base);
90 g_assert (g_filter_output_stream_get_close_base_stream (G_FILTER_OUTPUT_STREAM (out)));
93 g_assert (g_output_stream_close (out, NULL, &error));
94 g_assert_no_error (error);
95 g_assert (g_output_stream_is_closed (base));
98 g_object_unref (base);
100 base = g_memory_output_stream_new (g_malloc0 (30), 30, g_realloc, g_free);
101 out = g_buffered_output_stream_new (base);
103 g_filter_output_stream_set_close_base_stream (G_FILTER_OUTPUT_STREAM (out), FALSE);
106 g_assert (g_output_stream_close (out, NULL, &error));
107 g_assert_no_error (error);
108 g_assert (!g_output_stream_is_closed (base));
110 g_object_unref (out);
111 g_object_unref (base);
117 GMemoryOutputStream *base;
123 const gchar buffer[] = "abcdefghijklmnopqrstuvwxyz";
125 base = G_MEMORY_OUTPUT_STREAM (g_memory_output_stream_new (g_malloc0 (30), 30, g_realloc, g_free));
126 out = g_buffered_output_stream_new_sized (G_OUTPUT_STREAM (base), 8);
127 seekable = G_SEEKABLE (out);
131 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (out)), ==, 0);
132 ret = g_output_stream_write_all (out, buffer, 4, &bytes_written, NULL, &error);
133 g_assert_no_error (error);
134 g_assert_cmpint (bytes_written, ==, 4);
136 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (out)), ==, 4);
137 g_assert_cmpint (g_memory_output_stream_get_data_size (base), ==, 0);
139 /* Forward relative seek */
140 ret = g_seekable_seek (seekable, 2, G_SEEK_CUR, NULL, &error);
141 g_assert_no_error (error);
143 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (out)), ==, 6);
144 g_assert_cmpint ('a', ==, ((gchar *)g_memory_output_stream_get_data (base))[0]);
145 g_assert_cmpint ('b', ==, ((gchar *)g_memory_output_stream_get_data (base))[1]);
146 g_assert_cmpint ('c', ==, ((gchar *)g_memory_output_stream_get_data (base))[2]);
147 g_assert_cmpint ('d', ==, ((gchar *)g_memory_output_stream_get_data (base))[3]);
148 ret = g_output_stream_write_all (out, buffer, 2, &bytes_written, NULL, &error);
149 g_assert_no_error (error);
151 g_assert_cmpint (bytes_written, ==, 2);
152 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (out)), ==, 8);
154 /* Backward relative seek */
155 ret = g_seekable_seek (seekable, -4, G_SEEK_CUR, NULL, &error);
156 g_assert_no_error (error);
158 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (out)), ==, 4);
159 g_assert_cmpint ('a', ==, ((gchar *)g_memory_output_stream_get_data (base))[0]);
160 g_assert_cmpint ('b', ==, ((gchar *)g_memory_output_stream_get_data (base))[1]);
161 g_assert_cmpint ('c', ==, ((gchar *)g_memory_output_stream_get_data (base))[2]);
162 g_assert_cmpint ('d', ==, ((gchar *)g_memory_output_stream_get_data (base))[3]);
163 g_assert_cmpint ('a', ==, ((gchar *)g_memory_output_stream_get_data (base))[6]);
164 g_assert_cmpint ('b', ==, ((gchar *)g_memory_output_stream_get_data (base))[7]);
165 ret = g_output_stream_write_all (out, buffer, 2, &bytes_written, NULL, &error);
166 g_assert_no_error (error);
168 g_assert_cmpint (bytes_written, ==, 2);
169 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (out)), ==, 6);
172 ret = g_seekable_seek (seekable, 2, G_SEEK_SET, NULL, &error);
173 g_assert_no_error (error);
175 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (out)), ==, 2);
176 g_assert_cmpint ('a', ==, ((gchar *)g_memory_output_stream_get_data (base))[0]);
177 g_assert_cmpint ('b', ==, ((gchar *)g_memory_output_stream_get_data (base))[1]);
178 g_assert_cmpint ('c', ==, ((gchar *)g_memory_output_stream_get_data (base))[2]);
179 g_assert_cmpint ('d', ==, ((gchar *)g_memory_output_stream_get_data (base))[3]);
180 g_assert_cmpint ('a', ==, ((gchar *)g_memory_output_stream_get_data (base))[4]);
181 g_assert_cmpint ('b', ==, ((gchar *)g_memory_output_stream_get_data (base))[5]);
182 g_assert_cmpint ('a', ==, ((gchar *)g_memory_output_stream_get_data (base))[6]);
183 g_assert_cmpint ('b', ==, ((gchar *)g_memory_output_stream_get_data (base))[7]);
184 ret = g_output_stream_write_all (out, buffer, 2, &bytes_written, NULL, &error);
185 g_assert_no_error (error);
187 g_assert_cmpint (bytes_written, ==, 2);
188 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (out)), ==, 4);
191 ret = g_seekable_seek (seekable, 6 - 30, G_SEEK_END, NULL, &error);
192 g_assert_no_error (error);
194 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (out)), ==, 6);
195 g_assert_cmpint ('a', ==, ((gchar *)g_memory_output_stream_get_data (base))[0]);
196 g_assert_cmpint ('b', ==, ((gchar *)g_memory_output_stream_get_data (base))[1]);
197 g_assert_cmpint ('a', ==, ((gchar *)g_memory_output_stream_get_data (base))[2]);
198 g_assert_cmpint ('b', ==, ((gchar *)g_memory_output_stream_get_data (base))[3]);
199 g_assert_cmpint ('a', ==, ((gchar *)g_memory_output_stream_get_data (base))[4]);
200 g_assert_cmpint ('b', ==, ((gchar *)g_memory_output_stream_get_data (base))[5]);
201 g_assert_cmpint ('a', ==, ((gchar *)g_memory_output_stream_get_data (base))[6]);
202 g_assert_cmpint ('b', ==, ((gchar *)g_memory_output_stream_get_data (base))[7]);
203 ret = g_output_stream_write_all (out, buffer + 2, 2, &bytes_written, NULL, &error);
204 g_assert_no_error (error);
206 g_assert_cmpint (bytes_written, ==, 2);
207 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (out)), ==, 8);
210 ret = g_output_stream_flush (out, NULL, &error);
211 g_assert_no_error (error);
213 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (out)), ==, 8);
214 g_assert_cmpint ('a', ==, ((gchar *)g_memory_output_stream_get_data (base))[0]);
215 g_assert_cmpint ('b', ==, ((gchar *)g_memory_output_stream_get_data (base))[1]);
216 g_assert_cmpint ('a', ==, ((gchar *)g_memory_output_stream_get_data (base))[2]);
217 g_assert_cmpint ('b', ==, ((gchar *)g_memory_output_stream_get_data (base))[3]);
218 g_assert_cmpint ('a', ==, ((gchar *)g_memory_output_stream_get_data (base))[4]);
219 g_assert_cmpint ('b', ==, ((gchar *)g_memory_output_stream_get_data (base))[5]);
220 g_assert_cmpint ('c', ==, ((gchar *)g_memory_output_stream_get_data (base))[6]);
221 g_assert_cmpint ('d', ==, ((gchar *)g_memory_output_stream_get_data (base))[7]);
223 g_object_unref (out);
224 g_object_unref (base);
230 GMemoryOutputStream *base_stream;
231 GOutputStream *stream;
242 stream_data = g_malloc0 (len);
243 base_stream = G_MEMORY_OUTPUT_STREAM (g_memory_output_stream_new (stream_data, len, g_realloc, g_free));
244 stream = g_buffered_output_stream_new_sized (G_OUTPUT_STREAM (base_stream), 8);
245 seekable = G_SEEKABLE (stream);
247 g_assert (g_seekable_can_truncate (seekable));
250 g_assert_cmpint (g_memory_output_stream_get_size (base_stream), ==, len);
251 g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 0);
254 res = g_output_stream_write_all (stream, "ab", 2, &bytes_written, NULL, &error);
255 g_assert_no_error (error);
257 res = g_output_stream_write_all (stream, "cd", 2, &bytes_written, NULL, &error);
258 g_assert_no_error (error);
261 res = g_output_stream_flush (stream, NULL, &error);
262 g_assert_no_error (error);
265 g_assert_cmpint (g_memory_output_stream_get_size (base_stream), ==, len);
266 g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 4);
267 g_assert_cmpint (stream_data[0], ==, 'a');
268 g_assert_cmpint (stream_data[1], ==, 'b');
269 g_assert_cmpint (stream_data[2], ==, 'c');
270 g_assert_cmpint (stream_data[3], ==, 'd');
272 /* Truncate at position */
273 res = g_seekable_truncate (seekable, 4, NULL, &error);
274 g_assert_no_error (error);
276 g_assert_cmpint (g_memory_output_stream_get_size (base_stream), ==, 4);
277 g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 4);
278 g_assert_cmpint (stream_data[0], ==, 'a');
279 g_assert_cmpint (stream_data[1], ==, 'b');
280 g_assert_cmpint (stream_data[2], ==, 'c');
281 g_assert_cmpint (stream_data[3], ==, 'd');
283 /* Truncate beyond position */
284 res = g_seekable_truncate (seekable, 6, NULL, &error);
285 g_assert_no_error (error);
287 g_assert_cmpint (g_memory_output_stream_get_size (base_stream), ==, 6);
288 g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 4);
289 g_assert_cmpint (stream_data[0], ==, 'a');
290 g_assert_cmpint (stream_data[1], ==, 'b');
291 g_assert_cmpint (stream_data[2], ==, 'c');
292 g_assert_cmpint (stream_data[3], ==, 'd');
294 /* Truncate before position */
295 res = g_seekable_truncate (seekable, 2, NULL, &error);
296 g_assert_no_error (error);
298 g_assert_cmpint (g_memory_output_stream_get_size (base_stream), ==, 2);
299 g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 2);
300 g_assert_cmpint (stream_data[0], ==, 'a');
301 g_assert_cmpint (stream_data[1], ==, 'b');
303 g_object_unref (stream);
304 g_object_unref (base_stream);
308 main (int argc, char *argv[])
312 g_test_init (&argc, &argv, NULL);
314 g_test_add_func ("/buffered-output-stream/write", test_write);
315 g_test_add_func ("/buffered-output-stream/grow", test_grow);
316 g_test_add_func ("/buffered-output-stream/seek", test_seek);
317 g_test_add_func ("/buffered-output-stream/truncate", test_truncate);
318 g_test_add_func ("/filter-output-stream/close", test_close);
320 return g_test_run ();