nm-param-spec-specialized.c 26 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */

/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA.
 *
19 20
 * Copyright 2007 - 2011 Red Hat, Inc.
 * Copyright 2007 - 2008 Novell, Inc.
21
 */
22

23
#include "nm-default.h"
24

Dan Winship's avatar
Dan Winship committed
25
#include "nm-gvaluearray-compat.h"
26 27 28 29 30 31 32
#include "nm-param-spec-specialized.h"

struct _NMParamSpecSpecialized {
	GParamSpec parent;
};

#include <string.h>
33
#include <netinet/in.h>
34 35
#include <dbus/dbus-glib.h>

36 37
#include "nm-dbus-glib-types.h"

38
/*****************************************************************************/
39
/* _gvalues_compare */
40

41
static gint _gvalues_compare (const GValue *value1, const GValue *value2);
42 43

static gboolean
44
type_is_fixed_size (GType type, gsize *tsize)
45 46 47
{
	switch (type) {
	case G_TYPE_CHAR:
48 49
		if (tsize) *tsize = sizeof (char);
		return TRUE;
50
	case G_TYPE_UCHAR:
51 52
		if (tsize) *tsize = sizeof (guchar);
		return TRUE;
53
	case G_TYPE_BOOLEAN:
54 55
		if (tsize) *tsize = sizeof (gboolean);
		return TRUE;
56
	case G_TYPE_LONG:
57 58
		if (tsize) *tsize = sizeof (glong);
		return TRUE;
59
	case G_TYPE_ULONG:
60 61
		if (tsize) *tsize = sizeof (gulong);
		return TRUE;
62
	case G_TYPE_INT:
63 64
		if (tsize) *tsize = sizeof (gint);
		return TRUE;
65
	case G_TYPE_UINT:
66 67
		if (tsize) *tsize = sizeof (guint);
		return TRUE;
68
	case G_TYPE_INT64:
69 70
		if (tsize) *tsize = sizeof (gint64);
		return TRUE;
71
	case G_TYPE_UINT64:
72 73
		if (tsize) *tsize = sizeof (guint64);
		return TRUE;
74
	case G_TYPE_FLOAT:
75 76
		if (tsize) *tsize = sizeof (gfloat);
		return TRUE;
77
	case G_TYPE_DOUBLE:
78
		if (tsize) *tsize = sizeof (gdouble);
79 80 81 82 83 84
		return TRUE;
	default:
		return FALSE;
	}
}

85 86
#define FLOAT_FACTOR 0.00000001

87
static gint
88
_gvalues_compare_fixed (const GValue *value1, const GValue *value2)
89 90 91 92 93
{
	int ret = 0;

	switch (G_VALUE_TYPE (value1)) {
	case G_TYPE_CHAR: {
94 95
		gchar val1 = g_value_get_schar (value1);
		gchar val2 = g_value_get_schar (value2);
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
		if (val1 != val2)
			ret = val1 < val2 ? -1 : val1 > val2;
		break;
	}
	case G_TYPE_UCHAR: {
		guchar val1 = g_value_get_uchar (value1);
		guchar val2 = g_value_get_uchar (value2);
		if (val1 != val2)
			ret = val1 < val2 ? -1 : val1 > val2;
		break;
	}
	case G_TYPE_BOOLEAN: {
		gboolean val1 = g_value_get_boolean (value1);
		gboolean val2 = g_value_get_boolean (value2);
		if (val1 != val2)
			ret = val1 < val2 ? -1 : val1 > val2;
		break;
	}
	case G_TYPE_LONG: {
		glong val1 = g_value_get_long (value1);
		glong val2 = g_value_get_long (value2);
		if (val1 != val2)
			ret = val1 < val2 ? -1 : val1 > val2;
		break;
	}
	case G_TYPE_ULONG: {
		gulong val1 = g_value_get_ulong (value1);
		gulong val2 = g_value_get_ulong (value2);
		if (val1 != val2)
			ret = val1 < val2 ? -1 : val1 > val2;
		break;
	}
	case G_TYPE_INT: {
		gint val1 = g_value_get_int (value1);
		gint val2 = g_value_get_int (value2);
		if (val1 != val2)
			ret = val1 < val2 ? -1 : val1 > val2;
		break;
	}
	case G_TYPE_UINT: {
		guint val1 = g_value_get_uint (value1);
		guint val2 = g_value_get_uint (value2);
		if (val1 != val2)
			ret = val1 < val2 ? -1 : val1 > val2;
		break;
	}
	case G_TYPE_INT64: {
		gint64 val1 = g_value_get_int64 (value1);
		gint64 val2 = g_value_get_int64 (value2);
		if (val1 != val2)
			ret = val1 < val2 ? -1 : val1 > val2;
		break;
	}
	case G_TYPE_UINT64: {
		guint64 val1 = g_value_get_uint64 (value1);
		guint64 val2 = g_value_get_uint64 (value2);
		if (val1 != val2)
			ret = val1 < val2 ? -1 : val1 > val2;
		break;
	}
	case G_TYPE_FLOAT: {
		gfloat val1 = g_value_get_float (value1);
		gfloat val2 = g_value_get_float (value2);
159 160
		float diff = val1 - val2;

161
		/* Can't use == or != here due to inexactness of FP */
162
		if (diff > FLOAT_FACTOR || diff < -FLOAT_FACTOR)
163 164 165 166 167 168
			ret = val1 < val2 ? -1 : val1 > val2;
		break;
	}
	case G_TYPE_DOUBLE: {
		gdouble val1 = g_value_get_double (value1);
		gdouble val2 = g_value_get_double (value2);
169 170 171
		double diff = val1 - val2;

		if (diff > FLOAT_FACTOR || diff < -FLOAT_FACTOR)
172 173 174 175 176 177 178 179 180 181 182
			ret = val1 < val2 ? -1 : val1 > val2;
		break;
	}
	default:
		g_warning ("Unhandled fixed size type '%s'", G_VALUE_TYPE_NAME (value1));
	}

	return ret;
}

static gint
183
_gvalues_compare_string (const GValue *value1, const GValue *value2)
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
{
	const char *str1 = g_value_get_string (value1);
	const char *str2 = g_value_get_string (value2);

	if (str1 == str2)
		return 0;

	if (!str1)
		return 1;
	if (!str2)
		return -1;

	return strcmp (str1, str2);
}

static gint
200
_gvalues_compare_strv (const GValue *value1, const GValue *value2)
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
{
	char **strv1;
	char **strv2;
	gint ret;
	guint i = 0;

	strv1 = (char **) g_value_get_boxed (value1);
	strv2 = (char **) g_value_get_boxed (value2);

	while (strv1[i] && strv2[i]) {
		ret = strcmp (strv1[i], strv2[i]);
		if (ret)
			return ret;
		i++;
	}

	if (strv1[i] == NULL && strv2[i] == NULL)
		return 0;

	if (strv1[i])
		return 1;

	return -1;
}

static void
227
_gvalue_destroy (gpointer data)
228 229 230 231 232 233 234 235
{
	GValue *value = (GValue *) data;

	g_value_unset (value);
	g_slice_free (GValue, value);
}

static GValue *
236
_gvalue_dup (const GValue *value)
237 238 239 240 241 242 243 244 245 246 247 248 249 250
{
	GValue *dup;

	dup = g_slice_new0 (GValue);
	g_value_init (dup, G_VALUE_TYPE (value));
	g_value_copy (value, dup);

	return dup;
}

static void
iterate_collection (const GValue *value, gpointer user_data)
{
	GSList **list = (GSList **) user_data;
251

252
	*list = g_slist_prepend (*list, _gvalue_dup (value));
253 254 255
}

static gint
256
_gvalues_compare_collection (const GValue *value1, const GValue *value2)
257 258 259 260 261
{
	gint ret;
	guint len1;
	guint len2;
	GType value_type = dbus_g_type_get_collection_specialization (G_VALUE_TYPE (value1));
262
	gsize element_size = 0;
263

264
	if (type_is_fixed_size (value_type, &element_size)) {
265 266 267 268 269 270 271 272 273
		gpointer data1 = NULL;
		gpointer data2 = NULL;

		dbus_g_type_collection_get_fixed ((GValue *) value1, &data1, &len1);
		dbus_g_type_collection_get_fixed ((GValue *) value2, &data2, &len2);

		if (len1 != len2)
			ret = len1 < len2 ? -1 : len1 > len2;
		else
274
			ret = memcmp (data1, data2, len1 * element_size);
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
	} else {
		GSList *list1 = NULL;
		GSList *list2 = NULL;

		dbus_g_type_collection_value_iterate (value1, iterate_collection, &list1);
		len1 = g_slist_length (list1);
		dbus_g_type_collection_value_iterate (value2, iterate_collection, &list2);
		len2 = g_slist_length (list2);

		if (len1 != len2)
			ret = len1 < len2 ? -1 : len1 > len2;
		else {
			GSList *iter1;
			GSList *iter2;

			for (iter1 = list1, iter2 = list2, ret = 0;
291 292
			     ret == 0 && iter1 && iter2;
			     iter1 = iter1->next, iter2 = iter2->next)
293
				ret = _gvalues_compare ((GValue *) iter1->data, (GValue *) iter2->data);
294 295
		}

296 297
		g_slist_free_full (list1, _gvalue_destroy);
		g_slist_free_full (list2, _gvalue_destroy);
298 299 300 301 302 303 304
	}

	return ret;
}

static void
iterate_map (const GValue *key_val,
305 306
             const GValue *value_val,
             gpointer user_data)
307 308 309
{
	GHashTable **hash = (GHashTable **) user_data;

310
	g_hash_table_insert (*hash, g_value_dup_string (key_val), _gvalue_dup (value_val));
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
}

typedef struct {
	GHashTable *hash2;
	gint ret;
} CompareMapInfo;

static void
compare_one_map_item (gpointer key, gpointer val, gpointer user_data)
{
	CompareMapInfo *info = (CompareMapInfo *) user_data;
	GValue *value2;

	if (info->ret)
		return;

	value2 = (GValue *) g_hash_table_lookup (info->hash2, key);
	if (value2)
329
		info->ret = _gvalues_compare ((GValue *) val, value2);
330 331 332 333 334
	else
		info->ret = 1;
}

static gint
335
_gvalues_compare_map (const GValue *value1, const GValue *value2)
336 337 338 339 340 341 342 343 344
{
	GHashTable *hash1 = NULL;
	GHashTable *hash2 = NULL;
	guint len1;
	guint len2;
	gint ret = 0;

	if (dbus_g_type_get_map_key_specialization (G_VALUE_TYPE (value1)) != G_TYPE_STRING) {
		g_warning ("Can not compare maps with '%s' for keys",
345
		           g_type_name (dbus_g_type_get_map_key_specialization (G_VALUE_TYPE (value1))));
346 347 348
		return 0;
	}

349
	hash1 = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, _gvalue_destroy);
Thomas Haller's avatar
Thomas Haller committed
350
	dbus_g_type_map_value_iterate (value1, iterate_map, &hash1);
351 352
	len1 = g_hash_table_size (hash1);

353
	hash2 = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, _gvalue_destroy);
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
	dbus_g_type_map_value_iterate (value2, iterate_map, &hash2);
	len2 = g_hash_table_size (hash2);

	if (len1 != len2)
		ret = len1 < len2 ? -1 : len1 > len2;
	else {
		CompareMapInfo info;

		info.ret = 0;
		info.hash2 = hash2;
		g_hash_table_foreach (hash1, compare_one_map_item, &info);
		ret = info.ret;
	}

	g_hash_table_destroy (hash1);
	g_hash_table_destroy (hash2);

	return ret;
}

374
static gint
375
_gvalue_ip6_address_compare (const GValue *value1, const GValue *value2)
376 377 378 379 380
{
	GValueArray *values1, *values2;
	GValue *tmp_val;
	GByteArray *addr1, *addr2;
	guint32 prefix1, prefix2;
381
	GByteArray *gw1, *gw2;
382 383 384 385 386 387 388 389 390 391
	gint ret = 0;
	int i;

	/* IP6 addresses are GValueArrays (see nm-dbus-glib-types.h) */
	values1 = g_value_get_boxed (value1);
	values2 = g_value_get_boxed (value2);

	/* Since they are NM IPv6 address structures, we expect both
	 * to contain two elements as specified in nm-dbus-glib-types.h.
	 */
392 393
	g_return_val_if_fail (values1->n_values == 3, 0);
	g_return_val_if_fail (values2->n_values == 3, 0);
394 395 396 397 398 399 400

	/* First struct IPv6 address */
	tmp_val = g_value_array_get_nth (values1, 0);
	addr1 = g_value_get_boxed (tmp_val);
	/* First struct IPv6 prefix */
	tmp_val = g_value_array_get_nth (values1, 1);
	prefix1 = g_value_get_uint (tmp_val);
401 402 403
	/* First struct IPv6 gateway */
	tmp_val = g_value_array_get_nth (values1, 2);
	gw1 = g_value_get_boxed (tmp_val);
404 405 406 407 408 409 410

	/* Second struct IPv6 address */
	tmp_val = g_value_array_get_nth (values2, 0);
	addr2 = g_value_get_boxed (tmp_val);
	/* Second struct IPv6 prefix */
	tmp_val = g_value_array_get_nth (values2, 1);
	prefix2 = g_value_get_uint (tmp_val);
411 412 413
	/* Second struct IPv6 gateway */
	tmp_val = g_value_array_get_nth (values2, 2);
	gw2 = g_value_get_boxed (tmp_val);
414 415 416 417 418 419 420 421 422 423

	/* Compare IPv6 addresses */
	if (prefix1 != prefix2)
		return prefix1 < prefix2 ? -1 : prefix1 > prefix2;

	if (!IN6_ARE_ADDR_EQUAL ((struct in6_addr *)addr1->data, (struct in6_addr *)addr2->data)) {
		for (i = 0; ret == 0 && i < addr1->len; i++)
			ret = addr1->data[i] < addr2->data[i] ? -1 : addr1->data[i] > addr2->data[i];
	}

424 425 426 427 428
	if (!IN6_ARE_ADDR_EQUAL ((struct in6_addr *) gw1->data, (struct in6_addr *) gw2->data)) {
		for (i = 0; ret == 0 && i < gw1->len; i++)
			ret = gw1->data[i] < gw2->data[i] ? -1 : gw1->data[i] > gw2->data[i];
	}

429 430 431 432
	return ret;
}

static gint
433
_gvalue_ip6_route_compare (const GValue *value1, const GValue *value2)
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
{
	GValueArray *values1, *values2;
	GValue *tmp_val;
	GByteArray *dest1, *dest2;
	GByteArray *next_hop1, *next_hop2;
	guint32 prefix1, prefix2;
	guint32 metric1, metric2;
	gint ret = 0;
	int i;

	/* IP6 routes are GValueArrays (see nm-dbus-glib-types.h) */
	values1 = g_value_get_boxed (value1);
	values2 = g_value_get_boxed (value2);

	/* Since they are NM IPv6 route structures, we expect both
	 * to contain 4 elements as specified in nm-dbus-glib-types.h.
	 */
	g_return_val_if_fail (values1->n_values == 4, 0);
	g_return_val_if_fail (values2->n_values == 4, 0);

	/* First struct IPv6 route */
	tmp_val = g_value_array_get_nth (values1, 0);
	dest1 = g_value_get_boxed (tmp_val);
	tmp_val = g_value_array_get_nth (values1, 1);
	prefix1 = g_value_get_uint (tmp_val);
	tmp_val = g_value_array_get_nth (values1, 2);
	next_hop1 = g_value_get_boxed (tmp_val);
	tmp_val = g_value_array_get_nth (values1, 3);
	metric1 = g_value_get_uint (tmp_val);

	/* Second struct IPv6 route */
	tmp_val = g_value_array_get_nth (values2, 0);
	dest2 = g_value_get_boxed (tmp_val);
	tmp_val = g_value_array_get_nth (values2, 1);
	prefix2 = g_value_get_uint (tmp_val);
	tmp_val = g_value_array_get_nth (values2, 2);
	next_hop2 = g_value_get_boxed (tmp_val);
	tmp_val = g_value_array_get_nth (values2, 3);
	metric2 = g_value_get_uint (tmp_val);

	/* Compare the routes */
	if (prefix1 != prefix2)
		return prefix1 < prefix2 ? -1 : prefix1 > prefix2;

	if (!IN6_ARE_ADDR_EQUAL ((struct in6_addr *)dest1->data, (struct in6_addr *)dest2->data)) {
		for (i = 0; ret == 0 && i < dest1->len; i++)
			ret = dest1->data[i] < dest2->data[i] ? -1 : dest1->data[i] > dest2->data[i];
	}

	if (!IN6_ARE_ADDR_EQUAL ((struct in6_addr *)next_hop1->data, (struct in6_addr *)next_hop2->data)) {
		for (i = 0; ret == 0 && i < next_hop1->len; i++)
			ret = next_hop1->data[i] < next_hop2->data[i] ? -1 : next_hop1->data[i] > next_hop2->data[i];
	}

	if (metric1 != metric2)
		ret = metric1 < metric2 ? -1 : metric1 > metric2;

	return ret;
}

494
static gint
495
_gvalues_compare_struct (const GValue *value1, const GValue *value2)
496
{
497
	/* value1 and value2 must contain the same type since
498
	 * _gvalues_compare() enforced that already.
499 500 501
	 */

	if (G_VALUE_HOLDS (value1, DBUS_TYPE_G_IP6_ADDRESS)) {
502
		return _gvalue_ip6_address_compare (value1, value2);
503
	} else if (G_VALUE_HOLDS (value1, DBUS_TYPE_G_IP6_ROUTE)) {
504
		return _gvalue_ip6_route_compare (value1, value2);
505 506 507 508
	} else {
		g_warning ("Don't know how to compare structures");
		return (value1 == value2);
	}
509 510 511
}

gint
512
_gvalues_compare (const GValue *value1, const GValue *value2)
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
{
	GType type1;
	GType type2;
	gint ret;

	if (value1 == value2)
		return 0;
	if (!value1)
		return 1;
	if (!value2)
		return -1;

	type1 = G_VALUE_TYPE (value1);
	type2 = G_VALUE_TYPE (value2);

	if (type1 != type2)
		return type1 < type2 ? -1 : type1 > type2;

531
	if (type_is_fixed_size (type1, NULL))
532
		ret = _gvalues_compare_fixed (value1, value2);
533
	else if (type1 == G_TYPE_STRING)
534
		ret = _gvalues_compare_string (value1, value2);
535 536 537 538 539 540 541 542 543 544 545
	else if (G_VALUE_HOLDS_BOXED (value1)) {
		gpointer p1 = g_value_get_boxed (value1);
		gpointer p2 = g_value_get_boxed (value2);

		if (p1 == p2)
			ret = 0; /* Exactly the same values */
		else if (!p1)
			ret = 1; /* The comparision functions below don't handle NULLs */
		else if (!p2)
			ret = -1; /* The comparision functions below don't handle NULLs */
		else if (type1 == G_TYPE_STRV)
546
			ret = _gvalues_compare_strv (value1, value2);
547
		else if (dbus_g_type_is_collection (type1))
548
			ret = _gvalues_compare_collection (value1, value2);
549
		else if (dbus_g_type_is_map (type1))
550
			ret = _gvalues_compare_map (value1, value2);
551
		else if (dbus_g_type_is_struct (type1))
552
			ret = _gvalues_compare_struct (value1, value2);
553
		else if (type1 == G_TYPE_VALUE)
554
			ret = _gvalues_compare ((GValue *) g_value_get_boxed (value1), (GValue *) g_value_get_boxed (value2));
555 556 557 558 559 560 561 562 563 564 565 566
		else {
			g_warning ("Don't know how to compare boxed types '%s'", g_type_name (type1));
			ret = value1 == value2;
		}
	} else {
		g_warning ("Don't know how to compare types '%s'", g_type_name (type1));
		ret = value1 == value2;
	}

	return ret;
}

567
/*****************************************************************************/
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596

static void
param_specialized_init (GParamSpec *pspec)
{
}

static void
param_specialized_set_default (GParamSpec *pspec, GValue *value)
{
	value->data[0].v_pointer = NULL;
}

static gboolean
param_specialized_validate (GParamSpec *pspec, GValue *value)
{
	NMParamSpecSpecialized *sspec = NM_PARAM_SPEC_SPECIALIZED (pspec);
	GType value_type = G_VALUE_TYPE (value);
	gboolean changed = FALSE;

	if (!g_value_type_compatible (value_type, G_PARAM_SPEC_VALUE_TYPE (sspec))) {
		g_value_reset (value);
		changed = TRUE;
	}

	return changed;
}

static gint
param_specialized_values_cmp (GParamSpec *pspec,
597 598
                              const GValue *value1,
                              const GValue *value2)
599
{
600
	return _gvalues_compare (value1, value2);
601 602 603
}

GType
604
_nm_param_spec_specialized_get_type (void)
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
{
	static GType type;

	if (G_UNLIKELY (type) == 0) {
		static const GParamSpecTypeInfo pspec_info = {
			sizeof (NMParamSpecSpecialized),
			0,
			param_specialized_init,
			G_TYPE_OBJECT, /* value_type */
			NULL,          /* finalize */
			param_specialized_set_default,
			param_specialized_validate,
			param_specialized_values_cmp,
		};
		type = g_param_type_register_static ("NMParamSpecSpecialized", &pspec_info);
	}

	return type;
}

GParamSpec *
626
_nm_param_spec_specialized (const char *name,
627 628 629 630
                            const char *nick,
                            const char *blurb,
                            GType specialized_type,
                            GParamFlags flags)
631 632 633 634 635 636
{
	NMParamSpecSpecialized *pspec;

	g_return_val_if_fail (g_type_is_a (specialized_type, G_TYPE_BOXED), NULL);

	pspec = g_param_spec_internal (NM_TYPE_PARAM_SPEC_SPECIALIZED,
637
	                               name, nick, blurb, flags);
638 639 640 641 642 643

	G_PARAM_SPEC (pspec)->value_type = specialized_type;

	return G_PARAM_SPEC (pspec);
}

644
/*****************************************************************************/
645 646 647 648 649 650 651
/* Tests */

#if 0

static void
compare_ints (void)
{
652 653
	GValue value1 = G_VALUE_INIT;
	GValue value2 = G_VALUE_INIT;
654 655 656 657 658 659

	g_value_init (&value1, G_TYPE_INT);
	g_value_init (&value2, G_TYPE_INT);

	g_value_set_int (&value1, 5);
	g_value_set_int (&value2, 5);
660
	g_print ("Comparing ints 5 and 5: %d\n", _gvalues_compare (&value1, &value2));
661 662

	g_value_set_int (&value2, 10);
663
	g_print ("Comparing ints 5 and 10: %d\n", _gvalues_compare (&value1, &value2));
664 665

	g_value_set_int (&value2, 1);
666
	g_print ("Comparing ints 5 and 1: %d\n", _gvalues_compare (&value1, &value2));
667 668 669 670 671
}

static void
compare_strings (void)
{
672 673
	GValue value1 = G_VALUE_INIT;
	GValue value2 = G_VALUE_INIT;
674 675 676 677 678 679 680 681
	const char *str1 = "hello";
	const char *str2 = "world";

	g_value_init (&value1, G_TYPE_STRING);
	g_value_init (&value2, G_TYPE_STRING);

	g_value_set_string (&value1, str1);
	g_value_set_string (&value2, str1);
682
	g_print ("Comparing identical strings: %d\n", _gvalues_compare (&value1, &value2));
683 684

	g_value_set_string (&value2, str2);
685
	g_print ("Comparing different strings: %d\n", _gvalues_compare (&value1, &value2));
686 687 688 689 690
}

static void
compare_strv (void)
{
691 692
	GValue value1 = G_VALUE_INIT;
	GValue value2 = G_VALUE_INIT;
693 694 695 696 697 698 699 700 701 702
	char *strv1[] = { "foo", "bar", "baz", NULL };
	char *strv2[] = { "foo", "bar", "bar", NULL };
	char *strv3[] = { "foo", "bar", NULL };
	char *strv4[] = { "foo", "bar", "baz", "bam", NULL };

	g_value_init (&value1, G_TYPE_STRV);
	g_value_init (&value2, G_TYPE_STRV);

	g_value_set_boxed (&value1, strv1);
	g_value_set_boxed (&value2, strv1);
703
	g_print ("Comparing identical strv's: %d\n", _gvalues_compare (&value1, &value2));
704 705

	g_value_set_boxed (&value2, strv2);
706
	g_print ("Comparing different strv's: %d\n", _gvalues_compare (&value1, &value2));
707 708

	g_value_set_boxed (&value2, strv3);
709
	g_print ("Comparing different len (smaller) strv's: %d\n", _gvalues_compare (&value1, &value2));
710 711

	g_value_set_boxed (&value2, strv4);
712
	g_print ("Comparing different len (longer) strv's: %d\n", _gvalues_compare (&value1, &value2));
713 714 715 716 717 718 719
}

static void
compare_garrays (void)
{
	GArray *array1;
	GArray *array2;
720 721
	GValue value1 = G_VALUE_INIT;
	GValue value2 = G_VALUE_INIT;
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
	int i;

	g_value_init (&value1, DBUS_TYPE_G_UINT_ARRAY);
	array1 = g_array_new (FALSE, FALSE, sizeof (guint32));

	g_value_init (&value2, DBUS_TYPE_G_UINT_ARRAY);
	array2 = g_array_new (FALSE, FALSE, sizeof (guint32));

	for (i = 0; i < 5; i++) {
		g_array_append_val (array1, i);
		g_array_append_val (array2, i);
	}

	g_value_set_boxed (&value1, array1);
	g_value_set_boxed (&value2, array2);

738
	g_print ("Comparing identical arrays's: %d\n", _gvalues_compare (&value1, &value2));
739 740 741

	g_array_remove_index (array2, 0);
	g_value_set_boxed (&value2, array2);
742
	g_print ("Comparing different length arrays's: %d\n", _gvalues_compare (&value1, &value2));
743 744 745 746

	i = 7;
	g_array_prepend_val (array2, i);
	g_value_set_boxed (&value2, array2);
747
	g_print ("Comparing different arrays's: %d\n", _gvalues_compare (&value1, &value2));
748 749 750 751 752 753 754
}

static void
compare_ptrarrays (void)
{
	GPtrArray *array1;
	GPtrArray *array2;
755 756
	GValue value1 = G_VALUE_INIT;
	GValue value2 = G_VALUE_INIT;
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771

	g_value_init (&value1, dbus_g_type_get_collection ("GPtrArray", G_TYPE_STRING));
	array1 = g_ptr_array_new ();

	g_value_init (&value2, dbus_g_type_get_collection ("GPtrArray", G_TYPE_STRING));
	array2 = g_ptr_array_new ();

	g_ptr_array_add (array1, "hello");
	g_ptr_array_add (array1, "world");
	g_value_set_boxed (&value1, array1);

	g_ptr_array_add (array2, "hello");
	g_ptr_array_add (array2, "world");
	g_value_set_boxed (&value2, array2);

772
	g_print ("Comparing identical ptr arrays's: %d\n", _gvalues_compare (&value1, &value2));
773 774 775

	g_ptr_array_add (array2, "boo");
	g_value_set_boxed (&value2, array2);
776
	g_print ("Comparing different len ptr arrays's: %d\n", _gvalues_compare (&value1, &value2));
777 778 779

	g_ptr_array_add (array1, "booz");
	g_value_set_boxed (&value1, array1);
780
	g_print ("Comparing different ptr arrays's: %d\n", _gvalues_compare (&value1, &value2));
781 782 783 784 785 786 787
}

static void
compare_str_hash (void)
{
	GHashTable *hash1;
	GHashTable *hash2;
788 789
	GValue value1 = G_VALUE_INIT;
	GValue value2 = G_VALUE_INIT;
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804

	g_value_init (&value1, dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_STRING));
	g_value_init (&value2, dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_STRING));

	hash1 = g_hash_table_new (g_str_hash, g_str_equal);
	hash2 = g_hash_table_new (g_str_hash, g_str_equal);

	g_hash_table_insert (hash1, "key1", "hello");
	g_hash_table_insert (hash1, "key2", "world");

	g_hash_table_insert (hash2, "key1", "hello");
	g_hash_table_insert (hash2, "key2", "world");

	g_value_set_boxed (&value1, hash1);
	g_value_set_boxed (&value2, hash2);
805
	g_print ("Comparing identical str hashes: %d\n", _gvalues_compare (&value1, &value2));
806 807 808

	g_hash_table_remove (hash2, "key2");
	g_value_set_boxed (&value2, hash2);
809
	g_print ("Comparing different length str hashes: %d\n", _gvalues_compare (&value1, &value2));
810 811 812

	g_hash_table_insert (hash2, "key2", "moon");
	g_value_set_boxed (&value2, hash2);
813
	g_print ("Comparing different str hashes: %d\n", _gvalues_compare (&value1, &value2));
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
}

static GValue *
str_to_gvalue (const char *str)
{
	GValue *value;

	value = g_slice_new0 (GValue);
	g_value_init (value, G_TYPE_STRING);
	g_value_set_string (value, str);

	return value;
}

static GValue *
int_to_gvalue (int i)
{
	GValue *value;

	value = g_slice_new0 (GValue);
	g_value_init (value, G_TYPE_INT);
	g_value_set_int (value, i);

	return value;
}

static void
compare_gvalue_hash (void)
{
	GHashTable *hash1;
	GHashTable *hash2;
845 846
	GValue value1 = G_VALUE_INIT;
	GValue value2 = G_VALUE_INIT;
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861

	g_value_init (&value1, dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE));
	g_value_init (&value2, dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE));

	hash1 = g_hash_table_new (g_str_hash, g_str_equal);
	hash2 = g_hash_table_new (g_str_hash, g_str_equal);

	g_hash_table_insert (hash1, "key1", str_to_gvalue ("hello"));
	g_hash_table_insert (hash1, "key2", int_to_gvalue (5));

	g_hash_table_insert (hash2, "key1", str_to_gvalue ("hello"));
	g_hash_table_insert (hash2, "key2", int_to_gvalue (5));

	g_value_set_boxed (&value1, hash1);
	g_value_set_boxed (&value2, hash2);
862
	g_print ("Comparing identical gvalue hashes: %d\n", _gvalues_compare (&value1, &value2));
863 864 865

	g_hash_table_remove (hash2, "key2");
	g_value_set_boxed (&value2, hash2);
866
	g_print ("Comparing different length str hashes: %d\n", _gvalues_compare (&value1, &value2));
867 868 869

	g_hash_table_insert (hash2, "key2", str_to_gvalue ("moon"));
	g_value_set_boxed (&value2, hash2);
870
	g_print ("Comparing different str hashes: %d\n", _gvalues_compare (&value1, &value2));
871 872
}

873 874 875 876 877 878 879 880 881
static void
compare_ip6_addresses (void)
{
	GValueArray *array1;
	GValueArray *array2;
	GValueArray *array3;
	GByteArray *ba1;
	GByteArray *ba2;
	GByteArray *ba3;
882 883 884
	GValue element = G_VALUE_INIT;
	GValue value1 = G_VALUE_INIT;
	GValue value2 = G_VALUE_INIT;
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
	struct in6_addr addr1;
	struct in6_addr addr2;
	struct in6_addr addr3;
	guint32 prefix1 = 64;
	guint32 prefix2 = 64;
	guint32 prefix3 = 0;

	inet_pton (AF_INET6, "1:2:3:4:5:6:7:8", &addr1, sizeof (struct in6_addr));
	inet_pton (AF_INET6, "ffff:2:3:4:5:6:7:8", &addr2, sizeof (struct in6_addr));
	inet_pton (AF_INET6, "::", &addr3, sizeof (struct in6_addr));

	/* address 1 */
	ba1 = g_byte_array_new ();
	array1 = g_value_array_new (2);
	g_value_init (&element, DBUS_TYPE_G_UCHAR_ARRAY);
	g_byte_array_append (ba1, (guint8 *) addr1.s6_addr, 16);
	g_value_take_boxed (&element, ba1);
	g_value_array_append (array1, &element);
	g_value_unset (&element);

	g_value_init (&element, G_TYPE_UINT);
	g_value_set_uint (&element, prefix1);
	g_value_array_append (array1, &element);
	g_value_unset (&element);

	/* address 2 */
	ba2 = g_byte_array_new ();
	array2 = g_value_array_new (2);
	g_value_init (&element, DBUS_TYPE_G_UCHAR_ARRAY);
	g_byte_array_append (ba2, (guint8 *) addr2.s6_addr, 16);
	g_value_take_boxed (&element, ba2);
	g_value_array_append (array2, &element);
	g_value_unset (&element);

	g_value_init (&element, G_TYPE_UINT);
	g_value_set_uint (&element, prefix2);
	g_value_array_append (array2, &element);
	g_value_unset (&element);

	/* address 3 */
	ba3 = g_byte_array_new ();
	array3 = g_value_array_new (2);
	g_value_init (&element, DBUS_TYPE_G_UCHAR_ARRAY);
	g_byte_array_append (ba3, (guint8 *) addr3.s6_addr, 16);
	g_value_take_boxed (&element, ba3);
	g_value_array_append (array3, &element);
	g_value_unset (&element);

	g_value_init (&element, G_TYPE_UINT);
	g_value_set_uint (&element, prefix3);
	g_value_array_append (array3, &element);
	g_value_unset (&element);

	g_value_init (&value1, DBUS_TYPE_G_IP6_ADDRESS);
	g_value_init (&value2, DBUS_TYPE_G_IP6_ADDRESS);

	g_value_set_boxed (&value1, array1);
	g_value_set_boxed (&value2, array1);
943
	g_print ("Comparing identical IPv6 address structures: %d\n", _gvalues_compare (&value1, &value2));
944 945 946

	g_value_set_boxed (&value1, array1);
	g_value_set_boxed (&value2, array2);
947
	g_print ("Comparing different IPv6 address structures: %d\n", _gvalues_compare (&value1, &value2));
948 949 950

	g_value_set_boxed (&value1, array1);
	g_value_set_boxed (&value2, array3);
951
	g_print ("Comparing different IPv6 address structures: %d\n", _gvalues_compare (&value1, &value2));
952 953
}

954 955 956 957 958 959 960 961 962 963 964 965 966 967
int
main (int argc, char *argv[])
{
	DBusGConnection *bus;

	bus = dbus_g_bus_get (DBUS_BUS_SESSION, NULL);

	compare_ints ();
	compare_strings ();
	compare_strv ();
	compare_garrays ();
	compare_ptrarrays ();
	compare_str_hash ();
	compare_gvalue_hash ();
968
	compare_ip6_addresses ();
969 970 971 972 973

	return 0;
}

#endif