summaryrefslogtreecommitdiff
path: root/tests/structs/structs.vala
blob: dabe021d8be36851cb83f0bf8a93de972c2bacdb (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using GLib;

struct SimpleStruct {
	public int field;
	public int array[10];
}

public struct PublicStruct {
	public int field;
}

struct StructWithCreationMethod {
	public StructWithCreationMethod () {
		stdout.printf ("StructWithCreationMethod\n");
	}

	public int field;
}

struct StructWithNamedCreationMethod {
	public StructWithNamedCreationMethod.named () {
		stdout.printf ("StructWithNamedCreationMethod\n");
	}

	public int field;
}


delegate void Func ();

struct StructWithFunc {
	public Func foo;

	public StructWithFunc (owned Func f) {
		foo = (owned) f;
	}
}

void test_in_parameter (SimpleStruct st) {
	stdout.printf ("test_in_parameter: st.field = %d\n", st.field);
}

void test_in_nullable_parameter (SimpleStruct? st) {
	assert (st.field == 1);
}

void test_ref_parameter (ref SimpleStruct st) {
	stdout.printf ("test_ref_parameter: st.field = %d\n", st.field);
	st.field++;
	st.array[0] = 10;
}

void test_out_parameter (out SimpleStruct st) {
	st = SimpleStruct ();
	st.field = 3;
}

void test_struct_with_func () {
	var foes = new StructWithFunc[] {
		StructWithFunc (() => {})
	};
}

void main () {
	stdout.printf ("Structs Test:\n");

	stdout.printf ("new SimpleStruct ()\n");
	var simple_struct = SimpleStruct ();
	stdout.printf ("new PublicStruct ()\n");
	var public_struct = PublicStruct ();
	stdout.printf ("new StructWithCreationMethod ()\n");
	var struct_with_creation_method = StructWithCreationMethod ();
	stdout.printf ("new StructWithNamedCreationMethod ()\n");
	var struct_with_named_creation_method = StructWithNamedCreationMethod.named ();

	stdout.printf ("new SimpleStruct () { field = 1 }\n");
	simple_struct = SimpleStruct () { field = 1 };
	stdout.printf ("simple_struct.field = %d\n", simple_struct.field);

	test_in_parameter (simple_struct);
	test_in_parameter ({1});
	test_in_nullable_parameter (simple_struct);
	test_ref_parameter (ref simple_struct);
	stdout.printf ("after test_ref_parameter: st.field = %d\n", simple_struct.field);
	assert (simple_struct.array[0] == 10);
	test_out_parameter (out simple_struct);
	stdout.printf ("after test_out_parameter: st.field = %d\n", simple_struct.field);

	stdout.printf (".\n");
}