2005-04-12 Dick Porter <dick@ximian.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 Test {
12
13         [MethodImplAttribute(MethodImplOptions.Synchronized)]
14         public int test () {
15                 Monitor.Exit (this);
16                 Monitor.Enter (this);
17                 return 2 + 2;
18         }
19
20         [MethodImplAttribute(MethodImplOptions.Synchronized)]
21         public static int test_static () {
22                 Monitor.Exit (typeof (Test));
23                 Monitor.Enter (typeof (Test));
24                 return 2 + 2;
25         }
26
27         [MethodImplAttribute(MethodImplOptions.Synchronized)]
28         public int test_exception () {
29                 Monitor.Exit (this);
30                 throw new Exception ("A");
31         }
32
33         [MethodImplAttribute(MethodImplOptions.Synchronized)]
34         public virtual int test_virtual () {
35                 Monitor.Exit (this);
36                 Monitor.Enter (this);
37                 return 2 + 2;
38         }
39
40         public delegate int Delegate1 ();
41
42         static public int Main (String[] args) {
43                 Test b = new Test ();
44                 int res;
45
46                 Console.WriteLine ("Test1...");
47                 b.test ();
48                 Console.WriteLine ("Test2...");
49                 test_static ();
50                 Console.WriteLine ("Test3...");
51                 try {
52                         b.test_exception ();
53                 }
54                 catch (SynchronizationLockException ex) {
55                         return 1;
56                 }
57                 catch (Exception ex) {
58                         // OK
59                 }
60
61                 Console.WriteLine ("Test4...");
62                 b.test_virtual ();
63
64                 Console.WriteLine ("Test5...");
65                 Delegate1 d = new Delegate1 (b.test);
66                 res = d ();
67
68                 Console.WriteLine ("Test6...");
69                 d = new Delegate1 (test_static);
70                 res = d ();
71
72                 Console.WriteLine ("Test7...");
73                 d = new Delegate1 (b.test_virtual);
74                 res = d ();
75
76                 Console.WriteLine ("Test8...");
77                 d = new Delegate1 (b.test_exception);
78                 try {
79                         d ();
80                 }
81                 catch (SynchronizationLockException ex) {
82                         return 2;
83                 }
84                 catch (Exception ex) {
85                         // OK
86                 }
87
88                 return 0;
89         }
90 }