summaryrefslogtreecommitdiff
path: root/tests/basic-types/gvariants-unboxing-safe.vala
blob: 03cc73b7117cd56c69fc521eab7648c4808653e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
struct Foo {
	public string s;
	public uint64 u64;
	public bool b;
}

void main () {
	Variant v;

	v = new Variant.int32 (4711);
	{
		bool? b = v as bool;
		assert (b == null);
	}
	{
		int16? i16 = v as int16;
		assert (i16 == null);
	}
	{
		int32? i32 = v as int32;
		assert (i32 == 4711);
	}
	{
		string? s = v as string;
		assert (s == null);
	}

	v = new Variant.boolean (true);
	{
		bool? b = v as bool;
		assert (b == true);
	}
	{
		int32? i32 = v as int32;
		assert (i32 == null);
	}

	v = new Variant.strv ({ "foo", "bar", "manam" });
	{
		string[]? sa = v as string[];
		assert (sa != null);
		assert (sa[2] == "manam");
	}

	Foo vsrc = { "foo", uint64.MAX, true };
	v = vsrc;
	assert ("(stb)" == v.get_type_string ());
	{
		Foo real_st = (Foo) v;
		assert (real_st.s == "foo");
		assert (real_st.u64 == uint64.MAX);
		assert (real_st.b == true);

		Foo? st = v as Foo;
		assert (st != null);
		assert (st.s == "foo");
		assert (st.u64 == uint64.MAX);
		assert (st.b == true);
	}

	HashTable<string,string> vsrc2 = new HashTable<string,string> (str_hash, str_equal);
	vsrc2.insert ("foo", "bar");
	vsrc2.insert ("bar", "manam");
	v = vsrc2;
	{
		HashTable<string,string> dict = v as HashTable<string,string>;
		assert (dict.lookup ("foo") == "bar");
		assert (dict.lookup ("bar") == "manam");
	}
	{
		HashTable<int,string>? dict = v as HashTable<int,string>;
		assert (dict == null);
	}
}