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 #include "gobject/gvaluecollector.h"
35
36 static void
test_enum_transformation(void)37 test_enum_transformation (void)
38 {
39 GType type;
40 GValue orig = { 0, };
41 GValue xform = { 0, };
42 GEnumValue values[] = { {0,"0","0"}, {1,"1","1"}};
43
44 type = g_enum_register_static ("TestEnum", values);
45
46 g_value_init (&orig, type);
47 g_value_set_enum (&orig, 1);
48
49 memset (&xform, 0, sizeof (GValue));
50 g_value_init (&xform, G_TYPE_CHAR);
51 g_value_transform (&orig, &xform);
52 g_assert (g_value_get_char (&xform) == 1);
53
54 memset (&xform, 0, sizeof (GValue));
55 g_value_init (&xform, G_TYPE_UCHAR);
56 g_value_transform (&orig, &xform);
57 g_assert (g_value_get_uchar (&xform) == 1);
58
59 memset (&xform, 0, sizeof (GValue));
60 g_value_init (&xform, G_TYPE_INT);
61 g_value_transform (&orig, &xform);
62 g_assert (g_value_get_int (&xform) == 1);
63
64 memset (&xform, 0, sizeof (GValue));
65 g_value_init (&xform, G_TYPE_UINT);
66 g_value_transform (&orig, &xform);
67 g_assert (g_value_get_uint (&xform) == 1);
68
69 memset (&xform, 0, sizeof (GValue));
70 g_value_init (&xform, G_TYPE_LONG);
71 g_value_transform (&orig, &xform);
72 g_assert (g_value_get_long (&xform) == 1);
73
74 memset (&xform, 0, sizeof (GValue));
75 g_value_init (&xform, G_TYPE_ULONG);
76 g_value_transform (&orig, &xform);
77 g_assert (g_value_get_ulong (&xform) == 1);
78
79 memset (&xform, 0, sizeof (GValue));
80 g_value_init (&xform, G_TYPE_INT64);
81 g_value_transform (&orig, &xform);
82 g_assert (g_value_get_int64 (&xform) == 1);
83
84 memset (&xform, 0, sizeof (GValue));
85 g_value_init (&xform, G_TYPE_UINT64);
86 g_value_transform (&orig, &xform);
87 g_assert (g_value_get_uint64 (&xform) == 1);
88 }
89
90
91 static void
test_gtype_value(void)92 test_gtype_value (void)
93 {
94 GType type;
95 GValue value = { 0, };
96 GValue copy = { 0, };
97
98 g_value_init (&value, G_TYPE_GTYPE);
99
100 g_value_set_gtype (&value, G_TYPE_BOXED);
101 type = g_value_get_gtype (&value);
102 g_assert (type == G_TYPE_BOXED);
103
104 g_value_init (©, G_TYPE_GTYPE);
105 g_value_copy (&value, ©);
106 type = g_value_get_gtype (©);
107 g_assert (type == G_TYPE_BOXED);
108 }
109
110 static gchar *
collect(GValue * value,...)111 collect (GValue *value, ...)
112 {
113 gchar *error;
114 va_list var_args;
115
116 error = NULL;
117
118 va_start (var_args, value);
119 G_VALUE_COLLECT (value, var_args, 0, &error);
120 va_end (var_args);
121
122 return error;
123 }
124
125 static gchar *
lcopy(GValue * value,...)126 lcopy (GValue *value, ...)
127 {
128 gchar *error;
129 va_list var_args;
130
131 error = NULL;
132
133 va_start (var_args, value);
134 G_VALUE_LCOPY (value, var_args, 0, &error);
135 va_end (var_args);
136
137 return error;
138 }
139
140 static void
test_collection(void)141 test_collection (void)
142 {
143 GValue value = { 0, };
144 gchar *error;
145
146 g_value_init (&value, G_TYPE_CHAR);
147 error = collect (&value, 'c');
148 g_assert (error == NULL);
149 g_assert (g_value_get_char (&value) == 'c');
150
151 g_value_unset (&value);
152 g_value_init (&value, G_TYPE_UCHAR);
153 error = collect (&value, 129);
154 g_assert (error == NULL);
155 g_assert (g_value_get_uchar (&value) == 129);
156
157 g_value_unset (&value);
158 g_value_init (&value, G_TYPE_BOOLEAN);
159 error = collect (&value, TRUE);
160 g_assert (error == NULL);
161 g_assert (g_value_get_boolean (&value) == TRUE);
162
163 g_value_unset (&value);
164 g_value_init (&value, G_TYPE_INT);
165 error = collect (&value, G_MAXINT);
166 g_assert (error == NULL);
167 g_assert (g_value_get_int (&value) == G_MAXINT);
168
169 g_value_unset (&value);
170 g_value_init (&value, G_TYPE_UINT);
171 error = collect (&value, G_MAXUINT);
172 g_assert (error == NULL);
173 g_assert (g_value_get_uint (&value) == G_MAXUINT);
174
175 g_value_unset (&value);
176 g_value_init (&value, G_TYPE_LONG);
177 error = collect (&value, G_MAXLONG);
178 g_assert (error == NULL);
179 g_assert (g_value_get_long (&value) == G_MAXLONG);
180
181 g_value_unset (&value);
182 g_value_init (&value, G_TYPE_ULONG);
183 error = collect (&value, G_MAXULONG);
184 g_assert (error == NULL);
185 g_assert (g_value_get_ulong (&value) == G_MAXULONG);
186
187 g_value_unset (&value);
188 g_value_init (&value, G_TYPE_INT64);
189 error = collect (&value, G_MAXINT64);
190 g_assert (error == NULL);
191 g_assert (g_value_get_int64 (&value) == G_MAXINT64);
192
193 g_value_unset (&value);
194 g_value_init (&value, G_TYPE_UINT64);
195 error = collect (&value, G_MAXUINT64);
196 g_assert (error == NULL);
197 g_assert (g_value_get_uint64 (&value) == G_MAXUINT64);
198
199 g_value_unset (&value);
200 g_value_init (&value, G_TYPE_FLOAT);
201 error = collect (&value, G_MAXFLOAT);
202 g_assert (error == NULL);
203 g_assert (g_value_get_float (&value) == G_MAXFLOAT);
204
205 g_value_unset (&value);
206 g_value_init (&value, G_TYPE_DOUBLE);
207 error = collect (&value, G_MAXDOUBLE);
208 g_assert (error == NULL);
209 g_assert (g_value_get_double (&value) == G_MAXDOUBLE);
210
211 g_value_unset (&value);
212 g_value_init (&value, G_TYPE_STRING);
213 error = collect (&value, "string ?");
214 g_assert (error == NULL);
215 g_assert (strcmp (g_value_get_string (&value), "string ?") == 0);
216
217 g_value_unset (&value);
218 g_value_init (&value, G_TYPE_GTYPE);
219 error = collect (&value, G_TYPE_BOXED);
220 g_assert (error == NULL);
221 g_assert (g_value_get_gtype (&value) == G_TYPE_BOXED);
222 }
223
224 static void
test_copying(void)225 test_copying (void)
226 {
227 GValue value = { 0, };
228 gchar *error;
229
230 {
231 gchar c = 0;
232
233 g_value_init (&value, G_TYPE_CHAR);
234 g_value_set_char (&value, 'c');
235 error = lcopy (&value, &c);
236 g_assert (error == NULL);
237 g_assert (c == 'c');
238 }
239
240 {
241 guchar c = 0;
242
243 g_value_unset (&value);
244 g_value_init (&value, G_TYPE_UCHAR);
245 g_value_set_uchar (&value, 129);
246 error = lcopy (&value, &c);
247 g_assert (error == NULL);
248 g_assert (c == 129);
249 }
250
251 {
252 gint c = 0;
253
254 g_value_unset (&value);
255 g_value_init (&value, G_TYPE_INT);
256 g_value_set_int (&value, G_MAXINT);
257 error = lcopy (&value, &c);
258 g_assert (error == NULL);
259 g_assert (c == G_MAXINT);
260 }
261
262 {
263 guint c = 0;
264
265 g_value_unset (&value);
266 g_value_init (&value, G_TYPE_UINT);
267 g_value_set_uint (&value, G_MAXUINT);
268 error = lcopy (&value, &c);
269 g_assert (error == NULL);
270 g_assert (c == G_MAXUINT);
271 }
272
273 {
274 glong c = 0;
275
276 g_value_unset (&value);
277 g_value_init (&value, G_TYPE_LONG);
278 g_value_set_long (&value, G_MAXLONG);
279 error = lcopy (&value, &c);
280 g_assert (error == NULL);
281 g_assert (c == G_MAXLONG);
282 }
283
284 {
285 gulong c = 0;
286
287 g_value_unset (&value);
288 g_value_init (&value, G_TYPE_ULONG);
289 g_value_set_ulong (&value, G_MAXULONG);
290 error = lcopy (&value, &c);
291 g_assert (error == NULL);
292 g_assert (c == G_MAXULONG);
293 }
294
295 {
296 gint64 c = 0;
297
298 g_value_unset (&value);
299 g_value_init (&value, G_TYPE_INT64);
300 g_value_set_int64 (&value, G_MAXINT64);
301 error = lcopy (&value, &c);
302 g_assert (error == NULL);
303 g_assert (c == G_MAXINT64);
304 }
305
306 {
307 guint64 c = 0;
308
309 g_value_unset (&value);
310 g_value_init (&value, G_TYPE_UINT64);
311 g_value_set_uint64 (&value, G_MAXUINT64);
312 error = lcopy (&value, &c);
313 g_assert (error == NULL);
314 g_assert (c == G_MAXUINT64);
315 }
316
317 {
318 gfloat c = 0;
319
320 g_value_unset (&value);
321 g_value_init (&value, G_TYPE_FLOAT);
322 g_value_set_float (&value, G_MAXFLOAT);
323 error = lcopy (&value, &c);
324 g_assert (error == NULL);
325 g_assert (c == G_MAXFLOAT);
326 }
327
328 {
329 gdouble c = 0;
330
331 g_value_unset (&value);
332 g_value_init (&value, G_TYPE_DOUBLE);
333 g_value_set_double (&value, G_MAXDOUBLE);
334 error = lcopy (&value, &c);
335 g_assert (error == NULL);
336 g_assert (c == G_MAXDOUBLE);
337 }
338
339 {
340 gchar *c = NULL;
341
342 g_value_unset (&value);
343 g_value_init (&value, G_TYPE_STRING);
344 g_value_set_string (&value, "string ?");
345 error = lcopy (&value, &c);
346 g_assert (error == NULL);
347 g_assert (strcmp (c, "string ?") == 0);
348 }
349
350 {
351 GType c = G_TYPE_NONE;
352
353 g_value_unset (&value);
354 g_value_init (&value, G_TYPE_GTYPE);
355 g_value_set_gtype (&value, G_TYPE_BOXED);
356 error = lcopy (&value, &c);
357 g_assert (error == NULL);
358 g_assert (c == G_TYPE_BOXED);
359 }
360 }
361
362
363 int
main(int argc,char * argv[])364 main (int argc, char *argv[])
365 {
366 g_type_init ();
367
368 test_enum_transformation ();
369 test_gtype_value ();
370 test_collection ();
371 test_copying ();
372
373 return 0;
374 }
375