2396
test_constructor (void)
2400
tree_instance_check_gvariant (TreeInstance *tree,
2403
const GVariantType *type;
2405
type = (GVariantType *) g_variant_type_info_get_type_string (tree->info);
2406
g_assert (g_variant_is_of_type (value, type));
2408
switch (g_variant_type_info_get_type_char (tree->info))
2410
case G_VARIANT_TYPE_INFO_CHAR_MAYBE:
2415
child = g_variant_get_maybe (value);
2417
if (child != NULL && tree->n_children == 1)
2418
equal = tree_instance_check_gvariant (tree->children[0], child);
2419
else if (child == NULL && tree->n_children == 0)
2425
g_variant_unref (child);
2431
case G_VARIANT_TYPE_INFO_CHAR_ARRAY:
2432
case G_VARIANT_TYPE_INFO_CHAR_TUPLE:
2433
case G_VARIANT_TYPE_INFO_CHAR_DICT_ENTRY:
2437
if (g_variant_n_children (value) != tree->n_children)
2440
for (i = 0; i < tree->n_children; i++)
2445
child = g_variant_get_child_value (value, i);
2446
equal = tree_instance_check_gvariant (tree->children[i], child);
2447
g_variant_unref (child);
2457
case G_VARIANT_TYPE_INFO_CHAR_VARIANT:
2459
const gchar *str1, *str2;
2463
child = g_variant_get_variant (value);
2464
str1 = g_variant_get_type_string (child);
2465
str2 = g_variant_type_info_get_type_string (tree->children[0]->info);
2466
/* GVariant only keeps one copy of type strings around */
2467
equal = str1 == str2 &&
2468
tree_instance_check_gvariant (tree->children[0], child);
2470
g_variant_unref (child);
2477
return g_variant_get_boolean (value) == tree->data.integer;
2480
return g_variant_get_byte (value) == (guchar) tree->data.integer;
2483
return g_variant_get_int16 (value) == (gint16) tree->data.integer;
2486
return g_variant_get_uint16 (value) == (guint16) tree->data.integer;
2489
return g_variant_get_int32 (value) == (gint32) tree->data.integer;
2492
return g_variant_get_uint32 (value) == (guint32) tree->data.integer;
2495
return g_variant_get_int64 (value) == (gint64) tree->data.integer;
2498
return g_variant_get_uint64 (value) == (guint64) tree->data.integer;
2501
return g_variant_get_handle (value) == (gint32) tree->data.integer;
2505
gdouble floating = g_variant_get_double (value);
2507
return memcmp (&floating, &tree->data.floating, sizeof floating) == 0;
2513
return strcmp (g_variant_get_string (value, NULL),
2514
tree->data.string) == 0;
2517
g_assert_not_reached ();
2522
tree_instance_build_gvariant (TreeInstance *tree,
2523
GVariantBuilder *builder,
2526
const GVariantType *type;
2528
type = (GVariantType *) g_variant_type_info_get_type_string (tree->info);
2530
if (g_variant_type_is_container (type))
2534
/* force GVariantBuilder to guess the type half the time */
2535
if (guess_ok && randomly (0.5))
2537
if (g_variant_type_is_array (type) && tree->n_children)
2538
type = G_VARIANT_TYPE_ARRAY;
2540
if (g_variant_type_is_maybe (type) && tree->n_children)
2541
type = G_VARIANT_TYPE_MAYBE;
2543
if (g_variant_type_is_tuple (type))
2544
type = G_VARIANT_TYPE_TUPLE;
2546
if (g_variant_type_is_dict_entry (type))
2547
type = G_VARIANT_TYPE_DICT_ENTRY;
2552
g_variant_builder_open (builder, type);
2554
for (i = 0; i < tree->n_children; i++)
2555
tree_instance_build_gvariant (tree->children[i], builder, guess_ok);
2557
g_variant_builder_close (builder);
2560
g_variant_builder_add_value (builder, tree_instance_get_gvariant (tree));
2565
tree_instance_check_iter (TreeInstance *tree,
2570
value = g_variant_iter_next_value (iter);
2572
if (g_variant_is_container (value))
2576
iter = g_variant_iter_new (value);
2577
g_variant_unref (value);
2579
if (g_variant_iter_n_children (iter) != tree->n_children)
2581
g_variant_iter_free (iter);
2585
for (i = 0; i < tree->n_children; i++)
2586
if (!tree_instance_check_iter (tree->children[i], iter))
2588
g_variant_iter_free (iter);
2592
g_assert (g_variant_iter_next_value (iter) == NULL);
2593
g_variant_iter_free (iter);
2602
equal = tree_instance_check_gvariant (tree, value);
2603
g_variant_unref (value);
2610
test_container (void)
2398
2612
TreeInstance *tree;
2399
2613
GVariant *value;
2417
test_constructors (void)
2421
for (i = 0; i < 1000; i++)
2423
test_constructor ();
2663
test_containers (void)
2667
for (i = 0; i < 100; i++)
2672
g_variant_type_info_assert_no_infos ();
2676
test_format_strings (void)
2681
g_assert (g_variant_format_string_scan ("i", NULL, &end) && *end == '\0');
2682
g_assert (g_variant_format_string_scan ("@i", NULL, &end) && *end == '\0');
2683
g_assert (g_variant_format_string_scan ("@ii", NULL, &end) && *end == 'i');
2684
g_assert (g_variant_format_string_scan ("^a&s", NULL, &end) && *end == '\0');
2685
g_assert (g_variant_format_string_scan ("(^as)", NULL, &end) &&
2687
g_assert (!g_variant_format_string_scan ("(^s)", NULL, &end));
2688
g_assert (!g_variant_format_string_scan ("(^a)", NULL, &end));
2689
g_assert (!g_variant_format_string_scan ("(z)", NULL, &end));
2690
g_assert (!g_variant_format_string_scan ("az", NULL, &end));
2691
g_assert (!g_variant_format_string_scan ("{**}", NULL, &end));
2692
g_assert (!g_variant_format_string_scan ("{@**}", NULL, &end));
2693
g_assert (g_variant_format_string_scan ("{@y*}", NULL, &end) &&
2695
g_assert (g_variant_format_string_scan ("{yv}", NULL, &end) &&
2697
g_assert (!g_variant_format_string_scan ("{vv}", NULL, &end));
2698
g_assert (!g_variant_format_string_scan ("{y}", NULL, &end));
2699
g_assert (!g_variant_format_string_scan ("{yyy}", NULL, &end));
2700
g_assert (!g_variant_format_string_scan ("{ya}", NULL, &end));
2701
g_assert (g_variant_format_string_scan ("&s", NULL, &end) && *end == '\0');
2702
g_assert (!g_variant_format_string_scan ("&as", NULL, &end));
2703
g_assert (!g_variant_format_string_scan ("@z", NULL, &end));
2704
g_assert (!g_variant_format_string_scan ("az", NULL, &end));
2705
g_assert (!g_variant_format_string_scan ("a&s", NULL, &end));
2707
type = g_variant_format_string_scan_type ("mm(@xy^a&s*?@?)", NULL, &end);
2708
g_assert (type && *end == '\0');
2709
g_assert (g_variant_type_equal (type, G_VARIANT_TYPE ("mm(xyas*?\?)")));
2710
g_variant_type_free (type);
2712
type = g_variant_format_string_scan_type ("mm(@xy^a&*?@?)", NULL, NULL);
2713
g_assert (type == NULL);
2717
exit_on_abort (int signal)
2723
do_failed_test (const gchar *pattern)
2725
if (g_test_trap_fork (1000000, G_TEST_TRAP_SILENCE_STDERR))
2727
signal (SIGABRT, exit_on_abort);
2731
g_test_trap_assert_failed ();
2732
g_test_trap_assert_stderr (pattern);
2738
test_invalid_varargs (void)
2740
if (do_failed_test ("*not a valid GVariant format string*"))
2742
g_variant_new ("z");
2746
if (do_failed_test ("*valid GVariant format string as a prefix*"))
2750
g_variant_new_va ("z", &end, NULL);
2754
if (do_failed_test ("*type of `q' but * has a type of `y'*"))
2756
g_variant_get (g_variant_new ("y", 'a'), "q");
2762
check_and_free (GVariant *value,
2765
gchar *valstr = g_variant_print (value, FALSE);
2766
g_assert_cmpstr (str, ==, valstr);
2767
g_variant_unref (value);
2775
GVariantBuilder array;
2777
g_variant_builder_init (&array, G_VARIANT_TYPE_ARRAY);
2778
g_variant_builder_add (&array, "{sv}", "size",
2779
g_variant_new ("(ii)", 800, 600));
2780
g_variant_builder_add (&array, "{sv}", "title",
2781
g_variant_new_string ("Test case"));
2782
g_variant_builder_add_value (&array,
2783
g_variant_new_dict_entry (g_variant_new_string ("temperature"),
2784
g_variant_new_variant (
2785
g_variant_new_double (37.5))));
2786
check_and_free (g_variant_new ("(ma{sv}m(a{sv})ma{sv}ii)",
2787
NULL, FALSE, NULL, &array, 7777, 8888),
2788
"(Nothing, Nothing, {'size': <(800, 600)>, "
2789
"'title': <'Test case'>, "
2790
"'temperature': <37.5>}, "
2793
check_and_free (g_variant_new ("(imimimmimmimmi)",
2800
"(123, Nothing, 123, Nothing, Just Nothing, 123)");
2802
check_and_free (g_variant_new ("(ybnixd)",
2803
'a', 1, 22, 33, (guint64) 44, 5.5),
2804
"(0x61, true, 22, 33, 44, 5.5)");
2806
check_and_free (g_variant_new ("(@y?*rv)",
2807
g_variant_new ("y", 'a'),
2808
g_variant_new ("y", 'b'),
2809
g_variant_new ("y", 'c'),
2810
g_variant_new ("(y)", 'd'),
2811
g_variant_new ("y", 'e')),
2812
"(0x61, 0x62, 0x63, (0x64,), <byte 0x65>)");
2816
GVariantBuilder array;
2823
g_variant_builder_init (&array, G_VARIANT_TYPE_ARRAY);
2824
for (i = 0; i < 100; i++)
2826
number = g_strdup_printf ("%d", i);
2827
g_variant_builder_add (&array, "s", number);
2831
value = g_variant_builder_end (&array);
2832
g_variant_iter_init (&iter, value);
2835
while (g_variant_iter_loop (&iter, "s", &number))
2837
gchar *check = g_strdup_printf ("%d", i++);
2838
g_assert_cmpstr (number, ==, check);
2841
g_assert (number == NULL);
2842
g_assert (i == 100);
2844
g_variant_unref (value);
2846
g_variant_builder_init (&array, G_VARIANT_TYPE_ARRAY);
2847
for (i = 0; i < 100; i++)
2848
g_variant_builder_add (&array, "mi", i % 2 == 0, i);
2849
value = g_variant_builder_end (&array);
2852
g_variant_iter_init (&iter, value);
2853
while (g_variant_iter_loop (&iter, "mi", NULL, &val))
2854
g_assert (val == i++ || val == 0);
2855
g_assert (i == 100);
2858
g_variant_iter_init (&iter, value);
2859
while (g_variant_iter_loop (&iter, "mi", &just, &val))
2866
g_assert (val == this);
2871
g_assert (val == 0);
2874
g_assert (i == 100);
2876
g_variant_unref (value);
2880
const gchar *strvector[] = {"/hello", "/world", NULL};
2881
const gchar *test_strs[] = {"/foo", "/bar", "/baz" };
2882
GVariantBuilder builder;
2883
GVariantIter *array;
2891
g_variant_builder_init (&builder, G_VARIANT_TYPE ("ao"));
2892
g_variant_builder_add (&builder, "o", "/foo");
2893
g_variant_builder_add (&builder, "o", "/bar");
2894
g_variant_builder_add (&builder, "o", "/baz");
2895
value = g_variant_new("(ao^ao^a&o)", &builder, strvector, strvector);
2896
g_variant_iter_init (&tuple, value);
2897
g_variant_iter_next (&tuple, "ao", &array);
2900
while (g_variant_iter_loop (array, "o", &str))
2901
g_assert_cmpstr (str, ==, test_strs[i++]);
2904
g_variant_iter_free (array);
2907
g_variant_iter_init (&tuple, value);
2908
g_variant_iter_next (&tuple, "ao", &array);
2911
while (g_variant_iter_loop (array, "&o", &str))
2912
g_assert_cmpstr (str, ==, test_strs[i++]);
2915
g_variant_iter_free (array);
2917
g_variant_iter_next (&tuple, "^a&o", &strv);
2918
g_variant_iter_next (&tuple, "^ao", &my_strv);
2920
g_assert_cmpstr (strv[0], ==, "/hello");
2921
g_assert_cmpstr (strv[1], ==, "/world");
2922
g_assert (strv[2] == NULL);
2923
g_assert_cmpstr (my_strv[0], ==, "/hello");
2924
g_assert_cmpstr (my_strv[1], ==, "/world");
2925
g_assert (my_strv[2] == NULL);
2927
g_variant_unref (value);
2928
g_strfreev (my_strv);
2933
const gchar *strvector[] = { "i", "ii", "iii", "iv", "v", "vi", NULL };
2934
GVariantBuilder builder;
2943
g_variant_builder_init (&builder, G_VARIANT_TYPE ("aag"));
2944
g_variant_builder_open (&builder, G_VARIANT_TYPE ("ag"));
2945
for (i = 0; i < 6; i++)
2947
g_variant_builder_add (&builder, "g", strvector[i]);
2949
g_variant_builder_add (&builder, "&g", strvector[i]);
2950
g_variant_builder_close (&builder);
2951
g_variant_builder_add (&builder, "^ag", strvector);
2952
g_variant_builder_add (&builder, "^ag", strvector);
2953
value = g_variant_new ("aag", &builder);
2955
g_variant_iter_init (&iter, value);
2956
while (g_variant_iter_loop (&iter, "^ag", &strv))
2957
for (i = 0; i < 6; i++)
2958
g_assert_cmpstr (strv[i], ==, strvector[i]);
2960
g_variant_iter_init (&iter, value);
2961
while (g_variant_iter_loop (&iter, "^a&g", &strv))
2962
for (i = 0; i < 6; i++)
2963
g_assert_cmpstr (strv[i], ==, strvector[i]);
2965
g_variant_iter_init (&iter, value);
2966
while (g_variant_iter_loop (&iter, "ag", &i2))
2971
while (g_variant_iter_loop (i2, "g", &str))
2972
g_assert_cmpstr (str, ==, strvector[i++]);
2976
g_variant_iter_init (&iter, value);
2977
i3 = g_variant_iter_copy (&iter);
2978
while (g_variant_iter_loop (&iter, "@ag", &sub))
2980
gchar *str = g_variant_print (sub, TRUE);
2981
g_assert_cmpstr (str, ==,
2982
"[signature 'i', 'ii', 'iii', 'iv', 'v', 'vi']");
2986
if (do_failed_test ("*NULL has already been returned*"))
2988
g_variant_iter_next_value (&iter);
2993
while (g_variant_iter_loop (i3, "*", &sub))
2995
gchar *str = g_variant_print (sub, TRUE);
2996
g_assert_cmpstr (str, ==,
2997
"[signature 'i', 'ii', 'iii', 'iv', 'v', 'vi']");
3001
g_variant_iter_free (i3);
3003
for (i = 0; i < g_variant_n_children (value); i++)
3007
g_variant_get_child (value, i, "*", &sub);
3009
for (j = 0; j < g_variant_n_children (sub); j++)
3011
const gchar *str = NULL;
3014
g_variant_get_child (sub, j, "&g", &str);
3015
g_assert_cmpstr (str, ==, strvector[j]);
3017
cval = g_variant_get_child_value (sub, j);
3018
g_variant_get (cval, "&g", &str);
3019
g_assert_cmpstr (str, ==, strvector[j]);
3020
g_variant_unref (cval);
3023
g_variant_unref (sub);
3026
g_variant_unref (value);
3045
/* test all 'Nothing' */
3046
value = g_variant_new ("(mymbmnmqmimumxmtmhmdmv)",
3049
FALSE, (gint16) 123,
3050
FALSE, (guint16) 123,
3051
FALSE, (gint32) 123,
3052
FALSE, (guint32) 123,
3053
FALSE, (gint64) 123,
3054
FALSE, (guint64) 123,
3056
FALSE, (gdouble) 37.5,
3060
g_variant_get (value, "(mymbmnmqmimumxmtmhmdmv)",
3074
memset (justs, 1, sizeof justs);
3075
g_variant_get (value, "(mymbmnmqmimumxmtmhmdmv)",
3087
g_assert (!(justs[0] || justs[1] || justs[2] || justs[3] || justs[4] ||
3088
justs[5] || justs[6] || justs[7] || justs[8] || justs[9]));
3091
memset (justs, 1, sizeof justs);
3092
byteval = i16val = u16val = i32val = u32val = i64val = u64val = hval = 88;
3096
g_variant_get (value, "(mymbmnmqmimumxmtmhmdmv)",
3097
&justs[0], &byteval,
3108
g_assert (!(justs[0] || justs[1] || justs[2] || justs[3] || justs[4] ||
3109
justs[5] || justs[6] || justs[7] || justs[8] || justs[9]));
3110
g_assert (byteval == '\0' && bval == FALSE);
3111
g_assert (i16val == 0 && u16val == 0 && i32val == 0 &&
3112
u32val == 0 && i64val == 0 && u64val == 0 &&
3113
hval == 0 && dval == 0.0);
3114
g_assert (vval == NULL);
3117
byteval = i16val = u16val = i32val = u32val = i64val = u64val = hval = 88;
3121
g_variant_get (value, "(mymbmnmqmimumxmtmhmdmv)",
3133
g_assert (byteval == '\0' && bval == FALSE);
3134
g_assert (i16val == 0 && u16val == 0 && i32val == 0 &&
3135
u32val == 0 && i64val == 0 && u64val == 0 &&
3136
hval == 0 && dval == 0.0);
3137
g_assert (vval == NULL);
3139
g_variant_unref (value);
3142
/* test all 'Just' */
3143
value = g_variant_new ("(mymbmnmqmimumxmtmhmdmv)",
3147
TRUE, (guint16) 123,
3149
TRUE, (guint32) 123,
3151
TRUE, (guint64) 123,
3153
TRUE, (gdouble) 37.5,
3154
g_variant_new ("()"));
3157
g_variant_get (value, "(mymbmnmqmimumxmtmhmdmv)",
3171
memset (justs, 0, sizeof justs);
3172
g_variant_get (value, "(mymbmnmqmimumxmtmhmdmv)",
3184
g_assert (justs[0] && justs[1] && justs[2] && justs[3] && justs[4] &&
3185
justs[5] && justs[6] && justs[7] && justs[8] && justs[9]);
3188
memset (justs, 0, sizeof justs);
3189
byteval = i16val = u16val = i32val = u32val = i64val = u64val = hval = 88;
3193
g_variant_get (value, "(mymbmnmqmimumxmtmhmdmv)",
3194
&justs[0], &byteval,
3205
g_assert (justs[0] && justs[1] && justs[2] && justs[3] && justs[4] &&
3206
justs[5] && justs[6] && justs[7] && justs[8] && justs[9]);
3207
g_assert (byteval == 'a' && bval == TRUE);
3208
g_assert (i16val == 123 && u16val == 123 && i32val == 123 &&
3209
u32val == 123 && i64val == 123 && u64val == 123 &&
3210
hval == -1 && dval == 37.5);
3211
g_assert (g_variant_is_of_type (vval, G_VARIANT_TYPE_UNIT));
3212
g_variant_unref (vval);
3215
byteval = i16val = u16val = i32val = u32val = i64val = u64val = hval = 88;
3219
g_variant_get (value, "(mymbmnmqmimumxmtmhmdmv)",
3231
g_assert (byteval == 'a' && bval == TRUE);
3232
g_assert (i16val == 123 && u16val == 123 && i32val == 123 &&
3233
u32val == 123 && i64val == 123 && u64val == 123 &&
3234
hval == -1 && dval == 37.5);
3235
g_assert (g_variant_is_of_type (vval, G_VARIANT_TYPE_UNIT));
3236
g_variant_unref (vval);
3238
g_variant_unref (value);
3241
g_variant_type_info_assert_no_infos ();
3245
hash_get (GVariant *value,
3246
const gchar *format,
3249
const gchar *endptr = NULL;
3253
hash = g_str_has_suffix (format, "#");
3255
va_start (ap, format);
3256
g_variant_get_va (value, format, hash ? &endptr : NULL, &ap);
3260
g_assert (*endptr == '#');
3264
hash_new (const gchar *format,
3267
const gchar *endptr = NULL;
3272
hash = g_str_has_suffix (format, "#");
3274
va_start (ap, format);
3275
value = g_variant_new_va (format, hash ? &endptr : NULL, &ap);
3279
g_assert (*endptr == '#');
3291
value = hash_new ("i", 234);
3292
hash_get (value, "i", &x);
3293
g_assert (x == 234);
3294
g_variant_unref (value);
3297
value = hash_new ("i#", 234);
3298
hash_get (value, "i#", &x);
3299
g_assert (x == 234);
3300
g_variant_unref (value);
3302
g_variant_type_info_assert_no_infos ();
3306
test_builder_memory (void)
3308
GVariantBuilder *hb;
3311
hb = g_variant_builder_new (G_VARIANT_TYPE_ARRAY);
3312
g_variant_builder_open (hb, G_VARIANT_TYPE_ARRAY);
3313
g_variant_builder_open (hb, G_VARIANT_TYPE_ARRAY);
3314
g_variant_builder_open (hb, G_VARIANT_TYPE_ARRAY);
3315
g_variant_builder_add (hb, "s", "some value");
3316
g_variant_builder_ref (hb);
3317
g_variant_builder_unref (hb);
3318
g_variant_builder_unref (hb);
3320
hb = g_variant_builder_new (G_VARIANT_TYPE_ARRAY);
3321
g_variant_builder_unref (hb);
3323
hb = g_variant_builder_new (G_VARIANT_TYPE_ARRAY);
3324
g_variant_builder_clear (hb);
3325
g_variant_builder_unref (hb);
3327
g_variant_builder_init (&sb, G_VARIANT_TYPE_ARRAY);
3328
g_variant_builder_open (&sb, G_VARIANT_TYPE_ARRAY);
3329
g_variant_builder_open (&sb, G_VARIANT_TYPE_ARRAY);
3330
g_variant_builder_add (&sb, "s", "some value");
3331
g_variant_builder_clear (&sb);
3333
g_variant_type_info_assert_no_infos ();
3339
const gint n_items = 4096;
3340
GVariant *items[n_items];
3344
table = g_hash_table_new_full (g_variant_hash, g_variant_equal,
3345
(GDestroyNotify ) g_variant_unref,
3348
for (i = 0; i < n_items; i++)
3354
tree = tree_instance_new (NULL, 0);
3355
items[i] = tree_instance_get_gvariant (tree);
3356
tree_instance_free (tree);
3358
for (j = 0; j < i; j++)
3359
if (g_variant_equal (items[i], items[j]))
3361
g_variant_unref (items[i]);
3365
g_hash_table_insert (table,
3366
g_variant_ref_sink (items[i]),
3367
GINT_TO_POINTER (i));
3370
for (i = 0; i < n_items; i++)
3374
result = g_hash_table_lookup (table, items[i]);
3375
g_assert_cmpint (GPOINTER_TO_INT (result), ==, i);
3378
g_hash_table_unref (table);
3380
g_variant_type_info_assert_no_infos ();