2002-07-28 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / tests / test-53.cs
1 //
2 // Tests the using statement implementation
3 //
4 using System;
5 using System.IO;
6
7 class MyDispose : IDisposable {
8         public bool disposed;
9         
10         public void Dispose ()
11         {
12                 disposed = true;
13         }
14 }
15
16 //
17 // This class does not implement IDiposable, but has an implicit conversion
18 // defined
19 //
20 class NoIDispose {
21         static public MyDispose x;
22
23         public NoIDispose ()
24         {
25         }
26         
27         static NoIDispose ()
28         {
29                 x = new MyDispose ();
30         }
31         
32         public static implicit operator MyDispose (NoIDispose a)
33         {
34                 return x;
35         }
36 }
37
38 class Y {
39         static void B ()
40         {
41                 using (NoIDispose a = new NoIDispose ()){
42                 }
43         }
44         
45 }
46
47 class X {
48         static int Main ()
49         {
50                 MyDispose copy_a, copy_b, copy_c;
51
52                 //
53                 // Test whether the two `a' and `b' get disposed
54                 //
55                 using (MyDispose a = new MyDispose (), b = new MyDispose ()){
56                         copy_a = a;
57                         copy_b = b;
58                 }
59
60                 if (!copy_a.disposed)
61                         return 1;
62                 if (!copy_b.disposed)
63                         return 2;
64
65                 Console.WriteLine ("Nested using clause disposed");
66
67                 //
68                 // See if the variable `b' is disposed if there is
69                 // an error thrown inside the using block.
70                 //
71                 copy_c = null;
72                 try {
73                         using (MyDispose c = new MyDispose ()){
74                                 copy_c = c;
75                                 throw new Exception ();
76                         }
77                 } catch {}
78
79                 if (!copy_c.disposed)
80                         return 3;
81                 else
82                         Console.WriteLine ("Disposal on finally block works");
83
84                 //
85                 // This should test if `a' is non-null before calling dispose
86                 // implicitly
87                 //
88                 using (MyDispose d = null){
89                 }
90
91                 Console.WriteLine ("Null test passed");
92                 
93                 //
94                 // This tests that a variable is permitted here if there is
95                 // an implicit conversion to a type that implement IDisposable
96                 //
97                 using (NoIDispose a = new NoIDispose ()){
98                 }
99
100                 //
101                 // See if we dispose the object that can be implicitly converted
102                 // to IDisposable 
103                 if (NoIDispose.x.disposed != true)
104                         return 4;
105                 else
106                         Console.WriteLine ("Implicit conversion from type to IDisposable pass");
107
108                 MyDispose bb = new MyDispose ();
109                 using (bb){
110                         
111                 }
112                 if (bb.disposed == false)
113                         return 6;
114                 
115                 Console.WriteLine ("All tests pass");
116                 return 0;
117         }
118 }
119