[gegl/gegl-color-gamma: 2/3] tests: Add unit tests for GeglColor parsing



commit c24c2829642328b25a0f3e47d785c3fddc6d40b3
Author: Jon Nordby <jononor gmail com>
Date:   Thu Aug 7 14:22:52 2014 +0200

    tests: Add unit tests for GeglColor parsing
    
    Currently failing as named and RGB hex does not interpret values as gamma-corrected.

 tests/simple/test-gegl-color.c |   94 +++++++++++++++++++++++++++++++++++----
 1 files changed, 84 insertions(+), 10 deletions(-)
---
diff --git a/tests/simple/test-gegl-color.c b/tests/simple/test-gegl-color.c
index cbafc71..4c5bdbe 100644
--- a/tests/simple/test-gegl-color.c
+++ b/tests/simple/test-gegl-color.c
@@ -15,11 +15,44 @@
  * Copyright (C) 2014 Jon Nordby <jononor gmail com>
  */
 
-/* Test GeglColor */
 
 #include <glib.h>
 #include <gegl.h>
+#include <math.h>
+#include <stdlib.h>
 
+/* Common */
+
+#define COLOR_FMT "%.3f"
+static const gdouble color_diff_threshold = 0.001;
+
+static void
+assert_compare_rgba(const gdouble actual[4], const gdouble expect[4])
+{
+    gdouble diff[4];
+
+    gboolean colors_equal = TRUE;
+    for (int i=0; i<4; i++) {
+        diff[i] = fabs(actual[0] - expect[0]);
+        if (diff[i] > color_diff_threshold) {
+            colors_equal = FALSE;
+        }
+    }
+
+    if (!colors_equal) {
+        gchar str[100];
+        g_snprintf(str, 100,
+                "GeglColor("COLOR_FMT","COLOR_FMT","COLOR_FMT","COLOR_FMT") != " \
+                "GeglColor("COLOR_FMT","COLOR_FMT","COLOR_FMT","COLOR_FMT")",
+                actual[0], actual[1], actual[2], actual[3],
+                expect[0], expect[1], expect[2], expect[3]
+        );
+        g_error(str);
+    }
+    g_assert(colors_equal);
+}
+
+/* Test GeglColor set_rgba/get_rgba roundtrip */
 #define TP_ROUNDTRIP "/GeglColor/rgba_set_get/"
 
 typedef struct _TestRgbaSetGetData {
@@ -34,20 +67,55 @@ static TestRgbaSetGetData roundtrip_cases[] = {
 };
 
 static void
-test_rgba_setget_roundtrip(const TestRgbaSetGetData *data)
+test_rgba_setget_roundtrip(const TestRgbaSetGetData *d)
 {
-    GeglColor *color = gegl_color_new(NULL);
     gdouble out[4] = { -1, -1, -1, -1 };
-    gdouble in[4] = { data->rgba[0], data->rgba[1], data->rgba[2], data->rgba[3] } ;
 
-    gegl_color_set_rgba(color, in[0], in[1], in[2], in[3]);
+    GeglColor *color = gegl_color_new(NULL);
+    gegl_color_set_rgba(color, d->rgba[0], d->rgba[1], d->rgba[2], d->rgba[3]);
     gegl_color_get_rgba(color, &out[0], &out[1], &out[2], &out[3]);
+    assert_compare_rgba(out, d->rgba);
+    g_object_unref(color);
+}
 
-    g_assert_cmpfloat((float)in[0], ==, (float)out[0]);
-    g_assert_cmpfloat((float)in[1], ==, (float)out[1]);
-    g_assert_cmpfloat((float)in[2], ==, (float)out[2]);
-    g_assert_cmpfloat((float)in[3], ==, (float)out[3]);
+/* Test GeglColor parsing of color strings */
+#define TP_PARSE "/GeglColor/parse/"
 
+typedef struct _TestParseString {
+    gchar *casename;
+    gchar *input;
+    gdouble expected[4];
+} TestParseString;
+
+static TestParseString parse_cases[] = {
+    /* cannot be tested because gegl_color_set_from_string causes g_warning 
+    { TP_PARSE"invalid", "s22003",  { 0.f, 1.f, 1.f, 0.67f } }, */
+
+    { TP_PARSE"named_white", "white",  { 1.f, 1.f, 1.f, 1.f } },
+    { TP_PARSE"named_black", "black",  { 0.f, 0.f, 0.f, 1.f } },
+    { TP_PARSE"named_olive", "olive",  { 0.21586f, 0.21586f, 0.f, 1.f } },
+
+    { TP_PARSE"rgb_hex6_black", "#000000",  { 0.f, 0.f, 0.f, 1.f } },
+    { TP_PARSE"rgb_hex6_white_lowercase", "#ffffff",  { 1.f, 1.f, 1.f, 1.f } },
+    { TP_PARSE"rgb_hex6_white_uppercase", "#FFFFFF",  { 1.f, 1.f, 1.f, 1.f } },
+    { TP_PARSE"rgb_hex8_olive_semitrans", "#808000aa",  { 0.21586f, 0.2195f, 0.f, 0.6666f } },
+    { TP_PARSE"rgb_hex3_", "#333",  { 0.0331f, 0.0331f, 0.0331f, 1.f } },
+    { TP_PARSE"rgb_hex4_", "#ccce",  { 0.6038f, 0.6038f, 0.6038f, 0.8549f } },
+
+    // Note: not CSS compatible!
+    { TP_PARSE"rgb_integer_black", "rgb(0,0,0)",  { 0.f, 0.f, 0.f, 1.f } },
+    { TP_PARSE"rgb_float_white", "rgb(1.0, 1.0, 1.0)",  { 1.f, 1.f, 1.f, 1.f } },
+    { TP_PARSE"rgba_middle_semitrans", "rgba(0.660, 0.330, 0.200, 0.4)",  { 0.660f, 0.330f, 0.200f, 0.4f } }
+};
+
+static void
+test_parse_string(const TestParseString *data)
+{
+    gdouble out[4] = { -1, -1, -1, -1 };
+
+    GeglColor *color = gegl_color_new(data->input);
+    gegl_color_get_rgba(color, &out[0], &out[1], &out[2], &out[3]);
+    assert_compare_rgba(out, data->expected);
     g_object_unref(color);
 }
 
@@ -62,10 +130,16 @@ main (int argc, char *argv[])
 
     // set/get roundtrip tests
     for (int i = 0; i < G_N_ELEMENTS(roundtrip_cases); i++) {
-        TestRgbaSetGetData *data = &roundtrip_cases[i];
+        const TestRgbaSetGetData *data = &roundtrip_cases[i];
         g_test_add_data_func(data->casename, data, (GTestDataFunc)test_rgba_setget_roundtrip);
     }
 
+    // parsing tests
+    for (int i = 0; i < G_N_ELEMENTS(parse_cases); i++) {
+        const TestParseString *data = &parse_cases[i];
+        g_test_add_data_func(data->casename, data, (GTestDataFunc)test_parse_string);
+    }
+
     result = g_test_run();
     gegl_exit();
     return result;


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