Blame tests/pixbuf-randomly-modified.c

Packit 979760
/* -*- Mode: C; c-basic-offset: 2; -*- */
Packit 979760
/* GdkPixbuf library - test loaders
Packit 979760
 *
Packit 979760
 * Copyright (C) 2001 Søren Sandmann (sandmann@daimi.au.dk)
Packit 979760
 *
Packit 979760
 * This program is free software; you can redistribute it and/or modify
Packit 979760
 * it under the terms of the GNU General Public License as published by
Packit 979760
 * the Free Software Foundation; either version 2 of the License, or
Packit 979760
 * (at your option) any later version.
Packit 979760
 *
Packit 979760
 * This program is distributed in the hope that it will be useful,
Packit 979760
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 979760
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 979760
 * GNU General Public License for more details.
Packit 979760
 *
Packit 979760
 * You should have received a copy of the GNU General Public License
Packit 979760
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Packit 979760
 */
Packit 979760
Packit 979760
#include "config.h"
Packit 979760
#include "gdk-pixbuf/gdk-pixbuf.h"
Packit 979760
#include "glib.h"
Packit 979760
#include <stdio.h>
Packit 979760
#include <stdlib.h>
Packit 979760
#include <time.h>
Packit 979760
#include <string.h>
Packit 979760
Packit 979760
#ifdef HAVE_SYS_TIME_H
Packit 979760
#include <sys/time.h>
Packit 979760
#endif
Packit 979760
Packit 979760
#ifdef HAVE_SYS_RESOURCE_H
Packit 979760
#include <sys/resource.h>
Packit 979760
#endif
Packit 979760
Packit 979760
#include "test-common.h"
Packit 979760
Packit 979760
static void
Packit 979760
randomly_modify (const gchar *image, guint size)
Packit 979760
{
Packit 979760
  int i, n;
Packit 979760
Packit 979760
  guchar *img_copy = g_malloc (size);
Packit 979760
  g_memmove (img_copy, image, size);
Packit 979760
  
Packit 979760
  n = MIN (100, size / 4);
Packit 979760
Packit 979760
  for (i = 0; i < n; i++)
Packit 979760
    {
Packit 979760
      FILE *f;
Packit 979760
      GdkPixbufLoader *loader;
Packit 979760
      
Packit 979760
      guint index = g_test_rand_int_range (0, size);
Packit 979760
      guchar byte = g_test_rand_int_range (0, 256);
Packit 979760
      
Packit 979760
      img_copy[index] = byte;
Packit 979760
      f = fopen ("pixbuf-randomly-modified-image", "w");
Packit 979760
      g_assert (f != NULL);
Packit 979760
      fwrite (img_copy, size, 1, f);
Packit 979760
      g_assert (!ferror (f));
Packit 979760
      fclose (f);
Packit 979760
Packit 979760
      loader = gdk_pixbuf_loader_new ();
Packit 979760
      gdk_pixbuf_loader_write (loader, img_copy, size, NULL);
Packit 979760
      gdk_pixbuf_loader_close (loader, NULL);
Packit 979760
      g_object_unref (loader);
Packit 979760
    }
Packit 979760
  g_free (img_copy);
Packit 979760
}
Packit 979760
Packit 979760
static void
Packit 979760
test_randomly_modified (gconstpointer data)
Packit 979760
{
Packit 979760
  GFile *file = G_FILE (data);
Packit 979760
  gchar *buffer;
Packit 979760
  gsize size;
Packit 979760
  gint iterations;
Packit 979760
  gint i;
Packit 979760
  GError *error = NULL;
Packit 979760
Packit 979760
  g_file_load_contents (file, NULL, &buffer, &size, NULL, &error);
Packit 979760
  g_assert_no_error (error);
Packit 979760
Packit 979760
  if (g_test_thorough ())
Packit 979760
    iterations = 1000;
Packit 979760
  else
Packit 979760
    iterations = 1;
Packit 979760
Packit 979760
  for (i = 0; i < iterations; i++)
Packit 979760
    randomly_modify (buffer, size);
Packit 979760
Packit 979760
  g_free (buffer);
Packit 979760
}
Packit 979760
Packit 979760
int
Packit 979760
main (int argc, char **argv)
Packit 979760
{
Packit 979760
  gchar *base_dir;
Packit 979760
  GFile *base, *test_images;
Packit 979760
#ifdef HAVE_SETRLIMIT
Packit 979760
  struct rlimit max_mem_size;
Packit 979760
Packit 979760
  max_mem_size.rlim_cur = 100 * 1024 * 1024; /* 100M */
Packit 979760
  max_mem_size.rlim_max = max_mem_size.rlim_cur;
Packit 979760
  setrlimit (RLIMIT_DATA, &max_mem_size);
Packit 979760
#endif
Packit 979760
Packit 979760
  g_test_init (&argc, &argv, NULL);
Packit 979760
Packit 979760
  base_dir = g_build_filename (g_test_get_dir (G_TEST_DIST), "test-images", NULL);
Packit 979760
  base = g_file_new_for_path (base_dir);
Packit 979760
Packit 979760
  test_images = g_file_get_child (base, "randomly-modified");
Packit 979760
  add_test_for_all_images ("/pixbuf", base, test_images, test_randomly_modified, NULL);
Packit 979760
  g_object_unref (test_images);
Packit 979760
Packit 979760
  test_images = g_file_get_child (base, "fail");
Packit 979760
  add_test_for_all_images ("/pixbuf/randomly-modified", base, test_images, test_randomly_modified, NULL);
Packit 979760
  g_object_unref (test_images);
Packit 979760
Packit 979760
  g_object_unref (base);
Packit 979760
  g_free (base_dir);
Packit 979760
Packit 979760
  g_test_message ("Modified image is written to pixbuf-randomly-modified-image");
Packit 979760
Packit 979760
  return g_test_run ();
Packit 979760
}