First set of licensing changes
[mono.git] / mono / mini / bench.cs
1 using System;
2 using System.Reflection;
3
4 /*
5  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
6  * Regression tests for the mono JIT.
7  *
8  * Each test needs to be of the form:
9  *
10  * static int test_<result>_<name> ();
11  *
12  * where <result> is an integer (the value that needs to be returned by
13  * the method to make it pass.
14  * <name> is a user-displayed name used to identify the test.
15  *
16  * The tests can be driven in two ways:
17  * *) running the program directly: Main() uses reflection to find and invoke
18  *      the test methods (this is useful mostly to check that the tests are correct)
19  * *) with the --regression switch of the jit (this is the preferred way since
20  *      all the tests will be run with optimizations on and off)
21  *
22  * The reflection logic could be moved to a .dll since we need at least another
23  * regression test file written in IL code to have better control on how
24  * the IL code looks.
25  */
26
27 class Tests {
28
29         static int Main (string[] args) {
30                 return TestDriver.RunTests (typeof (Tests), args);
31         }
32         
33         static public int test_0_many_nested_loops () {
34                 // we do the loop a few times otherwise it's too fast
35                 for (int i = 0; i < 5; ++i) {
36                 int n = 16;
37                 int x = 0;
38                 int a = n;
39                 while (a-- != 0) {
40                     int b = n;
41                     while (b-- != 0) {
42                         int c = n;
43                         while (c-- != 0) {
44                             int d = n;
45                         while (d-- != 0) {
46                                 int e = n;
47                                 while (e-- != 0) {
48                                     int f = n;
49                                     while (f-- != 0) {
50                                         x++;
51                                     }
52                                 }
53                         }
54                         }
55                     }
56                 }
57                 if (x != 16777216)
58                         return 1;
59                 }
60                 return 0;
61         }
62
63         public static int test_0_logic_run ()
64         {
65                 // GPL: Copyright (C) 2001  Southern Storm Software, Pty Ltd.
66                 int iter, i = 0;
67
68                 while (i++ < 10) {
69                 // Initialize.
70                 bool flag1 = true;
71                 bool flag2 = true;
72                 bool flag3 = true;
73                 bool flag4 = true;
74                 bool flag5 = true;
75                 bool flag6 = true;
76                 bool flag7 = true;
77                 bool flag8 = true;
78                 bool flag9 = true;
79                 bool flag10 = true;
80                 bool flag11 = true;
81                 bool flag12 = true;
82                 bool flag13 = true;
83
84                 // First set of tests.
85                 for(iter = 0; iter < 2000000; ++iter) {
86                         if((flag1 || flag2) && (flag3 || flag4) &&
87                            (flag5 || flag6 || flag7))
88                                 {
89                                 flag8 = !flag8;
90                                 flag9 = !flag9;
91                                 flag10 = !flag10;
92                                 flag11 = !flag11;
93                                 flag12 = !flag12;
94                                 flag13 = !flag13;
95                                 flag1 = !flag1;
96                                 flag2 = !flag2;
97                                 flag3 = !flag3;
98                                 flag4 = !flag4;
99                                 flag5 = !flag5;
100                                 flag6 = !flag6;
101                                 flag1 = !flag1;
102                                 flag2 = !flag2;
103                                 flag3 = !flag3;
104                                 flag4 = !flag4;
105                                 flag5 = !flag5;
106                                 flag6 = !flag6;
107                         }
108                 }
109                 }
110                 return 0;
111         }
112         static public int test_1028_sieve () {
113             //int NUM = ((argc == 2) ? atoi(argv[1]) : 1);
114             int NUM = 2000;
115             byte[] flags = new byte[8192 + 1];
116             int i, k;
117             int count = 0;
118
119             while (NUM-- != 0) {
120                 count = 0; 
121                 for (i=2; i <= 8192; i++) {
122                     flags[i] = 1;
123                 }
124                 for (i=2; i <= 8192; i++) {
125                     if (flags[i] != 0) {
126                         // remove all multiples of prime: i
127                         for (k=i+i; k <= 8192; k+=i) {
128                             flags[k] = 0;
129                         }
130                         count++;
131                     }
132                 }
133             }
134             //printf("Count: %d\n", count);
135             return(count);
136         }
137         
138         public static int fib (int n) {
139                 if (n < 2)
140                         return 1;
141                 return fib(n-2)+fib(n-1);
142         }
143
144         public static int test_3524578_fib () {
145                 for (int i = 0; i < 10; i++)
146                         fib (32);
147                 
148                 return fib (32);
149         }
150
151         private static ulong numMoves;
152
153         static void movetower (int disc, int from, int to, int use) {
154                 if (disc > 0) {         
155                         numMoves++;
156                         movetower (disc-1, from, use, to);
157                         movetower (disc-1, use, to, from);
158                 }
159         }
160
161         public static int test_0_hanoi () {
162                 int iterations = 5000;
163                 int numdiscs = 12;
164                 
165                 numMoves = 0;
166                 while (iterations > 0) {
167                         iterations--;
168                         movetower (numdiscs, 1, 3, 2);
169                 }
170                 if (numMoves != 20475000)
171                         return 1;
172                 return 0;
173         }
174
175         public static int test_0_castclass () {
176                 object a = "a";
177
178                 for (int i = 0; i < 100000000; i++) {
179                         string b = (string)a;
180                         if ((object)a != (object)b)
181                                 return 1;
182                 }
183                 return 0;
184         }
185         
186         public static int test_23005000_float () {
187                 double a, b, c, d;
188                 bool val;
189                 int loops = 0;
190                 a = 0.0;
191                 b = 0.0001;
192                 c = 2300.5;
193                 d = 1000.0;
194
195                 while (a < c) {
196                         if (a == d)
197                                 b *= 2;
198                         a += b;
199                         val = b >= c;
200                         if (val) break;
201                         loops++;
202                 }
203                 return loops;
204         }
205
206         /*
207         /// Gaussian blur of a generated grayscale picture
208         private int test_0_blur(int size) {
209                 const int num  = 5; // Number of time to blur
210                 byte[,] arr1 = new byte[size, size];
211                 byte[,] arr2 = new byte[size, size];
212
213                 int iterations = 1;
214                 
215                 while(iterations-- > 0) {
216
217                         // Draw fake picture
218                         for(int i = 0; i < size; i++) {
219                                 for(int j = 0; j < size; j++) {   
220                                         arr1[i, j] = (byte) (i%255); 
221                                 }
222                         }
223
224                         for(int n = 0; n < num; n++) { // num rounds of blurring
225                                 for(int i = 3; i < size-3; i++) // vertical blur arr1 -> arr2
226                                         for(int j = 0; j < size; j++)
227                                                 arr2[i, j] = (byte)((arr1[i-3, j] + arr1[i+3, j]
228                                                                      + 6*(arr1[i-2, j]+arr1[i+2, j])
229                                                                      + 15*(arr1[i-1, j]+arr1[i+1, j])
230                                                                      + 20*arr1[i, j] + 32)>>6);
231
232                                 for(int j = 3; j < size-3; j++) // horizontal blur arr1 -> arr2
233                                         for(int i = 0; i < size; i++)
234                                                 arr1[i, j] = (byte)((arr2[i, j-3] + arr2[i, j+3]
235                                                                      + 6*(arr2[i, j-2]+arr2[i, j+2])
236                                                                      + 15*(arr2[i, j-1]+arr2[i, j+1])
237                                                                      + 20*arr2[i, j] + 32)>>6);
238                         }
239                 }
240
241                 return 0;
242         }
243         */
244 }
245