PageRenderTime 34ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 1ms

/IronPython_Main/Runtime/Tests/ETScenariosCSLinq/CSLinq/ExpressionCompiler/binary_BackSl_comparison_BackSl_test2.cs

#
C# | 261 lines | 218 code | 33 blank | 10 comment | 42 complexity | 8f52872e28ced4060c63100abc5828fa 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. using Microsoft.Scripting.Utils;
  6. #endif
  7. using System.Reflection;
  8. using System;
  9. namespace ExpressionCompiler {
  10. //-------- Scenario 2177
  11. namespace Scenario2177{
  12. public class Test
  13. {
  14. [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "Equal__6", new string[] { "positive", "cslinq", "FullTrustOnly","Pri1" })]
  15. public static Expression Equal__6() {
  16. if(Main() != 0 ) {
  17. throw new Exception();
  18. } else {
  19. return Expression.Constant(0);
  20. }
  21. }
  22. public static int Main()
  23. {
  24. Ext.StartCapture();
  25. bool success = false;
  26. try
  27. {
  28. success = check_bool_Equal();
  29. }
  30. finally
  31. {
  32. Ext.StopCapture();
  33. }
  34. return success ? 0 : 1;
  35. }
  36. static bool check_bool_Equal() {
  37. bool[] svals = new bool[] { true };
  38. for (int i = 0; i < svals.Length; i++) {
  39. for (int j = 0; j < svals.Length; j++) {
  40. if (!check_bool_Equal(svals[i], svals[j])) {
  41. Console.WriteLine("bool_Equal failed");
  42. return false;
  43. }
  44. }
  45. }
  46. return true;
  47. }
  48. static bool check_bool_Equal(bool val0, bool val1) {
  49. Expression<Func<bool>> e =
  50. Expression.Lambda<Func<bool>>(
  51. Expression.Equal(Expression.Constant(val0, typeof(bool)),
  52. Expression.Constant(val1, typeof(bool))),
  53. new System.Collections.Generic.List<ParameterExpression>());
  54. Func<bool> f = e.Compile();
  55. bool fResult = default(bool);
  56. Exception fEx = null;
  57. try {
  58. fResult = f();
  59. }
  60. catch (Exception ex) {
  61. fEx = ex;
  62. }
  63. bool csResult = default(bool);
  64. Exception csEx = null;
  65. try {
  66. csResult = (bool) (val0 == val1);
  67. }
  68. catch (Exception ex) {
  69. csEx = ex;
  70. }
  71. if (fEx != null || csEx != null) {
  72. return fEx != null && csEx != null && fEx.GetType() == csEx.GetType();
  73. }
  74. else {
  75. return object.Equals(fResult, csResult);
  76. }
  77. }
  78. }
  79. public static class Ext {
  80. public static void StartCapture() {
  81. // MethodInfo m = Assembly.GetAssembly(typeof(Expression)).GetType("System.Linq.Expressions.ExpressionCompiler").GetMethod("StartCaptureToFile", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
  82. // m.Invoke(null, new object[] { "test.dll" });
  83. }
  84. public static void StopCapture() {
  85. // MethodInfo m = Assembly.GetAssembly(typeof(Expression)).GetType("System.Linq.Expressions.ExpressionCompiler").GetMethod("StopCapture", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
  86. // m.Invoke(null, new object[0]);
  87. }
  88. public static bool IsIntegralOrEnum(Type type) {
  89. switch (Type.GetTypeCode(GetNonNullableType(type))) {
  90. case TypeCode.Byte:
  91. case TypeCode.SByte:
  92. case TypeCode.Int16:
  93. case TypeCode.Int32:
  94. case TypeCode.Int64:
  95. case TypeCode.UInt16:
  96. case TypeCode.UInt32:
  97. case TypeCode.UInt64:
  98. return true;
  99. default:
  100. return false;
  101. }
  102. }
  103. public static bool IsFloating(Type type) {
  104. switch (Type.GetTypeCode(GetNonNullableType(type))) {
  105. case TypeCode.Single:
  106. case TypeCode.Double:
  107. return true;
  108. default:
  109. return false;
  110. }
  111. }
  112. public static Type GetNonNullableType(Type type) {
  113. return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>) ?
  114. type.GetGenericArguments()[0] :
  115. type;
  116. }
  117. }
  118. }
  119. //-------- Scenario 2178
  120. namespace Scenario2178{
  121. public class Test
  122. {
  123. [ETUtils.TestAttribute(ETUtils.TestState.Enabled, "NotEqual__6", new string[] { "positive", "cslinq", "FullTrustOnly","Pri1" })]
  124. public static Expression NotEqual__6() {
  125. if(Main() != 0 ) {
  126. throw new Exception();
  127. } else {
  128. return Expression.Constant(0);
  129. }
  130. }
  131. public static int Main()
  132. {
  133. Ext.StartCapture();
  134. bool success = false;
  135. try
  136. {
  137. success = check_bool_NotEqual();
  138. }
  139. finally
  140. {
  141. Ext.StopCapture();
  142. }
  143. return success ? 0 : 1;
  144. }
  145. static bool check_bool_NotEqual() {
  146. bool[] svals = new bool[] { true };
  147. for (int i = 0; i < svals.Length; i++) {
  148. for (int j = 0; j < svals.Length; j++) {
  149. if (!check_bool_NotEqual(svals[i], svals[j])) {
  150. Console.WriteLine("bool_NotEqual failed");
  151. return false;
  152. }
  153. }
  154. }
  155. return true;
  156. }
  157. static bool check_bool_NotEqual(bool val0, bool val1) {
  158. Expression<Func<bool>> e =
  159. Expression.Lambda<Func<bool>>(
  160. Expression.NotEqual(Expression.Constant(val0, typeof(bool)),
  161. Expression.Constant(val1, typeof(bool))),
  162. new System.Collections.Generic.List<ParameterExpression>());
  163. Func<bool> f = e.Compile();
  164. bool fResult = default(bool);
  165. Exception fEx = null;
  166. try {
  167. fResult = f();
  168. }
  169. catch (Exception ex) {
  170. fEx = ex;
  171. }
  172. bool csResult = default(bool);
  173. Exception csEx = null;
  174. try {
  175. csResult = (bool) (val0 != val1);
  176. }
  177. catch (Exception ex) {
  178. csEx = ex;
  179. }
  180. if (fEx != null || csEx != null) {
  181. return fEx != null && csEx != null && fEx.GetType() == csEx.GetType();
  182. }
  183. else {
  184. return object.Equals(fResult, csResult);
  185. }
  186. }
  187. }
  188. public static class Ext {
  189. public static void StartCapture() {
  190. // MethodInfo m = Assembly.GetAssembly(typeof(Expression)).GetType("System.Linq.Expressions.ExpressionCompiler").GetMethod("StartCaptureToFile", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
  191. // m.Invoke(null, new object[] { "test.dll" });
  192. }
  193. public static void StopCapture() {
  194. // MethodInfo m = Assembly.GetAssembly(typeof(Expression)).GetType("System.Linq.Expressions.ExpressionCompiler").GetMethod("StopCapture", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
  195. // m.Invoke(null, new object[0]);
  196. }
  197. public static bool IsIntegralOrEnum(Type type) {
  198. switch (Type.GetTypeCode(GetNonNullableType(type))) {
  199. case TypeCode.Byte:
  200. case TypeCode.SByte:
  201. case TypeCode.Int16:
  202. case TypeCode.Int32:
  203. case TypeCode.Int64:
  204. case TypeCode.UInt16:
  205. case TypeCode.UInt32:
  206. case TypeCode.UInt64:
  207. return true;
  208. default:
  209. return false;
  210. }
  211. }
  212. public static bool IsFloating(Type type) {
  213. switch (Type.GetTypeCode(GetNonNullableType(type))) {
  214. case TypeCode.Single:
  215. case TypeCode.Double:
  216. return true;
  217. default:
  218. return false;
  219. }
  220. }
  221. public static Type GetNonNullableType(Type type) {
  222. return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>) ?
  223. type.GetGenericArguments()[0] :
  224. type;
  225. }
  226. }
  227. }
  228. }