Add some more cases to the app-id unit tests
[glib.git] / tests / gobject / paramspec-test.c
blob74609dd17640c9a7d997d85678122b919d1a58d7
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, 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
26 #undef G_LOG_DOMAIN
28 #include <string.h>
30 #include <glib.h>
31 #include <glib-object.h>
33 static void
34 test_param_spec_char (void)
36 GParamSpec *pspec;
37 GValue value = G_VALUE_INIT;
38 gboolean modified;
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);
87 static void
88 test_param_spec_string (void)
90 GParamSpec *pspec;
91 GValue value = G_VALUE_INIT;
92 gboolean modified;
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] == '-');
143 /* test cset_nth */
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);
159 static void
160 test_param_spec_override (void)
162 GParamSpec *ospec, *pspec;
163 GValue value = G_VALUE_INIT;
164 gboolean modified;
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);
200 static void
201 test_param_spec_gtype (void)
203 GParamSpec *pspec;
204 GValue value = G_VALUE_INIT;
205 gboolean modified;
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);
226 static void
227 test_param_spec_variant (void)
229 GParamSpec *pspec;
230 GValue value = G_VALUE_INIT;
231 gboolean modified;
233 pspec = g_param_spec_variant ("variant", "nick", "blurb",
234 G_VARIANT_TYPE ("i"),
235 g_variant_new_int32 (42),
236 G_PARAM_READWRITE);
238 g_value_init (&value, G_TYPE_VARIANT);
239 g_value_set_variant (&value, g_variant_new_int32 (42));
241 g_assert (g_param_value_defaults (pspec, &value));
243 modified = g_param_value_validate (pspec, &value);
244 g_assert (!modified);
246 g_value_reset (&value);
247 g_value_set_variant (&value, g_variant_new_uint32 (41));
248 modified = g_param_value_validate (pspec, &value);
249 g_assert (modified);
250 g_assert_cmpint (g_variant_get_int32 (g_value_get_variant (&value)), ==, 42);
251 g_value_unset (&value);
253 g_param_spec_unref (pspec);
257 main (int argc, char *argv[])
259 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 ();