Merge branch 'test-ip_mreq_source-android-only' into 'master'
[glib.git] / tests / gobject / gvalue-test.c
blobb73fe3150f5190d103a6a9b5ee9d39606fa908cc
1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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/>.
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/.
25 #undef G_DISABLE_ASSERT
26 #undef G_LOG_DOMAIN
28 #include <string.h>
30 #include <glib.h>
31 #include <glib-object.h>
32 #include "gobject/gvaluecollector.h"
34 static void
35 test_enum_transformation (void)
37 GType type;
38 GValue orig = G_VALUE_INIT;
39 GValue xform = G_VALUE_INIT;
40 GEnumValue values[] = { {0,"0","0"}, {1,"1","1"}};
42 type = g_enum_register_static ("TestEnum", values);
44 g_value_init (&orig, type);
45 g_value_set_enum (&orig, 1);
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);
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);
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);
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);
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);
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);
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);
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);
90 static void
91 test_gtype_value (void)
93 GType type;
94 GValue value = G_VALUE_INIT;
95 GValue copy = G_VALUE_INIT;
97 g_value_init (&value, G_TYPE_GTYPE);
99 g_value_set_gtype (&value, G_TYPE_BOXED);
100 type = g_value_get_gtype (&value);
101 g_assert (type == G_TYPE_BOXED);
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);
109 static gchar *
110 collect (GValue *value, ...)
112 gchar *error;
113 va_list var_args;
115 error = NULL;
117 va_start (var_args, value);
118 G_VALUE_COLLECT (value, var_args, 0, &error);
119 va_end (var_args);
121 return error;
124 static gchar *
125 lcopy (GValue *value, ...)
127 gchar *error;
128 va_list var_args;
130 error = NULL;
132 va_start (var_args, value);
133 G_VALUE_LCOPY (value, var_args, 0, &error);
134 va_end (var_args);
136 return error;
139 static void
140 test_collection (void)
142 GValue value = G_VALUE_INIT;
143 gchar *error;
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');
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
229 g_value_unset (&value);
232 static void
233 test_copying (void)
235 GValue value = G_VALUE_INIT;
236 gchar *error;
239 gchar c = 0;
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');
249 guchar c = 0;
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);
260 gint c = 0;
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);
271 guint c = 0;
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);
282 glong c = 0;
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);
293 gulong c = 0;
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);
304 gint64 c = 0;
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);
315 guint64 c = 0;
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);
326 gfloat c = 0;
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);
337 gdouble c = 0;
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);
348 gchar *c = NULL;
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);
360 GType c = G_TYPE_NONE;
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);
371 GVariant *c = NULL;
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);
388 main (int argc, char *argv[])
390 g_test_init (&argc, &argv, NULL);
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);
397 return g_test_run ();