2005-05-22 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mono / mini / exceptions.cs
index 78578bc853bfcb175233e606d2a2382356ab790e..566334c264d855118d1b522d31892fe8fbb9f759 100644 (file)
@@ -6,7 +6,7 @@ using System.Reflection;
  *
  * Each test needs to be of the form:
  *
- * static int test_<result>_<name> ();
+ * public static int test_<result>_<name> ();
  *
  * where <result> is an integer (the value that needs to be returned by
  * the method to make it pass.
@@ -25,11 +25,11 @@ using System.Reflection;
 
 class Tests {
 
-       static int Main () {
+       public static int Main () {
                return TestDriver.RunTests (typeof (Tests));
        }
 
-       static int test_0_catch () {
+       public static int test_0_catch () {
                Exception x = new Exception ();
                
                try {
@@ -41,7 +41,7 @@ class Tests {
                return 1;
        }
 
-       static int test_0_finally_without_exc () {
+       public static int test_0_finally_without_exc () {
                int x;
                
                try {
@@ -55,7 +55,7 @@ class Tests {
                return x;
        }
 
-       static int test_0_finally () {
+       public static int test_0_finally () {
                int x = 1;
                
                try {
@@ -68,7 +68,7 @@ class Tests {
                return x;
        }
 
-       static int test_0_nested_finally () {
+       public static int test_0_nested_finally () {
                int a;
 
                try {
@@ -83,9 +83,10 @@ class Tests {
                return a;
        }               
 
-       static int test_0_byte_cast () {
+       public 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,11 +258,26 @@ 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;
        }
        
-       static int test_0_sbyte_cast () {
+       public static int test_0_sbyte_cast () {
                int a;
                long l;
                sbyte b = 0;
@@ -544,14 +561,30 @@ class Tests {
                if (failed)
                        return 20;
                if (b != 127)
-                       return -19;
+                       return -20;
+
+               try {
+                       ulong ul = 128;
+                       failed = true;
+                       checked {
+                               b = (sbyte)ul;
+                       }
+               }
+               catch (OverflowException) {
+                       failed = false;
+               }
+               if (failed)
+                       return 21;
+               if (b != 127)
+                       return -21;
 
                return 0;
        }
 
-       static int test_0_ushort_cast () {
+       public static int test_0_ushort_cast () {
                int a;
                long l;
+               ulong ul;
                ushort b;
                bool failed;
 
@@ -699,10 +732,22 @@ 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;
        }
        
-       static int test_0_short_cast () {
+       public static int test_0_short_cast () {
                int a;
                long l;
                short b;
@@ -901,10 +946,34 @@ class Tests {
                if (failed)
                        return 16;
 
+               try {
+                       l = 0x00000000ffffffff;
+                       failed = true;
+                       checked {
+                               b = (short)l;
+                       }
+               } catch (OverflowException) {
+                       failed = false;
+               }
+               if (failed)
+                       return 17;
+
+               try {
+                       ulong ul = 32768;
+                       failed = true;
+                       checked {
+                               b = (short)ul;
+                       }
+               } catch (OverflowException) {
+                       failed = false;
+               }
+               if (failed)
+                       return 18;
+
                return 0;
        }
        
-       static int test_0_int_cast () {
+       public static int test_0_int_cast () {
                int a;
                long l;
                bool failed;
@@ -1009,6 +1078,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;
@@ -1020,7 +1116,7 @@ class Tests {
                return 0;
        }
 
-       static int test_0_uint_cast () {
+       public static int test_0_uint_cast () {
                uint a;
                long l;
                bool failed;
@@ -1121,6 +1217,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;
@@ -1132,7 +1241,7 @@ class Tests {
                return 0;
        }
        
-       static int test_0_long_cast () {
+       public static int test_0_long_cast () {
                long a;
                bool failed;
 
@@ -1160,7 +1269,6 @@ class Tests {
                if (failed)
                        return 2;
                
-
                try {
                        double d = System.Int64.MinValue - 1024.0;
                        failed = false;                 
@@ -1196,10 +1304,16 @@ class Tests {
                return 0;
        }
 
-       static int test_0_ulong_cast () {
+       public static int test_0_ulong_cast () {
                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 +1337,7 @@ class Tests {
                }
                if (failed)
                        return 2;
-               
+               */      
 
                try {
                        double d = 0;
@@ -1257,16 +1371,47 @@ 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;
        }
 
-       static int test_0_simple_double_casts () {
+       public static int test_0_simple_double_casts () {
 
                double d = 0xffffffff;
 
                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 +1422,7 @@ class Tests {
                        
                if ((byte)d != 0)
                        return 4;
+               */
                        
                d = 0xffff;
 
@@ -1289,7 +1435,7 @@ class Tests {
                return 0;
        }
        
-       static int test_0_div_zero () {
+       public static int test_0_div_zero () {
                int d = 1;
                int q = 0;
                int val;
@@ -1342,11 +1488,11 @@ class Tests {
                return 0;
        }
 
-       static int return_55 () {
+       public static int return_55 () {
                return 55;
        }
 
-       static int test_0_cfold_div_zero () {
+       public static int test_0_cfold_div_zero () {
                // Test that constant folding doesn't cause division by zero exceptions
                if (return_55 () != return_55 ()) {
                        int d = 1;
@@ -1367,7 +1513,7 @@ class Tests {
                return 0;
        }
 
-       static int test_0_udiv_zero () {
+       public static int test_0_udiv_zero () {
                uint d = 1;
                uint q = 0;
                uint val;
@@ -1394,7 +1540,7 @@ class Tests {
                return 0;
        }
 
-       static int test_0_long_div_zero () {
+       public static int test_0_long_div_zero () {
                long d = 1;
                long q = 0;
                long val;
@@ -1447,7 +1593,7 @@ class Tests {
                return 0;
        }
 
-       static int test_0_ulong_div_zero () {
+       public static int test_0_ulong_div_zero () {
                ulong d = 1;
                ulong q = 0;
                ulong val;
@@ -1474,7 +1620,7 @@ class Tests {
                return 0;
        }
 
-       static int test_0_float_div_zero () {
+       public static int test_0_float_div_zero () {
                double d = 1;
                double q = 0;
                double val;
@@ -1501,7 +1647,7 @@ class Tests {
                return 0;
        }
 
-       static int test_0_invalid_unbox () {
+       public static int test_0_invalid_unbox () {
 
                int i = 123;
                object o = "Some string";
@@ -1519,7 +1665,7 @@ class Tests {
        }
 
        // Test that double[] can't be cast to double (bug #46027)
-       static int test_0_invalid_unbox_arrays () {
+       public static int test_0_invalid_unbox_arrays () {
                double[] d1 = { 1.0 };
                double[][] d2 = { d1 };
                Array a = d2;
@@ -1538,7 +1684,7 @@ class Tests {
         * jumps out of multiple exception clauses: we used to execute just 
         * one enclosing finally block.
         */
-       static int finally_level;
+       public static int finally_level;
        static void do_something () {
                int a = 0;
                try {
@@ -1552,7 +1698,7 @@ class Tests {
                }
        }
 
-       static int test_2_multiple_finally_clauses () {
+       public static int test_2_multiple_finally_clauses () {
                finally_level = 0;
                do_something ();
                if (finally_level == 1)
@@ -1560,7 +1706,7 @@ class Tests {
                return 0;
        }
 
-       static int test_3_checked_cast_un () {
+       public static int test_3_checked_cast_un () {
                 ulong i = 0x8000000034000000;
                 long j;
 
@@ -1575,7 +1721,7 @@ class Tests {
                return 3;
        }
        
-       static int test_4_checked_cast () {
+       public static int test_4_checked_cast () {
                 long i;
                 ulong j;
 
@@ -1602,7 +1748,7 @@ class Tests {
                7, 0, 7, 1, 7, 2, 7, 3, 7, 4, 7, 5, 7, 6, 7, 7, 7, 8,
        };
 
-       static int test_0_multi_dim_array_access () {
+       public static int test_0_multi_dim_array_access () {
                int [,] a = System.Array.CreateInstance (typeof (int),
                        new int [] {3,6}, new int [] {2,2 }) as int[,];
                 int x, y;
@@ -1638,7 +1784,7 @@ class Tests {
                o = "buddy";
        }
 
-       static int test_2_array_mismatch () {
+       public static int test_2_array_mismatch () {
                string[] a = { "hello", "world" };
                object[] b = a;
                bool passed = false;
@@ -1656,7 +1802,7 @@ class Tests {
                return 2;
        }
 
-       static int test_0_ovf () {
+       public static int test_0_ovf () {
                int ocount = 0;
                
                checked {
@@ -1894,7 +2040,7 @@ class Tests {
        }
 
        class Broken {
-               static int i;
+               public static int i;
 
                static Broken () {
                        throw new Exception ("Ugh!");
@@ -1905,7 +2051,7 @@ class Tests {
                }
        }
 
-       static int test_0_exception_in_cctor () {
+       public static int test_0_exception_in_cctor () {
                try {
                        Broken.DoSomething ();
                }
@@ -1915,7 +2061,7 @@ class Tests {
                return 0;
        }
 
-       static int test_5_regalloc () {
+       public static int test_5_regalloc () {
                int i = 0;
 
                try {
@@ -1968,13 +2114,22 @@ class Tests {
                }
        }
 
-       static int test_0_rethrow_stacktrace () {
+       public static int test_0_rethrow_stacktrace () {
                // Check that rethrowing an exception preserves the original stack trace
                try {
                        try {
                                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 +2139,113 @@ class Tests {
                }
 
                return 1;
-       }                       
+       }
+       
+       interface IFace {}
+       class Face : IFace {}
+               
+       public static int test_1_array_mismatch_2 () {
+               try {
+                       object [] o = new Face [1];
+                       o [0] = 1;
+                       return 0;
+               } catch (ArrayTypeMismatchException) {
+                       return 1;
+               }
+       }
+       
+       public static int test_1_array_mismatch_3 () {
+               try {
+                       object [] o = new IFace [1];
+                       o [0] = 1;
+                       return 0;
+               } catch (ArrayTypeMismatchException) {
+                       return 1;
+               }
+       }
+       
+       public static int test_1_array_mismatch_4 () {
+               try {
+                       object [][] o = new Face [5] [];
+                       o [0] = new object [5];
+                       
+                       return 0;
+               } catch (ArrayTypeMismatchException) {
+                       return 1;
+               }
+       }
+
+       public 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 */
+       public 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;
+       }
+
+       public static int test_0_regress_73242 () {
+               int [] arr = new int [10];
+               for (int i = 0; i < 10; ++i)
+                       arr [i] = 0;
+               try {
+                       throw new Exception ();
+               }
+               catch {
+               }
+               return 0;
+    }
+
+       public static int test_0_nullref () {
+               try {
+                       Array foo = null;
+                       foo.Clone();
+               } catch (NullReferenceException e) {
+                       return 0;
+               }
+               return 1;
+       }
 }