Added the complementary test cases of going from a negative int to an unsigned.
authorBrian Sullivan <briansul@microsoft.com>
Fri, 21 Jun 2019 01:43:32 +0000 (18:43 -0700)
committerBrian Sullivan <briansul@microsoft.com>
Fri, 21 Jun 2019 01:43:32 +0000 (18:43 -0700)
tests/src/JIT/Regression/JitBlue/GitHub_25134/GitHub_25134.cs

index e68e2b8..05b21d7 100644 (file)
@@ -8,26 +8,31 @@ using System.Runtime.CompilerServices;
 class Program
 {
     static bool s_caughtException;
-    static uint s_value = int.MaxValue + 1U;
-    static int  s_result = 0;
+
+    static uint s_uint_value = int.MaxValue + 1U;
+    static int  s_int_result = 0;
+
+    static int  s_int_value =  -1;
+    static uint s_uint_result = 0;
 
     [MethodImpl(MethodImplOptions.AggressiveInlining)]
     static int CastToIntChecked(uint value)
     {
+       // checked cast of uint to int
         return checked((int)value);
     }
 
-    // Testing a checked cast to Uint -- the inlining case
+    // Testing a checked cast of uint to int -- the inlining case
     //
     [MethodImpl(MethodImplOptions.NoInlining)]
     static void Test1()
     {
-        int result = CastToIntChecked(s_value);
-       s_result = result;
+        int result = CastToIntChecked(s_uint_value);
+       s_int_result = result;
        Console.WriteLine("Result is " + result);
     }
 
-    // Testing a checked cast to Uint -- the non-inlining case
+    // Testing a checked cast of uint to int -- the non-inlining case
     //
     [MethodImpl(MethodImplOptions.NoInlining)]
     static void Test2()
@@ -37,10 +42,51 @@ class Program
         {
             s_caughtException = false;
 
-            copy = s_value;
+            copy = s_uint_value;
 
             int result = checked((int)copy);
-            s_result = result;
+            s_int_result = result;
+            Console.WriteLine("Result is " + result);
+        }
+        catch (System.OverflowException ex)
+        {
+            s_caughtException = true;
+            Console.WriteLine("CORRECT: " + ex);
+            copy = 0;
+        }
+    }
+
+    [MethodImpl(MethodImplOptions.AggressiveInlining)]
+    static uint CastToUIntChecked(int value)
+    {
+       // checked cast of int to uint
+        return checked((uint)value);
+    }
+
+    // Testing a checked cast of int to uint -- the inlining case
+    //
+    [MethodImpl(MethodImplOptions.NoInlining)]
+    static void Test3()
+    {
+        uint result = CastToUIntChecked(s_int_value);
+       s_uint_result = result;
+       Console.WriteLine("Result is " + result);
+    }
+
+    // Testing a checked cast of int to uint -- the non-inlining case
+    //
+    [MethodImpl(MethodImplOptions.NoInlining)]
+    static void Test4()
+    {
+        uint copy = 0;
+        try
+        {
+            s_caughtException = false;
+
+            copy = s_uint_value;
+
+            int result = checked((int)copy);
+            s_int_result = result;
             Console.WriteLine("Result is " + result);
         }
         catch (System.OverflowException ex)
@@ -78,6 +124,29 @@ class Program
             failed = true;
         }
 
+        try
+        {
+            Test3();
+        }
+        catch (System.OverflowException ex)
+        {
+            s_caughtException = true;
+            Console.WriteLine("CORRECT: " + ex);
+        }
+
+        if (s_caughtException == false)
+        {
+            Console.WriteLine("FAILED - Test3");
+            failed = true;
+        }
+
+        Test4();
+        if (s_caughtException == false)
+        {
+            Console.WriteLine("FAILED - Test4");
+            failed = true;
+        }
+
         if (failed)
         {
             return 101;