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