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 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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
27 #undef G_DISABLE_ASSERT
33 #include <glib-object.h>
36 test_param_spec_char (void)
39 GValue value = { 0, };
42 pspec = g_param_spec_char ("char", "nick", "blurb",
43 20, 40, 30, G_PARAM_READWRITE);
45 g_assert (strcmp (g_param_spec_get_name (pspec), "char") == 0);
46 g_assert (strcmp (g_param_spec_get_nick (pspec), "nick") == 0);
47 g_assert (strcmp (g_param_spec_get_blurb (pspec), "blurb") == 0);
49 g_value_init (&value, G_TYPE_CHAR);
50 g_value_set_char (&value, 30);
52 g_assert (g_param_value_defaults (pspec, &value));
54 g_value_set_char (&value, 0);
55 modified = g_param_value_validate (pspec, &value);
56 g_assert (modified && g_value_get_char (&value) == 20);
58 g_value_set_char (&value, 20);
59 modified = g_param_value_validate (pspec, &value);
60 g_assert (!modified && g_value_get_char (&value) == 20);
62 g_value_set_char (&value, 40);
63 modified = g_param_value_validate (pspec, &value);
64 g_assert (!modified && g_value_get_char (&value) == 40);
66 g_value_set_char (&value, 60);
67 modified = g_param_value_validate (pspec, &value);
68 g_assert (modified && g_value_get_char (&value) == 40);
70 g_param_spec_unref (pspec);
74 test_param_spec_string (void)
77 GValue value = { 0, };
80 pspec = g_param_spec_string ("string", "nick", "blurb",
81 NULL, G_PARAM_READWRITE);
82 g_value_init (&value, G_TYPE_STRING);
84 g_value_set_string (&value, "foobar");
85 modified = g_param_value_validate (pspec, &value);
88 g_value_set_string (&value, "");
89 modified = g_param_value_validate (pspec, &value);
90 g_assert (!modified && g_value_get_string (&value) != NULL);
92 /* test ensure_non_null */
94 G_PARAM_SPEC_STRING (pspec)->ensure_non_null = TRUE;
96 g_value_set_string (&value, NULL);
97 modified = g_param_value_validate (pspec, &value);
98 g_assert (modified && g_value_get_string (&value) != NULL);
100 G_PARAM_SPEC_STRING (pspec)->ensure_non_null = FALSE;
102 /* test null_fold_if_empty */
104 G_PARAM_SPEC_STRING (pspec)->null_fold_if_empty = TRUE;
106 g_value_set_string (&value, "");
107 modified = g_param_value_validate (pspec, &value);
108 g_assert (modified && g_value_get_string (&value) == NULL);
110 g_value_set_static_string (&value, "");
111 modified = g_param_value_validate (pspec, &value);
112 g_assert (modified && g_value_get_string (&value) == NULL);
114 G_PARAM_SPEC_STRING (pspec)->null_fold_if_empty = FALSE;
116 /* test cset_first */
118 G_PARAM_SPEC_STRING (pspec)->cset_first = g_strdup ("abc");
119 G_PARAM_SPEC_STRING (pspec)->substitutor = '-';
121 g_value_set_string (&value, "ABC");
122 modified = g_param_value_validate (pspec, &value);
123 g_assert (modified && g_value_get_string (&value)[0] == '-');
125 g_value_set_static_string (&value, "ABC");
126 modified = g_param_value_validate (pspec, &value);
127 g_assert (modified && g_value_get_string (&value)[0] == '-');
131 G_PARAM_SPEC_STRING (pspec)->cset_nth = g_strdup ("abc");
133 g_value_set_string (&value, "aBC");
134 modified = g_param_value_validate (pspec, &value);
135 g_assert (modified && g_value_get_string (&value)[1] == '-');
137 g_value_set_static_string (&value, "aBC");
138 modified = g_param_value_validate (pspec, &value);
139 g_assert (modified && g_value_get_string (&value)[1] == '-');
141 g_value_unset (&value);
142 g_param_spec_unref (pspec);
146 test_param_spec_override (void)
148 GParamSpec *ospec, *pspec;
149 GValue value = { 0, };
152 ospec = g_param_spec_char ("char", "nick", "blurb",
153 20, 40, 30, G_PARAM_READWRITE);
155 pspec = g_param_spec_override ("override", ospec);
157 g_assert (strcmp (g_param_spec_get_name (pspec), "override") == 0);
158 g_assert (strcmp (g_param_spec_get_nick (pspec), "nick") == 0);
159 g_assert (strcmp (g_param_spec_get_blurb (pspec), "blurb") == 0);
161 g_value_init (&value, G_TYPE_CHAR);
162 g_value_set_char (&value, 30);
164 g_assert (g_param_value_defaults (pspec, &value));
166 g_value_set_char (&value, 0);
167 modified = g_param_value_validate (pspec, &value);
168 g_assert (modified && g_value_get_char (&value) == 20);
170 g_value_set_char (&value, 20);
171 modified = g_param_value_validate (pspec, &value);
172 g_assert (!modified && g_value_get_char (&value) == 20);
174 g_value_set_char (&value, 40);
175 modified = g_param_value_validate (pspec, &value);
176 g_assert (!modified && g_value_get_char (&value) == 40);
178 g_value_set_char (&value, 60);
179 modified = g_param_value_validate (pspec, &value);
180 g_assert (modified && g_value_get_char (&value) == 40);
182 g_param_spec_unref (pspec);
186 test_param_spec_gtype (void)
189 GValue value = { 0, };
192 pspec = g_param_spec_gtype ("gtype", "nick", "blurb",
193 G_TYPE_PARAM, G_PARAM_READWRITE);
195 g_value_init (&value, G_TYPE_GTYPE);
196 g_value_set_gtype (&value, G_TYPE_PARAM);
198 g_assert (g_param_value_defaults (pspec, &value));
200 g_value_set_gtype (&value, G_TYPE_INT);
201 modified = g_param_value_validate (pspec, &value);
202 g_assert (modified && g_value_get_gtype (&value) == G_TYPE_PARAM);
204 g_value_set_gtype (&value, G_TYPE_PARAM_INT);
205 modified = g_param_value_validate (pspec, &value);
206 g_assert (!modified && g_value_get_gtype (&value) == G_TYPE_PARAM_INT);
210 test_param_spec_variant (void)
213 GValue value = { 0, };
216 pspec = g_param_spec_variant ("variant", "nick", "blurb",
217 G_VARIANT_TYPE ("i"),
218 g_variant_new_int32 (42),
221 g_value_init (&value, G_TYPE_VARIANT);
222 g_value_set_variant (&value, g_variant_new_int32 (42));
224 g_assert (g_param_value_defaults (pspec, &value));
226 modified = g_param_value_validate (pspec, &value);
227 g_assert (!modified);
229 g_value_reset (&value);
230 g_value_set_variant (&value, g_variant_new_uint32 (41));
231 modified = g_param_value_validate (pspec, &value);
233 g_assert_cmpint (g_variant_get_int32 (g_value_get_variant (&value)), ==, 42);
234 g_value_unset (&value);
236 g_param_spec_unref (pspec);
240 main (int argc, char *argv[])
242 g_test_init (&argc, &argv, NULL);
245 g_test_add_func ("/paramspec/char", test_param_spec_char);
246 g_test_add_func ("/paramspec/string", test_param_spec_string);
247 g_test_add_func ("/paramspec/override", test_param_spec_override);
248 g_test_add_func ("/paramspec/gtype", test_param_spec_gtype);
249 g_test_add_func ("/paramspec/variant", test_param_spec_variant);
251 return g_test_run ();