1 #include <glib-object.h>
4 test_param_value (void)
9 g_value_init (&value, G_TYPE_PARAM);
10 g_assert (G_VALUE_HOLDS_PARAM (&value));
12 p = g_param_spec_int ("my-int", "My Int", "Blurb", 0, 20, 10, G_PARAM_READWRITE);
14 g_value_take_param (&value, p);
15 p2 = g_value_get_param (&value);
18 p2 = g_value_dup_param (&value);
19 g_assert (p2 == p); /* param specs use ref/unref for copy/free */
20 g_param_spec_unref (p2);
22 g_value_unset (&value);
25 static gint destroy_count;
28 my_destroy (gpointer data)
34 test_param_qdata (void)
40 q = g_quark_from_string ("bla");
42 p = g_param_spec_int ("my-int", "My Int", "Blurb", 0, 20, 10, G_PARAM_READWRITE);
43 g_param_spec_set_qdata (p, q, "bla");
44 bla = g_param_spec_get_qdata (p, q);
45 g_assert_cmpstr (bla, ==, "bla");
47 g_assert_cmpint (destroy_count, ==, 0);
48 g_param_spec_set_qdata_full (p, q, "bla", my_destroy);
49 g_param_spec_set_qdata_full (p, q, "blabla", my_destroy);
50 g_assert_cmpint (destroy_count, ==, 1);
51 g_assert_cmpstr (g_param_spec_steal_qdata (p, q), ==, "blabla");
52 g_assert_cmpint (destroy_count, ==, 1);
53 g_assert (g_param_spec_get_qdata (p, q) == NULL);
55 g_param_spec_ref_sink (p);
57 g_param_spec_unref (p);
61 test_param_validate (void)
64 GValue value = { 0, };
66 p = g_param_spec_int ("my-int", "My Int", "Blurb", 0, 20, 10, G_PARAM_READWRITE);
68 g_value_init (&value, G_TYPE_INT);
69 g_value_set_int (&value, 100);
70 g_assert (!g_param_value_defaults (p, &value));
71 g_assert (g_param_value_validate (p, &value));
72 g_assert_cmpint (g_value_get_int (&value), ==, 20);
74 g_param_value_set_default (p, &value);
75 g_assert (g_param_value_defaults (p, &value));
76 g_assert_cmpint (g_value_get_int (&value), ==, 10);
78 g_param_spec_unref (p);
82 test_param_strings (void)
86 /* test canonicalization */
87 p = g_param_spec_int ("my_int:bla", "My Int", "Blurb", 0, 20, 10, G_PARAM_READWRITE);
89 g_assert_cmpstr (g_param_spec_get_name (p), ==, "my-int-bla");
90 g_assert_cmpstr (g_param_spec_get_nick (p), ==, "My Int");
91 g_assert_cmpstr (g_param_spec_get_blurb (p), ==, "Blurb");
93 g_param_spec_unref (p);
95 /* test nick defaults to name */
96 p = g_param_spec_int ("my-int", NULL, NULL, 0, 20, 10, G_PARAM_READWRITE);
98 g_assert_cmpstr (g_param_spec_get_name (p), ==, "my-int");
99 g_assert_cmpstr (g_param_spec_get_nick (p), ==, "my-int");
100 g_assert (g_param_spec_get_blurb (p) == NULL);
102 g_param_spec_unref (p);
106 test_param_convert (void)
112 p = g_param_spec_int ("my-int", "My Int", "Blurb", 0, 20, 10, G_PARAM_READWRITE);
113 g_value_init (&v1, G_TYPE_UINT);
114 g_value_set_uint (&v1, 43);
116 g_value_init (&v2, G_TYPE_INT);
117 g_value_set_int (&v2, -4);
119 g_assert (!g_param_value_convert (p, &v1, &v2, TRUE));
120 g_assert_cmpint (g_value_get_int (&v2), ==, -4);
122 g_assert (g_param_value_convert (p, &v1, &v2, FALSE));
123 g_assert_cmpint (g_value_get_int (&v2), ==, 20);
125 g_param_spec_unref (p);
129 test_value_transform (void)
132 GValue dest = { 0, };
134 #define CHECK_INT_CONVERSION(type, getter, value) \
135 g_assert (g_value_type_transformable (G_TYPE_INT, type)); \
136 g_value_init (&src, G_TYPE_INT); \
137 g_value_init (&dest, type); \
138 g_value_set_int (&src, value); \
139 g_assert (g_value_transform (&src, &dest)); \
140 g_assert_cmpint (g_value_get_##getter (&dest), ==, value); \
141 g_value_unset (&src); \
142 g_value_unset (&dest);
144 CHECK_INT_CONVERSION(G_TYPE_CHAR, char, -124)
145 CHECK_INT_CONVERSION(G_TYPE_CHAR, char, 124)
146 CHECK_INT_CONVERSION(G_TYPE_UCHAR, uchar, 0)
147 CHECK_INT_CONVERSION(G_TYPE_UCHAR, uchar, 255)
148 CHECK_INT_CONVERSION(G_TYPE_INT, int, -12345)
149 CHECK_INT_CONVERSION(G_TYPE_INT, int, 12345)
150 CHECK_INT_CONVERSION(G_TYPE_UINT, uint, 0)
151 CHECK_INT_CONVERSION(G_TYPE_UINT, uint, 12345)
152 CHECK_INT_CONVERSION(G_TYPE_LONG, long, -12345678)
153 CHECK_INT_CONVERSION(G_TYPE_ULONG, ulong, 12345678)
154 CHECK_INT_CONVERSION(G_TYPE_INT64, int64, -12345678)
155 CHECK_INT_CONVERSION(G_TYPE_UINT64, uint64, 12345678)
156 CHECK_INT_CONVERSION(G_TYPE_FLOAT, float, 12345678)
157 CHECK_INT_CONVERSION(G_TYPE_DOUBLE, double, 12345678)
159 #define CHECK_BOOLEAN_CONVERSION(type, setter, value) \
160 g_assert (g_value_type_transformable (type, G_TYPE_BOOLEAN)); \
161 g_value_init (&src, type); \
162 g_value_init (&dest, G_TYPE_BOOLEAN); \
163 g_value_set_##setter (&src, value); \
164 g_assert (g_value_transform (&src, &dest)); \
165 g_assert_cmpint (g_value_get_boolean (&dest), ==, TRUE); \
166 g_value_set_##setter (&src, 0); \
167 g_assert (g_value_transform (&src, &dest)); \
168 g_assert_cmpint (g_value_get_boolean (&dest), ==, FALSE); \
169 g_value_unset (&src); \
170 g_value_unset (&dest);
172 CHECK_BOOLEAN_CONVERSION(G_TYPE_INT, int, -12345)
173 CHECK_BOOLEAN_CONVERSION(G_TYPE_UINT, uint, 12345)
174 CHECK_BOOLEAN_CONVERSION(G_TYPE_LONG, long, -12345678)
175 CHECK_BOOLEAN_CONVERSION(G_TYPE_ULONG, ulong, 12345678)
176 CHECK_BOOLEAN_CONVERSION(G_TYPE_INT64, int64, -12345678)
177 CHECK_BOOLEAN_CONVERSION(G_TYPE_UINT64, uint64, 12345678)
179 #define CHECK_STRING_CONVERSION(int_type, setter, int_value) \
180 g_assert (g_value_type_transformable (int_type, G_TYPE_STRING)); \
181 g_value_init (&src, int_type); \
182 g_value_init (&dest, G_TYPE_STRING); \
183 g_value_set_##setter (&src, int_value); \
184 g_assert (g_value_transform (&src, &dest)); \
185 g_assert_cmpstr (g_value_get_string (&dest), ==, #int_value); \
186 g_value_unset (&src); \
187 g_value_unset (&dest);
189 CHECK_STRING_CONVERSION(G_TYPE_INT, int, -12345)
190 CHECK_STRING_CONVERSION(G_TYPE_UINT, uint, 12345)
191 CHECK_STRING_CONVERSION(G_TYPE_LONG, long, -12345678)
192 CHECK_STRING_CONVERSION(G_TYPE_ULONG, ulong, 12345678)
193 CHECK_STRING_CONVERSION(G_TYPE_INT64, int64, -12345678)
194 CHECK_STRING_CONVERSION(G_TYPE_UINT64, uint64, 12345678)
195 CHECK_STRING_CONVERSION(G_TYPE_FLOAT, float, 0.500000)
196 CHECK_STRING_CONVERSION(G_TYPE_DOUBLE, double, -1.234567)
198 g_assert (!g_value_type_transformable (G_TYPE_STRING, G_TYPE_CHAR));
199 g_value_init (&src, G_TYPE_STRING);
200 g_value_init (&dest, G_TYPE_CHAR);
201 g_value_set_static_string (&src, "bla");
202 g_value_set_char (&dest, 'c');
203 g_assert (!g_value_transform (&src, &dest));
204 g_assert_cmpint (g_value_get_char (&dest), ==, 'c');
205 g_value_unset (&src);
206 g_value_unset (&dest);
210 main (int argc, char *argv[])
213 g_test_init (&argc, &argv, NULL);
215 g_test_add_func ("/param/value", test_param_value);
216 g_test_add_func ("/param/strings", test_param_strings);
217 g_test_add_func ("/param/qdata", test_param_qdata);
218 g_test_add_func ("/param/validate", test_param_validate);
219 g_test_add_func ("/param/convert", test_param_convert);
220 g_test_add_func ("/value/transform", test_value_transform);
222 return g_test_run ();