1 /* GLib testing framework examples and tests
2 * Copyright (C) 2007 Imendio AB
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.
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.
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.
23 #include <glib/glib.h>
29 test_read_chunks (void)
31 const char *data1
= "abcdefghijklmnopqrstuvwxyz";
32 const char *data2
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
33 const char *result
= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
35 gsize bytes_read
, pos
, len
, chunk_size
;
40 stream
= g_memory_input_stream_new ();
42 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream
),
44 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream
),
46 len
= strlen (data1
) + strlen (data2
);
48 for (chunk_size
= 1; chunk_size
< len
- 1; chunk_size
++)
53 bytes_read
= g_input_stream_read (stream
, buffer
, chunk_size
, NULL
, &error
);
54 g_assert_no_error (error
);
55 g_assert_cmpint (bytes_read
, ==, MIN (chunk_size
, len
- pos
));
56 g_assert (strncmp (buffer
, result
+ pos
, bytes_read
) == 0);
61 g_assert_cmpint (pos
, ==, len
);
62 res
= g_seekable_seek (G_SEEKABLE (stream
), 0, G_SEEK_SET
, NULL
, &error
);
63 g_assert_cmpint (res
, ==, TRUE
);
64 g_assert_no_error (error
);
67 g_object_unref (stream
);
73 async_read_chunk (GObject
*object
,
77 gsize
*bytes_read
= user_data
;
80 *bytes_read
= g_input_stream_read_finish (G_INPUT_STREAM (object
),
82 g_assert_no_error (error
);
84 g_main_loop_quit (loop
);
88 async_skipped_chunk (GObject
*object
,
92 gsize
*bytes_skipped
= user_data
;
95 *bytes_skipped
= g_input_stream_skip_finish (G_INPUT_STREAM (object
),
97 g_assert_no_error (error
);
99 g_main_loop_quit (loop
);
105 const char *data1
= "abcdefghijklmnopqrstuvwxyz";
106 const char *data2
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
107 const char *result
= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
109 gsize bytes_read
, bytes_skipped
;
110 gsize pos
, len
, chunk_size
;
111 GError
*error
= NULL
;
112 GInputStream
*stream
;
115 loop
= g_main_loop_new (NULL
, FALSE
);
117 stream
= g_memory_input_stream_new ();
119 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream
),
121 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream
),
123 len
= strlen (data1
) + strlen (data2
);
125 for (chunk_size
= 1; chunk_size
< len
- 1; chunk_size
++)
130 g_input_stream_read_async (stream
, buffer
, chunk_size
,
131 G_PRIORITY_DEFAULT
, NULL
,
132 async_read_chunk
, &bytes_read
);
133 g_main_loop_run (loop
);
135 g_assert_cmpint (bytes_read
, ==, MIN (chunk_size
, len
- pos
));
136 g_assert (strncmp (buffer
, result
+ pos
, bytes_read
) == 0);
141 g_assert_cmpint (pos
, ==, len
);
142 res
= g_seekable_seek (G_SEEKABLE (stream
), 0, G_SEEK_SET
, NULL
, &error
);
143 g_assert_cmpint (res
, ==, TRUE
);
144 g_assert_no_error (error
);
147 while (pos
+ chunk_size
+ 1 < len
)
149 g_input_stream_skip_async (stream
, chunk_size
,
150 G_PRIORITY_DEFAULT
, NULL
,
151 async_skipped_chunk
, &bytes_skipped
);
152 g_main_loop_run (loop
);
154 g_assert_cmpint (bytes_skipped
, ==, MIN (chunk_size
, len
- pos
));
156 pos
+= bytes_skipped
;
159 g_input_stream_read_async (stream
, buffer
, len
- pos
,
160 G_PRIORITY_DEFAULT
, NULL
,
161 async_read_chunk
, &bytes_read
);
162 g_main_loop_run (loop
);
164 g_assert_cmpint (bytes_read
, ==, len
- pos
);
165 g_assert (strncmp (buffer
, result
+ pos
, bytes_read
) == 0);
167 res
= g_seekable_seek (G_SEEKABLE (stream
), 0, G_SEEK_SET
, NULL
, &error
);
168 g_assert_cmpint (res
, ==, TRUE
);
169 g_assert_no_error (error
);
172 g_object_unref (stream
);
173 g_main_loop_unref (loop
);
179 const char *data1
= "abcdefghijklmnopqrstuvwxyz";
180 const char *data2
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
181 GInputStream
*stream
;
185 stream
= g_memory_input_stream_new ();
187 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream
),
189 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream
),
192 g_assert (G_IS_SEEKABLE (stream
));
193 g_assert (g_seekable_can_seek (G_SEEKABLE (stream
)));
196 g_assert (g_seekable_seek (G_SEEKABLE (stream
), 26, G_SEEK_SET
, NULL
, &error
));
197 g_assert_no_error (error
);
198 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (stream
)), ==, 26);
200 g_assert (g_input_stream_read (stream
, buffer
, 1, NULL
, &error
) == 1);
201 g_assert_no_error (error
);
203 g_assert (buffer
[0] == 'A');
205 g_assert (!g_seekable_seek (G_SEEKABLE (stream
), 26, G_SEEK_CUR
, NULL
, &error
));
206 g_assert_error (error
, G_IO_ERROR
, G_IO_ERROR_INVALID_ARGUMENT
);
207 g_error_free (error
);
209 g_object_unref (stream
);
215 const char *data1
= "abcdefghijklmnopqrstuvwxyz";
216 const char *data2
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
217 GInputStream
*stream
;
220 stream
= g_memory_input_stream_new ();
222 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream
),
224 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream
),
227 g_assert (G_IS_SEEKABLE (stream
));
228 g_assert (!g_seekable_can_truncate (G_SEEKABLE (stream
)));
231 g_assert (!g_seekable_truncate (G_SEEKABLE (stream
), 26, NULL
, &error
));
232 g_assert_error (error
, G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
);
233 g_error_free (error
);
235 g_object_unref (stream
);
239 test_read_bytes (void)
241 const char *data1
= "abcdefghijklmnopqrstuvwxyz";
242 const char *data2
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
243 GInputStream
*stream
;
244 GError
*error
= NULL
;
249 stream
= g_memory_input_stream_new ();
250 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream
),
252 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream
),
255 bytes
= g_input_stream_read_bytes (stream
, 26, NULL
, &error
);
256 g_assert_no_error (error
);
258 data
= g_bytes_get_data (bytes
, &size
);
259 g_assert_cmpint (size
, ==, 26);
260 g_assert (strncmp (data
, data1
, 26) == 0);
262 g_bytes_unref (bytes
);
263 g_object_unref (stream
);
267 test_from_bytes (void)
269 gchar data
[4096], buffer
[4096];
271 GError
*error
= NULL
;
272 GInputStream
*stream
;
275 for (i
= 0; i
< 4096; i
++)
276 data
[i
] = 1 + i
% 255;
278 bytes
= g_bytes_new_static (data
, 4096);
279 stream
= g_memory_input_stream_new_from_bytes (bytes
);
280 g_assert (g_input_stream_read (stream
, buffer
, 2048, NULL
, &error
) == 2048);
281 g_assert_no_error (error
);
282 g_assert (strncmp (data
, buffer
, 2048) == 0);
284 g_object_unref (stream
);
285 g_bytes_unref (bytes
);
292 g_test_init (&argc
, &argv
, NULL
);
294 g_test_add_func ("/memory-input-stream/read-chunks", test_read_chunks
);
295 g_test_add_func ("/memory-input-stream/async", test_async
);
296 g_test_add_func ("/memory-input-stream/seek", test_seek
);
297 g_test_add_func ("/memory-input-stream/truncate", test_truncate
);
298 g_test_add_func ("/memory-input-stream/read-bytes", test_read_bytes
);
299 g_test_add_func ("/memory-input-stream/from-bytes", test_from_bytes
);