1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 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
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <locale.h>
#include <cdk/cdk.h>

static void
test_color_parse (void)
{
  CdkRGBA color;
  CdkRGBA expected;
  gboolean res;

  res = cdk_rgba_parse (&color, "foo");
  g_assert (!res);

  res = cdk_rgba_parse (&color, "");
  g_assert (!res);

  expected.red = 100/255.;
  expected.green = 90/255.;
  expected.blue = 80/255.;
  expected.alpha = 0.1;
  res = cdk_rgba_parse (&color, "rgba(100,90,80,0.1)");
  g_assert (res);
  g_assert (cdk_rgba_equal (&color, &expected));

  expected.red = 0.4;
  expected.green = 0.3;
  expected.blue = 0.2;
  expected.alpha = 0.1;
  res = cdk_rgba_parse (&color, "rgba(40%,30%,20%,0.1)");
  g_assert (res);
  g_assert (cdk_rgba_equal (&color, &expected));

  res = cdk_rgba_parse (&color, "rgba(  40 % ,  30 %  ,   20 % ,  0.1    )");
  g_assert (res);
  g_assert (cdk_rgba_equal (&color, &expected));

  expected.red = 1.0;
  expected.green = 0.0;
  expected.blue = 0.0;
  expected.alpha = 1.0;
  res = cdk_rgba_parse (&color, "red");
  g_assert (res);
  g_assert (cdk_rgba_equal (&color, &expected));

  expected.red = 0.0;
  expected.green = 0x8080 / 65535.;
  expected.blue = 1.0;
  expected.alpha = 1.0;
  res = cdk_rgba_parse (&color, "#0080ff");
  g_assert (res);
  g_assert (cdk_rgba_equal (&color, &expected));

  expected.red = 0.0;
  expected.green = 0.0;
  expected.blue = 0.0;
  expected.alpha = 1.0;
  res = cdk_rgba_parse (&color, "rgb(0,0,0)");
  g_assert (res);
  g_assert (cdk_rgba_equal (&color, &expected));
}

static void
test_color_to_string (void)
{
  CdkRGBA rgba;
  CdkRGBA out;
  gchar *res;
  gchar *res_de;
  gchar *res_en;
  gchar *orig;

  /* Using /255. values for the r, g, b components should
   * make sure they round-trip exactly without rounding
   * from the double => integer => double conversions */
  rgba.red = 1.0;
  rgba.green = 128/255.;
  rgba.blue = 64/255.;
  rgba.alpha = 0.5;

  orig = g_strdup (setlocale (LC_ALL, NULL));
  res = cdk_rgba_to_string (&rgba);
  cdk_rgba_parse (&out, res);
  g_assert (cdk_rgba_equal (&rgba, &out));

  setlocale (LC_ALL, "de_DE.utf-8");
  res_de = cdk_rgba_to_string (&rgba);
  g_assert_cmpstr (res, ==, res_de);<--- syntax error

  setlocale (LC_ALL, "en_US.utf-8");
  res_en = cdk_rgba_to_string (&rgba);
  g_assert_cmpstr (res, ==, res_en);

  g_free (res);
  g_free (res_de);
  g_free (res_en);

  setlocale (LC_ALL, orig);
  g_free (orig);
}

static void
test_color_copy (void)
{
  CdkRGBA rgba;
  CdkRGBA *out;

  rgba.red = 0.0;
  rgba.green = 0.1;
  rgba.blue = 0.6;
  rgba.alpha = 0.9;

  out = cdk_rgba_copy (&rgba);
  g_assert (cdk_rgba_equal (&rgba, out));

  cdk_rgba_free (out);
}

static void
test_color_parse_nonsense (void)
{
  CdkRGBA color;
  gboolean res;

  g_test_bug ("667485");

  res = cdk_rgba_parse (&color, "rgb(,,)");
  g_assert (!res);

  res = cdk_rgba_parse (&color, "rgb(%,%,%)");
  g_assert (!res);

  res = cdk_rgba_parse (&color, "rgb(nan,nan,nan)");
  g_assert (!res);

  res = cdk_rgba_parse (&color, "rgb(inf,inf,inf)");
  g_assert (!res);

  res = cdk_rgba_parse (&color, "rgb(1p12,0,0)");
  g_assert (!res);

  res = cdk_rgba_parse (&color, "rgb(5d1%,1,1)");
  g_assert (!res);

  res = cdk_rgba_parse (&color, "rgb(0,0,0)moo");
  g_assert (!res);

  res = cdk_rgba_parse (&color, "rgb(0,0,0)  moo");
  g_assert (!res);
}

int
main (int argc, char *argv[])
{
        g_test_init (&argc, &argv, NULL);

        g_test_bug_base ("http://bugzilla.gnome.org");

        g_test_add_func ("/rgba/parse", test_color_parse);
        g_test_add_func ("/rgba/parse/nonsense", test_color_parse_nonsense);
        g_test_add_func ("/rgba/to-string", test_color_to_string);
        g_test_add_func ("/rgba/copy", test_color_copy);

        return g_test_run ();
}