public void ExecuteMakei1Array() { Func <I1[]> makei1Array = () => { return(new I1[] { 1, 2, 3, 4, 5 }); }; var engine = new ILEngine.ILInstructionEngine(); var i1result = engine.ExecuteTyped <I1[]>(makei1Array.Method, new object[] { null }); }
public void ExecuteTestInline() { Func <List <PointTest> > linqForEachSelectForSelectMany = () => { var rnd = new Random(); var range = Enumerable.Range(1, 10); var minValue = 5; var maxValue = 600; var points = range.Select(i => { var p = new PointTest { X = rnd.Next(minValue, maxValue), Y = rnd.Next(minValue, maxValue) }; return(p); }); var map = new GraphTest(points); var AllPoints = new List <LineTest>(); foreach (var p in map.Points) { for (var i = 0; i < map.Points.Count; i++) { if (p != map.Points[i]) { var ln = new LineTest(p, map.Points[i]); if (!AllPoints.Any(x => x == ln)) { AllPoints.Add(ln); } } } } //return result; var res = AllPoints.SelectMany(x => new[] { x.A, x.B }).ToList(); return(res); }; var engine = new ILEngine.ILInstructionEngine(); var expected = linqForEachSelectForSelectMany(); var IlExecuted = engine.ExecuteTyped <List <PointTest> >(linqForEachSelectForSelectMany.Method, engine); Assert.IsNotNull(IlExecuted); Assert.IsInstanceOfType(IlExecuted, typeof(List <PointTest>)); Assert.IsTrue(IlExecuted.Count == expected.Count); //TODO: this can fail if same points are generated // Assert.IsTrue(expected.SequenceEqual(IlExecuted)); //TODO: points are generated randomly }
public void ExecuteTestLabeledLoop() { Func <int> testLabeledLoop = () => { var i = 0; start: i += 1; if (i < 10) { goto start; } return(i); }; var engine = new ILEngine.ILInstructionEngine(); var loopResult = engine.ExecuteTyped <int>(testLabeledLoop.Method, new object[] { null }); }
public void ExecuteTestInline1() { var rnd = new Random(); var range = Enumerable.Range(1, 10); var minValue = 5; var maxValue = 600; var points = range.Select(i => { var p = new PointTest { X = rnd.Next(minValue, maxValue), Y = rnd.Next(minValue, maxValue) }; return(p); }); var map = new GraphTest(points); Func <List <LineTest> > UniqueLines = () => { var AllPoints = new List <LineTest>(); foreach (var p in map.Points) { for (var i = 0; i < map.Points.Count; i++) { if (p != map.Points[i]) { var ln = new LineTest(p, map.Points[i]); if (!AllPoints.Any(x => x == ln)) { AllPoints.Add(ln); } } } } return(AllPoints); }; var engine = new ILEngine.ILInstructionEngine(); var expected = UniqueLines(); var IlExecuted = engine.ExecuteTyped <List <LineTest> >(UniqueLines.Method, UniqueLines.Target);// <-- this fails because the func loads a local variable as an arg even though there is no arg in the func() Assert.IsNotNull(IlExecuted); Assert.IsInstanceOfType(IlExecuted, typeof(List <LineTest>)); Assert.IsTrue(IlExecuted.Count == expected.Count); //TODO: this can fail if same points are generated //Assert.IsTrue(expected.SequenceEqual(IlExecuted)); <-- //TODO: points are generated randomly }
public void ExecuteMakeUShortArray() { Func <ushort[]> makeushort = () => { var result = new ushort[5]; byte a = 1; sbyte b = 2; int i = 3; short s = 4; ushort u = 5; result[0] = a; result[1] = (ushort)b; result[2] = (ushort)i; result[3] = (ushort)s; result[4] = u; return(result); }; var mus = makeushort(); var engine = new ILEngine.ILInstructionEngine(); var ccresult = engine.ExecuteTyped <ushort[]>(makeushort.Method, new object[] { null }); }
public void ExecuteGetUShortFromArray() { Func <ushort> getushort = () => { var result = new ushort[5]; byte a = 1; sbyte b = 2; int i = 3; short s = 4; ushort u = 5; result[0] = a; result[1] = (ushort)b; result[2] = (ushort)i; result[3] = (ushort)s; result[4] = u; return(result[4]); }; var engine = new ILEngine.ILInstructionEngine(); var actual = engine.ExecuteTyped <ushort>(getushort.Method, new object[] { null }); ushort expected = 5; Assert.IsTrue(expected == actual, $"ExecuteGetUShortFromArray failed\r\nActual: {actual}\r\n:Expected: {expected}"); }
public void ExecuteTest() { Func <List <PointTest> > linqForEachSelectForSelectMany = () => { var rnd = new Random(); var range = Enumerable.Range(1, 10); var minValue = 5; var maxValue = 600; var points = range.Select(i => { var p = new PointTest { X = rnd.Next(minValue, maxValue), Y = rnd.Next(minValue, maxValue) }; return(p); }); var map = new GraphTest(points); Func <List <LineTest> > GetAllPoints = () => { var result = new List <LineTest>(); foreach (var p in map.Points) { for (var i = 0; i < map.Points.Count; i++) { if (p != map.Points[i]) { var ln = new LineTest(p, map.Points[i]); if (!result.Any(x => x == ln)) { result.Add(ln); } } } } return(result); }; var res = GetAllPoints().Distinct().SelectMany(x => new[] { x.A, x.B }).ToList(); return(res); }; var engine = new ILEngine.ILInstructionEngine(); var expected = linqForEachSelectForSelectMany(); var IlExecuted = engine.ExecuteTyped <List <PointTest> >(linqForEachSelectForSelectMany.Method, linqForEachSelectForSelectMany); Assert.IsNotNull(IlExecuted); Assert.IsInstanceOfType(IlExecuted, typeof(List <PointTest>)); Assert.IsTrue(IlExecuted.Count == expected.Count); // <-- TODO: Implemented distinct to deal with duplicate points. //Assert.IsTrue(expected.SequenceEqual(IlExecuted)); <-- points are random ExecuteTestInline1(); ExecuteTestInline(); Func <List <PointTest> > listCtor = () => { var pta = new PointTest() { X = 0, Y = 2 }; var ptb = new PointTest() { X = 2, Y = 2 }; var l = new List <PointTest>(new[] { pta, ptb }); return(l); }; var listCtorExpected = listCtor(); var listCtorResult = engine.ExecuteTyped <List <PointTest> >(listCtor.Method, listCtor.Target); Assert.IsNotNull(listCtorResult); Assert.IsTrue(listCtorResult.SequenceEqual(listCtorExpected)); Func <bool[]> linqListComp = () => { var pta = new PointTest() { X = 0, Y = 2 }; var ptb = new PointTest() { X = 2, Y = 2 }; var l = new List <PointTest>(new[] { pta, ptb }); //a is getting overwrote with b; var cmpLtResult = l.FirstOrDefault(x => x < ptb) == pta; var cmpLteResult = l.FirstOrDefault(x => x <= ptb) == pta; var cmpEqResult = l.FirstOrDefault(x => x == ptb) == ptb; var cmpGteResult = l.FirstOrDefault(x => x >= ptb) == ptb; var cmpGtResult = l.FirstOrDefault(x => x > ptb) == PointTest.Empty; return(new[] { cmpLtResult, cmpLteResult, cmpEqResult, cmpGteResult, cmpGtResult }); }; var linqCmpExpected = linqListComp(); var linqCmpResult = engine.ExecuteTyped <bool[]>(linqListComp.Method, linqListComp.Target); var cmpExpected = new bool[] { true, true, true, true, true }; Assert.IsNotNull(linqCmpResult); Assert.IsTrue(linqCmpResult.Length == 5); Assert.IsTrue(linqCmpResult.All(x => x)); }
public void ConvertIConvertibleByteCodeTest() { Func <List <string> > IConvertibleByteCodeTests = () => { var failedTests = new List <string>(); var except = new[] { TypeCode.DateTime, TypeCode.DBNull, TypeCode.Empty, TypeCode.Object /*,TypeCode.Boolean, TypeCode.Char*/ }; var exceptTypes = new[] { typeof(DateTime), typeof(DBNull), typeof(object), typeof(string) /*, typeof(bool), typeof(char)*/ }; var typeCodeValues = Enum.GetValues(typeof(TypeCode)).Cast <TypeCode>().Except(except).ToList(); var convertType = typeof(Convert); var minMaxValueFlags = BindingFlags.Public | BindingFlags.Static; var converterMethodGroups = convertType.GetMethods().Where(x => x.Name.StartsWith("To")).ToLookup(x => x.Name); foreach (var converterMethodGroup in converterMethodGroups) { var converterMethodName = converterMethodGroup.Key; var converterMethods = converterMethodGroup.ToList(); var convertToType = converterMethods.First().ReturnType; if (exceptTypes.Contains(convertToType) || convertToType == typeof(int)) { continue; } ParameterInfo[] parameters; var convertToThis = converterMethods .FirstOrDefault(x => (parameters = x.GetParameters()).Length == 1 && parameters[0].ParameterType == x.ReturnType); var convertFromInt = converterMethods .FirstOrDefault(x => (parameters = x.GetParameters()).Length == 1 && parameters[0].ParameterType == typeof(int)); var defaultValue = convertFromInt.Invoke(null, new object[] { 0 }); if (defaultValue is null) { failedTests.Add($"{convertToType.Name}.defaultValue is null"); } TypeCode targetTypeCode = Convert.GetTypeCode(defaultValue); switch (targetTypeCode) // switch isn't implemented yet :/ { case TypeCode.Char: { var maxValue = (object)(char)0; var minValue = (object)(char)ushort.MaxValue; var convertedMax = convertToThis.Invoke(null, new object[] { maxValue }); if (convertedMax is null) { failedTests.Add($"{convertToType.Name}.convertedMin is null"); } if (((IComparable)maxValue).CompareTo(convertedMax) != 0) { failedTests.Add($"{convertToType.Name}.maxValue({maxValue}) != (convertedMax){convertedMax}"); } var convertedMin = convertToThis.Invoke(null, new[] { minValue }); if (convertedMin is null) { failedTests.Add($"{convertToType.Name}.convertedMin is null"); } if (((IComparable)minValue).CompareTo(convertedMin) != 0) { failedTests.Add($"{convertToType.Name}.minValue({minValue}) != (convertedMin){convertedMin}"); } } break; case TypeCode.Boolean: { var maxValue = (object)true; var convertedMax = convertToThis.Invoke(null, new object[] { maxValue }); if (convertedMax is null) { failedTests.Add($"{convertToType.Name}.convertedMin is null"); } if (((IComparable)maxValue).CompareTo(convertedMax) != 0) { failedTests.Add($"{convertToType.Name}.maxValue({maxValue}) != (convertedMax){convertedMax}"); } var minValue = (object)false; var convertedMin = convertToThis.Invoke(null, new[] { minValue }); if (convertedMin is null) { failedTests.Add($"{convertToType.Name}.convertedMin is null"); } if (((IComparable)minValue).CompareTo(convertedMin) != 0) { failedTests.Add($"{convertToType.Name}.minValue({minValue}) != (convertedMin){convertedMin}"); } } break; default: { var maxValueField = convertToType.GetField("MaxValue", minMaxValueFlags); if (maxValueField is null && convertToType != typeof(bool)) { failedTests.Add($"{convertToType.Name}.maxValueField is null"); } var maxValue = maxValueField.GetValue(null); if (maxValue is null) { failedTests.Add($"{convertToType.Name}.maxValue is null"); } var convertedMax = convertToThis.Invoke(null, new[] { maxValue }); if (convertedMax is null) { failedTests.Add($"{convertToType.Name}.convertedMax is null"); } if (((IComparable)maxValue).CompareTo(convertedMax) != 0) { failedTests.Add($"{convertToType.Name}.maxValue({maxValue}) != (convertedMax){convertedMax}"); } var minValueField = convertToType.GetField("MinValue", minMaxValueFlags); if (minValueField is null) { failedTests.Add($"{convertToType.Name}.minValueField is null"); } var minValue = minValueField.GetValue(null); if (minValue is null) { failedTests.Add($"{convertToType.Name}.minValue is null"); } var convertedMin = convertToThis.Invoke(null, new[] { minValue }); if (convertedMin is null) { failedTests.Add($"{convertToType.Name}.convertedMin is null"); } if (((IComparable)minValue).CompareTo(convertedMin) != 0) { failedTests.Add($"{convertToType.Name}.minValue({minValue}) != (convertedMin){convertedMin}"); } } break; } foreach (var converterMethod in converterMethods) { parameters = converterMethod.GetParameters(); if (parameters.Length == 1 && !exceptTypes.Contains(parameters[0].ParameterType)) { var paramDefault = Convert.ChangeType(0, parameters[0].ParameterType); TypeCode paramTypeCode = Convert.GetTypeCode(paramDefault); if (targetTypeCode == TypeCode.Boolean && paramTypeCode == TypeCode.Char) { continue; } if (targetTypeCode == TypeCode.Char && (paramTypeCode == TypeCode.Boolean || paramTypeCode == TypeCode.Single || paramTypeCode == TypeCode.Double || paramTypeCode == TypeCode.Decimal)) { continue; } if (paramTypeCode == TypeCode.Char && (targetTypeCode == TypeCode.Boolean || targetTypeCode == TypeCode.Single || targetTypeCode == TypeCode.Double || targetTypeCode == TypeCode.Decimal)) { continue; } //var inline = Convert.ToBoolean('0'); var convertFromDefault = converterMethod.Invoke(null, new object[] { paramDefault }); if (convertFromDefault is null) { failedTests.Add($"{converterMethod.Name}({defaultValue}) method is null"); } var convertToBoolInt = Convert.ToInt32(convertFromDefault); if (convertToBoolInt != 0) { failedTests.Add($"{converterMethod.Name}({convertFromDefault}{{0}}) !=0"); } } } } return(failedTests); }; //make sure the test is succesful in c# List <string> expected = IConvertibleByteCodeTests(); var engine = new ILEngine.ILInstructionEngine(); var engineResult = engine.ExecuteTyped <List <string> >(IConvertibleByteCodeTests.Method, IConvertibleByteCodeTests.Target); }