libgimp, libgimpcolor: make real unit test of old (compiled but unused) …

… test-color-parser.c file.

The file libgimpcolor/test-color-parser.c was compiled but never actually called
by the build. Now that we have a nice infrastructure to test libgimp API, I am
moving it there with the new format. Doing this also allowed me to discover some
bugs in CSS parsing, as well as discover Python binding was failing here (cf.
the few previous commits).

Only one test is disabled so far, the one where 4 digits are used per channel in
hexadecimal notation: "#64649595eded". This format simply doesn't appear
anywhere in the spec, and also the result values in the samples listing don't
even fit. So far, I'm just unsure what to do with it, if we want to keep this
support (of some kind of higher precision hex notation, but not specified, so is
it even used by anyone?) or not.

All the other tests just work in both C and Python!
This commit is contained in:
Jehan 2024-04-20 12:39:52 +02:00
parent 106d18605a
commit 62ab8e2604
5 changed files with 269 additions and 136 deletions

View file

@ -1,4 +1,3 @@
libgimpcolor_sources = files(
'gimpadaptivesupersample.c',
'gimpbilinear.c',
@ -44,7 +43,6 @@ libgimpcolor_introspectable = [
libgimpcolor_headers_introspectable,
]
libgimpcolor = library('gimpcolor-' + gimp_api_version,
libgimpcolor_sources,
include_directories: rootInclude,
@ -62,18 +60,3 @@ install_headers(
libgimpcolor_headers,
subdir: gimp_api_name / 'libgimpcolor',
)
# Test program, not installed
executable('test-color-parser',
'test-color-parser.c',
include_directories: rootInclude,
dependencies: [
cairo, gdk_pixbuf, gegl, lcms, math,
babl,
# glib,
],
c_args: '-DG_LOG_DOMAIN="LibGimpColor"',
link_with: [ libgimpbase, libgimpcolor, ],
install: false,
)

View file

@ -1,119 +0,0 @@
/* unit tests for the color parsing routines in gimprgb-parse.c
*/
#include "config.h"
#include <stdlib.h>
#include <babl/babl.h>
#include <gegl.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <glib-object.h>
#include <cairo.h>
#include "gimpcolor.h"
#define DBL(c) ((gdouble)(c) / 255.0)
typedef struct
{
const gchar *str;
gboolean alpha;
gboolean fail;
const gdouble r;
const gdouble g;
const gdouble b;
const gdouble a;
} ColorSample;
static const ColorSample samples[] =
{
/* sample alpha fail red green blue alpha */
{ "#000000", FALSE, FALSE, 0.0, 0.0, 0.0, 0.0 },
{ "#FFff00", FALSE, FALSE, 1.0, 1.0, 0.0, 0.0 },
{ "#6495ed", FALSE, FALSE, DBL(100), DBL(149), DBL(237), 0.0 },
{ "#fff", FALSE, FALSE, 1.0, 1.0, 1.0, 0.0 },
{ "#64649595eded", FALSE, FALSE, 1.0, 1.0, 0.0, 0.0 },
{ "rgb(0,0,0)", FALSE, FALSE, 0.0, 0.0, 0.0, 0.0 },
{ "rgb(100,149,237)", FALSE, FALSE, DBL(100), DBL(149), DBL(237), 0.0 },
{ "rgba(100%,0,100%,0.5)", TRUE, FALSE, 255.0, 0.0, 255.0, 0.5 },
{ "rgba(100%,0,100%,0.5)", FALSE, TRUE, 255.0, 0.0, 255.0, 0.5 },
{ "rgb(100%,149,20%)", FALSE, FALSE, 1.0, DBL(149), 0.2, 0.0 },
{ "rgb(100%,149,20%)", TRUE, TRUE, 1.0, DBL(149), 0.2, 0.0 },
{ "rgb(foobar)", FALSE, TRUE, 0.0, 0.0, 0.0, 0.0 },
{ "rgb(100,149,237", FALSE, TRUE, 0.0, 0.0, 0.0, 0.0 },
{ "rED", FALSE, FALSE, 1.0, 0.0, 0.0, 0.0 },
{ "cornflowerblue", FALSE, FALSE, DBL(100), DBL(149), DBL(237), 0.0 },
{ " red", FALSE, FALSE, 1.0, 0.0, 0.0, 0.0 },
{ "red ", FALSE, FALSE, 1.0, 0.0, 0.0, 0.0 },
{ "red", TRUE, TRUE, 1.0, 0.0, 0.0, 0.0 },
{ "red blue", FALSE, TRUE, 0.0, 0.0, 0.0, 0.0 },
{ "transparent", FALSE, TRUE, 0.0, 0.0, 0.0, 0.0 },
{ "transparent", TRUE, FALSE, 0.0, 0.0, 0.0, 0.0 },
{ "23foobar", FALSE, TRUE, 0.0, 0.0, 0.0, 0.0 },
{ "", FALSE, TRUE, 0.0, 0.0, 0.0, 0.0 }
};
static gint
check_failure (const ColorSample *sample,
gboolean success,
GimpRGB *rgb)
{
if (success && sample->fail)
{
g_print ("Parser succeeded for sample \"%s\" but should have failed!\n"
" parsed color: (%g, %g, %g, %g)\n",
sample->str, rgb->r, rgb->g, rgb->b, rgb->a);
return 1;
}
if (!success && !sample->fail)
{
g_print ("Parser failed for sample \"%s\" but should have succeeded!\n"
" parsed color: (%g, %g, %g, %g)\n",
sample->str, rgb->r, rgb->g, rgb->b, rgb->a);
return 1;
}
return 0;
}
int
main (void)
{
gint failures = 0;
gint i;
g_print ("\nTesting the GIMP color parser ...\n");
for (i = 0; i < G_N_ELEMENTS (samples); i++)
{
GimpRGB rgb = { 0.0, 0.0, 0.0, 0.0 };
gboolean success;
if (samples[i].alpha)
success = gimp_rgba_parse_css (&rgb, samples[i].str, -1);
else
success = gimp_rgb_parse_css (&rgb, samples[i].str, -1);
failures += check_failure (samples + i, success, &rgb);
}
if (failures)
{
g_print ("%d out of %d samples failed!\n\n",
failures, (int)G_N_ELEMENTS (samples));
return EXIT_FAILURE;
}
else
{
g_print ("All %d samples passed.\n\n", (int)G_N_ELEMENTS (samples));
return EXIT_SUCCESS;
}
}