Merge pull request #495 from nicolas-raoul/fix-for-issue2907-with-no-formatting-changes
[mono.git] / mcs / tests / test-52.cs
1 //
2 // Tests the foreach on strings, and tests the implicit use of foreach
3 // to pull the enumerator from the class and identify the pattern to be called
4 //
5 using System;
6 using System.Collections;
7
8 class Y {
9         int count = 0;
10         
11         public bool MoveNext ()
12         {
13                 count++;
14                 return count != 10;
15         }
16         
17         public object Current {
18                 get {
19                         return count;
20                 }
21         }
22 }
23
24 class X {
25
26         static string [] a = {
27                 "one", "two", "three"
28         };
29
30         public Y GetEnumerator ()
31         {
32                 return new Y ();
33         }
34         
35         public static int Main ()
36         {
37                 //
38                 // String test
39                 //
40                 string total = "";
41                 
42                 foreach (string s in a){
43                         total = total + s;
44                 }
45                 if (total != "onetwothree")
46                         return 1;
47
48                 //
49                 // Pattern test
50                 //
51                 X x = new X ();
52
53                 int t = 0;
54                 foreach (object o in x){
55                         t += (int) o;
56                 }
57                 if (t != 45)
58                         return 2;
59
60                 //
61                 // Looking for GetEnumerator on interfaces test
62                 //
63                 Hashtable xx = new Hashtable ();
64                 xx.Add ("A", 10);
65                 xx.Add ("B", 20);
66
67                 IDictionary vars = xx;
68                 string total2 = "";
69                 foreach (string name in vars.Keys){
70                         total2 = total2 + name;
71                 }
72
73                 if ((total2 != "AB") && (total2 != "BA"))
74                         return 3;
75
76                 ArrayList list = new ArrayList ();
77                 list.Add ("one");
78                 list.Add ("two");
79                 list.Add ("three");
80                 int count = 0;
81
82                 //
83                 // This test will make sure that `break' inside foreach will
84                 // actually use a `leave' opcode instead of a `br' opcode
85                 //
86                 foreach (string s in list){
87                         if (s == "two"){
88                                 break;
89                         }
90                         count++;
91                 }
92                 if (count != 1)
93                         return 4;
94                 
95                 Console.WriteLine ("test passes");
96                 return 0;
97         }
98 }