Merge pull request #2208 from martinbooth/dataannotation_strings_from_corefx_without_resx
[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 Tests {
90
91         static int Main  (string[] args) {
92                 return TestDriver.RunTests (typeof (Tests), args);
93         }
94         
95         static public int test_0_sealed_class_devirt_right_method () {
96                 SealedFinal x = new SealedFinal ();
97                 if (x.method1 () != 4)
98                         return 1;
99                 if (x.method2 () != 2)
100                         return 2;
101                 if (x.method3 () != 1)
102                         return 1;
103                 return 0;       
104         }
105         
106         static public int test_0_sealed_method_devirt_right_method () {
107                 OpenFinal x = new OpenFinal ();
108                 if (x.method4 () != 3)
109                         return 1;
110                 if (x.method5 () != 2)
111                         return 2;
112                 return 0;       
113         }
114         
115         static public int test_0_sealed_class_devirt_right_method_using_delegates () {
116                 SealedFinal x = new SealedFinal ();
117                 IntNoArgs d1 = new IntNoArgs(x.method1);
118                 IntNoArgs d2 = new IntNoArgs(x.method2);
119                 IntNoArgs d3 = new IntNoArgs(x.method3);
120                 
121                 if (d1 () != 4)
122                         return 1;
123                 if (d2 () != 2)
124                         return 2;
125                 if (d3 () != 1)
126                         return 1;
127                 return 0;       
128         }
129         
130         static public int test_0_sealed_method_devirt_right_method_using_delegates () {
131                 OpenFinal x = new OpenFinal ();
132                 IntNoArgs d1 = new IntNoArgs(x.method4);
133                 IntNoArgs d2 = new IntNoArgs(x.method5);
134                 
135                 if (d1 () != 3)
136                         return 1;
137                 if (d2 () != 2)
138                         return 2;
139                 return 0;       
140         }
141         
142         
143         static public int test_0_delegate_over_static_method_devirtualize_ok () {
144                 IntNoArgs d1 = new IntNoArgs(OpenFinal.staticMethod);
145                 IntNoArgs d2 = new IntNoArgs(SealedFinal.staticMethod);
146                 
147                 if (d1 () != 3)
148                         return 1;
149                 if (d2 () != 4)
150                         return 2;
151                         
152                 return 0;
153         }
154
155         static public int test_0_npe_still_happens() {
156                 OpenFinal x = null;
157                 SealedFinal y = null;
158                 
159                 try {
160                         y.method1();
161                         return 1;
162                 } catch(NullReferenceException e) {
163                         ;//ok
164                 }
165
166                 try {
167                         y.method2();
168                         return 2;
169                 } catch(NullReferenceException e) {
170                         ;//ok
171                 }
172
173                 try {
174                         y.method3();
175                         return 3;
176                 } catch(NullReferenceException e) {
177                         ;//ok
178                 }
179                 
180                 try {
181                         x.method4();
182                         return 4;
183                 } catch(NullReferenceException e) {
184                         ;//ok
185                 }
186
187                 try {
188                         x.method5();
189                         return 5;
190                 } catch(NullReferenceException e) {
191                         ;//ok
192                 }
193                 
194                 return 0;
195         }
196 }