From 1d1c17d9ee69ece6c28da0544d12a29b1c3e4dc9 Mon Sep 17 00:00:00 2001 From: Ryan Lortie Date: Tue, 22 Oct 2013 16:15:29 -0400 Subject: [PATCH] GMemoryOutputStream: improve seek tests Improve test coverage for testing seeking on fixed vs. resizable GMemoryOutputStream. https://bugzilla.gnome.org/show_bug.cgi?id=684842 --- gio/tests/memory-output-stream.c | 97 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 94 insertions(+), 3 deletions(-) diff --git a/gio/tests/memory-output-stream.c b/gio/tests/memory-output-stream.c index 32e361d..b8e6697 100644 --- a/gio/tests/memory-output-stream.c +++ b/gio/tests/memory-output-stream.c @@ -55,7 +55,7 @@ test_truncate (void) } static void -test_seek (void) +test_seek_fixed (void) { GOutputStream *mo; GError *error; @@ -64,20 +64,110 @@ test_seek (void) g_assert (G_IS_SEEKABLE (mo)); g_assert (g_seekable_can_seek (G_SEEKABLE (mo))); + g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 0); error = NULL; + g_assert (!g_seekable_seek (G_SEEKABLE (mo), 222, G_SEEK_CUR, NULL, &error)); + g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT); + g_clear_error (&error); + g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 0); + g_assert (g_seekable_seek (G_SEEKABLE (mo), 26, G_SEEK_SET, NULL, &error)); g_assert_no_error (error); g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 26); + g_assert (g_seekable_seek (G_SEEKABLE (mo), 20, G_SEEK_CUR, NULL, &error)); + g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 46); + g_assert_no_error (error); + g_assert (!g_seekable_seek (G_SEEKABLE (mo), 200, G_SEEK_CUR, NULL, &error)); g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT); - g_error_free (error); + g_clear_error (&error); + g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 46); + + g_assert (!g_seekable_seek (G_SEEKABLE (mo), 1, G_SEEK_END, NULL, &error)); + g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT); + g_clear_error (&error); + g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 46); + + g_assert (g_seekable_seek (G_SEEKABLE (mo), 0, G_SEEK_END, NULL, &error)); + g_assert_no_error (error); + g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 100); + + g_assert (g_seekable_seek (G_SEEKABLE (mo), -1, G_SEEK_END, NULL, &error)); + g_assert_no_error (error); + g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 99); g_object_unref (mo); } static void +test_seek_resizable_stream (GOutputStream *mo) +{ + GError *error; + + g_assert (G_IS_SEEKABLE (mo)); + g_assert (g_seekable_can_seek (G_SEEKABLE (mo))); + g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 0); + + error = NULL; + g_assert (g_seekable_seek (G_SEEKABLE (mo), 222, G_SEEK_CUR, NULL, &error)); + g_assert_no_error (error); + g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 222); + + g_assert (g_seekable_seek (G_SEEKABLE (mo), 26, G_SEEK_SET, NULL, &error)); + g_assert_no_error (error); + g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 26); + + g_assert (g_seekable_seek (G_SEEKABLE (mo), 20, G_SEEK_CUR, NULL, &error)); + g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 46); + g_assert_no_error (error); + + g_assert (g_seekable_seek (G_SEEKABLE (mo), 200, G_SEEK_CUR, NULL, &error)); + g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 246); + g_assert_no_error (error); + + g_assert (g_seekable_seek (G_SEEKABLE (mo), 1, G_SEEK_END, NULL, &error)); + g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 1); + g_assert_no_error (error); + + g_assert (g_seekable_seek (G_SEEKABLE (mo), 0, G_SEEK_END, NULL, &error)); + g_assert_no_error (error); + g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 0); + + /* The 'end' is still zero, so this should fail */ + g_assert (!g_seekable_seek (G_SEEKABLE (mo), -1, G_SEEK_END, NULL, &error)); + g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT); + g_clear_error (&error); + g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 0); +} + +static void +test_seek_resizable (void) +{ + GOutputStream *mo; + gint i; + + /* For resizable streams, the initially allocated size is purely an + * implementation detail. We should not be able to tell the + * difference based on the seek API, so make a bunch of streams with + * different sizes and subject them to the same test. + */ + for (i = 0; i < 1024; i++) + { + mo = g_memory_output_stream_new (g_malloc (i), i, g_realloc, g_free); + + test_seek_resizable_stream (mo); + + g_assert_cmpint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo)), ==, 0); + /* No writes = no resizes */ + g_assert_cmpint (g_memory_output_stream_get_size (G_MEMORY_OUTPUT_STREAM (mo)), ==, i); + + g_object_unref (mo); + } +} + +static void test_data_size (void) { GOutputStream *mo; @@ -230,7 +320,8 @@ main (int argc, g_test_bug_base ("http://bugzilla.gnome.org/"); g_test_add_func ("/memory-output-stream/truncate", test_truncate); - g_test_add_func ("/memory-output-stream/seek", test_seek); + g_test_add_func ("/memory-output-stream/seek/fixed", test_seek_fixed); + g_test_add_func ("/memory-output-stream/seek/resizable", test_seek_resizable); g_test_add_func ("/memory-output-stream/get-data-size", test_data_size); g_test_add_func ("/memory-output-stream/properties", test_properties); g_test_add_func ("/memory-output-stream/write-bytes", test_write_bytes); -- 2.7.4