[System] UriKind.RelativeOrAbsolute workaround.
[mono.git] / mono / tests / synchronized.cs
1 //
2 //  synchronized.cs:
3 //
4 //    Tests for the 'synchronized' method attribute
5 //
6
7 using System;
8 using System.Threading;
9 using System.Runtime.CompilerServices;
10
11 class Tests {
12
13         // We use Monitor.Pulse to test that the object is synchronized
14
15         [MethodImplAttribute(MethodImplOptions.Synchronized)]
16         public int test () {
17                 Monitor.Pulse (this);
18                 //Monitor.Enter (this);
19                 return 2 + 2;
20         }
21
22         [MethodImplAttribute(MethodImplOptions.Synchronized)]
23         public static int test_static () {
24                 Monitor.Pulse (typeof (Tests));
25                 return 2 + 2;
26         }
27
28         [MethodImplAttribute(MethodImplOptions.Synchronized)]
29         public int test_exception () {
30                 Monitor.Exit (this);
31                 throw new Exception ("A");
32         }
33
34         [MethodImplAttribute(MethodImplOptions.Synchronized)]
35         public virtual int test_virtual () {
36                 Monitor.Pulse (this);
37                 return 2 + 2;
38         }
39
40         public static bool is_synchronized (object o) {
41                 try {
42                         Monitor.Pulse (o);
43                 }
44                 catch (SynchronizationLockException ex) {
45                         return false;
46                 }
47                 return true;
48         }
49
50         class Gen<T>
51         {
52                 [MethodImpl(MethodImplOptions.Synchronized)]
53                 public static void Run ()
54                 {
55                 }
56         }
57
58         public delegate int Delegate1 ();
59
60         static public int Main (String[] args) {
61                 Tests b = new Tests ();
62                 int res, err;
63
64                 Console.WriteLine ("Test1...");
65                 b.test ();
66                 if (is_synchronized (b))
67                         return 1;
68
69                 Console.WriteLine ("Test2...");
70                 test_static ();
71                 if (is_synchronized (typeof (Tests)))
72                         return 1;
73
74                 Console.WriteLine ("Test3...");
75                 try {
76                         b.test_exception ();
77                 }
78                 catch (SynchronizationLockException ex) {
79                         return 1;
80                 }
81                 catch (Exception ex) {
82                         // OK
83                 }
84                 if (is_synchronized (b))
85                         return 1;
86
87                 Console.WriteLine ("Test4...");
88                 b.test_virtual ();
89                 if (is_synchronized (b))
90                         return 1;
91
92                 Console.WriteLine ("Test5...");
93                 Delegate1 d = new Delegate1 (b.test);
94                 res = d ();
95                 if (is_synchronized (b))
96                         return 1;
97
98                 Console.WriteLine ("Test6...");
99                 d = new Delegate1 (test_static);
100                 res = d ();
101                 if (is_synchronized (typeof (Tests)))
102                         return 1;
103
104                 Console.WriteLine ("Test7...");
105                 d = new Delegate1 (b.test_virtual);
106                 res = d ();
107                 if (is_synchronized (b))
108                         return 1;
109
110                 Console.WriteLine ("Test8...");
111                 d = new Delegate1 (b.test_exception);
112                 try {
113                         d ();
114                 }
115                 catch (SynchronizationLockException ex) {
116                         return 2;
117                 }
118                 catch (Exception ex) {
119                         // OK
120                 }
121                 if (is_synchronized (b))
122                         return 1;
123
124                 Monitor.Enter (typeof (Gen<>));
125                 Thread t = new Thread (() =>
126                         {
127                                 Gen<object>.Run ();
128                         });
129                 t.Start ();
130                 t.Join ();
131                 Monitor.Exit (typeof (Gen<>));
132
133                 return 0;
134         }
135 }