1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18 /*
19 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GLib Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GLib at ftp://ftp.gtk.org/pub/gtk/.
23 */
24
25 #undef G_DISABLE_ASSERT
26 #undef G_LOG_DOMAIN
27
28 #include <string.h>
29
30 #include <glib.h>
31 #include <glib-object.h>
32
33 static void
test_param_spec_char(void)34 test_param_spec_char (void)
35 {
36 GParamSpec *pspec;
37 GValue value = G_VALUE_INIT;
38 gboolean modified;
39
40 pspec = g_param_spec_char ("char", "nick", "blurb",
41 20, 40, 30, G_PARAM_READWRITE);
42
43 g_assert (strcmp (g_param_spec_get_name (pspec), "char") == 0);
44 g_assert (strcmp (g_param_spec_get_nick (pspec), "nick") == 0);
45 g_assert (strcmp (g_param_spec_get_blurb (pspec), "blurb") == 0);
46
47 g_value_init (&value, G_TYPE_CHAR);
48 g_value_set_char (&value, 30);
49
50 g_assert (g_param_value_defaults (pspec, &value));
51
52 g_value_set_char (&value, 0);
53 modified = g_param_value_validate (pspec, &value);
54 g_assert (modified && g_value_get_char (&value) == 20);
55
56 g_value_set_char (&value, 20);
57 modified = g_param_value_validate (pspec, &value);
58 g_assert (!modified && g_value_get_char (&value) == 20);
59
60 g_value_set_char (&value, 40);
61 modified = g_param_value_validate (pspec, &value);
62 g_assert (!modified && g_value_get_char (&value) == 40);
63
64 g_value_set_char (&value, 60);
65 modified = g_param_value_validate (pspec, &value);
66 g_assert (modified && g_value_get_char (&value) == 40);
67
68 g_value_set_schar (&value, 0);
69 modified = g_param_value_validate (pspec, &value);
70 g_assert (modified && g_value_get_schar (&value) == 20);
71
72 g_value_set_schar (&value, 20);
73 modified = g_param_value_validate (pspec, &value);
74 g_assert (!modified && g_value_get_schar (&value) == 20);
75
76 g_value_set_schar (&value, 40);
77 modified = g_param_value_validate (pspec, &value);
78 g_assert (!modified && g_value_get_schar (&value) == 40);
79
80 g_value_set_schar (&value, 60);
81 modified = g_param_value_validate (pspec, &value);
82 g_assert (modified && g_value_get_schar (&value) == 40);
83
84 g_param_spec_unref (pspec);
85 }
86
87 static void
test_param_spec_string(void)88 test_param_spec_string (void)
89 {
90 GParamSpec *pspec;
91 GValue value = G_VALUE_INIT;
92 gboolean modified;
93
94 pspec = g_param_spec_string ("string", "nick", "blurb",
95 NULL, G_PARAM_READWRITE);
96 g_value_init (&value, G_TYPE_STRING);
97
98 g_value_set_string (&value, "foobar");
99 modified = g_param_value_validate (pspec, &value);
100 g_assert (!modified);
101
102 g_value_set_string (&value, "");
103 modified = g_param_value_validate (pspec, &value);
104 g_assert (!modified && g_value_get_string (&value) != NULL);
105
106 /* test ensure_non_null */
107
108 G_PARAM_SPEC_STRING (pspec)->ensure_non_null = TRUE;
109
110 g_value_set_string (&value, NULL);
111 modified = g_param_value_validate (pspec, &value);
112 g_assert (modified && g_value_get_string (&value) != NULL);
113
114 G_PARAM_SPEC_STRING (pspec)->ensure_non_null = FALSE;
115
116 /* test null_fold_if_empty */
117
118 G_PARAM_SPEC_STRING (pspec)->null_fold_if_empty = TRUE;
119
120 g_value_set_string (&value, "");
121 modified = g_param_value_validate (pspec, &value);
122 g_assert (modified && g_value_get_string (&value) == NULL);
123
124 g_value_set_static_string (&value, "");
125 modified = g_param_value_validate (pspec, &value);
126 g_assert (modified && g_value_get_string (&value) == NULL);
127
128 G_PARAM_SPEC_STRING (pspec)->null_fold_if_empty = FALSE;
129
130 /* test cset_first */
131
132 G_PARAM_SPEC_STRING (pspec)->cset_first = g_strdup ("abc");
133 G_PARAM_SPEC_STRING (pspec)->substitutor = '-';
134
135 g_value_set_string (&value, "ABC");
136 modified = g_param_value_validate (pspec, &value);
137 g_assert (modified && g_value_get_string (&value)[0] == '-');
138
139 g_value_set_static_string (&value, "ABC");
140 modified = g_param_value_validate (pspec, &value);
141 g_assert (modified && g_value_get_string (&value)[0] == '-');
142
143 /* test cset_nth */
144
145 G_PARAM_SPEC_STRING (pspec)->cset_nth = g_strdup ("abc");
146
147 g_value_set_string (&value, "aBC");
148 modified = g_param_value_validate (pspec, &value);
149 g_assert (modified && g_value_get_string (&value)[1] == '-');
150
151 g_value_set_static_string (&value, "aBC");
152 modified = g_param_value_validate (pspec, &value);
153 g_assert (modified && g_value_get_string (&value)[1] == '-');
154
155 g_value_unset (&value);
156 g_param_spec_unref (pspec);
157 }
158
159 static void
test_param_spec_override(void)160 test_param_spec_override (void)
161 {
162 GParamSpec *ospec, *pspec;
163 GValue value = G_VALUE_INIT;
164 gboolean modified;
165
166 ospec = g_param_spec_char ("char", "nick", "blurb",
167 20, 40, 30, G_PARAM_READWRITE);
168
169 pspec = g_param_spec_override ("override", ospec);
170
171 g_assert (strcmp (g_param_spec_get_name (pspec), "override") == 0);
172 g_assert (strcmp (g_param_spec_get_nick (pspec), "nick") == 0);
173 g_assert (strcmp (g_param_spec_get_blurb (pspec), "blurb") == 0);
174
175 g_value_init (&value, G_TYPE_CHAR);
176 g_value_set_char (&value, 30);
177
178 g_assert (g_param_value_defaults (pspec, &value));
179
180 g_value_set_char (&value, 0);
181 modified = g_param_value_validate (pspec, &value);
182 g_assert (modified && g_value_get_char (&value) == 20);
183
184 g_value_set_char (&value, 20);
185 modified = g_param_value_validate (pspec, &value);
186 g_assert (!modified && g_value_get_char (&value) == 20);
187
188 g_value_set_char (&value, 40);
189 modified = g_param_value_validate (pspec, &value);
190 g_assert (!modified && g_value_get_char (&value) == 40);
191
192 g_value_set_char (&value, 60);
193 modified = g_param_value_validate (pspec, &value);
194 g_assert (modified && g_value_get_char (&value) == 40);
195
196 g_param_spec_unref (pspec);
197 g_param_spec_unref (ospec);
198 }
199
200 static void
test_param_spec_gtype(void)201 test_param_spec_gtype (void)
202 {
203 GParamSpec *pspec;
204 GValue value = G_VALUE_INIT;
205 gboolean modified;
206
207 pspec = g_param_spec_gtype ("gtype", "nick", "blurb",
208 G_TYPE_PARAM, G_PARAM_READWRITE);
209
210 g_value_init (&value, G_TYPE_GTYPE);
211 g_value_set_gtype (&value, G_TYPE_PARAM);
212
213 g_assert (g_param_value_defaults (pspec, &value));
214
215 g_value_set_gtype (&value, G_TYPE_INT);
216 modified = g_param_value_validate (pspec, &value);
217 g_assert (modified && g_value_get_gtype (&value) == G_TYPE_PARAM);
218
219 g_value_set_gtype (&value, G_TYPE_PARAM_INT);
220 modified = g_param_value_validate (pspec, &value);
221 g_assert (!modified && g_value_get_gtype (&value) == G_TYPE_PARAM_INT);
222
223 g_param_spec_unref (pspec);
224 }
225
226 static void
test_param_spec_variant(void)227 test_param_spec_variant (void)
228 {
229 GParamSpec *pspec;
230 GValue value = G_VALUE_INIT;
231 GValue value2 = G_VALUE_INIT;
232 GValue value3 = G_VALUE_INIT;
233 GValue value4 = G_VALUE_INIT;
234 GValue value5 = G_VALUE_INIT;
235 gboolean modified;
236
237 pspec = g_param_spec_variant ("variant", "nick", "blurb",
238 G_VARIANT_TYPE ("i"),
239 g_variant_new_int32 (42),
240 G_PARAM_READWRITE);
241
242 g_value_init (&value, G_TYPE_VARIANT);
243 g_value_set_variant (&value, g_variant_new_int32 (42));
244
245 g_value_init (&value2, G_TYPE_VARIANT);
246 g_value_set_variant (&value2, g_variant_new_int32 (43));
247
248 g_value_init (&value3, G_TYPE_VARIANT);
249 g_value_set_variant (&value3, g_variant_new_int16 (42));
250
251 g_value_init (&value4, G_TYPE_VARIANT);
252 g_value_set_variant (&value4, g_variant_new_parsed ("[@u 15, @u 10]"));
253
254 g_value_init (&value5, G_TYPE_VARIANT);
255 g_value_set_variant (&value5, NULL);
256
257 g_assert_true (g_param_value_defaults (pspec, &value));
258 g_assert_false (g_param_value_defaults (pspec, &value2));
259 g_assert_false (g_param_value_defaults (pspec, &value3));
260 g_assert_false (g_param_value_defaults (pspec, &value4));
261 g_assert_false (g_param_value_defaults (pspec, &value5));
262
263 modified = g_param_value_validate (pspec, &value);
264 g_assert_false (modified);
265
266 g_value_reset (&value);
267 g_value_set_variant (&value, g_variant_new_uint32 (41));
268 modified = g_param_value_validate (pspec, &value);
269 g_assert_true (modified);
270 g_assert_cmpint (g_variant_get_int32 (g_value_get_variant (&value)), ==, 42);
271 g_value_unset (&value);
272
273 g_value_unset (&value5);
274 g_value_unset (&value4);
275 g_value_unset (&value3);
276 g_value_unset (&value2);
277
278 g_param_spec_unref (pspec);
279 }
280
281 /* Test g_param_values_cmp() for #GParamSpecVariant. */
282 static void
test_param_spec_variant_cmp(void)283 test_param_spec_variant_cmp (void)
284 {
285 const struct
286 {
287 const GVariantType *pspec_type;
288 const gchar *v1;
289 enum
290 {
291 LESS_THAN = -1,
292 EQUAL = 0,
293 GREATER_THAN = 1,
294 NOT_EQUAL,
295 } expected_result;
296 const gchar *v2;
297 }
298 vectors[] =
299 {
300 { G_VARIANT_TYPE ("i"), "@i 1", LESS_THAN, "@i 2" },
301 { G_VARIANT_TYPE ("i"), "@i 2", EQUAL, "@i 2" },
302 { G_VARIANT_TYPE ("i"), "@i 3", GREATER_THAN, "@i 2" },
303 { G_VARIANT_TYPE ("i"), NULL, LESS_THAN, "@i 2" },
304 { G_VARIANT_TYPE ("i"), NULL, EQUAL, NULL },
305 { G_VARIANT_TYPE ("i"), "@i 1", GREATER_THAN, NULL },
306 { G_VARIANT_TYPE ("i"), "@u 1", LESS_THAN, "@u 2" },
307 { G_VARIANT_TYPE ("i"), "@as ['hi']", NOT_EQUAL, "@u 2" },
308 { G_VARIANT_TYPE ("i"), "@as ['hi']", NOT_EQUAL, "@as ['there']" },
309 { G_VARIANT_TYPE ("i"), "@as ['hi']", EQUAL, "@as ['hi']" },
310 };
311 gsize i;
312
313 for (i = 0; i < G_N_ELEMENTS (vectors); i++)
314 {
315 GParamSpec *pspec;
316 GValue v1 = G_VALUE_INIT;
317 GValue v2 = G_VALUE_INIT;
318 gint cmp;
319
320 pspec = g_param_spec_variant ("variant", "nick", "blurb",
321 vectors[i].pspec_type,
322 NULL,
323 G_PARAM_READWRITE);
324
325 g_value_init (&v1, G_TYPE_VARIANT);
326 g_value_set_variant (&v1, (vectors[i].v1 != NULL) ? g_variant_new_parsed (vectors[i].v1) : NULL);
327
328 g_value_init (&v2, G_TYPE_VARIANT);
329 g_value_set_variant (&v2, (vectors[i].v2 != NULL) ? g_variant_new_parsed (vectors[i].v2) : NULL);
330
331 cmp = g_param_values_cmp (pspec, &v1, &v2);
332
333 switch (vectors[i].expected_result)
334 {
335 case LESS_THAN:
336 case EQUAL:
337 case GREATER_THAN:
338 g_assert_cmpint (cmp, ==, vectors[i].expected_result);
339 break;
340 case NOT_EQUAL:
341 g_assert_cmpint (cmp, !=, 0);
342 break;
343 default:
344 g_assert_not_reached ();
345 }
346
347 g_value_unset (&v2);
348 g_value_unset (&v1);
349 g_param_spec_unref (pspec);
350 }
351 }
352
353 int
main(int argc,char * argv[])354 main (int argc, char *argv[])
355 {
356 g_test_init (&argc, &argv, NULL);
357
358 g_test_add_func ("/paramspec/char", test_param_spec_char);
359 g_test_add_func ("/paramspec/string", test_param_spec_string);
360 g_test_add_func ("/paramspec/override", test_param_spec_override);
361 g_test_add_func ("/paramspec/gtype", test_param_spec_gtype);
362 g_test_add_func ("/paramspec/variant", test_param_spec_variant);
363 g_test_add_func ("/paramspec/variant/cmp", test_param_spec_variant_cmp);
364
365 return g_test_run ();
366 }
367