1using GLib;
2
3class Maman.Bar : Object {
4	public void do_action () {
5		stdout.printf (" 2");
6	}
7
8	public static void do_static_action () {
9		stdout.printf (" 2");
10	}
11
12	public virtual void do_virtual_action () {
13		stdout.printf (" BAD");
14	}
15}
16
17class Maman.SubBar : Bar {
18	public override void do_virtual_action () {
19		stdout.printf (" 2");
20	}
21
22	static void accept_ref_string (ref string str) {
23	}
24
25	static void test_classes_methods_ref_parameters () {
26		string str = "hello";
27		accept_ref_string (ref str);
28	}
29
30	public static int main () {
31		stdout.printf ("Inheritance Test: 1");
32
33		var bar = new SubBar ();
34		bar.do_action ();
35
36		stdout.printf (" 3\n");
37
38		stdout.printf ("Static Inheritance Test: 1");
39
40		do_static_action ();
41
42		stdout.printf (" 3\n");
43
44		stdout.printf ("Virtual Method Test: 1");
45
46		bar.do_virtual_action ();
47
48		stdout.printf (" 3\n");
49
50		// test symbol resolving to check that methods of implemented
51		// interfaces take precedence of methods in base classes
52		stdout.printf ("Interface Inheritance Test: 1");
53
54		var foobar = new SubFooBar ();
55		foobar.do_action ();
56
57		stdout.printf (" 3\n");
58
59		test_classes_methods_ref_parameters ();
60
61		BaseAccess.test ();
62
63		string str, str2;
64		weak string weak_str;
65		string[] array;
66
67		test_out (out str);
68		assert (str == "hello");
69
70		test_out_weak (out weak_str);
71		assert (weak_str == "hello");
72
73		test_out_weak (out str2);
74		assert (str2 == "hello");
75
76		test_ref (ref str);
77		assert (str == "world");
78
79		test_ref_weak (ref weak_str);
80		assert (weak_str == "world");
81
82		test_out_array_no_length (out array);
83		assert (array[0] == "hello");
84		assert (array[1] == "world");
85		assert (array.length < 0);
86
87		// LeakSanitizer -fsanitize=address
88		if (array.length == -1) {
89			array.length = (int) strv_length (array);
90		}
91
92		ClassTest.run_test ();
93
94		return 0;
95	}
96}
97
98interface Maman.Foo {
99	public void do_action () {
100		stdout.printf (" 2");
101	}
102}
103
104class Maman.FooBar : Object {
105	public void do_action () {
106		stdout.printf (" BAD");
107	}
108}
109
110class Maman.SubFooBar : FooBar, Foo {
111}
112
113// http://bugzilla.gnome.org/show_bug.cgi?id=523263
114
115abstract class Maman.AbstractBase : Object {
116	public abstract void foo ();
117}
118
119abstract class Maman.AbstractDerived : AbstractBase {
120	public override void foo () {
121	}
122}
123
124class Maman.DeepDerived : AbstractDerived {
125}
126
127// http://bugzilla.gnome.org/show_bug.cgi?id=528457
128namespace Maman.BaseAccess {
129	public interface IFoo : Object {
130		public abstract int interface_method ();
131
132		public abstract int virtual_interface_method ();
133	}
134
135	public class Foo : Object, IFoo {
136		public virtual int virtual_method () {
137			return 1;
138		}
139
140		public int interface_method () {
141			return 2;
142		}
143
144		public virtual int virtual_interface_method () {
145			return 3;
146		}
147	}
148
149	public class Bar : Foo {
150		public override int virtual_method () {
151			return base.virtual_method () * 10 + 4;
152		}
153
154		public override int virtual_interface_method () {
155			return base.virtual_interface_method () * 10 + 5;
156		}
157	}
158
159	public class FooBar : Foo, IFoo {
160		public int interface_method () {
161			return base.interface_method () * 10 + 6;
162		}
163
164		public int virtual_interface_method () {
165			return -1;
166		}
167	}
168
169	public void test () {
170		var bar = new Bar ();
171		var foobar = new FooBar ();
172		assert (bar.virtual_method () == 14);
173		assert (bar.virtual_interface_method () == 35);
174		assert (foobar.interface_method () == 26);
175	}
176}
177
178void test_out (out string bar) {
179	bar = "hello";
180}
181
182void test_out_weak (out weak string bar) {
183	bar = "hello";
184}
185
186void test_ref (ref string bar) {
187	assert (bar == "hello");
188	bar = "world";
189}
190
191void test_ref_weak (ref weak string bar) {
192	assert (bar == "hello");
193	bar = "world";
194}
195
196void test_out_array_no_length ([CCode (array_length = false)] out string[] bar) {
197	bar = {"hello", "world"};
198}
199
200class Maman.ClassTest {
201	public class void class_method () {
202		stdout.printf(" OK\n");
203	}
204
205	public void instance_method () {
206		stdout.printf ("Access class method in instance method:");
207		class_method ();
208	}
209
210	class construct {
211		stdout.printf ("Access class method in class constructor:");
212		class_method ();
213	}
214
215	static construct {
216		stdout.printf ("Access class method in static constructor:");
217		class_method ();
218	}
219
220	public static void run_test () {
221		var c = new ClassTest ();
222
223		stdout.printf ("Access class method by member access:");
224		c.class_method ();
225
226		c.instance_method ();
227	}
228}
229