[kdbus] KDBUS_ITEM_PAYLOAD_OFF items are (once again) relative to msg header
[platform/upstream/glib.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, see <http://www.gnu.org/licenses/>.
16  */
17
18 /*
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/. 
23  */
24
25 #undef G_DISABLE_ASSERT
26 #undef G_LOG_DOMAIN
27
28 #include <string.h>
29
30 #include <glib.h>
31 #include <glib-object.h>
32
33 static void
34 test_param_spec_char (void)
35 {
36   GParamSpec *pspec;
37   GValue value = G_VALUE_INIT;
38   gboolean modified;
39  
40   pspec = g_param_spec_char ("char", "nick", "blurb",
41                              20, 40, 30, G_PARAM_READWRITE);
42
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);
46
47   g_value_init (&value, G_TYPE_CHAR);
48   g_value_set_char (&value, 30);
49
50   g_assert (g_param_value_defaults (pspec, &value));
51   
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);
55
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);
59
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);
63
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);
67
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);
71
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);
75
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);
79
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);
83
84   g_param_spec_unref (pspec);
85 }
86
87 static void
88 test_param_spec_string (void)
89 {
90   GParamSpec *pspec;
91   GValue value = G_VALUE_INIT;
92   gboolean modified;
93
94   pspec = g_param_spec_string ("string", "nick", "blurb",
95                                NULL, G_PARAM_READWRITE);
96   g_value_init (&value, G_TYPE_STRING);
97
98   g_value_set_string (&value, "foobar");
99   modified = g_param_value_validate (pspec, &value);
100   g_assert (!modified);
101
102   g_value_set_string (&value, "");
103   modified = g_param_value_validate (pspec, &value);
104   g_assert (!modified && g_value_get_string (&value) != NULL);
105
106   /* test ensure_non_null */
107
108   G_PARAM_SPEC_STRING (pspec)->ensure_non_null = TRUE;
109
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);
113
114   G_PARAM_SPEC_STRING (pspec)->ensure_non_null = FALSE;
115
116   /* test null_fold_if_empty */
117
118   G_PARAM_SPEC_STRING (pspec)->null_fold_if_empty = TRUE;
119
120   g_value_set_string (&value, "");
121   modified = g_param_value_validate (pspec, &value);
122   g_assert (modified && g_value_get_string (&value) == NULL);
123
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);
127
128   G_PARAM_SPEC_STRING (pspec)->null_fold_if_empty = FALSE;
129
130   /* test cset_first */
131
132   G_PARAM_SPEC_STRING (pspec)->cset_first = g_strdup ("abc");
133   G_PARAM_SPEC_STRING (pspec)->substitutor = '-';
134
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] == '-');
138
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] == '-');
142
143   /* test cset_nth */
144
145   G_PARAM_SPEC_STRING (pspec)->cset_nth = g_strdup ("abc");
146
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] == '-');
150
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] == '-');
154
155   g_value_unset (&value);
156   g_param_spec_unref (pspec);
157 }
158
159 static void
160 test_param_spec_override (void)
161 {
162   GParamSpec *ospec, *pspec;
163   GValue value = G_VALUE_INIT;
164   gboolean modified;
165  
166   ospec = g_param_spec_char ("char", "nick", "blurb",
167                              20, 40, 30, G_PARAM_READWRITE);
168
169   pspec = g_param_spec_override ("override", ospec);
170
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);
174
175   g_value_init (&value, G_TYPE_CHAR);
176   g_value_set_char (&value, 30);
177
178   g_assert (g_param_value_defaults (pspec, &value));
179   
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);
183
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);
187
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);
191
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);
195
196   g_param_spec_unref (pspec);
197   g_param_spec_unref (ospec);
198 }
199
200 static void
201 test_param_spec_gtype (void)
202 {
203   GParamSpec *pspec;
204   GValue value = G_VALUE_INIT;
205   gboolean modified;
206   
207   pspec = g_param_spec_gtype ("gtype", "nick", "blurb",
208                               G_TYPE_PARAM, G_PARAM_READWRITE);
209   
210   g_value_init (&value, G_TYPE_GTYPE);
211   g_value_set_gtype (&value, G_TYPE_PARAM);
212
213   g_assert (g_param_value_defaults (pspec, &value));
214   
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);
218
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);
222
223   g_param_spec_unref (pspec);
224 }
225
226 static void
227 test_param_spec_variant (void)
228 {
229   GParamSpec *pspec;
230   GValue value = G_VALUE_INIT;
231   gboolean modified;
232
233   pspec = g_param_spec_variant ("variant", "nick", "blurb",
234                                 G_VARIANT_TYPE ("i"),
235                                 g_variant_new_int32 (42),
236                                 G_PARAM_READWRITE);
237
238   g_value_init (&value, G_TYPE_VARIANT);
239   g_value_set_variant (&value, g_variant_new_int32 (42));
240
241   g_assert (g_param_value_defaults (pspec, &value));
242
243   modified = g_param_value_validate (pspec, &value);
244   g_assert (!modified);
245
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);
252
253   g_param_spec_unref (pspec);
254 }
255
256 int
257 main (int argc, char *argv[])
258 {
259   g_test_init (&argc, &argv, NULL);
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 }