}
static void
+async_skipped_chunk (GObject *object,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ gsize *bytes_skipped = user_data;
+ GError *error = NULL;
+
+ *bytes_skipped = g_input_stream_skip_finish (G_INPUT_STREAM (object),
+ result, &error);
+ g_assert_no_error (error);
+
+ g_main_loop_quit (loop);
+}
+
+static void
test_async (void)
{
const char *data1 = "abcdefghijklmnopqrstuvwxyz";
const char *data2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const char *result = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char buffer[128];
- gsize bytes_read, pos, len, chunk_size;
+ gsize bytes_read, bytes_skipped;
+ gsize pos, len, chunk_size;
GError *error = NULL;
GInputStream *stream;
gboolean res;
res = g_seekable_seek (G_SEEKABLE (stream), 0, G_SEEK_SET, NULL, &error);
g_assert_cmpint (res, ==, TRUE);
g_assert_no_error (error);
+
+ pos = 0;
+ while (pos + chunk_size + 1 < len)
+ {
+ g_input_stream_skip_async (stream, chunk_size,
+ G_PRIORITY_DEFAULT, NULL,
+ async_skipped_chunk, &bytes_skipped);
+ g_main_loop_run (loop);
+
+ g_assert_cmpint (bytes_skipped, ==, MIN (chunk_size, len - pos));
+
+ pos += bytes_skipped;
+ }
+
+ g_input_stream_read_async (stream, buffer, len - pos,
+ G_PRIORITY_DEFAULT, NULL,
+ async_read_chunk, &bytes_read);
+ g_main_loop_run (loop);
+
+ g_assert_cmpint (bytes_read, ==, len - pos);
+ g_assert (strncmp (buffer, result + pos, bytes_read) == 0);
+
+ res = g_seekable_seek (G_SEEKABLE (stream), 0, G_SEEK_SET, NULL, &error);
+ g_assert_cmpint (res, ==, TRUE);
+ g_assert_no_error (error);
}
g_object_unref (stream);
g_object_unref (stream);
}
+static void
+test_from_bytes (void)
+{
+ gchar data[4096], buffer[4096];
+ GBytes *bytes;
+ GError *error = NULL;
+ GInputStream *stream;
+ gint i;
+
+ for (i = 0; i < 4096; i++)
+ data[i] = 1 + i % 255;
+
+ bytes = g_bytes_new_static (data, 4096);
+ stream = g_memory_input_stream_new_from_bytes (bytes);
+ g_assert (g_input_stream_read (stream, buffer, 2048, NULL, &error) == 2048);
+ g_assert_no_error (error);
+ g_assert (strncmp (data, buffer, 2048) == 0);
+
+ g_object_unref (stream);
+ g_bytes_unref (bytes);
+}
+
int
main (int argc,
char *argv[])
g_test_add_func ("/memory-input-stream/seek", test_seek);
g_test_add_func ("/memory-input-stream/truncate", test_truncate);
g_test_add_func ("/memory-input-stream/read-bytes", test_read_bytes);
+ g_test_add_func ("/memory-input-stream/from-bytes", test_from_bytes);
return g_test_run();
}