Resolve "you are registering twice the same counter address" warning on Windows Relea...
[mono.git] / mcs / tests / test-254.cs
1 using System;
2 using System.Reflection;
3
4 [assembly: Test]
5
6 namespace N 
7 {
8 }
9
10 [AttributeUsage(AttributeTargets.All)]
11 public class TestAttribute: Attribute
12 {
13 }
14
15 public class Test_1
16 {
17     [return: Test]
18     public void Test (int a)
19     {
20     }
21 }
22
23 [return: Test]
24 public delegate Delegate test_delegate(int i);
25
26
27 public class Test_2
28 {
29     public int Test
30     {
31         [return: Test]
32         get {
33             return 4;
34         }
35
36                 [return: Test]
37                 set {
38                 }
39     }
40
41         public bool Test2
42         {
43                 [param: Test]
44                 set {}
45         }
46 }
47
48 public class Test_3
49 {
50         [field: Test]
51         public event test_delegate e_1;
52
53         [method: Test]
54         public event test_delegate e_2;
55 }
56
57 public class Test_4
58 {
59         // TODO: Where to apply ?
60
61         [event: Test]
62         public event test_delegate e_1 {
63                 add {}
64                 remove {}
65         }
66
67         public event test_delegate e_2 {
68                 [return: Test]
69                 add {}
70                 [return: Test]
71                 remove {}
72         }
73
74         public event test_delegate e_3 {
75                 [param: Test]
76                 add {}
77                 [param: Test]
78                 remove {}
79         }
80 }
81
82
83 public class ClassMain
84 {
85         static bool failed = false;
86     
87         static void Assert (object[] attrs, bool expected_presence, int tc)
88         {
89                 if (attrs.Length == 1 && expected_presence)
90                         return;
91
92                 if (!expected_presence && attrs.Length == 0)
93                         return;
94
95                 Console.WriteLine ("#" + tc.ToString () + " failed");
96                 failed = true;
97         }
98
99         public static int Main () {
100                 MethodInfo mi = typeof (Test_1).GetMethod ("Test");
101                 Assert (mi.GetParameters ()[0].GetCustomAttributes (true), false, 1);
102                 Assert (mi.GetCustomAttributes (true), false, 2);
103                 Assert (mi.ReturnTypeCustomAttributes.GetCustomAttributes (true), true, 3);
104         
105                 mi = typeof (test_delegate).GetMethod ("Invoke");
106                 Assert (mi.GetParameters ()[0].GetCustomAttributes (true), false, 4);
107                 Assert (mi.GetCustomAttributes (true), false, 5);
108                 Assert (mi.ReturnTypeCustomAttributes.GetCustomAttributes (true), true, 6);
109
110                 /* Under net 2.0, SerializableAttribute is returned */
111                 if (typeof (test_delegate).GetCustomAttributes (false).Length != 1)
112                         Assert (typeof (test_delegate).GetCustomAttributes (false), false, 7);
113
114                 PropertyInfo pi = typeof (Test_2).GetProperty ("Test");
115                 Assert (pi.GetCustomAttributes (true), false, 31);
116                 Assert (pi.GetGetMethod ().GetCustomAttributes (true), false, 32);
117                 Assert (pi.GetGetMethod ().ReturnTypeCustomAttributes.GetCustomAttributes (true), true, 33);
118                 Assert (pi.GetSetMethod ().GetCustomAttributes (true), false, 34);
119                 Assert (pi.GetSetMethod ().ReturnTypeCustomAttributes.GetCustomAttributes (true), true, 35);
120                 pi = typeof (Test_2).GetProperty ("Test2");
121                 Assert (pi.GetCustomAttributes (true), false, 36);
122                 Assert (pi.GetSetMethod ().GetCustomAttributes (true), false, 37);
123                 Assert (pi.GetSetMethod ().ReturnTypeCustomAttributes.GetCustomAttributes (true), false, 38);
124                 Assert (pi.GetSetMethod ().GetParameters ()[0].GetCustomAttributes (true), true, 39);
125
126                 EventInfo ei = typeof(Test_3).GetEvent ("e_1");
127                 Assert (ei.GetCustomAttributes (true), false, 41);
128                 Assert (ei.GetAddMethod ().GetCustomAttributes (true), false, 42);
129                 Assert (ei.GetAddMethod ().ReturnTypeCustomAttributes.GetCustomAttributes (true), false, 43);
130                 Assert (ei.GetRemoveMethod ().GetCustomAttributes (true), false, 44);
131                 Assert (ei.GetRemoveMethod ().ReturnTypeCustomAttributes.GetCustomAttributes (true), false, 45);
132                 FieldInfo fi = typeof(Test_3).GetField ("e_1", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
133                 Assert (fi.GetCustomAttributes (true), true, 46);
134
135                 ei = typeof(Test_3).GetEvent ("e_2");
136                 Assert (ei.GetCustomAttributes (true), false, 51);
137                 Assert (ei.GetAddMethod ().GetCustomAttributes (true), true, 52);
138                 Assert (ei.GetAddMethod ().ReturnTypeCustomAttributes.GetCustomAttributes (true), false, 53);
139                 Assert (ei.GetRemoveMethod ().GetCustomAttributes (true), true, 54);
140                 Assert (ei.GetRemoveMethod ().ReturnTypeCustomAttributes.GetCustomAttributes (true), false, 55);
141                 fi = typeof(Test_3).GetField ("e_2", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
142                 Assert (fi.GetCustomAttributes (true), false, 56);
143
144                 ei = typeof(Test_4).GetEvent ("e_2");
145                 Assert (ei.GetCustomAttributes (true), false, 71);
146                 Assert (ei.GetAddMethod ().GetCustomAttributes (true), false, 72);
147                 Assert (ei.GetAddMethod ().ReturnTypeCustomAttributes.GetCustomAttributes (true), true, 73);
148                 Assert (ei.GetRemoveMethod ().GetCustomAttributes (true), false, 74);
149                 Assert (ei.GetRemoveMethod ().ReturnTypeCustomAttributes.GetCustomAttributes (true), true, 75);
150                 fi = typeof(Test_3).GetField ("e_2", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
151                 Assert (fi.GetCustomAttributes (true), false, 76);
152
153                 ei = typeof(Test_4).GetEvent ("e_3");
154                 Assert (ei.GetCustomAttributes (true), false, 81);
155                 Assert (ei.GetAddMethod ().GetCustomAttributes (true), false, 82);
156                 Assert (ei.GetAddMethod ().ReturnTypeCustomAttributes.GetCustomAttributes (true), false, 83);
157                 Assert (ei.GetAddMethod ().GetParameters ()[0].GetCustomAttributes (true), true, 84);
158                 Assert (ei.GetRemoveMethod ().GetCustomAttributes (true), false, 85);
159                 Assert (ei.GetRemoveMethod ().ReturnTypeCustomAttributes.GetCustomAttributes (true), false, 86);
160                 Assert (ei.GetRemoveMethod ().GetParameters ()[0].GetCustomAttributes (true), true, 87);
161                 fi = typeof(Test_3).GetField ("e_2", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
162                 Assert (fi.GetCustomAttributes (true), false, 88);
163
164                 return failed ? 1 : 0;
165         }
166 }