1 | /* Pango |
2 | * test-color.c: Test program for pango_color_parse() |
3 | * |
4 | * Copyright (C) 2002 Matthias Clasen |
5 | * |
6 | * This library is free software; you can redistribute it and/or |
7 | * modify it under the terms of the GNU Library General Public |
8 | * License as published by the Free Software Foundation; either |
9 | * version 2 of the License, or (at your option) any later version. |
10 | * |
11 | * This library is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | * Library General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU Library General Public |
17 | * License along with this library; if not, write to the |
18 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
19 | * Boston, MA 02111-1307, USA. |
20 | */ |
21 | |
22 | #include <glib.h> |
23 | #include <pango/pango.h> |
24 | |
25 | typedef struct _ColorSpec { |
26 | const gchar *spec; |
27 | gboolean valid; |
28 | int color_or_alpha; |
29 | guint16 red; |
30 | guint16 green; |
31 | guint16 blue; |
32 | guint16 alpha; |
33 | } ColorSpec; |
34 | |
35 | #define COLOR 1 |
36 | #define ALPHA 2 |
37 | #define BOTH 3 |
38 | |
39 | static void |
40 | test_one_color (ColorSpec *spec) |
41 | { |
42 | PangoColor color; |
43 | gboolean accepted; |
44 | guint16 alpha; |
45 | |
46 | if (spec->color_or_alpha & COLOR) |
47 | { |
48 | accepted = pango_color_parse (color: &color, spec: spec->spec); |
49 | |
50 | if (!spec->valid) |
51 | { |
52 | g_assert_false (accepted); |
53 | } |
54 | else |
55 | { |
56 | g_assert_true (accepted); |
57 | g_assert_cmpuint (color.red, ==, spec->red); |
58 | g_assert_cmpuint (color.green, ==, spec->green); |
59 | g_assert_cmpuint (color.blue, ==, spec->blue); |
60 | } |
61 | } |
62 | |
63 | if (spec->color_or_alpha & ALPHA) |
64 | { |
65 | accepted = pango_color_parse_with_alpha (color: &color, alpha: &alpha, spec: spec->spec); |
66 | |
67 | if (!spec->valid) |
68 | { |
69 | g_assert_false (accepted); |
70 | } |
71 | else |
72 | { |
73 | g_assert_true (accepted); |
74 | g_assert_cmpuint (color.red, ==, spec->red); |
75 | g_assert_cmpuint (color.green, ==, spec->green); |
76 | g_assert_cmpuint (color.blue, ==, spec->blue); |
77 | g_assert_cmpuint (alpha, ==, spec->alpha); |
78 | } |
79 | } |
80 | } |
81 | |
82 | ColorSpec specs [] = { |
83 | { "#abc" , 1, BOTH, 0xaaaa, 0xbbbb, 0xcccc, 0xffff }, |
84 | { "#aabbcc" , 1, BOTH, 0xaaaa, 0xbbbb, 0xcccc, 0xffff }, |
85 | { "#aaabbbccc" , 1, BOTH, 0xaaaa, 0xbbbb, 0xcccc, 0xffff }, |
86 | { "#100100100" , 1, BOTH, 0x1001, 0x1001, 0x1001, 0xffff }, |
87 | { "#aaaabbbbcccc" , 1, COLOR, 0xaaaa, 0xbbbb, 0xcccc, 0xffff }, |
88 | { "#fff" , 1, BOTH, 0xffff, 0xffff, 0xffff, 0xffff }, |
89 | { "#ffffff" , 1, BOTH, 0xffff, 0xffff, 0xffff, 0xffff }, |
90 | { "#fffffffff" , 1, BOTH, 0xffff, 0xffff, 0xffff, 0xffff }, |
91 | { "#ffffffffffff" , 1, COLOR, 0xffff, 0xffff, 0xffff, 0xffff }, |
92 | { "#000" , 1, BOTH, 0x0000, 0x0000, 0x0000, 0xffff }, |
93 | { "#000000" , 1, BOTH, 0x0000, 0x0000, 0x0000, 0xffff }, |
94 | { "#000000000" , 1, BOTH, 0x0000, 0x0000, 0x0000, 0xffff }, |
95 | { "#000000000000" , 1, COLOR, 0x0000, 0x0000, 0x0000, 0xffff }, |
96 | { "#AAAABBBBCCCC" , 1, COLOR, 0xaaaa, 0xbbbb, 0xcccc, 0xffff }, |
97 | { "#aa bb cc " , 0, BOTH, 0, 0, 0, 0 }, |
98 | { "#aa bb ccc" , 0, BOTH, 0, 0, 0, 0 }, |
99 | { "#ab" , 0, BOTH, 0, 0, 0, 0 }, |
100 | { "#aabb" , 0, COLOR, 0, 0, 0, 0 }, |
101 | { "#aaabb" , 0, BOTH, 0, 0, 0, 0 }, |
102 | { "aaabb" , 0, BOTH, 0, 0, 0, 0 }, |
103 | { "" , 0, BOTH, 0, 0, 0, 0 }, |
104 | { "#" , 0, BOTH, 0, 0, 0, 0 }, |
105 | { "##fff" , 0, BOTH, 0, 0, 0, 0 }, |
106 | { "#0000ff+" , 0, BOTH, 0, 0, 0, 0 }, |
107 | { "#0000f+" , 0, BOTH, 0, 0, 0, 0 }, |
108 | { "#0x00x10x2" , 0, BOTH, 0, 0, 0, 0 }, |
109 | { "#abcd" , 1, ALPHA, 0xaaaa, 0xbbbb, 0xcccc, 0xdddd }, |
110 | { "#aabbccdd" , 1, ALPHA, 0xaaaa, 0xbbbb, 0xcccc, 0xdddd }, |
111 | { "#aaaabbbbccccdddd" , |
112 | 1, ALPHA, 0xaaaa, 0xbbbb, 0xcccc, 0xdddd }, |
113 | { NULL, 0, BOTH, 0, 0, 0, 0 } |
114 | }; |
115 | |
116 | static void |
117 | test_color (void) |
118 | { |
119 | ColorSpec *spec; |
120 | |
121 | for (spec = specs; spec->spec; spec++) |
122 | test_one_color (spec); |
123 | } |
124 | |
125 | static void |
126 | test_color_copy (void) |
127 | { |
128 | PangoColor orig = { 0, 200, 5000 }; |
129 | PangoColor *copy; |
130 | |
131 | copy = pango_color_copy (src: &orig); |
132 | |
133 | g_assert_cmpint (orig.red, ==, copy->red); |
134 | g_assert_cmpint (orig.green, ==, copy->green); |
135 | g_assert_cmpint (orig.blue, ==, copy->blue); |
136 | |
137 | pango_color_free (color: copy); |
138 | } |
139 | |
140 | static void |
141 | test_color_serialize (void) |
142 | { |
143 | PangoColor orig = { 0, 200, 5000 }; |
144 | char *string; |
145 | |
146 | string = pango_color_to_string (color: &orig); |
147 | |
148 | g_assert_cmpstr (string, ==, "#000000c81388" ); |
149 | |
150 | g_free (mem: string); |
151 | } |
152 | |
153 | int |
154 | main (int argc, char *argv[]) |
155 | { |
156 | g_test_init (argc: &argc, argv: &argv, NULL); |
157 | |
158 | g_test_add_func (testpath: "/color/parse" , test_func: test_color); |
159 | g_test_add_func (testpath: "/color/copy" , test_func: test_color_copy); |
160 | g_test_add_func (testpath: "/color/serialize" , test_func: test_color_serialize); |
161 | |
162 | return g_test_run (); |
163 | } |
164 | |