コード例 #1
0
        private static void VerifyNullableStructWithStringConstant(Sc?value)
        {
            Expression <Func <Sc?> > e =
                Expression.Lambda <Func <Sc?> >(
                    Expression.Constant(value, typeof(Sc?)),
                    Enumerable.Empty <ParameterExpression>());
            Func <Sc?> f = e.Compile();

            Assert.Equal(value, f());
        }
コード例 #2
0
        private static Sc?[] GenerateNullableStructWithStringArray(int size)
        {
            Sc?[] array  = new Sc?[] { default(Sc), new Sc(), new Sc(null) };
            Sc?[] result = new Sc?[size];
            for (int i = 0; i < size; i++)
            {
                result[i] = array[i % array.Length];
            }

            return(result);
        }
コード例 #3
0
 public static void CheckNullableStructWithStringCoalesceTest()
 {
     Sc?[] array1 = new Sc?[] { null, default(Sc), new Sc(), new Sc(null) };
     Sc?[] array2 = new Sc?[] { null, default(Sc), new Sc(), new Sc(null) };
     for (int i = 0; i < array1.Length; i++)
     {
         for (int j = 0; j < array2.Length; j++)
         {
             VerifyNullableStructWithStringCoalesce(array1[i], array2[j]);
         }
     }
 }
コード例 #4
0
        private static void VerifyStructWithStringCoalesce(Sc?a, Sc b, bool useInterpreter)
        {
            Expression <Func <Sc> > e =
                Expression.Lambda <Func <Sc> >(
                    Expression.Coalesce(
                        Expression.Constant(a, typeof(Sc?)),
                        Expression.Constant(b, typeof(Sc))),
                    Enumerable.Empty <ParameterExpression>());
            Func <Sc> f = e.Compile(useInterpreter);

            Assert.Equal(a ?? b, f());
        }
コード例 #5
0
 public static void CheckStructWithStringCoalesceTest(bool useInterpreter)
 {
     Sc?[] array1 = new Sc?[] { null, default(Sc), new Sc(), new Sc(null) };
     Sc[]  array2 = new Sc[] { default(Sc), new Sc(), new Sc(null) };
     for (int i = 0; i < array1.Length; i++)
     {
         for (int j = 0; j < array2.Length; j++)
         {
             VerifyStructWithStringCoalesce(array1[i], array2[j], useInterpreter);
         }
     }
 }
コード例 #6
0
        private static void VerifyNullableStructWithString(bool condition, Sc?a, Sc?b, bool useInterpreter)
        {
            Expression <Func <Sc?> > e =
                Expression.Lambda <Func <Sc?> >(
                    Expression.Condition(
                        Expression.Constant(condition, typeof(bool)),
                        Expression.Constant(a, typeof(Sc?)),
                        Expression.Constant(b, typeof(Sc?))),
                    Enumerable.Empty <ParameterExpression>());
            Func <Sc?> f = e.Compile(useInterpreter);

            Assert.Equal(condition ? a : b, f());
        }
コード例 #7
0
 public static void CheckTernaryNullableStructWithStringTest()
 {
     bool[] array1 = new bool[] { false, true };
     Sc?[]  array2 = new Sc?[] { null, default(Sc), new Sc(), new Sc(null) };
     for (int i = 0; i < array1.Length; i++)
     {
         for (int j = 0; j < array2.Length; j++)
         {
             for (int k = 0; k < array2.Length; k++)
             {
                 VerifyNullableStructWithString(array1[i], array2[j], array2[k]);
             }
         }
     }
 }
コード例 #8
0
 public static void CheckTernaryArrayNullableStructWithStringArrayTest(bool useInterpreter)
 {
     bool[]  array1 = new bool[] { false, true };
     Sc?[][] array2 = new Sc?[][] { null, new Sc?[0], new Sc?[] { default(Sc), new Sc(), new Sc(null) }, new Sc?[100] };
     for (int i = 0; i < array1.Length; i++)
     {
         for (int j = 0; j < array2.Length; j++)
         {
             for (int k = 0; k < array2.Length; k++)
             {
                 VerifyArrayNullableStructWithStringArray(array1[i], array2[j], array2[k], useInterpreter);
             }
         }
     }
 }
コード例 #9
0
        private static void VerifyNullableStructWithStringCoalesce(Sc?a, Sc?b)
        {
            Expression <Func <Sc?> > e =
                Expression.Lambda <Func <Sc?> >(
                    Expression.Coalesce(
                        Expression.Constant(a, typeof(Sc?)),
                        Expression.Constant(b, typeof(Sc?))),
                    Enumerable.Empty <ParameterExpression>());
            Func <Sc?> f = e.Compile();

            // compute with expression tree
            Sc?       etResult    = default(Sc?);
            Exception etException = null;

            try
            {
                etResult = f();
            }
            catch (Exception ex)
            {
                etException = ex;
            }

            // compute with real IL
            Sc?       csResult    = default(Sc?);
            Exception csException = null;

            try
            {
                csResult = a ?? b;
            }
            catch (Exception ex)
            {
                csException = ex;
            }

            // either both should have failed the same way or they should both produce the same result
            if (etException != null || csException != null)
            {
                Assert.NotNull(etException);
                Assert.NotNull(csException);
                Assert.Equal(csException.GetType(), etException.GetType());
            }
            else
            {
                Assert.Equal(csResult, etResult);
            }
        }
コード例 #10
0
        private static void VerifyNullableStructWithString(bool condition, Sc?a, Sc?b)
        {
            Expression <Func <Sc?> > e =
                Expression.Lambda <Func <Sc?> >(
                    Expression.Condition(
                        Expression.Constant(condition, typeof(bool)),
                        Expression.Constant(a, typeof(Sc?)),
                        Expression.Constant(b, typeof(Sc?))),
                    Enumerable.Empty <ParameterExpression>());
            Func <Sc?> f = e.Compile();

            Sc?       result = default(Sc?);
            Exception fEx    = null;

            try
            {
                result = f();
            }
            catch (Exception ex)
            {
                fEx = ex;
            }

            Sc?       expected = default(Sc?);
            Exception csEx     = null;

            try
            {
                expected = condition ? a : b;
            }
            catch (Exception ex)
            {
                csEx = ex;
            }

            if (fEx != null || csEx != null)
            {
                Assert.NotNull(fEx);
                Assert.NotNull(csEx);
                Assert.Equal(csEx.GetType(), fEx.GetType());
            }
            else
            {
                Assert.Equal(expected, result);
            }
        }
コード例 #11
0
        public static void CheckNullableStructWithStringArrayListTest()
        {
            Sc?[][] array = new Sc?[][]
            {
                new Sc?[] {  },
                new Sc?[] { default(Sc) },
                new Sc?[] { default(Sc), new Sc(), new Sc(null) }
            };
            Expression[][] exprs = new Expression[array.Length][];
            for (int i = 0; i < array.Length; i++)
            {
                exprs[i] = new Expression[array[i].Length];
                for (int j = 0; j < array[i].Length; j++)
                {
                    Sc?val = array[i][j];
                    exprs[i][j] = Expression.Constant(val, typeof(Sc?));
                }
            }

            for (int i = 0; i < array.Length; i++)
            {
                VerifyNullableStructWithStringArrayList(array[i], exprs[i]);
            }
        }