Tizen 2.1 base
[platform/upstream/glib2.0.git] / tests / gobject / paramspec-test.c
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 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, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
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/. 
25  */
26
27 #undef G_DISABLE_ASSERT
28 #undef G_LOG_DOMAIN
29
30 #include <string.h>
31
32 #include <glib.h>
33 #include <glib-object.h>
34
35 static void
36 test_param_spec_char (void)
37 {
38   GParamSpec *pspec;
39   GValue value = G_VALUE_INIT;
40   gboolean modified;
41  
42   pspec = g_param_spec_char ("char", "nick", "blurb",
43                              20, 40, 30, G_PARAM_READWRITE);
44
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);
48
49   g_value_init (&value, G_TYPE_CHAR);
50   g_value_set_char (&value, 30);
51
52   g_assert (g_param_value_defaults (pspec, &value));
53   
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);
57
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);
61
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);
65
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);
69
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);
73
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);
77
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);
81
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);
85
86   g_param_spec_unref (pspec);
87 }
88
89 static void
90 test_param_spec_string (void)
91 {
92   GParamSpec *pspec;
93   GValue value = G_VALUE_INIT;
94   gboolean modified;
95
96   pspec = g_param_spec_string ("string", "nick", "blurb",
97                                NULL, G_PARAM_READWRITE);
98   g_value_init (&value, G_TYPE_STRING);
99
100   g_value_set_string (&value, "foobar");
101   modified = g_param_value_validate (pspec, &value);
102   g_assert (!modified);
103
104   g_value_set_string (&value, "");
105   modified = g_param_value_validate (pspec, &value);
106   g_assert (!modified && g_value_get_string (&value) != NULL);
107
108   /* test ensure_non_null */
109
110   G_PARAM_SPEC_STRING (pspec)->ensure_non_null = TRUE;
111
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);
115
116   G_PARAM_SPEC_STRING (pspec)->ensure_non_null = FALSE;
117
118   /* test null_fold_if_empty */
119
120   G_PARAM_SPEC_STRING (pspec)->null_fold_if_empty = TRUE;
121
122   g_value_set_string (&value, "");
123   modified = g_param_value_validate (pspec, &value);
124   g_assert (modified && g_value_get_string (&value) == NULL);
125
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);
129
130   G_PARAM_SPEC_STRING (pspec)->null_fold_if_empty = FALSE;
131
132   /* test cset_first */
133
134   G_PARAM_SPEC_STRING (pspec)->cset_first = g_strdup ("abc");
135   G_PARAM_SPEC_STRING (pspec)->substitutor = '-';
136
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] == '-');
140
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] == '-');
144
145   /* test cset_nth */
146
147   G_PARAM_SPEC_STRING (pspec)->cset_nth = g_strdup ("abc");
148
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] == '-');
152
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] == '-');
156
157   g_value_unset (&value);
158   g_param_spec_unref (pspec);
159 }
160
161 static void
162 test_param_spec_override (void)
163 {
164   GParamSpec *ospec, *pspec;
165   GValue value = G_VALUE_INIT;
166   gboolean modified;
167  
168   ospec = g_param_spec_char ("char", "nick", "blurb",
169                              20, 40, 30, G_PARAM_READWRITE);
170
171   pspec = g_param_spec_override ("override", ospec);
172
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);
176
177   g_value_init (&value, G_TYPE_CHAR);
178   g_value_set_char (&value, 30);
179
180   g_assert (g_param_value_defaults (pspec, &value));
181   
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);
185
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);
189
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);
193
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);
197
198   g_param_spec_unref (pspec);
199 }
200
201 static void
202 test_param_spec_gtype (void)
203 {
204   GParamSpec *pspec;
205   GValue value = G_VALUE_INIT;
206   gboolean modified;
207   
208   pspec = g_param_spec_gtype ("gtype", "nick", "blurb",
209                               G_TYPE_PARAM, G_PARAM_READWRITE);
210   
211   g_value_init (&value, G_TYPE_GTYPE);
212   g_value_set_gtype (&value, G_TYPE_PARAM);
213
214   g_assert (g_param_value_defaults (pspec, &value));
215   
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);
219
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);
223 }
224
225 static void
226 test_param_spec_variant (void)
227 {
228   GParamSpec *pspec;
229   GValue value = G_VALUE_INIT;
230   gboolean modified;
231
232   pspec = g_param_spec_variant ("variant", "nick", "blurb",
233                                 G_VARIANT_TYPE ("i"),
234                                 g_variant_new_int32 (42),
235                                 G_PARAM_READWRITE);
236
237   g_value_init (&value, G_TYPE_VARIANT);
238   g_value_set_variant (&value, g_variant_new_int32 (42));
239
240   g_assert (g_param_value_defaults (pspec, &value));
241
242   modified = g_param_value_validate (pspec, &value);
243   g_assert (!modified);
244
245   g_value_reset (&value);
246   g_value_set_variant (&value, g_variant_new_uint32 (41));
247   modified = g_param_value_validate (pspec, &value);
248   g_assert (modified);
249   g_assert_cmpint (g_variant_get_int32 (g_value_get_variant (&value)), ==, 42);
250   g_value_unset (&value);
251
252   g_param_spec_unref (pspec);
253 }
254
255 int
256 main (int argc, char *argv[])
257 {
258   g_test_init (&argc, &argv, NULL);
259   g_type_init ();
260
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);
266
267   return g_test_run ();
268 }