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);
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");
40 g_object_unref (base);
49 const gchar buffer[] = "abcdefghijklmnopqrstuvwxyz";
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);
62 g_assert (g_seekable_can_seek (G_SEEKABLE (out)));
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");
83 g_object_unref (base);
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)));
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);
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);
123 GMemoryOutputStream *base;
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);
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);
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);
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);
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);
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);
174 g_assert_cmpint (bytes_written, ==, 2);
175 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (out)), ==, 6);
178 ret = g_seekable_seek (seekable, 2, G_SEEK_SET, NULL, &error);
179 g_assert_no_error (error);
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);
193 g_assert_cmpint (bytes_written, ==, 2);
194 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (out)), ==, 4);
197 ret = g_seekable_seek (seekable, 6 - 30, G_SEEK_END, NULL, &error);
198 g_assert_no_error (error);
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);
212 g_assert_cmpint (bytes_written, ==, 2);
213 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (out)), ==, 8);
216 ret = g_output_stream_flush (out, NULL, &error);
217 g_assert_no_error (error);
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);
236 GMemoryOutputStream *base_stream;
237 GOutputStream *stream;
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));
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);
260 res = g_output_stream_write_all (stream, "ab", 2, &bytes_written, NULL, &error);
261 g_assert_no_error (error);
263 res = g_output_stream_write_all (stream, "cd", 2, &bytes_written, NULL, &error);
264 g_assert_no_error (error);
267 res = g_output_stream_flush (stream, NULL, &error);
268 g_assert_no_error (error);
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);
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);
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);
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 ();