1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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/>.
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/.
25 #undef G_DISABLE_ASSERT
31 #include <glib-object.h>
34 test_param_spec_char (void)
37 GValue value
= G_VALUE_INIT
;
40 pspec
= g_param_spec_char ("char", "nick", "blurb",
41 20, 40, 30, G_PARAM_READWRITE
);
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);
47 g_value_init (&value
, G_TYPE_CHAR
);
48 g_value_set_char (&value
, 30);
50 g_assert (g_param_value_defaults (pspec
, &value
));
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);
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);
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);
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);
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);
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);
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);
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);
84 g_param_spec_unref (pspec
);
88 test_param_spec_string (void)
91 GValue value
= G_VALUE_INIT
;
94 pspec
= g_param_spec_string ("string", "nick", "blurb",
95 NULL
, G_PARAM_READWRITE
);
96 g_value_init (&value
, G_TYPE_STRING
);
98 g_value_set_string (&value
, "foobar");
99 modified
= g_param_value_validate (pspec
, &value
);
100 g_assert (!modified
);
102 g_value_set_string (&value
, "");
103 modified
= g_param_value_validate (pspec
, &value
);
104 g_assert (!modified
&& g_value_get_string (&value
) != NULL
);
106 /* test ensure_non_null */
108 G_PARAM_SPEC_STRING (pspec
)->ensure_non_null
= TRUE
;
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
);
114 G_PARAM_SPEC_STRING (pspec
)->ensure_non_null
= FALSE
;
116 /* test null_fold_if_empty */
118 G_PARAM_SPEC_STRING (pspec
)->null_fold_if_empty
= TRUE
;
120 g_value_set_string (&value
, "");
121 modified
= g_param_value_validate (pspec
, &value
);
122 g_assert (modified
&& g_value_get_string (&value
) == NULL
);
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
);
128 G_PARAM_SPEC_STRING (pspec
)->null_fold_if_empty
= FALSE
;
130 /* test cset_first */
132 G_PARAM_SPEC_STRING (pspec
)->cset_first
= g_strdup ("abc");
133 G_PARAM_SPEC_STRING (pspec
)->substitutor
= '-';
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] == '-');
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] == '-');
145 G_PARAM_SPEC_STRING (pspec
)->cset_nth
= g_strdup ("abc");
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] == '-');
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] == '-');
155 g_value_unset (&value
);
156 g_param_spec_unref (pspec
);
160 test_param_spec_override (void)
162 GParamSpec
*ospec
, *pspec
;
163 GValue value
= G_VALUE_INIT
;
166 ospec
= g_param_spec_char ("char", "nick", "blurb",
167 20, 40, 30, G_PARAM_READWRITE
);
169 pspec
= g_param_spec_override ("override", ospec
);
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);
175 g_value_init (&value
, G_TYPE_CHAR
);
176 g_value_set_char (&value
, 30);
178 g_assert (g_param_value_defaults (pspec
, &value
));
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);
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);
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);
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);
196 g_param_spec_unref (pspec
);
197 g_param_spec_unref (ospec
);
201 test_param_spec_gtype (void)
204 GValue value
= G_VALUE_INIT
;
207 pspec
= g_param_spec_gtype ("gtype", "nick", "blurb",
208 G_TYPE_PARAM
, G_PARAM_READWRITE
);
210 g_value_init (&value
, G_TYPE_GTYPE
);
211 g_value_set_gtype (&value
, G_TYPE_PARAM
);
213 g_assert (g_param_value_defaults (pspec
, &value
));
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
);
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
);
223 g_param_spec_unref (pspec
);
227 test_param_spec_variant (void)
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
;
237 pspec
= g_param_spec_variant ("variant", "nick", "blurb",
238 G_VARIANT_TYPE ("i"),
239 g_variant_new_int32 (42),
242 g_value_init (&value
, G_TYPE_VARIANT
);
243 g_value_set_variant (&value
, g_variant_new_int32 (42));
245 g_value_init (&value2
, G_TYPE_VARIANT
);
246 g_value_set_variant (&value2
, g_variant_new_int32 (43));
248 g_value_init (&value3
, G_TYPE_VARIANT
);
249 g_value_set_variant (&value3
, g_variant_new_int16 (42));
251 g_value_init (&value4
, G_TYPE_VARIANT
);
252 g_value_set_variant (&value4
, g_variant_new_parsed ("[@u 15, @u 10]"));
254 g_value_init (&value5
, G_TYPE_VARIANT
);
255 g_value_set_variant (&value5
, NULL
);
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
));
263 modified
= g_param_value_validate (pspec
, &value
);
264 g_assert_false (modified
);
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
);
273 g_value_unset (&value5
);
274 g_value_unset (&value4
);
275 g_value_unset (&value3
);
276 g_value_unset (&value2
);
278 g_param_spec_unref (pspec
);
281 /* Test g_param_values_cmp() for #GParamSpecVariant. */
283 test_param_spec_variant_cmp (void)
287 const GVariantType
*pspec_type
;
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']" },
313 for (i
= 0; i
< G_N_ELEMENTS (vectors
); i
++)
316 GValue v1
= G_VALUE_INIT
;
317 GValue v2
= G_VALUE_INIT
;
320 pspec
= g_param_spec_variant ("variant", "nick", "blurb",
321 vectors
[i
].pspec_type
,
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
);
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
);
331 cmp
= g_param_values_cmp (pspec
, &v1
, &v2
);
333 switch (vectors
[i
].expected_result
)
338 g_assert_cmpint (cmp
, ==, vectors
[i
].expected_result
);
341 g_assert_cmpint (cmp
, !=, 0);
344 g_assert_not_reached ();
349 g_param_spec_unref (pspec
);
354 main (int argc
, char *argv
[])
356 g_test_init (&argc
, &argv
, NULL
);
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
);
365 return g_test_run ();