test-color-parser.c 3.77 KB
Newer Older
1 2 3 4 5 6 7
/* unit tests for the color parsing routines in gimprgb-parse.c
 */

#include "config.h"

#include <stdlib.h>

8
#include <babl/babl.h>
9
#include <gegl.h>
10
#include <gdk-pixbuf/gdk-pixbuf.h>
11

Sven Neumann's avatar
Sven Neumann committed
12
#include <glib-object.h>
13
#include <cairo.h>
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

#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;

Sven Neumann's avatar
Sven Neumann committed
32
static const ColorSample samples[] =
33
{
Sven Neumann's avatar
Sven Neumann committed
34 35
  /* sample                  alpha  fail   red       green     blue     alpha */

36 37 38 39 40 41 42 43
  { "#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 },
Sven Neumann's avatar
Sven Neumann committed
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 },
Sven Neumann's avatar
Sven Neumann committed
46 47 48 49 50 51 52 53 54 55 56 57 58
  { "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 }
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
};


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;

Sven Neumann's avatar
Sven Neumann committed
92
  g_print ("\nTesting the GIMP color parser ...\n");
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

  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",
110
               failures, (int)G_N_ELEMENTS (samples));
111 112 113 114
      return EXIT_FAILURE;
    }
  else
    {
115
      g_print ("All %d samples passed.\n\n", (int)G_N_ELEMENTS (samples));
116 117 118 119
      return EXIT_SUCCESS;
    }
}