Merge pull request #487 from mayerwin/patch-1
[mono.git] / mcs / tests / gtest-053.cs
1 //
2 // Important test: Type parameters and boxing (26.7.3).
3 //
4 // This tests the constrained_ prefix opcode.
5 //
6 using System;
7
8 public interface ICounter
9 {
10         void Increment ();
11 }
12
13 namespace ValueTypeCounters
14 {
15         public struct SimpleCounter : ICounter
16         {
17                 public int Value;
18
19                 public void Increment ()
20                 {
21                         Value += 2;
22                 }
23         }
24
25         public struct PrintingCounter : ICounter
26         {
27                 public int Value;
28
29                 public override string ToString ()
30                 {
31                         return Value.ToString ();
32                 }
33
34                 public void Increment ()
35                 {
36                         Value += 2;
37                 }
38         }
39
40         public struct ExplicitCounter : ICounter
41         {
42                 public int Value;
43
44                 public override string ToString ()
45                 {
46                         return Value.ToString ();
47                 }
48
49                 void ICounter.Increment ()
50                 {
51                         Value++;
52                 }
53         }
54
55         public struct InterfaceCounter : ICounter
56         {
57                 public int Value;
58
59                 public override string ToString ()
60                 {
61                         return Value.ToString ();
62                 }
63
64                 void ICounter.Increment ()
65                 {
66                         Value++;
67                 }
68
69                 public void Increment ()
70                 {
71                         Value += 2;
72                 }
73         }
74 }
75
76 namespace ReferenceTypeCounters
77 {
78         public class SimpleCounter : ICounter
79         {
80                 public int Value;
81
82                 public void Increment ()
83                 {
84                         Value += 2;
85                 }
86         }
87
88         public class PrintingCounter : ICounter
89         {
90                 public int Value;
91
92                 public override string ToString ()
93                 {
94                         return Value.ToString ();
95                 }
96
97                 public void Increment ()
98                 {
99                         Value += 2;
100                 }
101         }
102
103         public class ExplicitCounter : ICounter
104         {
105                 public int Value;
106
107                 public override string ToString ()
108                 {
109                         return Value.ToString ();
110                 }
111
112                 void ICounter.Increment ()
113                 {
114                         Value++;
115                 }
116         }
117
118         public class InterfaceCounter : ICounter
119         {
120                 public int Value;
121
122                 public override string ToString ()
123                 {
124                         return Value.ToString ();
125                 }
126
127                 void ICounter.Increment ()
128                 {
129                         Value++;
130                 }
131
132                 public void Increment ()
133                 {
134                         Value += 2;
135                 }
136         }
137 }
138
139 namespace Test
140 {
141         using V = ValueTypeCounters;
142         using R = ReferenceTypeCounters;
143
144         public class Test<T>
145                 where T : ICounter
146         {
147                 public static void Foo (T x)
148                 {
149                         Console.WriteLine (x.ToString ());
150                         x.Increment ();
151                         Console.WriteLine (x.ToString ());
152                 }
153         }
154
155         public class X
156         {
157                 public static void Main ()
158                 {
159                         Test<V.SimpleCounter>.Foo (new V.SimpleCounter ());
160                         Test<V.PrintingCounter>.Foo (new V.PrintingCounter ());
161                         Test<V.ExplicitCounter>.Foo (new V.ExplicitCounter ());
162                         Test<V.InterfaceCounter>.Foo (new V.InterfaceCounter ());
163                         Test<R.SimpleCounter>.Foo (new R.SimpleCounter ());
164                         Test<R.PrintingCounter>.Foo (new R.PrintingCounter ());
165                         Test<R.ExplicitCounter>.Foo (new R.ExplicitCounter ());
166                         Test<R.InterfaceCounter>.Foo (new R.InterfaceCounter ());
167                 }
168         }
169 }