[interp] Fix interp logging (#17636)
[mono-project.git] / mono / mini / devirtualization.cs
blob20ca3bdb70adf1891e8f2d96ca483deafda141c1
1 using System;
2 using System.Reflection;
4 /*
5 * Regression tests for the mono JIT.
7 * Each test needs to be of the form:
9 * static int test_<result>_<name> ();
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.
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)
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.
26 delegate int IntNoArgs();
28 public class Base {
30 public virtual int method1 () {
31 return 1;
34 public virtual int method2 () {
35 return 1;
38 public virtual int method3 () {
39 return 1;
42 public virtual int method4 () {
43 return 1;
46 public virtual int method5 () {
47 return 1;
52 public class Middle : Base {
53 public override int method2 () {
54 return 2;
57 public override int method4 () {
58 return 2;
61 public override sealed int method5 () {
62 return 2;
67 public class OpenFinal : Middle {
68 public override sealed int method4 () {
69 return 3;
72 static public int staticMethod() {
73 return 3;
78 sealed public class SealedFinal : Middle {
79 public override int method1 () {
80 return 4;
83 static public int staticMethod() {
84 return 4;
89 class DevirtualizationTests {
91 #if !__MOBILE__
92 static int Main (string[] args) {
93 return TestDriver.RunTests (typeof (DevirtualizationTests), args);
95 #endif
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;
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;
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);
123 if (d1 () != 4)
124 return 1;
125 if (d2 () != 2)
126 return 2;
127 if (d3 () != 1)
128 return 1;
129 return 0;
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);
137 if (d1 () != 3)
138 return 1;
139 if (d2 () != 2)
140 return 2;
141 return 0;
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);
149 if (d1 () != 3)
150 return 1;
151 if (d2 () != 4)
152 return 2;
154 return 0;
157 static public int test_0_npe_still_happens() {
158 OpenFinal x = null;
159 SealedFinal y = null;
161 try {
162 y.method1();
163 return 1;
164 } catch(NullReferenceException e) {
165 ;//ok
168 try {
169 y.method2();
170 return 2;
171 } catch(NullReferenceException e) {
172 ;//ok
175 try {
176 y.method3();
177 return 3;
178 } catch(NullReferenceException e) {
179 ;//ok
182 try {
183 x.method4();
184 return 4;
185 } catch(NullReferenceException e) {
186 ;//ok
189 try {
190 x.method5();
191 return 5;
192 } catch(NullReferenceException e) {
193 ;//ok
196 return 0;