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