New test.
[mono.git] / mcs / tests / test-43.cs
1 //
2 // This test is used for testing the foreach array support
3 //
4 using System;
5
6 class X {
7
8         static int test_single (int [] a)
9         {
10                 int total = 0;
11
12                 foreach (int i in a)
13                         total += i;
14
15                 return total;
16         }
17
18         static int test_continue (int [] a)
19         {
20                 int total = 0;
21                 int j = 0;
22                 
23                 foreach (int i in a){
24                         j++;
25                         if (j == 5)
26                                 continue;
27                         total += i;
28                 }
29
30                 return total;
31         }
32
33         //
34         // test that we can iterate on doubles, with a float contorl
35         // variable (this excercises the conversion operator and the
36         // proper loading on the array
37         //
38         static bool test_double (double [] d)
39         {
40                 float t = 1.0F;
41                 t += 2.0F + 3.0F;
42                 float x = 0;
43                 
44                 foreach (float f in d){
45                         x += f;
46                 }
47                 return x == t;
48         }
49         
50         static int test_break (int [] a)
51         {
52                 int total = 0;
53                 int j = 0;
54
55                 foreach (int i in a){
56                         j++;
57                         if (j == 5)
58                                 break;
59                         total += i;
60                 }
61
62                 return total;
63         }
64
65         static bool test_multi (int [,] d)
66         {
67                 int total = 0;
68                 
69                 foreach (int a in d){
70                         total += a;
71                 }
72                 return (total == 46);
73         }
74         
75         static int Main ()
76         {
77                 int [] a = new int [10];
78                 int [] b = new int [2];
79
80                 for (int i = 0; i < 10; i++)
81                         a [i] = 10 + i;
82
83                 for (int j = 0; j < 2; j++)
84                         b [j] = 50 + j;
85
86                 if (test_single (a) != 145)
87                         return 1;
88
89                 if (test_single (b) != 101)
90                         return 2;
91
92                 if (test_continue (a) != 131){
93                         Console.WriteLine ("Expecting: 131, got " + test_continue (a));
94                         return 3;
95                 }
96                 
97                 if (test_break (a) != 46){
98                         Console.WriteLine ("Expecting: 46, got " + test_break (a));
99                         return 4;
100                 }
101                 
102                 double [] d = new double [] { 1.0, 2.0, 3.0 };
103                 if (!test_double (d))
104                         return 5;
105
106                 int [,] jj = new int [2,2];
107                 jj [0,0] = 10;
108                 jj [0,1] = 2;
109                 jj [1,0] = 30;
110                 jj [1,1] = 4;
111                 if (!test_multi (jj))
112                         return 6;
113                         
114                 return 0;
115         }
116 }
117