summaryrefslogtreecommitdiff
path: root/tests/testcolor.c
blob: 26b4412ffd9ab16877ce6b3e96ad5098229a263e (plain)
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
/* Pango
 * test-color.c: Test program for pango_color_parse()
 *
 * Copyright (C) 2002 Matthias Clasen
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <glib.h>
#include <pango/pango.h>

typedef struct _ColorSpec {
  const gchar *spec;
  gboolean valid;
  guint16 red;
  guint16 green;
  guint16 blue;
  guint16 alpha;
} ColorSpec;

static void
test_one_color (ColorSpec *spec)
{
  PangoColor color;
  gboolean accepted;

  accepted = pango_color_parse (&color, spec->spec);

  if (!spec->valid)
    {
      g_assert_false (accepted);
    }
  else
    {
      g_assert_true (accepted);
      g_assert_cmpuint (color.red, ==, spec->red);
      g_assert_cmpuint (color.green, ==, spec->green);
      g_assert_cmpuint (color.blue, ==, spec->blue);
      g_assert_cmpuint (color.alpha, ==, spec->alpha);
    }
}

ColorSpec specs [] = {
  { "#abc",          1, 0xaaaa, 0xbbbb, 0xcccc, 0xffff },
  { "#aabbcc",       1, 0xaaaa, 0xbbbb, 0xcccc, 0xffff },
  { "#aaabbbccc",    1, 0xaaaa, 0xbbbb, 0xcccc, 0xffff },
  { "#100100100",    1, 0x1001, 0x1001, 0x1001, 0xffff },
  { "#aaaabbbbcccc", 1, 0xaaaa, 0xbbbb, 0xcccc, 0xffff },
  { "#fff",          1, 0xffff, 0xffff, 0xffff, 0xffff },
  { "#ffffff",       1, 0xffff, 0xffff, 0xffff, 0xffff },
  { "#fffffffff",    1, 0xffff, 0xffff, 0xffff, 0xffff },
  { "#ffffffffffff", 1, 0xffff, 0xffff, 0xffff, 0xffff },
  { "#000",          1, 0x0000, 0x0000, 0x0000, 0xffff },
  { "#000000",       1, 0x0000, 0x0000, 0x0000, 0xffff },
  { "#000000000",    1, 0x0000, 0x0000, 0x0000, 0xffff },
  { "#000000000000", 1, 0x0000, 0x0000, 0x0000, 0xffff },
  { "#AAAABBBBCCCC", 1, 0xaaaa, 0xbbbb, 0xcccc, 0xffff },
  { "#aa bb cc ",    0, 0, 0, 0, 0 },
  { "#aa bb ccc",    0, 0, 0, 0, 0 },
  { "#ab",           0, 0, 0, 0, 0 },
  { "#aabb",         1, 0xaaaa, 0xaaaa, 0xbbbb, 0xbbbb },
  { "#aaabb",        0, 0, 0, 0, 0 },
  { "aaabb",         0, 0, 0, 0, 0 },
  { "",              0, 0, 0, 0, 0 },
  { "#",             0, 0, 0, 0, 0 },
  { "##fff",         0, 0, 0, 0, 0 },
  { "#0000ff+",      0, 0, 0, 0, 0 },
  { "#0000f+",       0, 0, 0, 0, 0 },
  { "#0x00x10x2",    0, 0, 0, 0, 0 },
  { "#abcd",         1, 0xaaaa, 0xbbbb, 0xcccc, 0xdddd },
  { "#aabbccdd",     1, 0xaaaa, 0xbbbb, 0xcccc, 0xdddd },
  { "#aaaabbbbccccdddd", 1, 0xaaaa, 0xbbbb, 0xcccc, 0xdddd },
  { NULL,            0, 0, 0, 0, 0 }
};

static void
test_color (void)
{
  ColorSpec *spec;

  for (spec = specs; spec->spec; spec++)
    test_one_color (spec);
}

static void
test_color_copy (void)
{
  PangoColor orig = { 0, 200, 5000, 666 };
  PangoColor *copy;

  copy = pango_color_copy (&orig);

  g_assert_cmpint (orig.red, ==, copy->red);
  g_assert_cmpint (orig.green, ==, copy->green);
  g_assert_cmpint (orig.blue, ==, copy->blue);
  g_assert_cmpint (orig.alpha, ==, copy->alpha);

  pango_color_free (copy);
}

static void
test_color_serialize (void)
{
  PangoColor orig = { 0, 200, 5000, 666 };
  char *string;

  string = pango_color_to_string (&orig);

  g_assert_cmpstr (string, ==, "#000000c81388029a");

  g_free (string);
}

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

  g_test_add_func ("/color/parse", test_color);
  g_test_add_func ("/color/copy", test_color_copy);
  g_test_add_func ("/color/serialize", test_color_serialize);

  return g_test_run ();
}