Initial commit
[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 = { 0, };
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_param_spec_unref (pspec);
71 }
72
73 static void
74 test_param_spec_string (void)
75 {
76   GParamSpec *pspec;
77   GValue value = { 0, };
78   gboolean modified;
79
80   pspec = g_param_spec_string ("string", "nick", "blurb",
81                                NULL, G_PARAM_READWRITE);
82   g_value_init (&value, G_TYPE_STRING);
83
84   g_value_set_string (&value, "foobar");
85   modified = g_param_value_validate (pspec, &value);
86   g_assert (!modified);
87
88   g_value_set_string (&value, "");
89   modified = g_param_value_validate (pspec, &value);
90   g_assert (!modified && g_value_get_string (&value) != NULL);
91
92   /* test ensure_non_null */
93
94   G_PARAM_SPEC_STRING (pspec)->ensure_non_null = TRUE;
95
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);
99
100   G_PARAM_SPEC_STRING (pspec)->ensure_non_null = FALSE;
101
102   /* test null_fold_if_empty */
103
104   G_PARAM_SPEC_STRING (pspec)->null_fold_if_empty = TRUE;
105
106   g_value_set_string (&value, "");
107   modified = g_param_value_validate (pspec, &value);
108   g_assert (modified && g_value_get_string (&value) == NULL);
109
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);
113
114   G_PARAM_SPEC_STRING (pspec)->null_fold_if_empty = FALSE;
115
116   /* test cset_first */
117
118   G_PARAM_SPEC_STRING (pspec)->cset_first = g_strdup ("abc");
119   G_PARAM_SPEC_STRING (pspec)->substitutor = '-';
120
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] == '-');
124
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] == '-');
128
129   /* test cset_nth */
130
131   G_PARAM_SPEC_STRING (pspec)->cset_nth = g_strdup ("abc");
132
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] == '-');
136
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] == '-');
140
141   g_value_unset (&value);
142   g_param_spec_unref (pspec);
143 }
144
145 static void
146 test_param_spec_override (void)
147 {
148   GParamSpec *ospec, *pspec;
149   GValue value = { 0, };
150   gboolean modified;
151  
152   ospec = g_param_spec_char ("char", "nick", "blurb",
153                              20, 40, 30, G_PARAM_READWRITE);
154
155   pspec = g_param_spec_override ("override", ospec);
156
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);
160
161   g_value_init (&value, G_TYPE_CHAR);
162   g_value_set_char (&value, 30);
163
164   g_assert (g_param_value_defaults (pspec, &value));
165   
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);
169
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);
173
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);
177
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);
181
182   g_param_spec_unref (pspec);
183 }
184
185 static void
186 test_param_spec_gtype (void)
187 {
188   GParamSpec *pspec;
189   GValue value = { 0, };
190   gboolean modified;
191   
192   pspec = g_param_spec_gtype ("gtype", "nick", "blurb",
193                               G_TYPE_PARAM, G_PARAM_READWRITE);
194   
195   g_value_init (&value, G_TYPE_GTYPE);
196   g_value_set_gtype (&value, G_TYPE_PARAM);
197
198   g_assert (g_param_value_defaults (pspec, &value));
199   
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);
203
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);
207 }
208
209 int
210 main (int argc, char *argv[])
211 {
212   g_type_init (); 
213   
214   test_param_spec_char ();
215   test_param_spec_string ();
216   test_param_spec_override ();
217   test_param_spec_gtype ();
218
219   return 0;
220 }