Blame gio/tests/memory-input-stream.c

Packit ae235b
/* GLib testing framework examples and tests
Packit ae235b
 * Copyright (C) 2007 Imendio AB
Packit ae235b
 * Authors: Tim Janik
Packit ae235b
 *
Packit ae235b
 * This work is provided "as is"; redistribution and modification
Packit ae235b
 * in whole or in part, in any medium, physical or electronic is
Packit ae235b
 * permitted without restriction.
Packit ae235b
 *
Packit ae235b
 * This work is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Packit ae235b
 *
Packit ae235b
 * In no event shall the authors or contributors be liable for any
Packit ae235b
 * direct, indirect, incidental, special, exemplary, or consequential
Packit ae235b
 * damages (including, but not limited to, procurement of substitute
Packit ae235b
 * goods or services; loss of use, data, or profits; or business
Packit ae235b
 * interruption) however caused and on any theory of liability, whether
Packit ae235b
 * in contract, strict liability, or tort (including negligence or
Packit ae235b
 * otherwise) arising in any way out of the use of this software, even
Packit ae235b
 * if advised of the possibility of such damage.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include <glib/glib.h>
Packit ae235b
#include <gio/gio.h>
Packit ae235b
#include <stdlib.h>
Packit ae235b
#include <string.h>
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_read_chunks (void)
Packit ae235b
{
Packit ae235b
  const char *data1 = "abcdefghijklmnopqrstuvwxyz";
Packit ae235b
  const char *data2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Packit ae235b
  const char *result = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
Packit ae235b
  char buffer[128];
Packit ae235b
  gsize bytes_read, pos, len, chunk_size;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GInputStream *stream;
Packit ae235b
  gboolean res;
Packit ae235b
Packit ae235b
  stream = g_memory_input_stream_new ();
Packit ae235b
Packit ae235b
  g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
Packit ae235b
                                  data1, -1, NULL);  
Packit ae235b
  g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
Packit ae235b
                                  data2, -1, NULL);  
Packit ae235b
  len = strlen (data1) + strlen (data2);
Packit ae235b
Packit ae235b
  for (chunk_size = 1; chunk_size < len - 1; chunk_size++)
Packit ae235b
    {
Packit ae235b
      pos = 0;
Packit ae235b
      while (pos < len) 
Packit ae235b
        {
Packit ae235b
          bytes_read = g_input_stream_read (stream, buffer, chunk_size, NULL, &error);
Packit ae235b
          g_assert_no_error (error);
Packit ae235b
          g_assert_cmpint (bytes_read, ==, MIN (chunk_size, len - pos));
Packit ae235b
          g_assert (strncmp (buffer, result + pos, bytes_read) == 0);
Packit ae235b
Packit ae235b
          pos += bytes_read;
Packit ae235b
        }
Packit ae235b
      
Packit ae235b
      g_assert_cmpint (pos, ==, len);
Packit ae235b
      res = g_seekable_seek (G_SEEKABLE (stream), 0, G_SEEK_SET, NULL, &error);
Packit ae235b
      g_assert_cmpint (res, ==, TRUE);
Packit ae235b
      g_assert_no_error (error);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_object_unref (stream);
Packit ae235b
}
Packit ae235b
Packit ae235b
GMainLoop *loop;
Packit ae235b
Packit ae235b
static void
Packit ae235b
async_read_chunk (GObject      *object,
Packit ae235b
		  GAsyncResult *result,
Packit ae235b
		  gpointer      user_data)
Packit ae235b
{
Packit ae235b
  gsize *bytes_read = user_data;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  *bytes_read = g_input_stream_read_finish (G_INPUT_STREAM (object),
Packit ae235b
					    result, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
Packit ae235b
  g_main_loop_quit (loop);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
async_skipped_chunk (GObject      *object,
Packit ae235b
                     GAsyncResult *result,
Packit ae235b
                     gpointer      user_data)
Packit ae235b
{
Packit ae235b
  gsize *bytes_skipped = user_data;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  *bytes_skipped = g_input_stream_skip_finish (G_INPUT_STREAM (object),
Packit ae235b
                                               result, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
Packit ae235b
  g_main_loop_quit (loop);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_async (void)
Packit ae235b
{
Packit ae235b
  const char *data1 = "abcdefghijklmnopqrstuvwxyz";
Packit ae235b
  const char *data2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Packit ae235b
  const char *result = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
Packit ae235b
  char buffer[128];
Packit ae235b
  gsize bytes_read, bytes_skipped;
Packit ae235b
  gsize pos, len, chunk_size;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GInputStream *stream;
Packit ae235b
  gboolean res;
Packit ae235b
Packit ae235b
  loop = g_main_loop_new (NULL, FALSE);
Packit ae235b
Packit ae235b
  stream = g_memory_input_stream_new ();
Packit ae235b
Packit ae235b
  g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
Packit ae235b
                                  data1, -1, NULL);  
Packit ae235b
  g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
Packit ae235b
                                  data2, -1, NULL);  
Packit ae235b
  len = strlen (data1) + strlen (data2);
Packit ae235b
Packit ae235b
  for (chunk_size = 1; chunk_size < len - 1; chunk_size++)
Packit ae235b
    {
Packit ae235b
      pos = 0;
Packit ae235b
      while (pos < len) 
Packit ae235b
        {
Packit ae235b
          g_input_stream_read_async (stream, buffer, chunk_size,
Packit ae235b
				     G_PRIORITY_DEFAULT, NULL,
Packit ae235b
				     async_read_chunk, &bytes_read);
Packit ae235b
	  g_main_loop_run (loop);
Packit ae235b
Packit ae235b
          g_assert_cmpint (bytes_read, ==, MIN (chunk_size, len - pos));
Packit ae235b
          g_assert (strncmp (buffer, result + pos, bytes_read) == 0);
Packit ae235b
Packit ae235b
          pos += bytes_read;
Packit ae235b
        }
Packit ae235b
      
Packit ae235b
      g_assert_cmpint (pos, ==, len);
Packit ae235b
      res = g_seekable_seek (G_SEEKABLE (stream), 0, G_SEEK_SET, NULL, &error);
Packit ae235b
      g_assert_cmpint (res, ==, TRUE);
Packit ae235b
      g_assert_no_error (error);
Packit ae235b
Packit ae235b
      pos = 0;
Packit ae235b
      while (pos + chunk_size + 1 < len)
Packit ae235b
        {
Packit ae235b
          g_input_stream_skip_async (stream, chunk_size,
Packit ae235b
				     G_PRIORITY_DEFAULT, NULL,
Packit ae235b
				     async_skipped_chunk, &bytes_skipped);
Packit ae235b
	  g_main_loop_run (loop);
Packit ae235b
Packit ae235b
          g_assert_cmpint (bytes_skipped, ==, MIN (chunk_size, len - pos));
Packit ae235b
Packit ae235b
          pos += bytes_skipped;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      g_input_stream_read_async (stream, buffer, len - pos,
Packit ae235b
                                 G_PRIORITY_DEFAULT, NULL,
Packit ae235b
                                 async_read_chunk, &bytes_read);
Packit ae235b
      g_main_loop_run (loop);
Packit ae235b
Packit ae235b
      g_assert_cmpint (bytes_read, ==, len - pos);
Packit ae235b
      g_assert (strncmp (buffer, result + pos, bytes_read) == 0);
Packit ae235b
Packit ae235b
      res = g_seekable_seek (G_SEEKABLE (stream), 0, G_SEEK_SET, NULL, &error);
Packit ae235b
      g_assert_cmpint (res, ==, TRUE);
Packit ae235b
      g_assert_no_error (error);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_object_unref (stream);
Packit ae235b
  g_main_loop_unref (loop);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_seek (void)
Packit ae235b
{
Packit ae235b
  const char *data1 = "abcdefghijklmnopqrstuvwxyz";
Packit ae235b
  const char *data2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Packit ae235b
  GInputStream *stream;
Packit ae235b
  GError *error;
Packit ae235b
  char buffer[10];
Packit ae235b
Packit ae235b
  stream = g_memory_input_stream_new ();
Packit ae235b
Packit ae235b
  g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
Packit ae235b
                                  data1, -1, NULL);
Packit ae235b
  g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
Packit ae235b
                                  data2, -1, NULL);
Packit ae235b
Packit ae235b
  g_assert (G_IS_SEEKABLE (stream));
Packit ae235b
  g_assert (g_seekable_can_seek (G_SEEKABLE (stream)));
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  g_assert (g_seekable_seek (G_SEEKABLE (stream), 26, G_SEEK_SET, NULL, &error));
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert_cmpint (g_seekable_tell (G_SEEKABLE (stream)), ==, 26);
Packit ae235b
Packit ae235b
  g_assert (g_input_stream_read (stream, buffer, 1, NULL, &error) == 1);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
Packit ae235b
  g_assert (buffer[0] == 'A');
Packit ae235b
Packit ae235b
  g_assert (!g_seekable_seek (G_SEEKABLE (stream), 26, G_SEEK_CUR, NULL, &error));
Packit ae235b
  g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
Packit ae235b
  g_error_free (error);
Packit ae235b
Packit ae235b
  g_object_unref (stream);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_truncate (void)
Packit ae235b
{
Packit ae235b
  const char *data1 = "abcdefghijklmnopqrstuvwxyz";
Packit ae235b
  const char *data2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Packit ae235b
  GInputStream *stream;
Packit ae235b
  GError *error;
Packit ae235b
Packit ae235b
  stream = g_memory_input_stream_new ();
Packit ae235b
Packit ae235b
  g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
Packit ae235b
                                  data1, -1, NULL);
Packit ae235b
  g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
Packit ae235b
                                  data2, -1, NULL);
Packit ae235b
Packit ae235b
  g_assert (G_IS_SEEKABLE (stream));
Packit ae235b
  g_assert (!g_seekable_can_truncate (G_SEEKABLE (stream)));
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  g_assert (!g_seekable_truncate (G_SEEKABLE (stream), 26, NULL, &error));
Packit ae235b
  g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
Packit ae235b
  g_error_free (error);
Packit ae235b
Packit ae235b
  g_object_unref (stream);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_read_bytes (void)
Packit ae235b
{
Packit ae235b
  const char *data1 = "abcdefghijklmnopqrstuvwxyz";
Packit ae235b
  const char *data2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Packit ae235b
  GInputStream *stream;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GBytes *bytes;
Packit ae235b
  gsize size;
Packit ae235b
  gconstpointer data;
Packit ae235b
Packit ae235b
  stream = g_memory_input_stream_new ();
Packit ae235b
  g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
Packit ae235b
                                  data1, -1, NULL);
Packit ae235b
  g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
Packit ae235b
                                  data2, -1, NULL);
Packit ae235b
Packit ae235b
  bytes = g_input_stream_read_bytes (stream, 26, NULL, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
Packit ae235b
  data = g_bytes_get_data (bytes, &size);
Packit ae235b
  g_assert_cmpint (size, ==, 26);
Packit ae235b
  g_assert (strncmp (data, data1, 26) == 0);
Packit ae235b
Packit ae235b
  g_bytes_unref (bytes);
Packit ae235b
  g_object_unref (stream);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_from_bytes (void)
Packit ae235b
{
Packit ae235b
  gchar data[4096], buffer[4096];
Packit ae235b
  GBytes *bytes;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GInputStream *stream;
Packit ae235b
  gint i;
Packit ae235b
Packit ae235b
  for (i = 0; i < 4096; i++)
Packit ae235b
    data[i] = 1 + i % 255;
Packit ae235b
Packit ae235b
  bytes = g_bytes_new_static (data, 4096);
Packit ae235b
  stream = g_memory_input_stream_new_from_bytes (bytes);
Packit ae235b
  g_assert (g_input_stream_read (stream, buffer, 2048, NULL, &error) == 2048);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (strncmp (data, buffer, 2048) == 0);
Packit ae235b
Packit ae235b
  g_object_unref (stream);
Packit ae235b
  g_bytes_unref (bytes);
Packit ae235b
}
Packit ae235b
Packit ae235b
int
Packit ae235b
main (int   argc,
Packit ae235b
      char *argv[])
Packit ae235b
{
Packit ae235b
  g_test_init (&argc, &argv, NULL);
Packit ae235b
Packit ae235b
  g_test_add_func ("/memory-input-stream/read-chunks", test_read_chunks);
Packit ae235b
  g_test_add_func ("/memory-input-stream/async", test_async);
Packit ae235b
  g_test_add_func ("/memory-input-stream/seek", test_seek);
Packit ae235b
  g_test_add_func ("/memory-input-stream/truncate", test_truncate);
Packit ae235b
  g_test_add_func ("/memory-input-stream/read-bytes", test_read_bytes);
Packit ae235b
  g_test_add_func ("/memory-input-stream/from-bytes", test_from_bytes);
Packit ae235b
Packit ae235b
  return g_test_run();
Packit ae235b
}