app: s/sprintf/g_snprintf/ in xcf_save_image()
[gimp.git] / libgimpcolor / test-color-parser.c
blob4ce7c50927b0fd64a9585b286faa445284acb2fd
1 /* unit tests for the color parsing routines in gimprgb-parse.c
2 */
4 #include "config.h"
6 #include <stdlib.h>
8 #include <babl/babl.h>
9 #include <gegl.h>
10 #include <gdk-pixbuf/gdk-pixbuf.h>
12 #include <glib-object.h>
13 #include <cairo.h>
15 #include "gimpcolor.h"
18 #define DBL(c) ((gdouble)(c) / 255.0)
21 typedef struct
23 const gchar *str;
24 gboolean alpha;
25 gboolean fail;
26 const gdouble r;
27 const gdouble g;
28 const gdouble b;
29 const gdouble a;
30 } ColorSample;
32 static const ColorSample samples[] =
34 /* sample alpha fail red green blue alpha */
36 { "#000000", FALSE, FALSE, 0.0, 0.0, 0.0, 0.0 },
37 { "#FFff00", FALSE, FALSE, 1.0, 1.0, 0.0, 0.0 },
38 { "#6495ed", FALSE, FALSE, DBL(100), DBL(149), DBL(237), 0.0 },
39 { "#fff", FALSE, FALSE, 1.0, 1.0, 1.0, 0.0 },
40 { "#64649595eded", FALSE, FALSE, 1.0, 1.0, 0.0, 0.0 },
41 { "rgb(0,0,0)", FALSE, FALSE, 0.0, 0.0, 0.0, 0.0 },
42 { "rgb(100,149,237)", FALSE, FALSE, DBL(100), DBL(149), DBL(237), 0.0 },
43 { "rgba(100%,0,100%,0.5)", TRUE, FALSE, 255.0, 0.0, 255.0, 0.5 },
44 { "rgba(100%,0,100%,0.5)", FALSE, TRUE, 255.0, 0.0, 255.0, 0.5 },
45 { "rgb(100%,149,20%)", FALSE, FALSE, 1.0, DBL(149), 0.2, 0.0 },
46 { "rgb(100%,149,20%)", TRUE, TRUE, 1.0, DBL(149), 0.2, 0.0 },
47 { "rgb(foobar)", FALSE, TRUE, 0.0, 0.0, 0.0, 0.0 },
48 { "rgb(100,149,237", FALSE, TRUE, 0.0, 0.0, 0.0, 0.0 },
49 { "rED", FALSE, FALSE, 1.0, 0.0, 0.0, 0.0 },
50 { "cornflowerblue", FALSE, FALSE, DBL(100), DBL(149), DBL(237), 0.0 },
51 { " red", FALSE, FALSE, 1.0, 0.0, 0.0, 0.0 },
52 { "red ", FALSE, FALSE, 1.0, 0.0, 0.0, 0.0 },
53 { "red", TRUE, TRUE, 1.0, 0.0, 0.0, 0.0 },
54 { "red blue", FALSE, TRUE, 0.0, 0.0, 0.0, 0.0 },
55 { "transparent", FALSE, TRUE, 0.0, 0.0, 0.0, 0.0 },
56 { "transparent", TRUE, FALSE, 0.0, 0.0, 0.0, 0.0 },
57 { "23foobar", FALSE, TRUE, 0.0, 0.0, 0.0, 0.0 },
58 { "", FALSE, TRUE, 0.0, 0.0, 0.0, 0.0 }
62 static gint
63 check_failure (const ColorSample *sample,
64 gboolean success,
65 GimpRGB *rgb)
67 if (success && sample->fail)
69 g_print ("Parser succeeded for sample \"%s\" but should have failed!\n"
70 " parsed color: (%g, %g, %g, %g)\n",
71 sample->str, rgb->r, rgb->g, rgb->b, rgb->a);
72 return 1;
75 if (!success && !sample->fail)
77 g_print ("Parser failed for sample \"%s\" but should have succeeded!\n"
78 " parsed color: (%g, %g, %g, %g)\n",
79 sample->str, rgb->r, rgb->g, rgb->b, rgb->a);
80 return 1;
83 return 0;
86 int
87 main (void)
89 gint failures = 0;
90 gint i;
92 g_print ("\nTesting the GIMP color parser ...\n");
94 for (i = 0; i < G_N_ELEMENTS (samples); i++)
96 GimpRGB rgb = { 0.0, 0.0, 0.0, 0.0 };
97 gboolean success;
99 if (samples[i].alpha)
100 success = gimp_rgba_parse_css (&rgb, samples[i].str, -1);
101 else
102 success = gimp_rgb_parse_css (&rgb, samples[i].str, -1);
104 failures += check_failure (samples + i, success, &rgb);
107 if (failures)
109 g_print ("%d out of %d samples failed!\n\n",
110 failures, (int)G_N_ELEMENTS (samples));
111 return EXIT_FAILURE;
113 else
115 g_print ("All %d samples passed.\n\n", (int)G_N_ELEMENTS (samples));
116 return EXIT_SUCCESS;