2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mono / mini / exceptions.cs
index 78578bc853bfcb175233e606d2a2382356ab790e..0f42f004fc6058c884855dabf1c79f533164300a 100644 (file)
@@ -86,6 +86,7 @@ class Tests {
        static int test_0_byte_cast () {
                int a;
                long l;
+               ulong ul;
                byte b = 0;
                bool failed;
 
@@ -117,6 +118,7 @@ class Tests {
                if (b != 0)
                        return -2;
 
+
                try {
                        a = 256;
                        failed = true;
@@ -256,7 +258,22 @@ class Tests {
                        return 12;
                if (b != 0)
                        return -12;
-               
+
+               try {
+                       ul = 256;
+                       failed = true;
+                       checked {
+                               b = (byte)ul;
+                       }
+               }
+               catch (OverflowException) {
+                       failed = false;
+               }
+               if (failed)
+                       return 13;
+               if (b != 0)
+                       return -13;
+
                return 0;
        }
        
@@ -544,7 +561,7 @@ class Tests {
                if (failed)
                        return 20;
                if (b != 127)
-                       return -19;
+                       return -20;
 
                return 0;
        }
@@ -552,6 +569,7 @@ class Tests {
        static int test_0_ushort_cast () {
                int a;
                long l;
+               ulong ul;
                ushort b;
                bool failed;
 
@@ -699,6 +717,18 @@ class Tests {
                if (failed)
                        return 12;
 
+               try {
+                       ul = 0xfffff;
+                       failed = true;
+                       checked {
+                               b = (ushort)ul;
+                       }
+               } catch (OverflowException) {
+                       failed = false;
+               }
+               if (failed)
+                       return 13;
+
                return 0;
        }
        
@@ -901,6 +931,18 @@ class Tests {
                if (failed)
                        return 16;
 
+               try {
+                       l = 0x00000000ffffffff;
+                       failed = true;
+                       checked {
+                               b = (short)l;
+                       }
+               } catch (OverflowException) {
+                       failed = false;
+               }
+               if (failed)
+                       return 17;
+
                return 0;
        }
        
@@ -1009,6 +1051,33 @@ class Tests {
                if (failed)
                        return 8;
 
+               try {
+                       uint ui = System.UInt32.MaxValue;
+                       failed = true;
+                       checked {
+                               a = (int)ui;
+                       }
+               }
+               catch (OverflowException) {
+                       failed = false;
+               }
+               if (failed)
+                       return 9;
+
+               try {
+                       ulong ul = (long)(System.Int32.MaxValue) + 1;
+                       failed = true;
+                       checked {
+                               a = (int)ul;
+                       }
+               }
+               catch (OverflowException) {
+                       failed = false;
+               }
+               if (failed)
+                       return 10;
+
+
                {
                        int i; 
                        float f = 1.1f;
@@ -1121,6 +1190,19 @@ class Tests {
                if (failed)
                        return 8;
 
+               try {
+                       int i = -1;
+                       failed = true;
+                       checked {
+                               a = (uint)i;
+                       }
+               }
+               catch (OverflowException) {
+                       failed = false;
+               }
+               if (failed)
+                       return 9;
+
                {
                        uint i; 
                        float f = 1.1f;
@@ -1160,7 +1242,6 @@ class Tests {
                if (failed)
                        return 2;
                
-
                try {
                        double d = System.Int64.MinValue - 1024.0;
                        failed = false;                 
@@ -1200,6 +1281,12 @@ class Tests {
                ulong a;
                bool failed;
 
+               /*
+                * These tests depend on properties of x86 fp arithmetic so they won't work
+                * on other platforms.
+                */
+
+               /*
                try {
                        double d = System.UInt64.MaxValue - 1024.0;
                        failed = true;
@@ -1223,7 +1310,7 @@ class Tests {
                }
                if (failed)
                        return 2;
-               
+               */      
 
                try {
                        double d = 0;
@@ -1257,6 +1344,32 @@ class Tests {
                        }
                }
 
+               try {
+                       int i = -1;
+                       failed = true;
+                       checked {
+                               a = (ulong)i;
+                       }
+               }
+               catch (OverflowException) {
+                       failed = false;
+               }
+               if (failed)
+                       return 5;
+
+               try {
+                       int i = Int32.MinValue;
+                       failed = true;
+                       checked {
+                               a = (ulong)i;
+                       }
+               }
+               catch (OverflowException) {
+                       failed = false;
+               }
+               if (failed)
+                       return 6;
+
                return 0;
        }
 
@@ -1267,6 +1380,11 @@ class Tests {
                if ((uint)d != 4294967295)
                        return 1;
 
+               /*
+                * These tests depend on properties of x86 fp arithmetic so they won't work
+                * on other platforms.
+                */
+               /*
                d = 0xffffffffffffffff;
 
                if ((ulong)d != 0)
@@ -1277,6 +1395,7 @@ class Tests {
                        
                if ((byte)d != 0)
                        return 4;
+               */
                        
                d = 0xffff;
 
@@ -1975,6 +2094,15 @@ class Tests {
                                ThrowClass.rethrow2 ();
                        }
                        catch (Exception ex) {
+                               // Check that each catch clause has its own exception variable
+                               // If not, the throw below will overwrite the exception used
+                               // by the rethrow
+                               try {
+                                       throw new DivideByZeroException ();
+                               }
+                               catch (Exception foo) {
+                               }
+
                                throw;
                        }
                }
@@ -1984,6 +2112,91 @@ class Tests {
                }
 
                return 1;
-       }                       
+       }
+       
+       interface IFace {}
+       class Face : IFace {}
+               
+       static int test_1_array_mismatch_2 () {
+               try {
+                       object [] o = new Face [1];
+                       o [0] = 1;
+                       return 0;
+               } catch (ArrayTypeMismatchException) {
+                       return 1;
+               }
+       }
+       
+       static int test_1_array_mismatch_3 () {
+               try {
+                       object [] o = new IFace [1];
+                       o [0] = 1;
+                       return 0;
+               } catch (ArrayTypeMismatchException) {
+                       return 1;
+               }
+       }
+       
+       static int test_1_array_mismatch_4 () {
+               try {
+                       object [][] o = new Face [5] [];
+                       o [0] = new object [5];
+                       
+                       return 0;
+               } catch (ArrayTypeMismatchException) {
+                       return 1;
+               }
+       }
+
+       static int test_0_array_size () {
+               bool failed;
+
+               try {
+                       failed = true;
+                       int[] mem1 = new int [Int32.MaxValue];
+               }
+               catch (OutOfMemoryException e) {
+                       failed = false;
+               }
+               if (failed)
+                       return 1;
+
+               try {
+                       failed = true;
+                       int[,] mem2 = new int [Int32.MaxValue, Int32.MaxValue];
+               }
+               catch (OutOfMemoryException e) {
+                       failed = false;
+               }
+               if (failed)
+                       return 2;
+
+               return 0;
+       }
+
+       struct S {
+               int i, j, k, l, m, n;
+       }
+
+       static IntPtr[] addr;
+
+       static unsafe void throw_func (int i, S s) {
+               addr [i] = new IntPtr (&i);
+               throw new Exception ();
+       }
+
+       /* Test that arguments are correctly popped off the stack during unwinding */
+       static int test_0_stack_unwind () {
+               addr = new IntPtr [1000];
+               S s = new S ();
+               for (int j = 0; j < 1000; j++) {
+                       try {
+                               throw_func (j, s);
+                       }
+                       catch (Exception) {
+                       }
+               }
+               return (addr [0].ToInt64 () - addr [100].ToInt64 () < 100) ? 0 : 1;
+       }               
 }