]> Pileus Git - ~andy/gtk/blob - gdk/tests/rgba.c
gdk_frame_clock_get_frame_time(): use gint64 for time
[~andy/gtk] / gdk / tests / rgba.c
1 #include <locale.h>
2 #include <gdk/gdk.h>
3
4 static void
5 test_color_parse (void)
6 {
7   GdkRGBA color;
8   GdkRGBA expected;
9   gboolean res;
10
11   res = gdk_rgba_parse (&color, "foo");
12   g_assert (!res);
13
14   res = gdk_rgba_parse (&color, "");
15   g_assert (!res);
16
17   expected.red = 100/255.;
18   expected.green = 90/255.;
19   expected.blue = 80/255.;
20   expected.alpha = 0.1;
21   res = gdk_rgba_parse (&color, "rgba(100,90,80,0.1)");
22   g_assert (res);
23   g_assert (gdk_rgba_equal (&color, &expected));
24
25   expected.red = 0.4;
26   expected.green = 0.3;
27   expected.blue = 0.2;
28   expected.alpha = 0.1;
29   res = gdk_rgba_parse (&color, "rgba(40%,30%,20%,0.1)");
30   g_assert (res);
31   g_assert (gdk_rgba_equal (&color, &expected));
32
33   res = gdk_rgba_parse (&color, "rgba(  40 % ,  30 %  ,   20 % ,  0.1    )");
34   g_assert (res);
35   g_assert (gdk_rgba_equal (&color, &expected));
36
37   expected.red = 1.0;
38   expected.green = 0.0;
39   expected.blue = 0.0;
40   expected.alpha = 1.0;
41   res = gdk_rgba_parse (&color, "red");
42   g_assert (res);
43   g_assert (gdk_rgba_equal (&color, &expected));
44
45   expected.red = 0.0;
46   expected.green = 0x8080 / 65535.;
47   expected.blue = 1.0;
48   expected.alpha = 1.0;
49   res = gdk_rgba_parse (&color, "#0080ff");
50   g_assert (res);
51   g_assert (gdk_rgba_equal (&color, &expected));
52
53   expected.red = 0.0;
54   expected.green = 0.0;
55   expected.blue = 0.0;
56   expected.alpha = 1.0;
57   res = gdk_rgba_parse (&color, "rgb(0,0,0)");
58   g_assert (res);
59   g_assert (gdk_rgba_equal (&color, &expected));
60 }
61
62 static void
63 test_color_to_string (void)
64 {
65   GdkRGBA rgba;
66   GdkRGBA out;
67   gchar *res;
68   gchar *res_de;
69   gchar *res_en;
70   gchar *orig;
71
72   /* Using /255. values for the r, g, b components should
73    * make sure they round-trip exactly without rounding
74    * from the double => integer => double conversions */
75   rgba.red = 1.0;
76   rgba.green = 128/255.;
77   rgba.blue = 64/255.;
78   rgba.alpha = 0.5;
79
80   orig = g_strdup (setlocale (LC_ALL, NULL));
81   res = gdk_rgba_to_string (&rgba);
82   gdk_rgba_parse (&out, res);
83   g_assert (gdk_rgba_equal (&rgba, &out));
84
85   setlocale (LC_ALL, "de_DE.utf-8");
86   res_de = gdk_rgba_to_string (&rgba);
87   g_assert_cmpstr (res, ==, res_de);
88
89   setlocale (LC_ALL, "en_US.utf-8");
90   res_en = gdk_rgba_to_string (&rgba);
91   g_assert_cmpstr (res, ==, res_en);
92
93   g_free (res);
94   g_free (res_de);
95   g_free (res_en);
96
97   setlocale (LC_ALL, orig);
98   g_free (orig);
99 }
100
101 static void
102 test_color_copy (void)
103 {
104   GdkRGBA rgba;
105   GdkRGBA *out;
106
107   rgba.red = 0.0;
108   rgba.green = 0.1;
109   rgba.blue = 0.6;
110   rgba.alpha = 0.9;
111
112   out = gdk_rgba_copy (&rgba);
113   g_assert (gdk_rgba_equal (&rgba, out));
114
115   gdk_rgba_free (out);
116 }
117
118 static void
119 test_color_parse_nonsense (void)
120 {
121   GdkRGBA color;
122   gboolean res;
123
124   g_test_bug ("667485");
125
126   res = gdk_rgba_parse (&color, "rgb(,,)");
127   g_assert (!res);
128
129   res = gdk_rgba_parse (&color, "rgb(%,%,%)");
130   g_assert (!res);
131
132   res = gdk_rgba_parse (&color, "rgb(nan,nan,nan)");
133   g_assert (!res);
134
135   res = gdk_rgba_parse (&color, "rgb(inf,inf,inf)");
136   g_assert (!res);
137
138   res = gdk_rgba_parse (&color, "rgb(1p12,0,0)");
139   g_assert (!res);
140
141   res = gdk_rgba_parse (&color, "rgb(5d1%,1,1)");
142   g_assert (!res);
143
144   res = gdk_rgba_parse (&color, "rgb(0,0,0)moo");
145   g_assert (!res);
146
147   res = gdk_rgba_parse (&color, "rgb(0,0,0)  moo");
148   g_assert (!res);
149 }
150
151 int
152 main (int argc, char *argv[])
153 {
154         g_test_init (&argc, &argv, NULL);
155
156         g_test_bug_base ("http://bugzilla.gnome.org");
157
158         g_test_add_func ("/rgba/parse", test_color_parse);
159         g_test_add_func ("/rgba/parse/nonsense", test_color_parse_nonsense);
160         g_test_add_func ("/rgba/to-string", test_color_to_string);
161         g_test_add_func ("/rgba/copy", test_color_copy);
162
163         return g_test_run ();
164 }