From 516c5bf44c2dc1fdf2f75f164148e58a1e62b4fa Mon Sep 17 00:00:00 2001 From: Sven Neumann Date: Thu, 22 Jul 2004 23:30:52 +0000 Subject: [PATCH] libgimpcolor/Makefile.am added a simple unit test framework for the color 2004-07-23 Sven Neumann * libgimpcolor/Makefile.am * libgimpcolor/test-color-parser.c: added a simple unit test framework for the color parser. * libgimpcolor/gimprgb-parse.c: fixed parsing of rgba() values. * libgimpmath/test-md5.c: minor cleanup. --- ChangeLog | 10 +++ libgimpcolor/.cvsignore | 1 + libgimpcolor/Makefile.am | 20 ++++++ libgimpcolor/gimprgb-parse.c | 2 +- libgimpcolor/test-color-parser.c | 101 +++++++++++++++++++++++++++++++ libgimpmath/test-md5.c | 16 +++-- 6 files changed, 144 insertions(+), 6 deletions(-) create mode 100644 libgimpcolor/test-color-parser.c diff --git a/ChangeLog b/ChangeLog index 14568afaad..93dc4756cb 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2004-07-23 Sven Neumann + + * libgimpcolor/Makefile.am + * libgimpcolor/test-color-parser.c: added a simple unit test + framework for the color parser. + + * libgimpcolor/gimprgb-parse.c: fixed parsing of rgba() values. + + * libgimpmath/test-md5.c: minor cleanup. + 2004-07-23 Sven Neumann * libgimpcolor/gimprgb-parse.c (gimp_rgba_parse_css): added support diff --git a/libgimpcolor/.cvsignore b/libgimpcolor/.cvsignore index 0b61e7e470..121acffabf 100644 --- a/libgimpcolor/.cvsignore +++ b/libgimpcolor/.cvsignore @@ -1,6 +1,7 @@ Makefile Makefile.in makefile.mingw +test-color-parser *.lo _libs .libs diff --git a/libgimpcolor/Makefile.am b/libgimpcolor/Makefile.am index 5b798b42c7..75091534da 100644 --- a/libgimpcolor/Makefile.am +++ b/libgimpcolor/Makefile.am @@ -97,6 +97,26 @@ libgimpcolor_2_0_la_DEPENDENCIES = $(gimpcolor_def) $(libgimpbase) libgimpcolor_2_0_la_LIBADD = $(GLIB_LIBS) $(libm) + +# +# test programs, not to be built by default and never installed +# + +TESTS = test-color-parser$(EXEEXT) + +EXTRA_PROGRAMS = test-color-parser + +test_color_parser_DEPENDENCIES = \ + $(top_builddir)/libgimpcolor/libgimpcolor-$(GIMP_API_VERSION).la + +test_color_parser_LDADD = \ + $(GLIB_LIBS) \ + $(test_color_parser_DEPENDENCIES) + + +CLEANFILES = $(EXTRA_PROGRAMS) + + install-data-local: install-ms-lib install-libtool-import-lib uninstall-local: uninstall-ms-lib uninstall-libtool-import-lib diff --git a/libgimpcolor/gimprgb-parse.c b/libgimpcolor/gimprgb-parse.c index ae34b0a097..052205123f 100644 --- a/libgimpcolor/gimprgb-parse.c +++ b/libgimpcolor/gimprgb-parse.c @@ -524,7 +524,7 @@ else if (css[3] == '(') while (*end && *end != ',' && *end != '%' && *end != ')') end++; - if (*end == '%') + if (i == 3 || *end == '%') { values[i] = g_ascii_strtod (css, (gchar **) &end); diff --git a/libgimpcolor/test-color-parser.c b/libgimpcolor/test-color-parser.c new file mode 100644 index 0000000000..86c91a89b3 --- /dev/null +++ b/libgimpcolor/test-color-parser.c @@ -0,0 +1,101 @@ +/* unit tests for the color parsing routines in gimprgb-parse.c + */ + +#include "config.h" + +#include +#include + +#include + +#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[] = +{ + { "#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 }, + { "rgb(100%,149,20%)", FALSE, FALSE, 1.0, DBL(149), 0.2, 0.0 }, + { "red", FALSE, FALSE, 1.0, 0.0, 0.0, 0.0 }, + { "cornflowerblue", FALSE, FALSE, DBL(100), DBL(149), DBL(237), 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 ("Testing the GIMP color parser ...\n\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, G_N_ELEMENTS (samples)); + return EXIT_FAILURE; + } + else + { + g_print ("All %d samples passed.\n\n", G_N_ELEMENTS (samples)); + return EXIT_SUCCESS; + } +} + diff --git a/libgimpmath/test-md5.c b/libgimpmath/test-md5.c index 4582c8f668..7d148d3836 100644 --- a/libgimpmath/test-md5.c +++ b/libgimpmath/test-md5.c @@ -3,12 +3,16 @@ * as given in section A.5 of RFC 1321, reproduced below. */ -#include +#include "config.h" +#include #include +#include + #include "gimpmd5.h" + static const gchar * test[7][2] = { { "", "d41d8cd98f00b204e9800998ecf8427e" }, @@ -41,6 +45,7 @@ main (void) g_snprintf (buf, 3, "%02x", digest[j]); g_print (buf); + if (strncmp (buf, test[i][1] + j*2, 2)) correct = FALSE; } @@ -48,13 +53,14 @@ main (void) if (!correct) { - g_print - ("\nWRONG digest!! Please report to http://bugzilla.gnome.org/\n"); - return 1; + g_print ("\nWRONG digest!! " + "Please report to http://bugzilla.gnome.org/\n"); + return EXIT_FAILURE; } } g_print ("\nLooks good.\n\n"); - return 0; + + return EXIT_SUCCESS; }