2010-03-06 Zoltan Varga <vargaz@gmail.com>
[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         public delegate int Delegate1 ();
51
52         static public int Main (String[] args) {
53                 Tests b = new Tests ();
54                 int res, err;
55
56                 Console.WriteLine ("Test1...");
57                 b.test ();
58                 if (is_synchronized (b))
59                         return 1;
60
61                 Console.WriteLine ("Test2...");
62                 test_static ();
63                 if (is_synchronized (typeof (Tests)))
64                         return 1;
65
66                 Console.WriteLine ("Test3...");
67                 try {
68                         b.test_exception ();
69                 }
70                 catch (SynchronizationLockException ex) {
71                         // OK, this exception is thrown by the synchronized finalizer
72                 }
73                 catch (Exception ex) {
74                         // OK
75                 }
76                 if (is_synchronized (b))
77                         return 1;
78
79                 Console.WriteLine ("Test4...");
80                 b.test_virtual ();
81                 if (is_synchronized (b))
82                         return 1;
83
84                 Console.WriteLine ("Test5...");
85                 Delegate1 d = new Delegate1 (b.test);
86                 res = d ();
87                 if (is_synchronized (b))
88                         return 1;
89
90                 Console.WriteLine ("Test6...");
91                 d = new Delegate1 (test_static);
92                 res = d ();
93                 if (is_synchronized (typeof (Tests)))
94                         return 1;
95
96                 Console.WriteLine ("Test7...");
97                 d = new Delegate1 (b.test_virtual);
98                 res = d ();
99                 if (is_synchronized (b))
100                         return 1;
101
102                 Console.WriteLine ("Test8...");
103                 d = new Delegate1 (b.test_exception);
104                 try {
105                         d ();
106                 }
107                 catch (SynchronizationLockException ex) {
108                         // OK, this exception is thrown by the synchronized finalizer
109                 }
110                 catch (Exception ex) {
111                         // OK
112                 }
113                 if (is_synchronized (b))
114                         return 1;
115
116                 // Monitor tests
117                 
118                 // Never locked
119                 try {
120                         Monitor.Exit (locker);
121                         return 3;
122                 } catch (SynchronizationLockException) {
123                 }                       
124
125                 // Not locked
126                 Monitor.Enter (locker2);
127                 Monitor.Exit (locker2);
128                 try {
129                         Monitor.Exit (locker2);
130                         return 4;
131                 } catch (SynchronizationLockException) {
132                 }
133
134                 return 0;
135         }
136
137         static object locker = new object ();
138         static object locker2 = new object ();
139 }