[PATCH 2/9] Added a test for timezone location to offset mapping

David Shea dshea at redhat.com
Wed Nov 6 22:56:52 UTC 2013


tzmapdata/cc.png defines a mapping between coordinates and time zones by
drawing each timezone in a particular color. This test ensures that
every color in cc.png corresponds to an offset in TimezoneMap.c, and
that every offset in TimezoneMap.c corresponds to a color in cc.png.
---
 .gitignore                                  |   3 +
 widgets/Makefile.am                         |   2 +-
 widgets/configure.ac                        |   4 +-
 widgets/tests/Makefile.am                   |  20 ++++
 widgets/tests/tzmapdata/.gitignore          |   1 +
 widgets/tests/tzmapdata/Makefile.am         |  33 ++++++
 widgets/tests/tzmapdata/test-tzmapdata-cc.c | 155 ++++++++++++++++++++++++++++
 7 files changed, 216 insertions(+), 2 deletions(-)
 create mode 100644 widgets/tests/Makefile.am
 create mode 100644 widgets/tests/tzmapdata/.gitignore
 create mode 100644 widgets/tests/tzmapdata/Makefile.am
 create mode 100644 widgets/tests/tzmapdata/test-tzmapdata-cc.c

diff --git a/.gitignore b/.gitignore
index 8a10aaa..c906ffd 100644
--- a/.gitignore
+++ b/.gitignore
@@ -87,5 +87,8 @@ widgets/doc/html
 widgets/doc/xml
 widgets/gtk-doc.make
 widgets/src/gettext.h
+widgets/tests/*/*.log
+widgets/tests/*/*.trs
+widgets/tests/*/test-suite.log
 anaconda.po
 data/liveinst/gnome/fedora-welcome.po
diff --git a/widgets/Makefile.am b/widgets/Makefile.am
index ad80f87..d018c6e 100644
--- a/widgets/Makefile.am
+++ b/widgets/Makefile.am
@@ -21,7 +21,7 @@
 
 ACLOCAL_AMFLAGS = -I m4
 
-SUBDIRS = src python glade doc data
+SUBDIRS = src python glade doc data tests
 
 MAINTAINERCLEANFILES = Makefile.in aclocal.m4 config.guess config.h.in config.sub \
 	configure depcomp gtk-doc.make install-sh ltmain.sh missing py-compile \
diff --git a/widgets/configure.ac b/widgets/configure.ac
index 19dedb9..a40bbd3 100644
--- a/widgets/configure.ac
+++ b/widgets/configure.ac
@@ -66,5 +66,7 @@ AC_CONFIG_FILES([Makefile
                  src/Makefile
                  python/Makefile
                  data/Makefile
-                 data/tzmapdata/Makefile])
+                 data/tzmapdata/Makefile
+                 tests/Makefile
+                 tests/tzmapdata/Makefile])
 AC_OUTPUT
diff --git a/widgets/tests/Makefile.am b/widgets/tests/Makefile.am
new file mode 100644
index 0000000..d428ef7
--- /dev/null
+++ b/widgets/tests/Makefile.am
@@ -0,0 +1,20 @@
+# Makefile.am for anaconda widgets tests
+#
+# Copyright (C) 2013  Red Hat, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as published
+# by the Free Software Foundation; either version 2.1 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+# Author: David Shea <dshea at redhat.com>
+
+SUBDIRS = tzmapdata
diff --git a/widgets/tests/tzmapdata/.gitignore b/widgets/tests/tzmapdata/.gitignore
new file mode 100644
index 0000000..65e10db
--- /dev/null
+++ b/widgets/tests/tzmapdata/.gitignore
@@ -0,0 +1 @@
+test-tzmapdata-cc
diff --git a/widgets/tests/tzmapdata/Makefile.am b/widgets/tests/tzmapdata/Makefile.am
new file mode 100644
index 0000000..2f05d28
--- /dev/null
+++ b/widgets/tests/tzmapdata/Makefile.am
@@ -0,0 +1,33 @@
+# Makefile.am for anaconda timezone data tests
+#
+# Copyright (C) 2013  Red Hat, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as published
+# by the Free Software Foundation; either version 2.1 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+# Author: David Shea <dshea at redhat.com>
+
+# from widgets/src/Makefile.am
+WIDGETSDATA = '"$(datadir)/anaconda"'
+TZMAPDATA='"tzmapdata"'
+
+check_PROGRAMS = test-tzmapdata-cc
+
+test_tzmapdata_cc_SOURCES = test-tzmapdata-cc.c $(top_srcdir)/src/tz.c
+test_tzmapdata_cc_CFLAGS = $(GTK_CFLAGS) -I$(top_srcdir)/src \
+			   -DWIDGETS_DATADIR=$(WIDGETSDATA) \
+			   -DTZMAP_DATADIR=$(TZMAPDATA)
+test_tzmapdata_cc_LDADD = $(GTK_LIBS) -lm
+
+AM_TESTS_ENVIRONMENT = ANACONDA_WIDGETS_DATA=$(top_srcdir)/data; export ANACONDA_WIDGETS_DATA;
+TESTS = test-tzmapdata-cc
diff --git a/widgets/tests/tzmapdata/test-tzmapdata-cc.c b/widgets/tests/tzmapdata/test-tzmapdata-cc.c
new file mode 100644
index 0000000..a855844
--- /dev/null
+++ b/widgets/tests/tzmapdata/test-tzmapdata-cc.c
@@ -0,0 +1,155 @@
+/* Copyright (C) 2013 Red Hat, Inc
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Author: David Shea <dshea at redhat.com>
+ *
+ */
+
+/*
+ * This test ensures that every color in the timezone map color code data (cc.png)
+ * has a matching entry in TimezoneMap.c mapping the color to a UTC offset.
+ */
+
+#include <gtk/gtk.h>
+
+/* Include the TimezoneMap file so we can use the color_codes array */
+#include "TimezoneMap.c"
+
+/* Store an x,y pixbuf coordinate */
+struct coord
+{
+    int x;
+    int y;
+};
+
+static guint int32_hash(gconstpointer v)
+{
+    return (guint) *(const gint32*) v;
+}
+
+static gboolean int32_equal(gconstpointer v1, gconstpointer v2)
+{
+    return *((const gint32*) v1) == *((const gint32*) v2);
+}
+
+int main(void)
+{
+    gchar *file;
+    GdkPixbuf *tzcc_pixbuf;
+    GError *err = NULL;
+    int width, height, rowstride, n_channels;
+    int x, y;
+    guchar *pixels;
+
+    GHashTable *pixel_table;
+    GHashTableIter pixel_iter;
+    guchar *p;
+    gboolean code_found;
+    struct coord *c;
+    int i;
+
+    int status = 0;
+
+    file = g_strdup_printf("%s/" TZMAP_DATADIR "/cc.png", get_widgets_datadir());
+    tzcc_pixbuf = gdk_pixbuf_new_from_file(file, &err);
+    g_free(file);
+    if (err)
+    {
+        fprintf(stderr, "Unable to load timezone color code data: %s\n", err->message);
+        g_clear_error(&err);
+        return 99; /* hard error */
+    }
+
+    /* Sanity checks on the image, copied from the gdk-pixbuf docs */
+    n_channels = gdk_pixbuf_get_n_channels(tzcc_pixbuf);
+    g_assert(gdk_pixbuf_get_colorspace(tzcc_pixbuf ) == GDK_COLORSPACE_RGB);
+    g_assert(gdk_pixbuf_get_bits_per_sample(tzcc_pixbuf) == 8);
+    g_assert(gdk_pixbuf_get_has_alpha(tzcc_pixbuf));
+    g_assert(n_channels == 4);
+
+    width = gdk_pixbuf_get_width(tzcc_pixbuf);
+    height = gdk_pixbuf_get_height(tzcc_pixbuf);
+    rowstride = gdk_pixbuf_get_rowstride(tzcc_pixbuf);
+    pixels = gdk_pixbuf_get_pixels(tzcc_pixbuf);
+
+    /*
+     * Create a GHashTable to store the different pixel values in the color map.
+     * Pixels are returned by gdk-pixbuf as four guchar values, so we can compare
+     * them as 32-bit ints. Annoyingly, GLib only provides a comparison functions
+     * for gint and gint64.
+     */
+    pixel_table = g_hash_table_new_full(int32_hash, int32_equal, NULL, g_free);
+
+    /* Walk the pixels in the colormap and add them to the set */
+    for (x = 0; x < width; x++)
+    {
+        for (y = 0; y < height; y++)
+        {
+            c = g_malloc(sizeof (struct coord));
+            c->x = x;
+            c->y = y;
+
+            g_hash_table_insert(pixel_table, pixels + y * rowstride + x * n_channels, c);
+        }
+    }
+
+    /* Iterate over the set of unique pixels and check that they are in the color map */
+    printf("Searching for colors with no color_code...\n");
+    g_hash_table_iter_init(&pixel_iter, pixel_table);
+    while (g_hash_table_iter_next(&pixel_iter, (void **)&p, (void **)&c))
+    {
+        code_found = FALSE;
+        for (i = 0; color_codes[i].offset != -100; i++)
+        {
+            if (color_codes[i].red == p[0] && color_codes[i].green == p[1] &&
+                    color_codes[i].blue == p[2] && color_codes[i].alpha == p[3])
+            {
+                code_found = TRUE;
+                break;
+            }
+        }
+
+        if (!code_found)
+        {
+            printf("Missing color code for RGBA (%d, %d, %d, %d) at (%d, %d)\n",
+                    p[0], p[1], p[2], p[3], c->x, c->y);
+            status = 1;
+        }
+    }
+
+    /* Iterate over the color codes and check that pixels exist for each */
+    printf("Searching for color codes with no pixel...\n");
+    for (i = 0; color_codes[i].offset != -100; i++)
+    {
+        guchar test_pixel[4];
+        test_pixel[0] = color_codes[i].red;
+        test_pixel[1] = color_codes[i].green;
+        test_pixel[2] = color_codes[i].blue;
+        test_pixel[3] = color_codes[i].alpha;
+        if (NULL == g_hash_table_lookup(pixel_table, test_pixel))
+        {
+            printf("Missing pixel for RGBA (%d, %d, %d, %d) at offset %g\n",
+                    color_codes[i].red, color_codes[i].green, color_codes[i].blue,
+                    color_codes[i].alpha, color_codes[i].offset);
+            status = 1;
+        }
+    }
+
+    g_hash_table_destroy(pixel_table);
+    g_object_unref(tzcc_pixbuf);
+
+    return status;
+}
-- 
1.8.3.1



More information about the anaconda-patches mailing list