[gdk-pixbuf] tests: Fix up pixbuf tests to know how to find data files



commit 67b5737712ea43c54ac31a19f349cef74aa2287f
Author: Colin Walters <walters verbum org>
Date:   Mon Jun 24 16:37:57 2013 +0100

    tests: Fix up pixbuf tests to know how to find data files
    
    These tests were broken by: 8ec55ff29e0626a05a3aa9d92060e28767ee142f
    
    Previously we were relying on hand-written .test files
    which used sh -c and wildcard expansion, but with modern
    GLib we can just use g_test_get_dir() to find the datadir
    relative to our binary path.

 tests/pixbuf-randomly-modified.c       |   32 +++++++++++++++++---
 tests/pixbuf-randomly-modified.test.in |    3 --
 tests/pixbuf-threads.c                 |   49 ++++++++++++++++---------------
 tests/pixbuf-threads.test.in           |    3 --
 4 files changed, 52 insertions(+), 35 deletions(-)
---
diff --git a/tests/pixbuf-randomly-modified.c b/tests/pixbuf-randomly-modified.c
index 83dd2ea..8dd176b 100644
--- a/tests/pixbuf-randomly-modified.c
+++ b/tests/pixbuf-randomly-modified.c
@@ -98,18 +98,18 @@ int
 main (int argc, char **argv)
 {
   int seed, i;
+  GError *err = NULL;
   gboolean got_seed = FALSE;
   GPtrArray *files = g_ptr_array_new ();
   int l, iterations;
 
+  g_test_init (&argc, &argv, NULL);
+
   if (g_getenv ("ITERATIONS"))
     iterations = atoi (g_getenv ("ITERATIONS"));
   else
     iterations = 1000;
 
-  if (argc == 1)
-    usage ();
-
   seed = time (NULL);
 
   for (i = 1; i < argc; ++i)
@@ -142,17 +142,33 @@ main (int argc, char **argv)
 #endif
   g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
 
+  if (files->len == 0)
+    {
+      const gchar *name;
+      const gchar *distdir = g_test_get_dir (G_TEST_DIST);
+      gchar *test_images_dir = g_build_filename (distdir, "test-images", NULL);
+      GDir *dir = g_dir_open (test_images_dir, 0, &err);
+      if (!dir)
+       goto out;
+      while ((name = g_dir_read_name (dir)) != NULL)
+       g_ptr_array_add (files, g_build_filename (test_images_dir, name, NULL));
+      g_dir_close (dir);
+      g_free (test_images_dir);
+    }
+
+  g_assert_cmpint (files->len, >, 0);
+
   for (l = 0; l < iterations; l++)
     for (i = 0; i < files->len; ++i)
       {
        gchar *contents;
        gsize size;
-       GError *err = NULL;
 
        fflush (stdout);
        if (!g_file_get_contents (files->pdata[i], &contents, &size, &err))
          {
-           g_warning ("%s: error: %s\n", (char *)files->pdata[i], err->message);
+           g_prefix_error (&err, "Reading %s: ", (char *)files->pdata[i]);
+           goto out;
          }
        else
          {
@@ -166,5 +182,11 @@ main (int argc, char **argv)
          }
       }
 
+ out:
+  if (err)
+    {
+      g_printerr ("%s\n", err->message);
+      return 1;
+    }
   return 0;
 }
diff --git a/tests/pixbuf-threads.c b/tests/pixbuf-threads.c
index 268b01f..8af6acf 100644
--- a/tests/pixbuf-threads.c
+++ b/tests/pixbuf-threads.c
@@ -78,20 +78,16 @@ load_image (gpointer  data,
   g_object_unref (loader);
 }
 
-static void
-usage (void)
-{
-  g_print ("usage: pixbuf-threads [--verbose] <files>\n");
-  exit (EXIT_FAILURE);
-}
-
 int
 main (int argc, char **argv)
 {
   int i, start;
   GThreadPool *pool;
+  GPtrArray *files = g_ptr_array_new_with_free_func ((GDestroyNotify)g_free);
   int l, iterations;
 
+  g_test_init (&argc, &argv, NULL);
+
 #if !GLIB_CHECK_VERSION (2, 35, 3)
   g_type_init ();
 #endif
@@ -108,11 +104,8 @@ main (int argc, char **argv)
 
   g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
 
-  if (argc == 1)
-    usage();
-
   start = 1;
-  if (strcmp (argv[1], "--verbose") == 0)
+  if (argc > 1 && strcmp (argv[1], "--verbose") == 0)
     {
       verbose = TRUE;
       start = 2;
@@ -121,19 +114,27 @@ main (int argc, char **argv)
   pool = g_thread_pool_new (load_image, NULL, 20, FALSE, NULL);
 
   l = 0;
-  i = start;
-  while (1) {
-    i++;
-    if (i == argc)
-      {
-        i = start;
-        l++;
-      }
-    g_thread_pool_push (pool, argv[i], NULL);
-    if (verbose) g_print ("now %d items pending\n", g_thread_pool_unprocessed (pool));
-    if (l == iterations)
-      break;
-  }
+
+  for (i = start; i < argc; i++)
+    g_ptr_array_add (files, argv[i]);
+
+  if (files->len == 0)
+    {
+      const gchar *distdir = g_test_get_dir (G_TEST_DIST);
+      g_ptr_array_add (files, g_build_filename (distdir, "test-images", "valid_jpeg_progressive_test", 
NULL));
+      g_ptr_array_add (files, g_build_filename (distdir, "test-images", "valid_png_test", NULL));
+    }
+
+  g_assert_cmpint (files->len, >, 0);
+
+  for (l = 0; l < iterations; l++)
+    {
+      for (i = 0; i < files->len; i++)
+       {
+         g_thread_pool_push (pool, files->pdata[i], NULL);
+         if (verbose) g_print ("now %d items pending\n", g_thread_pool_unprocessed (pool));
+       }
+    }
 
   g_thread_pool_free (pool, FALSE, TRUE);
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]