PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/IronPython_Main/Runtime/Tests/ETScenarios/Operators/OnesComplement.cs

#
C# | 270 lines | 208 code | 39 blank | 23 comment | 0 complexity | fce971ae24f00051379a65b876d383d6 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  1. #if !CLR2
  2. using System.Linq.Expressions;
  3. #else
  4. using Microsoft.Scripting.Ast;
  5. #endif
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Reflection;
  9. namespace ETScenarios.Operators {
  10. using EU = ETUtils.ExpressionUtils;
  11. using Expr = Expression;
  12. using AstUtils = Microsoft.Scripting.Ast.Utils;
  13. public class OnesComplement {
  14. // OnesComplement of sbyte, short, int, long
  15. // OnesComplement of single, double, decimal constants
  16. [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "OnesComplement 1", new string[] { "positive", "OnesComplement", "operators", "Pri1" })]
  17. public static Expr OnesComplement1(EU.IValidator V) {
  18. List<Expression> Expressions = new List<Expression>();
  19. //Expressions.Add(EU.GenAreEqual(Expr.Constant((sbyte)(-1), typeof(sbyte)), Expr.OnesComplement(Expr.Constant((sbyte)1, typeof(sbyte))), "OnesComplement 1"));
  20. Expressions.Add(EU.GenAreEqual(Expr.Constant((short)(-2), typeof(short)), Expr.OnesComplement(Expr.Constant((short)1, typeof(short))), "OnesComplement 2"));
  21. Expressions.Add(EU.GenAreEqual(Expr.Constant((Int16)(-2), typeof(Int16)), Expr.OnesComplement(Expr.Constant((Int16)1, typeof(Int16))), "OnesComplement 3"));
  22. Expressions.Add(EU.GenAreEqual(Expr.Constant((Int32)(-2), typeof(Int32)), Expr.OnesComplement(Expr.Constant((Int32)1, typeof(Int32))), "OnesComplement 4"));
  23. Expressions.Add(EU.GenAreEqual(Expr.Constant((long)(-2), typeof(long)), Expr.OnesComplement(Expr.Constant((long)1, typeof(long))), "OnesComplement 5"));
  24. /*Expressions.Add(EU.GenAreEqual(Expr.Constant((Single)(-2), typeof(Single)), Expr.OnesComplement(Expr.Constant((Single)1, typeof(Single))), "OnesComplement 6"));
  25. Expressions.Add(EU.GenAreEqual(Expr.Constant((Double)(-2), typeof(Double)), Expr.OnesComplement(Expr.Constant((Double)1, typeof(Double))), "OnesComplement 7"));
  26. Expressions.Add(EU.GenAreEqual(Expr.Constant((Decimal)(-2), typeof(Decimal)), Expr.OnesComplement(Expr.Constant((Decimal)1, typeof(Decimal))), "OnesComplement 8"));*/
  27. var tree = EU.BlockVoid(Expressions);
  28. V.Validate(tree);
  29. return tree;
  30. }
  31. // OnesComplement of byte
  32. [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "OnesComplement 2", new string[] { "positive", "OnesComplement", "operators", "Pri1" })]
  33. public static Expr OnesComplement2(EU.IValidator V) {
  34. List<Expression> Expressions = new List<Expression>();
  35. Expressions.Add(EU.GenAreEqual(Expr.Constant((byte)(byte.MaxValue - 1)), Expr.OnesComplement(Expr.Constant((byte)1, typeof(byte)))));
  36. var tree = EU.BlockVoid(Expressions);
  37. V.Validate(tree);
  38. return tree;
  39. }
  40. // OnesComplement of sbyte
  41. [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "OnesComplement 3", new string[] { "positive", "OnesComplement", "operators", "Pri1" })]
  42. public static Expr OnesComplement3(EU.IValidator V) {
  43. List<Expression> Expressions = new List<Expression>();
  44. Expressions.Add(EU.GenAreEqual(Expr.Constant((sbyte)(-2), typeof(sbyte)), Expr.OnesComplement(Expr.Constant((sbyte)1, typeof(sbyte))), "OnesComplement 1"));
  45. var tree = EU.BlockVoid(Expressions);
  46. V.Validate(tree);
  47. return tree;
  48. }
  49. // OnesComplement of ushort
  50. [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "OnesComplement 4", new string[] { "positive", "OnesComplement", "operators", "Pri1" })]
  51. public static Expr OnesComplement4(EU.IValidator V) {
  52. List<Expression> Expressions = new List<Expression>();
  53. Expressions.Add(EU.GenAreEqual(Expr.Constant((ushort)(ushort.MaxValue - 1)), Expr.OnesComplement(Expr.Constant((ushort)1, typeof(ushort)))));
  54. var tree = EU.BlockVoid(Expressions);
  55. V.Validate(tree);
  56. return tree;
  57. }
  58. // OnesComplement of UInt16
  59. [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "OnesComplement 5", new string[] { "positive", "OnesComplement", "operators", "Pri1" })]
  60. public static Expr OnesComplement5(EU.IValidator V) {
  61. List<Expression> Expressions = new List<Expression>();
  62. Expressions.Add(EU.GenAreEqual(Expr.Constant((UInt16)(UInt16.MaxValue - 1)), Expr.OnesComplement(Expr.Constant((UInt16)1, typeof(UInt16)))));
  63. var tree = EU.BlockVoid(Expressions);
  64. V.Validate(tree);
  65. return tree;
  66. }
  67. // OnesComplement of UInt32
  68. [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "OnesComplement 6", new string[] { "ositive", "OnesComplement", "operators", "Pri1" })]
  69. public static Expr OnesComplement6(EU.IValidator V) {
  70. List<Expression> Expressions = new List<Expression>();
  71. Expressions.Add(EU.GenAreEqual(Expr.Constant((UInt32)(UInt32.MaxValue - 1)), Expr.OnesComplement(Expr.Constant((UInt32)1, typeof(UInt32)))));
  72. var tree = EU.BlockVoid(Expressions);
  73. V.Validate(tree);
  74. return tree;
  75. }
  76. // OnesComplement of ulong
  77. [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "OnesComplement 7", new string[] { "positive", "OnesComplement", "operators", "Pri1" })]
  78. public static Expr OnesComplement7(EU.IValidator V) {
  79. List<Expression> Expressions = new List<Expression>();
  80. Expressions.Add(EU.GenAreEqual(Expr.Constant((ulong)(ulong.MaxValue - 1)), Expr.OnesComplement(Expr.Constant((ulong)1, typeof(ulong)))));
  81. var tree = EU.BlockVoid(Expressions);
  82. V.Validate(tree);
  83. return tree;
  84. }
  85. // OnesComplement of boolean constant
  86. [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "OnesComplement 8", new string[] { "negative", "OnesComplement", "operators", "Pri1" }, Exception = typeof(InvalidOperationException))]
  87. public static Expr OnesComplement8(EU.IValidator V) {
  88. List<Expression> Expressions = new List<Expression>();
  89. Expressions.Add(EU.Throws<System.InvalidOperationException>(() => { Expr.OnesComplement(Expr.Constant(true)); }));
  90. return Expr.Empty();
  91. }
  92. // OnesComplement of string constant
  93. [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "OnesComplement 9", new string[] { "negative", "OnesComplement", "operators", "Pri1" }, Exception = typeof(InvalidOperationException))]
  94. public static Expr OnesComplement9(EU.IValidator V) {
  95. List<Expression> Expressions = new List<Expression>();
  96. Expressions.Add(EU.Throws<System.InvalidOperationException>(() => { Expr.OnesComplement(Expr.Constant("Test")); }));
  97. return Expr.Empty();
  98. }
  99. // OnesComplement of class object, no user defined operator
  100. public class MyType {
  101. public int Data { get; set; }
  102. public MyType(int x) { Data = x; }
  103. }
  104. [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "OnesComplement 10", new string[] { "negative", "OnesComplement", "operators", "Pri1" }, Exception = typeof(InvalidOperationException))]
  105. public static Expr OnesComplement10(EU.IValidator V) {
  106. List<Expression> Expressions = new List<Expression>();
  107. Expressions.Add(EU.Throws<System.InvalidOperationException>(() => { Expr.OnesComplement(Expr.Constant(new MyType(2))); }));
  108. return Expr.Empty();
  109. }
  110. // OnesComplement of structure object, no user defined operator
  111. public struct MyStruct {
  112. }
  113. [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "OnesComplement 11", new string[] { "negative", "OnesComplement", "operators", "Pri1" }, Exception = typeof(InvalidOperationException))]
  114. public static Expr OnesComplement11(EU.IValidator V) {
  115. List<Expression> Expressions = new List<Expression>();
  116. Expressions.Add(EU.Throws<System.InvalidOperationException>(() => { Expr.OnesComplement(Expr.Constant(new MyStruct())); }));
  117. return Expr.Empty();
  118. }
  119. // Pass null to method
  120. [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "OnesComplement 12", new string[] { "positive", "OnesComplement", "operators", "Pri1" })]
  121. public static Expr OnesComplement12(EU.IValidator V) {
  122. List<Expression> Expressions = new List<Expression>();
  123. Expressions.Add(EU.GenAreEqual(Expr.Constant(-2), Expr.OnesComplement(Expr.Constant(1), null)));
  124. var tree = EU.BlockVoid(Expressions);
  125. V.Validate(tree);
  126. return tree;
  127. }
  128. // Pass a methodinfo that takes no arguments
  129. public static int OnesComplementNoArgs() {
  130. return -1;
  131. }
  132. [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "OnesComplement 13", new string[] { "negative", "OnesComplement", "operators", "Pri1" }, Exception = typeof(ArgumentException))]
  133. public static Expr OnesComplement13(EU.IValidator V) {
  134. List<Expression> Expressions = new List<Expression>();
  135. MethodInfo mi = typeof(OnesComplement).GetMethod("OnesComplementNoArgs");
  136. EU.Throws<System.ArgumentException>(() =>
  137. {
  138. Expressions.Add(EU.GenAreEqual(Expr.Constant(-1), Expr.OnesComplement(Expr.Constant(1), mi)));
  139. });
  140. return Expr.Empty();
  141. }
  142. // Pass a methodinfo that takes a paramarray
  143. public static int OnesComplementParams(params int[] x) {
  144. return -1;
  145. }
  146. [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "OnesComplement 14", new string[] { "negative", "OnesComplement", "operators", "Pri1" }, Exception = typeof(InvalidOperationException))]
  147. public static Expr OnesComplement14(EU.IValidator V) {
  148. List<Expression> Expressions = new List<Expression>();
  149. MethodInfo mi = typeof(OnesComplement).GetMethod("OnesComplementParams");
  150. EU.Throws<System.InvalidOperationException>(() =>
  151. {
  152. Expressions.Add(EU.GenAreEqual(Expr.Constant(-1), Expr.OnesComplement(Expr.Constant(1), mi)));
  153. });
  154. var tree = EU.BlockVoid(Expressions);
  155. return tree;
  156. }
  157. // With a valid method info, pass a value that widen to the argument of the method
  158. public static int validOnesComplement(double x) {
  159. return (Int32)(-(x * x));
  160. }
  161. [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "OnesComplement 15", new string[] { "negative", "OnesComplement", "operators", "Pri1" }, Exception = typeof(InvalidOperationException))]
  162. public static Expr OnesComplement15(EU.IValidator V) {
  163. List<Expression> Expressions = new List<Expression>();
  164. MethodInfo mi = typeof(OnesComplement).GetMethod("validOnesComplement");
  165. EU.Throws<System.InvalidOperationException>(() =>
  166. {
  167. Expressions.Add(EU.GenAreEqual(Expr.Constant(-4), Expr.OnesComplement(Expr.Constant((Int32)2, typeof(Int32)), mi)));
  168. });
  169. var tree = EU.BlockVoid(Expressions);
  170. return tree;
  171. }
  172. // With a valid method info, pass a value of a derived class of the methodinfo's argument
  173. public class MyDerivedType : MyType {
  174. public MyDerivedType(int x) : base(x) { }
  175. }
  176. public static int OnesComplementBase(MyType x) {
  177. return -(x.Data * x.Data);
  178. }
  179. [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "OnesComplement 16", new string[] { "positive", "OnesComplement", "operators", "Pri1" })]
  180. public static Expr OnesComplement16(EU.IValidator V) {
  181. List<Expression> Expressions = new List<Expression>();
  182. MethodInfo mi = typeof(OnesComplement).GetMethod("OnesComplementBase");
  183. Expressions.Add(
  184. EU.GenAreEqual(
  185. Expr.Constant(-4),
  186. Expr.OnesComplement(Expr.Constant(new MyDerivedType(2)), mi),
  187. "OnesComplement 1"
  188. )
  189. );
  190. var tree = EU.BlockVoid(Expressions);
  191. V.Validate(tree);
  192. return tree;
  193. }
  194. // With a valid methodinfo that returns boolean, pass an argument of nullable type
  195. public static bool OnesComplementNullableTest(int x) {
  196. return (x > 0) ? true : false;
  197. }
  198. [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "OnesComplement 17", new string[] { "positive", "OnesComplement", "operators", "Pri1" })]
  199. public static Expr OnesComplement17(EU.IValidator V) {
  200. List<Expression> Expressions = new List<Expression>();
  201. MethodInfo mi = typeof(OnesComplement).GetMethod("OnesComplementNullableTest");
  202. Expressions.Add(
  203. EU.GenAreEqual(
  204. Expr.Constant(true, typeof(Nullable<bool>)),
  205. Expr.OnesComplement(
  206. Expr.Constant((Nullable<int>)1, typeof(Nullable<int>)),
  207. mi
  208. )
  209. )
  210. );
  211. var tree = EU.BlockVoid(Expressions);
  212. V.Validate(tree);
  213. return tree;
  214. }
  215. // Overflow type (by negative type.minValue)
  216. [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "OnesComplement 19", new string[] { "positive", "OnesComplement", "operators", "Pri1" })]
  217. public static Expr OnesComplement19(EU.IValidator V) {
  218. List<Expression> Expressions = new List<Expression>();
  219. Expressions.Add(
  220. EU.GenAreEqual(
  221. Expr.Constant(Int32.MaxValue),
  222. Expr.OnesComplement(Expr.Constant(Int32.MinValue)),
  223. "OnesComplement 1"
  224. )
  225. );
  226. var tree = EU.BlockVoid(Expressions);
  227. V.Validate(tree);
  228. return tree;
  229. }
  230. }
  231. }