In metadata/
[mono.git] / mono / mini / gc-test.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 class Tests {
27
28         static int Main () {
29                 return TestDriver.RunTests (typeof (Tests));
30         }
31
32         public static int test_36_simple () {
33                 // Overflow the registers
34                 object o1 = (1);
35                 object o2 = (2);
36                 object o3 = (3);
37                 object o4 = (4);
38                 object o5 = (5);
39                 object o6 = (6);
40                 object o7 = (7);
41                 object o8 = (8);
42
43                 /* Prevent the variables from being local to a bb */
44                 bool b = o1 != null;
45                 GC.Collect (0);
46
47                 if (b)
48                         return (int)o1 + (int)o2 + (int)o3 + (int)o4 + (int)o5 + (int)o6 + (int)o7 + (int)o8;
49                 else
50                         return 0;
51         }
52
53         public static int test_36_liveness () {
54                 object o = 5;
55                 object o1, o2, o3, o4, o5, o6, o7, o8;
56
57                 bool b = o != null;
58
59                 GC.Collect (1);
60
61                 o1 = (1);
62                 o2 = (2);
63                 o3 = (3);
64                 o4 = (4);
65                 o5 = (5);
66                 o6 = (6);
67                 o7 = (7);
68                 o8 = (8);
69
70                 if (b)
71                         return (int)o1 + (int)o2 + (int)o3 + (int)o4 + (int)o5 + (int)o6 + (int)o7 + (int)o8;
72                 else
73                         return 0;
74         }
75
76         struct FooStruct {
77                 public object o1;
78                 public int i;
79                 public object o2;
80
81                 public FooStruct (int i1, int i, int i2) {
82                         this.o1 = i1;
83                         this.i = i;
84                         this.o2 = i2;
85                 }
86         }
87
88         public static int test_4_vtype () {
89                 FooStruct s = new FooStruct (1, 2, 3);
90
91                 GC.Collect (1);
92
93                 return (int)s.o1 + (int)s.o2;
94         }
95
96         class BigClass {
97                 public object o1, o2, o3, o4, o5, o6, o7, o8, o9, o10;
98                 public object o11, o12, o13, o14, o15, o16, o17, o18, o19, o20;
99                 public object o21, o22, o23, o24, o25, o26, o27, o28, o29, o30;
100                 public object o31, o32;
101         }
102
103         static void set_fields (BigClass b) {
104                 b.o31 = 31;
105                 b.o32 = 32;
106
107                 b.o1 = 1;
108                 b.o2 = 2;
109                 b.o3 = 3;
110                 b.o4 = 4;
111                 b.o5 = 5;
112                 b.o6 = 6;
113                 b.o7 = 7;
114                 b.o8 = 8;
115                 b.o9 = 9;
116                 b.o10 = 10;
117                 b.o11 = 11;
118                 b.o12 = 12;
119                 b.o13 = 13;
120                 b.o14 = 14;
121                 b.o15 = 15;
122                 b.o16 = 16;
123                 b.o17 = 17;
124                 b.o18 = 18;
125                 b.o19 = 19;
126                 b.o20 = 20;
127                 b.o21 = 21;
128                 b.o22 = 22;
129                 b.o23 = 23;
130                 b.o24 = 24;
131                 b.o25 = 25;
132                 b.o26 = 26;
133                 b.o27 = 27;
134                 b.o28 = 28;
135                 b.o29 = 29;
136                 b.o30 = 30;
137         }
138
139         // Test marking of objects with > 32 fields
140         public static int test_528_mark_runlength_large () {
141                 BigClass b = new BigClass ();
142
143                 /* 
144                  * Do the initialization in a separate method so no object refs remain in
145                  * spill slots.
146                  */
147                 set_fields (b);
148
149                 GC.Collect (1);
150
151                 return 
152                         (int)b.o1 + (int)b.o2 + (int)b.o3 + (int)b.o4 + (int)b.o5 +
153                         (int)b.o6 + (int)b.o7 + (int)b.o8 + (int)b.o9 + (int)b.o10 +
154                         (int)b.o11 + (int)b.o12 + (int)b.o13 + (int)b.o14 + (int)b.o15 +
155                         (int)b.o16 + (int)b.o17 + (int)b.o18 + (int)b.o19 + (int)b.o20 +
156                         (int)b.o21 + (int)b.o22 + (int)b.o23 + (int)b.o24 + (int)b.o25 +
157                         (int)b.o26 + (int)b.o27 + (int)b.o28 + (int)b.o29 + (int)b.o30 +
158                         (int)b.o31 + (int)b.o32;
159         }
160 }