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
= G_VALUE_INIT
;
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_value_set_schar (&value
, 0);
71 modified
= g_param_value_validate (pspec
, &value
);
72 g_assert (modified
&& g_value_get_schar (&value
) == 20);
74 g_value_set_schar (&value
, 20);
75 modified
= g_param_value_validate (pspec
, &value
);
76 g_assert (!modified
&& g_value_get_schar (&value
) == 20);
78 g_value_set_schar (&value
, 40);
79 modified
= g_param_value_validate (pspec
, &value
);
80 g_assert (!modified
&& g_value_get_schar (&value
) == 40);
82 g_value_set_schar (&value
, 60);
83 modified
= g_param_value_validate (pspec
, &value
);
84 g_assert (modified
&& g_value_get_schar (&value
) == 40);
86 g_param_spec_unref (pspec
);
90 test_param_spec_string (void)
93 GValue value
= G_VALUE_INIT
;
96 pspec
= g_param_spec_string ("string", "nick", "blurb",
97 NULL
, G_PARAM_READWRITE
);
98 g_value_init (&value
, G_TYPE_STRING
);
100 g_value_set_string (&value
, "foobar");
101 modified
= g_param_value_validate (pspec
, &value
);
102 g_assert (!modified
);
104 g_value_set_string (&value
, "");
105 modified
= g_param_value_validate (pspec
, &value
);
106 g_assert (!modified
&& g_value_get_string (&value
) != NULL
);
108 /* test ensure_non_null */
110 G_PARAM_SPEC_STRING (pspec
)->ensure_non_null
= TRUE
;
112 g_value_set_string (&value
, NULL
);
113 modified
= g_param_value_validate (pspec
, &value
);
114 g_assert (modified
&& g_value_get_string (&value
) != NULL
);
116 G_PARAM_SPEC_STRING (pspec
)->ensure_non_null
= FALSE
;
118 /* test null_fold_if_empty */
120 G_PARAM_SPEC_STRING (pspec
)->null_fold_if_empty
= TRUE
;
122 g_value_set_string (&value
, "");
123 modified
= g_param_value_validate (pspec
, &value
);
124 g_assert (modified
&& g_value_get_string (&value
) == NULL
);
126 g_value_set_static_string (&value
, "");
127 modified
= g_param_value_validate (pspec
, &value
);
128 g_assert (modified
&& g_value_get_string (&value
) == NULL
);
130 G_PARAM_SPEC_STRING (pspec
)->null_fold_if_empty
= FALSE
;
132 /* test cset_first */
134 G_PARAM_SPEC_STRING (pspec
)->cset_first
= g_strdup ("abc");
135 G_PARAM_SPEC_STRING (pspec
)->substitutor
= '-';
137 g_value_set_string (&value
, "ABC");
138 modified
= g_param_value_validate (pspec
, &value
);
139 g_assert (modified
&& g_value_get_string (&value
)[0] == '-');
141 g_value_set_static_string (&value
, "ABC");
142 modified
= g_param_value_validate (pspec
, &value
);
143 g_assert (modified
&& g_value_get_string (&value
)[0] == '-');
147 G_PARAM_SPEC_STRING (pspec
)->cset_nth
= g_strdup ("abc");
149 g_value_set_string (&value
, "aBC");
150 modified
= g_param_value_validate (pspec
, &value
);
151 g_assert (modified
&& g_value_get_string (&value
)[1] == '-');
153 g_value_set_static_string (&value
, "aBC");
154 modified
= g_param_value_validate (pspec
, &value
);
155 g_assert (modified
&& g_value_get_string (&value
)[1] == '-');
157 g_value_unset (&value
);
158 g_param_spec_unref (pspec
);
162 test_param_spec_override (void)
164 GParamSpec
*ospec
, *pspec
;
165 GValue value
= G_VALUE_INIT
;
168 ospec
= g_param_spec_char ("char", "nick", "blurb",
169 20, 40, 30, G_PARAM_READWRITE
);
171 pspec
= g_param_spec_override ("override", ospec
);
173 g_assert (strcmp (g_param_spec_get_name (pspec
), "override") == 0);
174 g_assert (strcmp (g_param_spec_get_nick (pspec
), "nick") == 0);
175 g_assert (strcmp (g_param_spec_get_blurb (pspec
), "blurb") == 0);
177 g_value_init (&value
, G_TYPE_CHAR
);
178 g_value_set_char (&value
, 30);
180 g_assert (g_param_value_defaults (pspec
, &value
));
182 g_value_set_char (&value
, 0);
183 modified
= g_param_value_validate (pspec
, &value
);
184 g_assert (modified
&& g_value_get_char (&value
) == 20);
186 g_value_set_char (&value
, 20);
187 modified
= g_param_value_validate (pspec
, &value
);
188 g_assert (!modified
&& g_value_get_char (&value
) == 20);
190 g_value_set_char (&value
, 40);
191 modified
= g_param_value_validate (pspec
, &value
);
192 g_assert (!modified
&& g_value_get_char (&value
) == 40);
194 g_value_set_char (&value
, 60);
195 modified
= g_param_value_validate (pspec
, &value
);
196 g_assert (modified
&& g_value_get_char (&value
) == 40);
198 g_param_spec_unref (pspec
);
202 test_param_spec_gtype (void)
205 GValue value
= G_VALUE_INIT
;
208 pspec
= g_param_spec_gtype ("gtype", "nick", "blurb",
209 G_TYPE_PARAM
, G_PARAM_READWRITE
);
211 g_value_init (&value
, G_TYPE_GTYPE
);
212 g_value_set_gtype (&value
, G_TYPE_PARAM
);
214 g_assert (g_param_value_defaults (pspec
, &value
));
216 g_value_set_gtype (&value
, G_TYPE_INT
);
217 modified
= g_param_value_validate (pspec
, &value
);
218 g_assert (modified
&& g_value_get_gtype (&value
) == G_TYPE_PARAM
);
220 g_value_set_gtype (&value
, G_TYPE_PARAM_INT
);
221 modified
= g_param_value_validate (pspec
, &value
);
222 g_assert (!modified
&& g_value_get_gtype (&value
) == G_TYPE_PARAM_INT
);
226 test_param_spec_variant (void)
229 GValue value
= G_VALUE_INIT
;
232 pspec
= g_param_spec_variant ("variant", "nick", "blurb",
233 G_VARIANT_TYPE ("i"),
234 g_variant_new_int32 (42),
237 g_value_init (&value
, G_TYPE_VARIANT
);
238 g_value_set_variant (&value
, g_variant_new_int32 (42));
240 g_assert (g_param_value_defaults (pspec
, &value
));
242 modified
= g_param_value_validate (pspec
, &value
);
243 g_assert (!modified
);
245 g_value_reset (&value
);
246 g_value_set_variant (&value
, g_variant_new_uint32 (41));
247 modified
= g_param_value_validate (pspec
, &value
);
249 g_assert_cmpint (g_variant_get_int32 (g_value_get_variant (&value
)), ==, 42);
250 g_value_unset (&value
);
252 g_param_spec_unref (pspec
);
256 main (int argc
, char *argv
[])
258 g_test_init (&argc
, &argv
, NULL
);
261 g_test_add_func ("/paramspec/char", test_param_spec_char
);
262 g_test_add_func ("/paramspec/string", test_param_spec_string
);
263 g_test_add_func ("/paramspec/override", test_param_spec_override
);
264 g_test_add_func ("/paramspec/gtype", test_param_spec_gtype
);
265 g_test_add_func ("/paramspec/variant", test_param_spec_variant
);
267 return g_test_run ();