/src/System.Linq.Expressions/tests/BinaryOperators/Bitwise/BinaryExclusiveOrTests.cs
C# | 271 lines | 233 code | 35 blank | 3 comment | 16 complexity | 3d6b3f03d94c3e3afee2c56a541ed07f MD5 | raw file
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the MIT license.
- // See the LICENSE file in the project root for more information.
- using System;
- using Xunit;
- namespace System.Linq.Expressions.Tests
- {
- public static class BinaryExclusiveOrTests
- {
- #region Test methods
- [Theory, ClassData(typeof(CompilationTypes))]
- public static void CheckByteExclusiveOrTest(bool useInterpreter)
- {
- byte[] array = new byte[] { 0, 1, byte.MaxValue };
- for (int i = 0; i < array.Length; i++)
- {
- for (int j = 0; j < array.Length; j++)
- {
- VerifyByteExclusiveOr(array[i], array[j], useInterpreter);
- }
- }
- }
- [Theory, ClassData(typeof(CompilationTypes))]
- public static void CheckSByteExclusiveOrTest(bool useInterpreter)
- {
- sbyte[] array = new sbyte[] { 0, 1, -1, sbyte.MinValue, sbyte.MaxValue };
- for (int i = 0; i < array.Length; i++)
- {
- for (int j = 0; j < array.Length; j++)
- {
- VerifySByteExclusiveOr(array[i], array[j], useInterpreter);
- }
- }
- }
- [Theory, ClassData(typeof(CompilationTypes))]
- public static void CheckUShortExclusiveOrTest(bool useInterpreter)
- {
- ushort[] array = new ushort[] { 0, 1, ushort.MaxValue };
- for (int i = 0; i < array.Length; i++)
- {
- for (int j = 0; j < array.Length; j++)
- {
- VerifyUShortExclusiveOr(array[i], array[j], useInterpreter);
- }
- }
- }
- [Theory, ClassData(typeof(CompilationTypes))]
- public static void CheckShortExclusiveOrTest(bool useInterpreter)
- {
- short[] array = new short[] { 0, 1, -1, short.MinValue, short.MaxValue };
- for (int i = 0; i < array.Length; i++)
- {
- for (int j = 0; j < array.Length; j++)
- {
- VerifyShortExclusiveOr(array[i], array[j], useInterpreter);
- }
- }
- }
- [Theory, ClassData(typeof(CompilationTypes))]
- public static void CheckUIntExclusiveOrTest(bool useInterpreter)
- {
- uint[] array = new uint[] { 0, 1, uint.MaxValue };
- for (int i = 0; i < array.Length; i++)
- {
- for (int j = 0; j < array.Length; j++)
- {
- VerifyUIntExclusiveOr(array[i], array[j], useInterpreter);
- }
- }
- }
- [Theory, ClassData(typeof(CompilationTypes))]
- public static void CheckIntExclusiveOrTest(bool useInterpreter)
- {
- int[] array = new int[] { 0, 1, -1, int.MinValue, int.MaxValue };
- for (int i = 0; i < array.Length; i++)
- {
- for (int j = 0; j < array.Length; j++)
- {
- VerifyIntExclusiveOr(array[i], array[j], useInterpreter);
- }
- }
- }
- [Theory, ClassData(typeof(CompilationTypes))]
- public static void CheckULongExclusiveOrTest(bool useInterpreter)
- {
- ulong[] array = new ulong[] { 0, 1, ulong.MaxValue };
- for (int i = 0; i < array.Length; i++)
- {
- for (int j = 0; j < array.Length; j++)
- {
- VerifyULongExclusiveOr(array[i], array[j], useInterpreter);
- }
- }
- }
- [Theory, ClassData(typeof(CompilationTypes))]
- public static void CheckLongExclusiveOrTest(bool useInterpreter)
- {
- long[] array = new long[] { 0, 1, -1, long.MinValue, long.MaxValue };
- for (int i = 0; i < array.Length; i++)
- {
- for (int j = 0; j < array.Length; j++)
- {
- VerifyLongExclusiveOr(array[i], array[j], useInterpreter);
- }
- }
- }
- #endregion
- #region Test verifiers
- private static void VerifyByteExclusiveOr(byte a, byte b, bool useInterpreter)
- {
- Expression<Func<byte>> e =
- Expression.Lambda<Func<byte>>(
- Expression.ExclusiveOr(
- Expression.Constant(a, typeof(byte)),
- Expression.Constant(b, typeof(byte))),
- Enumerable.Empty<ParameterExpression>());
- Func<byte> f = e.Compile(useInterpreter);
- Assert.Equal((byte)(a ^ b), f());
- }
- private static void VerifySByteExclusiveOr(sbyte a, sbyte b, bool useInterpreter)
- {
- Expression<Func<sbyte>> e =
- Expression.Lambda<Func<sbyte>>(
- Expression.ExclusiveOr(
- Expression.Constant(a, typeof(sbyte)),
- Expression.Constant(b, typeof(sbyte))),
- Enumerable.Empty<ParameterExpression>());
- Func<sbyte> f = e.Compile(useInterpreter);
- Assert.Equal((sbyte)(a ^ b), f());
- }
- private static void VerifyUShortExclusiveOr(ushort a, ushort b, bool useInterpreter)
- {
- Expression<Func<ushort>> e =
- Expression.Lambda<Func<ushort>>(
- Expression.ExclusiveOr(
- Expression.Constant(a, typeof(ushort)),
- Expression.Constant(b, typeof(ushort))),
- Enumerable.Empty<ParameterExpression>());
- Func<ushort> f = e.Compile(useInterpreter);
- Assert.Equal((ushort)(a ^ b), f());
- }
- private static void VerifyShortExclusiveOr(short a, short b, bool useInterpreter)
- {
- Expression<Func<short>> e =
- Expression.Lambda<Func<short>>(
- Expression.ExclusiveOr(
- Expression.Constant(a, typeof(short)),
- Expression.Constant(b, typeof(short))),
- Enumerable.Empty<ParameterExpression>());
- Func<short> f = e.Compile(useInterpreter);
- Assert.Equal((short)(a ^ b), f());
- }
- private static void VerifyUIntExclusiveOr(uint a, uint b, bool useInterpreter)
- {
- Expression<Func<uint>> e =
- Expression.Lambda<Func<uint>>(
- Expression.ExclusiveOr(
- Expression.Constant(a, typeof(uint)),
- Expression.Constant(b, typeof(uint))),
- Enumerable.Empty<ParameterExpression>());
- Func<uint> f = e.Compile(useInterpreter);
- Assert.Equal(a ^ b, f());
- }
- private static void VerifyIntExclusiveOr(int a, int b, bool useInterpreter)
- {
- Expression<Func<int>> e =
- Expression.Lambda<Func<int>>(
- Expression.ExclusiveOr(
- Expression.Constant(a, typeof(int)),
- Expression.Constant(b, typeof(int))),
- Enumerable.Empty<ParameterExpression>());
- Func<int> f = e.Compile(useInterpreter);
- Assert.Equal(a ^ b, f());
- }
- private static void VerifyULongExclusiveOr(ulong a, ulong b, bool useInterpreter)
- {
- Expression<Func<ulong>> e =
- Expression.Lambda<Func<ulong>>(
- Expression.ExclusiveOr(
- Expression.Constant(a, typeof(ulong)),
- Expression.Constant(b, typeof(ulong))),
- Enumerable.Empty<ParameterExpression>());
- Func<ulong> f = e.Compile(useInterpreter);
- Assert.Equal(a ^ b, f());
- }
- private static void VerifyLongExclusiveOr(long a, long b, bool useInterpreter)
- {
- Expression<Func<long>> e =
- Expression.Lambda<Func<long>>(
- Expression.ExclusiveOr(
- Expression.Constant(a, typeof(long)),
- Expression.Constant(b, typeof(long))),
- Enumerable.Empty<ParameterExpression>());
- Func<long> f = e.Compile(useInterpreter);
- Assert.Equal(a ^ b, f());
- }
- #endregion
- [Fact]
- public static void CannotReduce()
- {
- Expression exp = Expression.ExclusiveOr(Expression.Constant(0), Expression.Constant(0));
- Assert.False(exp.CanReduce);
- Assert.Same(exp, exp.Reduce());
- Assert.Throws<ArgumentException>(null, () => exp.ReduceAndCheck());
- }
- [Fact]
- public static void ThrowsOnLeftNull()
- {
- Assert.Throws<ArgumentNullException>("left", () => Expression.ExclusiveOr(null, Expression.Constant("")));
- }
- [Fact]
- public static void ThrowsOnRightNull()
- {
- Assert.Throws<ArgumentNullException>("right", () => Expression.ExclusiveOr(Expression.Constant(""), null));
- }
- private static class Unreadable<T>
- {
- public static T WriteOnly
- {
- set { }
- }
- }
- [Fact]
- public static void ThrowsOnLeftUnreadable()
- {
- Expression value = Expression.Property(null, typeof(Unreadable<int>), "WriteOnly");
- Assert.Throws<ArgumentException>("left", () => Expression.ExclusiveOr(value, Expression.Constant(1)));
- }
- [Fact]
- public static void ThrowsOnRightUnreadable()
- {
- Expression value = Expression.Property(null, typeof(Unreadable<int>), "WriteOnly");
- Assert.Throws<ArgumentException>("right", () => Expression.ExclusiveOr(Expression.Constant(1), value));
- }
- }
- }