Exemplo n.º 1
0
        public void TestNop()
        {
            var Instructions = new List <ILInstruction>();
            int idx          = 0;
            var instruction  = new ILInstruction {
                OpCode = OpCodes.Nop, ByteIndex = idx
            };
            var result = engine.ExecuteTyped(Instructions, null, null);

            Assert.IsNull(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// After the quality matrix has been computed,
        /// it can be used to find an optimal path from any starting state to the goal state.
        /// the method assumes that the goal state is reachable from the starting state
        /// </summary>
        /// <param name="qMaze"></param>
        /// <param name="quality"></param>
        private static void Walk(QMaze qMaze, double[][] quality)
        {
            int curr = qMaze.Start; int next;

            /* original algorithm assumed fixed start. removed due to needing random sequence.
             * this however prevents the ability to walk the maze after the solution is found because.
             * the alogrithm does not set quality values for the starting rank.
             * instead, will use no-op as the fixed staring pointing*/
            /*
             * int maxI = 0;
             * int maxK = 0;
             * var bestQ = double.MinValue;
             * for (var i = 0; i < quality.Length; i++)
             * {
             * for (var k = 0; k < quality[i].Length; k++)
             * {
             *     if (quality[i][k] > bestQ)
             *     {
             *         bestQ = quality[i][k];
             *         maxI = i;
             *         maxK = k;
             *     }
             * }
             * }
             * var opCodeI = QOpCodeLearingGenerator.OpCodes[maxI];
             * var opCodeK = QOpCodeLearingGenerator.OpCodes[maxK];
             */
            var           opCode   = QOpCodeLearingGenerator.OpCodes[curr];
            List <OpCode> solution = new List <OpCode>();

            solution.Add(opCode);
            Console.Write(opCode + "->");
            while (curr != qMaze.Goal)
            {
                next   = ArgMax(quality[curr]);
                opCode = QOpCodeLearingGenerator.OpCodes[next];
                solution.Add(opCode);
                Console.Write(opCode + "->");
                curr = next;
            }

            var writer = new ILInstructionWriter(solution);
            List <ILInstruction> ilInstructions = writer.GetInstructionStream();
            var engine = new ILInstructionEngine();

            dynamic[] args   = new dynamic[] { 1 };
            var       result = engine.ExecuteTyped(ilInstructions, args: args);

            Console.WriteLine("done");
        }
Exemplo n.º 3
0
        private static void ExecuteInstructions(object obj)
        {
            var state  = (ExecutionState)obj;
            var engine = new ILInstructionEngine();

            try
            {
                state.Result = engine.ExecuteTyped(state.ilInstructions, args: state.Arguments);
            }
            catch (Exception ex)
            {
                state.Error = ex;
                //throw new ExecutionEngineException("Engine execution failed", ex, state);
            }
        }
Exemplo n.º 4
0
        public void ExecuteTypedNetIO()
        {
            Func <string> downloadGoogle = () =>
            {
                var wc = new System.Net.WebClient();
                var s  = wc.DownloadString("http://www.google.com");
                return(s);
            };
            var dfn_1 = ResolveFn(downloadGoogle);


            var engine = new ILInstructionEngine();
            var r_0    = engine.ExecuteTyped(dfn_1, this);

            Assert.IsNotNull(r_0);
            Assert.IsTrue(r_0 is string);
            Assert.IsInstanceOfType(r_0, typeof(string));


            var staticDl = ResolveFn(NetIOTestClass.DLGoogleStatic);

            var r_1 = engine.ExecuteTyped(dfn_1, this);

            Assert.IsTrue(r_1 is string);

            Func <NetIOTestClass> ioctor = () => new NetIOTestClass();
            var r1_ctor = ResolveFn(ioctor);
            var invoked = (NetIOTestClass)engine.ExecuteTyped(ResolveFn(() => new NetIOTestClass()), this);

            var r1_fn1 = ResolveFn(invoked.Download);
            var r1_fn2 = ResolveFn(invoked.DownloadWithDefaultChars);
            var r1_fn3 = ResolveFn <char[], string>(invoked.DownloadWithChars);

            var r1_r1 = engine.ExecuteTyped(r1_fn1, invoked);
            var r1_r2 = engine.ExecuteTyped(r1_fn2, invoked);
            var r1_r3 = engine.ExecuteTyped(r1_fn3, invoked, "http://google.com".ToCharArray());
        }
Exemplo n.º 5
0
        public void ExecuteNativeTests()
        {
            var engine = new ILInstructionEngine();

            Func <int, short> fnA    = (x) => (short)x;
            object            objOne = 1;


            Action <int> rsSwitch = (i) =>
            {
                var iLOpCodeValues = (ILOpCodeValues)i;

                switch (iLOpCodeValues)
                {
                case ILOpCodeValues.Nop:
                    Console.WriteLine("Parsed NoOp");
                    break;

                case ILOpCodeValues.Add:
                    Console.WriteLine("Parsed Add");
                    break;

                default:
                    Console.WriteLine("Invalid value");
                    break;
                }

                var ilShort = (short)i;
                switch (ilShort)
                {
                case 0:
                    Console.WriteLine("Parsed NoOp");
                    break;

                case 0x58:
                    Console.WriteLine("Parsed Add");
                    break;

                default:
                    Console.WriteLine("Invalid value");
                    break;
                }
            };
            var rsMi = Resolve <int>(() => rsSwitch);

            engine.ExecuteTyped(rsMi, this, 0);


            engine.ExecuteTyped(ResolveFn <int, short>((x) => (short)x), this, (object)1);



            engine.ExecuteTyped(ResolveFn <int, short>((x) => (short)x), this, (object)1);

            var intType = typeof(int);
            var token   = intType.MetadataToken;
            //-> 33554683

            var res_00 = engine.ExecuteTyped <int>(ResolveFn <object, int>((x) => (int)x), this, (object)1);

            engine.ExecuteTyped(ResolveFn <object, short>((x) => (short)x), this, (object)1);

            engine.ExecuteTyped(ResolveFn(() => 1 + int.Parse("1")), this);
            engine.ExecuteTyped(ResolveFn(() => 1 - int.Parse("1")), this);
            engine.ExecuteTyped(ResolveFn(() => 1 * int.Parse("1")), this);
            engine.ExecuteTyped(ResolveFn(() => 1 / int.Parse("1")), this);
            engine.ExecuteTyped(ResolveFn(() => 1 % int.Parse("1")), this);
            engine.ExecuteTyped(ResolveFn(() => 1 < int.Parse("1")), this);
            engine.ExecuteTyped(ResolveFn(() => 1 <= int.Parse("1")), this);
            engine.ExecuteTyped(ResolveFn(() => 1 >= int.Parse("1")), this);
            engine.ExecuteTyped(ResolveFn(() => 1 > int.Parse("1")), this);

            engine.ExecuteTyped(ResolveFn(() => 1ul + double.Parse("1.0")), this);
            engine.ExecuteTyped(ResolveFn(() => 1ul - double.Parse("1.0")), this);
            engine.ExecuteTyped(ResolveFn(() => 1ul * double.Parse("1.0")), this);
            engine.ExecuteTyped(ResolveFn(() => 1ul / double.Parse("1.0")), this);
            engine.ExecuteTyped(ResolveFn(() => 1ul % double.Parse("1.0")), this);
            engine.ExecuteTyped(ResolveFn(() => 1ul < double.Parse("1.0")), this);
            engine.ExecuteTyped(ResolveFn(() => 1ul <= double.Parse("1.0")), this);
            engine.ExecuteTyped(ResolveFn(() => 1ul >= double.Parse("1.0")), this);
            engine.ExecuteTyped(ResolveFn(() => 1ul > double.Parse("1.0")), this);


            engine.ExecuteTyped(ResolveFn(() => 1ul + float.Parse("1.0")), this);
            engine.ExecuteTyped(ResolveFn(() => 1ul - float.Parse("1.0")), this);
            engine.ExecuteTyped(ResolveFn(() => 1ul * float.Parse("1.0")), this);
            engine.ExecuteTyped(ResolveFn(() => 1ul / float.Parse("1.0")), this);
            engine.ExecuteTyped(ResolveFn(() => 1ul % float.Parse("1.0")), this);
            engine.ExecuteTyped(ResolveFn(() => 1ul < float.Parse("1.0")), this);
            engine.ExecuteTyped(ResolveFn(() => 1ul <= float.Parse("1.0")), this);
            engine.ExecuteTyped(ResolveFn(() => 1ul >= float.Parse("1.0")), this);
            engine.ExecuteTyped(ResolveFn(() => 1ul > float.Parse("1.0")), this);

            engine.ExecuteTyped(ResolveFn(() => 1ul + decimal.Parse("1.0")), this);
            engine.ExecuteTyped(ResolveFn(() => 1ul - decimal.Parse("1.0")), this);
            engine.ExecuteTyped(ResolveFn(() => 1ul * decimal.Parse("1.0")), this);
            engine.ExecuteTyped(ResolveFn(() => 1ul / decimal.Parse("1.0")), this);
            engine.ExecuteTyped(ResolveFn(() => 1ul % decimal.Parse("1.0")), this);
            engine.ExecuteTyped(ResolveFn(() => 1ul < decimal.Parse("1.0")), this);
            engine.ExecuteTyped(ResolveFn(() => 1ul <= decimal.Parse("1.0")), this);
            engine.ExecuteTyped(ResolveFn(() => 1ul >= decimal.Parse("1.0")), this);
            engine.ExecuteTyped(ResolveFn(() => 1ul > decimal.Parse("1.0")), this);


            engine.ExecuteTyped(ResolveFn(() => float.Parse("1.0000") > (double)decimal.Parse("1.0")), this);
        }
Exemplo n.º 6
0
        public void ExecuteTypedTest()
        {
            var engine = new ILInstructionEngine();



            var rwl_0 = Resolve(Console.WriteLine);

            engine.ExecuteTyped(rwl_0);

            Assert.ThrowsException <TargetParameterCountException>(() => engine.ExecuteTyped(rwl_0, "someArg"));

            var rwl_2 = Resolve <string>(() => Console.WriteLine);

            Assert.ThrowsException <TargetParameterCountException>(() => engine.ExecuteTyped(rwl_2));

            engine.ExecuteTyped(rwl_2, $"Executed: " + nameof(rwl_2));

            var rwl_3 = Resolve <string, object>(() => Console.WriteLine);

            Assert.ThrowsException <TargetParameterCountException>(() => engine.ExecuteTyped(rwl_3));
            Assert.ThrowsException <TargetParameterCountException>(() => engine.ExecuteTyped(rwl_3, "Executed: {0}"));
            engine.ExecuteTyped(rwl_3, "Executed: {0}", nameof(rwl_3));
            Assert.ThrowsException <TargetParameterCountException>(() => engine.ExecuteTyped(rwl_3, "Executed: {0}", nameof(rwl_3), "somearg"));


            var rwl_4 = Resolve <string, object, object>(() => Console.WriteLine);

            Assert.ThrowsException <TargetParameterCountException>(() => engine.ExecuteTyped(rwl_4));
            Assert.ThrowsException <TargetParameterCountException>(() => engine.ExecuteTyped(rwl_4, "Executed: {0} {1}"));
            Assert.ThrowsException <TargetParameterCountException>(() => engine.ExecuteTyped(rwl_4, "Executed: {0} {1}", nameof(rwl_4)));
            engine.ExecuteTyped(rwl_4, "Executed: {0} {1}", nameof(rwl_4), "arg1");



            var rwl_5 = Resolve <string, object, object, object>(() => Console.WriteLine);

            Assert.ThrowsException <TargetParameterCountException>(() => engine.ExecuteTyped(rwl_5));
            Assert.ThrowsException <TargetParameterCountException>(() => engine.ExecuteTyped(rwl_5, "Executed: {0} {1}"));
            Assert.ThrowsException <TargetParameterCountException>(() => engine.ExecuteTyped(rwl_5, "Executed: {0} {1}", nameof(rwl_5)));
            Assert.ThrowsException <TargetParameterCountException>(() => engine.ExecuteTyped(rwl_5, "Executed: {0} {1}", nameof(rwl_5), "arg2"));
            engine.ExecuteTyped(rwl_5, "Executed: {0} {1} {2}", nameof(rwl_5), "arg1", "arg2");



            var rwl_6 = Resolve <string, object, object, object, object>(() => Console.WriteLine);

            Assert.ThrowsException <NotSupportedException>(() =>
                                                           engine.ExecuteTyped(rwl_6, "Executed: {0} {1} {2} {3}", nameof(rwl_6), "arg1", "arg2", "arg3")
                                                           );


            var rwl_7 = Resolve <string, object[]>(() => Console.WriteLine);

            engine.ExecuteTyped(rwl_7, "Executed: {0} {1} {2} {3} {4}", new object[] { nameof(rwl_7), "arg1", "arg2", "arg3", "arg4" });
            engine.ExecuteTyped(rwl_7, "Executed: {0} {1} {2} {3} {4}", nameof(rwl_7), "arg1", "arg2", "arg3", "arg4");
        }