[mini] Fix test compiling when running !MOBILE
[mono.git] / mono / mini / devirtualization.cs
1 using System;
2 using System.Reflection;
3
4 /*
5  * Regression tests for the mono JIT.
6  *
7  * Each test needs to be of the form:
8  *
9  * static int test_<result>_<name> ();
10  *
11  * where <result> is an integer (the value that needs to be returned by
12  * the method to make it pass.
13  * <name> is a user-displayed name used to identify the test.
14  *
15  * The tests can be driven in two ways:
16  * *) running the program directly: Main() uses reflection to find and invoke
17  *      the test methods (this is useful mostly to check that the tests are correct)
18  * *) with the --regression switch of the jit (this is the preferred way since
19  *      all the tests will be run with optimizations on and off)
20  *
21  * The reflection logic could be moved to a .dll since we need at least another
22  * regression test file written in IL code to have better control on how
23  * the IL code looks.
24  */
25
26 delegate int IntNoArgs();
27
28 public class Base {
29
30         public virtual int method1 () {
31                 return 1;
32         }
33
34         public virtual int method2 () {
35                 return 1;
36         }
37         
38         public virtual int method3 () {
39                 return 1;
40         }
41
42         public virtual int method4 () {
43                 return 1;
44         }
45
46         public virtual int method5 () {
47                 return 1;
48         }
49         
50 }
51
52 public class Middle : Base {
53         public override int method2 () {
54                 return 2;
55         }
56         
57         public override int method4 () {
58                 return 2;
59         }
60         
61         public override sealed int method5 () {
62                 return 2;
63         }
64 }
65
66
67 public class OpenFinal : Middle {
68         public override sealed int method4 () {
69                 return 3;
70         }
71         
72         static public int staticMethod() {
73                 return 3;
74         }
75
76 }
77
78 sealed public class SealedFinal : Middle {
79         public override int method1 () {
80                 return 4;
81         }
82         
83         static public int staticMethod() {
84                 return 4;
85         }
86 }
87
88
89 class DevirtualizationTests {
90
91 #if !__MOBILE__
92         static int Main  (string[] args) {
93                 return TestDriver.RunTests (typeof (DevirtualizationTests), args);
94         }
95 #endif
96         
97         static public int test_0_sealed_class_devirt_right_method () {
98                 SealedFinal x = new SealedFinal ();
99                 if (x.method1 () != 4)
100                         return 1;
101                 if (x.method2 () != 2)
102                         return 2;
103                 if (x.method3 () != 1)
104                         return 1;
105                 return 0;       
106         }
107         
108         static public int test_0_sealed_method_devirt_right_method () {
109                 OpenFinal x = new OpenFinal ();
110                 if (x.method4 () != 3)
111                         return 1;
112                 if (x.method5 () != 2)
113                         return 2;
114                 return 0;       
115         }
116         
117         static public int test_0_sealed_class_devirt_right_method_using_delegates () {
118                 SealedFinal x = new SealedFinal ();
119                 IntNoArgs d1 = new IntNoArgs(x.method1);
120                 IntNoArgs d2 = new IntNoArgs(x.method2);
121                 IntNoArgs d3 = new IntNoArgs(x.method3);
122                 
123                 if (d1 () != 4)
124                         return 1;
125                 if (d2 () != 2)
126                         return 2;
127                 if (d3 () != 1)
128                         return 1;
129                 return 0;       
130         }
131         
132         static public int test_0_sealed_method_devirt_right_method_using_delegates () {
133                 OpenFinal x = new OpenFinal ();
134                 IntNoArgs d1 = new IntNoArgs(x.method4);
135                 IntNoArgs d2 = new IntNoArgs(x.method5);
136                 
137                 if (d1 () != 3)
138                         return 1;
139                 if (d2 () != 2)
140                         return 2;
141                 return 0;       
142         }
143         
144         
145         static public int test_0_delegate_over_static_method_devirtualize_ok () {
146                 IntNoArgs d1 = new IntNoArgs(OpenFinal.staticMethod);
147                 IntNoArgs d2 = new IntNoArgs(SealedFinal.staticMethod);
148                 
149                 if (d1 () != 3)
150                         return 1;
151                 if (d2 () != 4)
152                         return 2;
153                         
154                 return 0;
155         }
156
157         static public int test_0_npe_still_happens() {
158                 OpenFinal x = null;
159                 SealedFinal y = null;
160                 
161                 try {
162                         y.method1();
163                         return 1;
164                 } catch(NullReferenceException e) {
165                         ;//ok
166                 }
167
168                 try {
169                         y.method2();
170                         return 2;
171                 } catch(NullReferenceException e) {
172                         ;//ok
173                 }
174
175                 try {
176                         y.method3();
177                         return 3;
178                 } catch(NullReferenceException e) {
179                         ;//ok
180                 }
181                 
182                 try {
183                         x.method4();
184                         return 4;
185                 } catch(NullReferenceException e) {
186                         ;//ok
187                 }
188
189                 try {
190                         x.method5();
191                         return 5;
192                 } catch(NullReferenceException e) {
193                         ;//ok
194                 }
195                 
196                 return 0;
197         }
198 }