PageRenderTime 39ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/src/System.Linq.Expressions/tests/BinaryOperators/Bitwise/BinaryExclusiveOrTests.cs

https://gitlab.com/0072016/0072016-corefx-
C# | 271 lines | 233 code | 35 blank | 3 comment | 16 complexity | 3d6b3f03d94c3e3afee2c56a541ed07f MD5 | raw file
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using Xunit;
  6. namespace System.Linq.Expressions.Tests
  7. {
  8. public static class BinaryExclusiveOrTests
  9. {
  10. #region Test methods
  11. [Theory, ClassData(typeof(CompilationTypes))]
  12. public static void CheckByteExclusiveOrTest(bool useInterpreter)
  13. {
  14. byte[] array = new byte[] { 0, 1, byte.MaxValue };
  15. for (int i = 0; i < array.Length; i++)
  16. {
  17. for (int j = 0; j < array.Length; j++)
  18. {
  19. VerifyByteExclusiveOr(array[i], array[j], useInterpreter);
  20. }
  21. }
  22. }
  23. [Theory, ClassData(typeof(CompilationTypes))]
  24. public static void CheckSByteExclusiveOrTest(bool useInterpreter)
  25. {
  26. sbyte[] array = new sbyte[] { 0, 1, -1, sbyte.MinValue, sbyte.MaxValue };
  27. for (int i = 0; i < array.Length; i++)
  28. {
  29. for (int j = 0; j < array.Length; j++)
  30. {
  31. VerifySByteExclusiveOr(array[i], array[j], useInterpreter);
  32. }
  33. }
  34. }
  35. [Theory, ClassData(typeof(CompilationTypes))]
  36. public static void CheckUShortExclusiveOrTest(bool useInterpreter)
  37. {
  38. ushort[] array = new ushort[] { 0, 1, ushort.MaxValue };
  39. for (int i = 0; i < array.Length; i++)
  40. {
  41. for (int j = 0; j < array.Length; j++)
  42. {
  43. VerifyUShortExclusiveOr(array[i], array[j], useInterpreter);
  44. }
  45. }
  46. }
  47. [Theory, ClassData(typeof(CompilationTypes))]
  48. public static void CheckShortExclusiveOrTest(bool useInterpreter)
  49. {
  50. short[] array = new short[] { 0, 1, -1, short.MinValue, short.MaxValue };
  51. for (int i = 0; i < array.Length; i++)
  52. {
  53. for (int j = 0; j < array.Length; j++)
  54. {
  55. VerifyShortExclusiveOr(array[i], array[j], useInterpreter);
  56. }
  57. }
  58. }
  59. [Theory, ClassData(typeof(CompilationTypes))]
  60. public static void CheckUIntExclusiveOrTest(bool useInterpreter)
  61. {
  62. uint[] array = new uint[] { 0, 1, uint.MaxValue };
  63. for (int i = 0; i < array.Length; i++)
  64. {
  65. for (int j = 0; j < array.Length; j++)
  66. {
  67. VerifyUIntExclusiveOr(array[i], array[j], useInterpreter);
  68. }
  69. }
  70. }
  71. [Theory, ClassData(typeof(CompilationTypes))]
  72. public static void CheckIntExclusiveOrTest(bool useInterpreter)
  73. {
  74. int[] array = new int[] { 0, 1, -1, int.MinValue, int.MaxValue };
  75. for (int i = 0; i < array.Length; i++)
  76. {
  77. for (int j = 0; j < array.Length; j++)
  78. {
  79. VerifyIntExclusiveOr(array[i], array[j], useInterpreter);
  80. }
  81. }
  82. }
  83. [Theory, ClassData(typeof(CompilationTypes))]
  84. public static void CheckULongExclusiveOrTest(bool useInterpreter)
  85. {
  86. ulong[] array = new ulong[] { 0, 1, ulong.MaxValue };
  87. for (int i = 0; i < array.Length; i++)
  88. {
  89. for (int j = 0; j < array.Length; j++)
  90. {
  91. VerifyULongExclusiveOr(array[i], array[j], useInterpreter);
  92. }
  93. }
  94. }
  95. [Theory, ClassData(typeof(CompilationTypes))]
  96. public static void CheckLongExclusiveOrTest(bool useInterpreter)
  97. {
  98. long[] array = new long[] { 0, 1, -1, long.MinValue, long.MaxValue };
  99. for (int i = 0; i < array.Length; i++)
  100. {
  101. for (int j = 0; j < array.Length; j++)
  102. {
  103. VerifyLongExclusiveOr(array[i], array[j], useInterpreter);
  104. }
  105. }
  106. }
  107. #endregion
  108. #region Test verifiers
  109. private static void VerifyByteExclusiveOr(byte a, byte b, bool useInterpreter)
  110. {
  111. Expression<Func<byte>> e =
  112. Expression.Lambda<Func<byte>>(
  113. Expression.ExclusiveOr(
  114. Expression.Constant(a, typeof(byte)),
  115. Expression.Constant(b, typeof(byte))),
  116. Enumerable.Empty<ParameterExpression>());
  117. Func<byte> f = e.Compile(useInterpreter);
  118. Assert.Equal((byte)(a ^ b), f());
  119. }
  120. private static void VerifySByteExclusiveOr(sbyte a, sbyte b, bool useInterpreter)
  121. {
  122. Expression<Func<sbyte>> e =
  123. Expression.Lambda<Func<sbyte>>(
  124. Expression.ExclusiveOr(
  125. Expression.Constant(a, typeof(sbyte)),
  126. Expression.Constant(b, typeof(sbyte))),
  127. Enumerable.Empty<ParameterExpression>());
  128. Func<sbyte> f = e.Compile(useInterpreter);
  129. Assert.Equal((sbyte)(a ^ b), f());
  130. }
  131. private static void VerifyUShortExclusiveOr(ushort a, ushort b, bool useInterpreter)
  132. {
  133. Expression<Func<ushort>> e =
  134. Expression.Lambda<Func<ushort>>(
  135. Expression.ExclusiveOr(
  136. Expression.Constant(a, typeof(ushort)),
  137. Expression.Constant(b, typeof(ushort))),
  138. Enumerable.Empty<ParameterExpression>());
  139. Func<ushort> f = e.Compile(useInterpreter);
  140. Assert.Equal((ushort)(a ^ b), f());
  141. }
  142. private static void VerifyShortExclusiveOr(short a, short b, bool useInterpreter)
  143. {
  144. Expression<Func<short>> e =
  145. Expression.Lambda<Func<short>>(
  146. Expression.ExclusiveOr(
  147. Expression.Constant(a, typeof(short)),
  148. Expression.Constant(b, typeof(short))),
  149. Enumerable.Empty<ParameterExpression>());
  150. Func<short> f = e.Compile(useInterpreter);
  151. Assert.Equal((short)(a ^ b), f());
  152. }
  153. private static void VerifyUIntExclusiveOr(uint a, uint b, bool useInterpreter)
  154. {
  155. Expression<Func<uint>> e =
  156. Expression.Lambda<Func<uint>>(
  157. Expression.ExclusiveOr(
  158. Expression.Constant(a, typeof(uint)),
  159. Expression.Constant(b, typeof(uint))),
  160. Enumerable.Empty<ParameterExpression>());
  161. Func<uint> f = e.Compile(useInterpreter);
  162. Assert.Equal(a ^ b, f());
  163. }
  164. private static void VerifyIntExclusiveOr(int a, int b, bool useInterpreter)
  165. {
  166. Expression<Func<int>> e =
  167. Expression.Lambda<Func<int>>(
  168. Expression.ExclusiveOr(
  169. Expression.Constant(a, typeof(int)),
  170. Expression.Constant(b, typeof(int))),
  171. Enumerable.Empty<ParameterExpression>());
  172. Func<int> f = e.Compile(useInterpreter);
  173. Assert.Equal(a ^ b, f());
  174. }
  175. private static void VerifyULongExclusiveOr(ulong a, ulong b, bool useInterpreter)
  176. {
  177. Expression<Func<ulong>> e =
  178. Expression.Lambda<Func<ulong>>(
  179. Expression.ExclusiveOr(
  180. Expression.Constant(a, typeof(ulong)),
  181. Expression.Constant(b, typeof(ulong))),
  182. Enumerable.Empty<ParameterExpression>());
  183. Func<ulong> f = e.Compile(useInterpreter);
  184. Assert.Equal(a ^ b, f());
  185. }
  186. private static void VerifyLongExclusiveOr(long a, long b, bool useInterpreter)
  187. {
  188. Expression<Func<long>> e =
  189. Expression.Lambda<Func<long>>(
  190. Expression.ExclusiveOr(
  191. Expression.Constant(a, typeof(long)),
  192. Expression.Constant(b, typeof(long))),
  193. Enumerable.Empty<ParameterExpression>());
  194. Func<long> f = e.Compile(useInterpreter);
  195. Assert.Equal(a ^ b, f());
  196. }
  197. #endregion
  198. [Fact]
  199. public static void CannotReduce()
  200. {
  201. Expression exp = Expression.ExclusiveOr(Expression.Constant(0), Expression.Constant(0));
  202. Assert.False(exp.CanReduce);
  203. Assert.Same(exp, exp.Reduce());
  204. Assert.Throws<ArgumentException>(null, () => exp.ReduceAndCheck());
  205. }
  206. [Fact]
  207. public static void ThrowsOnLeftNull()
  208. {
  209. Assert.Throws<ArgumentNullException>("left", () => Expression.ExclusiveOr(null, Expression.Constant("")));
  210. }
  211. [Fact]
  212. public static void ThrowsOnRightNull()
  213. {
  214. Assert.Throws<ArgumentNullException>("right", () => Expression.ExclusiveOr(Expression.Constant(""), null));
  215. }
  216. private static class Unreadable<T>
  217. {
  218. public static T WriteOnly
  219. {
  220. set { }
  221. }
  222. }
  223. [Fact]
  224. public static void ThrowsOnLeftUnreadable()
  225. {
  226. Expression value = Expression.Property(null, typeof(Unreadable<int>), "WriteOnly");
  227. Assert.Throws<ArgumentException>("left", () => Expression.ExclusiveOr(value, Expression.Constant(1)));
  228. }
  229. [Fact]
  230. public static void ThrowsOnRightUnreadable()
  231. {
  232. Expression value = Expression.Property(null, typeof(Unreadable<int>), "WriteOnly");
  233. Assert.Throws<ArgumentException>("right", () => Expression.ExclusiveOr(Expression.Constant(1), value));
  234. }
  235. }
  236. }