c6a23620c5676565fe57615b1b48f56beb2a8e2e
[mono.git] / mono / mini / bench.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 (string[] args) {
29                 return TestDriver.RunTests (typeof (Tests), args);
30         }
31         
32         static public int test_0_many_nested_loops () {
33                 // we do the loop a few times otherwise it's too fast
34                 for (int i = 0; i < 5; ++i) {
35                 int n = 16;
36                 int x = 0;
37                 int a = n;
38                 while (a-- != 0) {
39                     int b = n;
40                     while (b-- != 0) {
41                         int c = n;
42                         while (c-- != 0) {
43                             int d = n;
44                         while (d-- != 0) {
45                                 int e = n;
46                                 while (e-- != 0) {
47                                     int f = n;
48                                     while (f-- != 0) {
49                                         x++;
50                                     }
51                                 }
52                         }
53                         }
54                     }
55                 }
56                 if (x != 16777216)
57                         return 1;
58                 }
59                 return 0;
60         }
61
62         public static int test_0_logic_run ()
63         {
64                 // GPL: Copyright (C) 2001  Southern Storm Software, Pty Ltd.
65                 int iter, i = 0;
66
67                 while (i++ < 10) {
68                 // Initialize.
69                 bool flag1 = true;
70                 bool flag2 = true;
71                 bool flag3 = true;
72                 bool flag4 = true;
73                 bool flag5 = true;
74                 bool flag6 = true;
75                 bool flag7 = true;
76                 bool flag8 = true;
77                 bool flag9 = true;
78                 bool flag10 = true;
79                 bool flag11 = true;
80                 bool flag12 = true;
81                 bool flag13 = true;
82
83                 // First set of tests.
84                 for(iter = 0; iter < 2000000; ++iter) {
85                         if((flag1 || flag2) && (flag3 || flag4) &&
86                            (flag5 || flag6 || flag7))
87                                 {
88                                 flag8 = !flag8;
89                                 flag9 = !flag9;
90                                 flag10 = !flag10;
91                                 flag11 = !flag11;
92                                 flag12 = !flag12;
93                                 flag13 = !flag13;
94                                 flag1 = !flag1;
95                                 flag2 = !flag2;
96                                 flag3 = !flag3;
97                                 flag4 = !flag4;
98                                 flag5 = !flag5;
99                                 flag6 = !flag6;
100                                 flag1 = !flag1;
101                                 flag2 = !flag2;
102                                 flag3 = !flag3;
103                                 flag4 = !flag4;
104                                 flag5 = !flag5;
105                                 flag6 = !flag6;
106                         }
107                 }
108                 }
109                 return 0;
110         }
111         static public int test_1028_sieve () {
112             //int NUM = ((argc == 2) ? atoi(argv[1]) : 1);
113             int NUM = 2000;
114             byte[] flags = new byte[8192 + 1];
115             int i, k;
116             int count = 0;
117
118             while (NUM-- != 0) {
119                 count = 0; 
120                 for (i=2; i <= 8192; i++) {
121                     flags[i] = 1;
122                 }
123                 for (i=2; i <= 8192; i++) {
124                     if (flags[i] != 0) {
125                         // remove all multiples of prime: i
126                         for (k=i+i; k <= 8192; k+=i) {
127                             flags[k] = 0;
128                         }
129                         count++;
130                     }
131                 }
132             }
133             //printf("Count: %d\n", count);
134             return(count);
135         }
136         
137         public static int fib (int n) {
138                 if (n < 2)
139                         return 1;
140                 return fib(n-2)+fib(n-1);
141         }
142
143         public static int test_3524578_fib () {
144                 for (int i = 0; i < 10; i++)
145                         fib (32);
146                 
147                 return fib (32);
148         }
149
150         private static ulong numMoves;
151
152         static void movetower (int disc, int from, int to, int use) {
153                 if (disc > 0) {         
154                         numMoves++;
155                         movetower (disc-1, from, use, to);
156                         movetower (disc-1, use, to, from);
157                 }
158         }
159
160         public static int test_0_hanoi () {
161                 int iterations = 5000;
162                 int numdiscs = 12;
163                 
164                 numMoves = 0;
165                 while (iterations > 0) {
166                         iterations--;
167                         movetower (numdiscs, 1, 3, 2);
168                 }
169                 if (numMoves != 20475000)
170                         return 1;
171                 return 0;
172         }
173
174         public static int test_0_castclass () {
175                 object a = "a";
176
177                 for (int i = 0; i < 100000000; i++) {
178                         string b = (string)a;
179                         if ((object)a != (object)b)
180                                 return 1;
181                 }
182                 return 0;
183         }
184         
185         public static int test_23005000_float () {
186                 double a, b, c, d;
187                 bool val;
188                 int loops = 0;
189                 a = 0.0;
190                 b = 0.0001;
191                 c = 2300.5;
192                 d = 1000.0;
193
194                 while (a < c) {
195                         if (a == d)
196                                 b *= 2;
197                         a += b;
198                         val = b >= c;
199                         if (val) break;
200                         loops++;
201                 }
202                 return loops;
203         }
204
205         /*
206         /// Gaussian blur of a generated grayscale picture
207         private int test_0_blur(int size) {
208                 const int num  = 5; // Number of time to blur
209                 byte[,] arr1 = new byte[size, size];
210                 byte[,] arr2 = new byte[size, size];
211
212                 int iterations = 1;
213                 
214                 while(iterations-- > 0) {
215
216                         // Draw fake picture
217                         for(int i = 0; i < size; i++) {
218                                 for(int j = 0; j < size; j++) {   
219                                         arr1[i, j] = (byte) (i%255); 
220                                 }
221                         }
222
223                         for(int n = 0; n < num; n++) { // num rounds of blurring
224                                 for(int i = 3; i < size-3; i++) // vertical blur arr1 -> arr2
225                                         for(int j = 0; j < size; j++)
226                                                 arr2[i, j] = (byte)((arr1[i-3, j] + arr1[i+3, j]
227                                                                      + 6*(arr1[i-2, j]+arr1[i+2, j])
228                                                                      + 15*(arr1[i-1, j]+arr1[i+1, j])
229                                                                      + 20*arr1[i, j] + 32)>>6);
230
231                                 for(int j = 3; j < size-3; j++) // horizontal blur arr1 -> arr2
232                                         for(int i = 0; i < size; i++)
233                                                 arr1[i, j] = (byte)((arr2[i, j-3] + arr2[i, j+3]
234                                                                      + 6*(arr2[i, j-2]+arr2[i, j+2])
235                                                                      + 15*(arr2[i, j-1]+arr2[i, j+1])
236                                                                      + 20*arr2[i, j] + 32)>>6);
237                         }
238                 }
239
240                 return 0;
241         }
242         */
243 }
244