2003-04-11 Ville Palo <vi64pa@kolumbus.fi>
[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 int Main ()
66         {
67                 int [] a = new int [10];
68                 int [] b = new int [2];
69
70                 for (int i = 0; i < 10; i++)
71                         a [i] = 10 + i;
72
73                 for (int j = 0; j < 2; j++)
74                         b [j] = 50 + j;
75
76                 if (test_single (a) != 145)
77                         return 1;
78
79                 if (test_single (b) != 101)
80                         return 2;
81
82                 if (test_continue (a) != 131){
83                         Console.WriteLine ("Expecting: 131, got " + test_continue (a));
84                         return 3;
85                 }
86                 
87                 if (test_break (a) != 46){
88                         Console.WriteLine ("Expecting: 46, got " + test_break (a));
89                         return 4;
90                 }
91                 
92                 double [] d = new double [] { 1.0, 2.0, 3.0 };
93                 if (!test_double (d))
94                         return 5;
95
96                 return 0;
97         }
98 }
99