Ejemplo n.º 1
0
        public void TestReadInput_TryReadEmptyInputArray_ReturnsEmptyArray()
        {
            IInterpreter interpreter = new CInterpreter();

            int[] testValue = new int[] { 5 };

            /*program looks like
             * t <- [int]
             */
            var program = new CProgramASTNode(new List <IASTNode>()
            {
                new CAssignmentASTNode("t", new CReadInputASTNode(new CValueASTNode(new int[] { 0 })))
            });

            int[][] inputData = new int[1][];

            IInputStream inputStream = new CInputStream(inputData);

            Assert.DoesNotThrow(() =>
            {
                var result = interpreter.Eval(program, inputStream);

                Assert.AreEqual(result, CIntrinsicsUtils.mNullArray);
            });
        }
Ejemplo n.º 2
0
        public void TestEval_TestSafeDivisionOption_ReturnsCorrectDivisionResult()
        {
            IInterpreter interpreter = new CInterpreter(E_INTERPRETER_ATTRIBUTES.IA_IS_SAFE_DIVISION_ENABLED);

            var program = new CProgramASTNode(new List <IASTNode>()
            {
                new CAssignmentASTNode("x", new CCallASTNode(E_INTRINSIC_FUNC_TYPE.IFT_MAP, new List <IASTNode>()
                {
                    new CUnaryLambdaFuncASTNode(E_OPERATION_TYPE.OT_DIV, new CValueASTNode(new int[] { 0 })),
                    new CValueASTNode(new int[] { 0, 1, 2, 3, 4 })
                }))
            });

            int[][] inputData = new int[][]
            {
                new int[] { 4 }, //x
            };

            int[] expectedResult = new int[] { 2, 2, 2, 2 };

            IInputStream inputStream = new CInputStream(inputData);

            Assert.DoesNotThrow(() =>
            {
                var result = interpreter.Eval(program, inputStream);
            });
        }
Ejemplo n.º 3
0
        public void TestIfStatement_FalseCondition_ReturnsEvaluatedExpressionOfChosenBranch()
        {
            IInterpreter interpreter = new CInterpreter();

            int[] testValue = new int[] { 5 };

            /*program looks like
             * t <- 5
             * r <- if t (!= 5) then 5 else -1
             */
            var program = new CProgramASTNode(new List <IASTNode>()
            {
                new CAssignmentASTNode("t", new CValueASTNode(testValue)),
                new CAssignmentASTNode("r", new CIfThenElseASTNode(new CIdentifierASTNode("t"),
                                                                   new CLambdaPredicateASTNode(E_LOGIC_OP_TYPE.LOT_NEQ, new CValueASTNode(testValue), null),
                                                                   new CValueASTNode(testValue),
                                                                   new CValueASTNode(new int[] { -1 })))
            });

            int[][] inputData = new int[3][];

            IInputStream inputStream = new CInputStream(inputData);

            Assert.DoesNotThrow(() =>
            {
                var result = interpreter.Eval(program, inputStream);

                Assert.AreEqual(result.Length, 1);
                Assert.AreEqual(result[0], -1);
            });
        }
Ejemplo n.º 4
0
        public void TestEval_TestPassArrayAsIfVar_ShouldProcessCorrectlyWithoutExceptions()
        {
            IInterpreter interpreter = new CInterpreter(E_INTERPRETER_ATTRIBUTES.IA_IS_SAFE_DIVISION_ENABLED);

            var program = new CProgramASTNode(new List <IASTNode>()
            {
                new CAssignmentASTNode("x", new CIfThenElseASTNode(new CReadInputASTNode(new CValueASTNode(new int[] { 0 })),
                                                                   new CLambdaPredicateASTNode(E_LOGIC_OP_TYPE.LOT_NEQ, new CValueASTNode(new int[] { -8 }), null),
                                                                   new CValueASTNode(new int[] { 0 }),
                                                                   new CValueASTNode(new int[] { -1 })))
            });

            int[][] inputData = new int[][]
            {
                new int[] { 4 }, //x
            };

            int[] expectedResult = new int[] { 2, 2, 2, 2 };

            IInputStream inputStream = new CInputStream(inputData);

            Assert.DoesNotThrow(() =>
            {
                var result = interpreter.Eval(program, inputStream);
            });
        }
Ejemplo n.º 5
0
        public void TestEval_TestReferencingByIdentifierInExpr_ReturnsValueStoredWithinVar()
        {
            IInterpreter interpreter = new CInterpreter();

            /*program looks like
             * x <- int
             * y <- IF x (> 0) THEN x ELSE NULL
             */
            var program = new CProgramASTNode(new List <IASTNode>()
            {
                new CAssignmentASTNode("x", new CReadInputASTNode(new CValueASTNode(new int[] { 0 }))),
                new CAssignmentASTNode("z", new CIfThenElseASTNode(new CIdentifierASTNode("x", E_NODE_ATTRIBUTES.NA_ID_SHOULD_EXIST),
                                                                   new CLambdaPredicateASTNode(E_LOGIC_OP_TYPE.LOT_GT, new CValueASTNode(new int[] { 0 }), null),
                                                                   new CIdentifierASTNode("x", E_NODE_ATTRIBUTES.NA_ID_SHOULD_EXIST),
                                                                   new CValueASTNode(CIntrinsicsUtils.mNullArray)))
            });

            int[][] inputData = new int[][]
            {
                new int[] { 4 }, //x
            };

            int[] expectedResult = new int[] { 2, 2, 2, 2 };

            IInputStream inputStream = new CInputStream(inputData);

            Assert.DoesNotThrow(() =>
            {
                var result = interpreter.Eval(program, inputStream);

                Assert.AreEqual(result.Length, 1);
                Assert.AreEqual(inputData[0][0], result[0]);
            });
        }
Ejemplo n.º 6
0
        public void TestEval_TestReadArbitraryParameter_ReturnsReadParameter(int index)
        {
            IInterpreter interpreter = new CInterpreter();

            var program = new CProgramASTNode(new List <IASTNode>()
            {
                new CAssignmentASTNode("x", new CReadInputASTNode(new CValueASTNode(new int[] { index })))
            });

            int[][] inputData = new int[][]
            {
                new int[] { 4 }, //x
            };

            int[] expectedResult = new int[] { 2, 2, 2, 2 };

            IInputStream inputStream = new CInputStream(inputData);

            Assert.DoesNotThrow(() =>
            {
                var result = interpreter.Eval(program, inputStream);

                if (index < inputData.Length)
                {
                    Assert.AreEqual(result.Length, 1);
                    Assert.AreEqual(inputData[index][0], result[0]);
                }
                else
                {
                    Assert.AreSame(CIntrinsicsUtils.mNullArray, result);
                }
            });
        }
Ejemplo n.º 7
0
        public void TestEquals_CheckUpEmptyAndSameTrees_ShouldReturnsTrue()
        {
            IASTNode firstSubtree = new CProgramASTNode();

            IASTNode secondSubtree = new CProgramASTNode();

            Assert.IsTrue(firstSubtree.Equals(secondSubtree));
        }
Ejemplo n.º 8
0
        public void TestEval_NullArguments_ThrowsNullArgumentException()
        {
            IInterpreter interpreter = new CInterpreter();

            IASTNode program = new CProgramASTNode();

            int[][] input = new int[1][];

            IInputStream inputStream = new CInputStream(input);

            Assert.Throws <ArgumentNullException>(() => { interpreter.Eval(program, null); });
            Assert.Throws <ArgumentNullException>(() => { interpreter.Eval(null, inputStream); });
        }
Ejemplo n.º 9
0
        public void TestVisitCallNode_SimpleProgram_ReturnsValue()
        {
            IInterpreter interpreter = new CInterpreter();

            int[][] inputData = new int[3][];

            int[] testArray = new int[] { 5, 4, -3, 1 };

            int sum = 0;

            foreach (int unit in testArray)
            {
                sum += unit;
            }

            int[] expectedValue = new int[] { testArray[0], sum };

            // simple program that returns a sum of length and a first element of an array
            // program's code looks like this
            // a <- [5, 4, -3, 1]
            // t <- head a
            // s <- sum a
            // r <- concat t s
            IASTNode program = new CProgramASTNode(new List <IASTNode>()
            {
                new CAssignmentASTNode("a", new CValueASTNode(testArray)),
                new CAssignmentASTNode("t", new CCallASTNode(E_INTRINSIC_FUNC_TYPE.IFT_HEAD, new List <IASTNode>()
                {
                    new CIdentifierASTNode("a")
                })),
                new CAssignmentASTNode("s", new CCallASTNode(E_INTRINSIC_FUNC_TYPE.IFT_SUM, new List <IASTNode>()
                {
                    new CIdentifierASTNode("a")
                })),
                new CAssignmentASTNode("r", new CCallASTNode(E_INTRINSIC_FUNC_TYPE.IFT_CONCAT, new List <IASTNode>()
                {
                    new CIdentifierASTNode("t"), new CIdentifierASTNode("s")
                }))
            });

            IInputStream inputStream = new CInputStream(inputData);

            Assert.DoesNotThrow(() => {
                var result = interpreter.Eval(program, inputStream);

                for (int i = 0; i < result.Length; ++i)
                {
                    Assert.AreEqual(expectedValue[i], result[i]);
                }
            });
        }
Ejemplo n.º 10
0
        public void TestEquals_CheckUpEmptyTreesWithDiffrentChildren_ShouldReturnsFalse()
        {
            IASTNode firstSubtree = new CProgramASTNode(new List <IASTNode>()
            {
                new CAssignmentASTNode("x", new CReadInputASTNode(new CValueASTNode(new int[] { 0 })))
            });

            IASTNode secondSubtree = new CProgramASTNode(new List <IASTNode>()
            {
                new CAssignmentASTNode("y", new CReadInputASTNode(new CValueASTNode(new int[] { 0 })))
            });

            Assert.IsFalse(firstSubtree.Equals(secondSubtree));
        }
Ejemplo n.º 11
0
        public void TestPrint_PassValue_GenerateCorrectString()
        {
            var program = new CProgramASTNode(new List <IASTNode>()
            {
                new CAssignmentASTNode("x", new CValueASTNode(new int[] { 1 })),
            });

            CASTPrinter printer = new CASTPrinter();

            Assert.DoesNotThrow(() =>
            {
                string result = printer.Print(program);
            });
        }
Ejemplo n.º 12
0
        public void TestEval_EmptyProgram_ReturnsEmptyArray()
        {
            IInterpreter interpreter = new CInterpreter();

            IASTNode program = new CProgramASTNode();

            int[][] input = new int[1][];

            IInputStream inputStream = new CInputStream(input);

            var result = interpreter.Eval(program, inputStream);

            Assert.IsNotNull(result);
            Assert.IsInstanceOf <int[]>(result);
            Assert.IsEmpty(result);
        }
Ejemplo n.º 13
0
        public void TestEval_SimpleProgramWithInputAndIntrinsics_ReturnsResult()
        {
            IInterpreter interpreter = new CInterpreter();

            /*program looks like
             * x <- [int]
             * y <- [int]
             * z <- VecOp (-) x y
             */
            var program = new CProgramASTNode(new List <IASTNode>()
            {
                new CAssignmentASTNode("x", new CReadInputASTNode(new CValueASTNode(new int[] { 0 }))),
                new CAssignmentASTNode("y", new CReadInputASTNode(new CValueASTNode(new int[] { 1 }))),
                new CAssignmentASTNode("z", new CCallASTNode(E_INTRINSIC_FUNC_TYPE.IFT_VECOP,
                                                             new List <IASTNode>()
                {
                    new CBinaryLambdaFuncASTNode(E_OPERATION_TYPE.OT_SUB),
                    new CIdentifierASTNode("x"),
                    new CIdentifierASTNode("y")
                }))
            });

            int[][] inputData = new int[][]
            {
                new int[] { 4, 4, 4, 4 }, //x
                new int[] { 2, 2, 2, 2 }  //y
            };

            int[] expectedResult = new int[] { 2, 2, 2, 2 };

            IInputStream inputStream = new CInputStream(inputData);

            Assert.DoesNotThrow(() =>
            {
                var result = interpreter.Eval(program, inputStream);

                for (int i = 0; i < Math.Min(result.Length, expectedResult.Length); ++i)
                {
                    Assert.AreEqual(expectedResult[i], result[i]);
                }
            });
        }
Ejemplo n.º 14
0
        public void TestGetDepth_ComputeDepthOfTree_ReturnsTreeDepth()
        {
            IASTNode program = new CProgramASTNode(new List <IASTNode>()
            {
                new CAssignmentASTNode("x", new CReadInputASTNode(new CValueASTNode(new int[] { 2 }))),
                new CAssignmentASTNode("y", new CCallASTNode(E_INTRINSIC_FUNC_TYPE.IFT_GET,
                                                             new List <IASTNode>()
                {
                    new CReadInputASTNode(new CValueASTNode(new int[] { 1 })),
                    new CCallASTNode(E_INTRINSIC_FUNC_TYPE.IFT_LEN,
                                     new List <IASTNode>()
                    {
                        new CValueASTNode(new int[] { 0, 2 })
                    })
                }))
            });

            int expectedDepth = 4;

            Assert.AreEqual(expectedDepth, program.Depth);
        }
Ejemplo n.º 15
0
        public void TestVisitAssigmentNode_SingleAssigmentOfValue_ReturnsValue()
        {
            IInterpreter interpreter = new CInterpreter();

            int[][] inputData = new int[3][];

            int[] expectedValue = new int[] { 42 };

            // simple program that should assign 42 to a variable t
            IASTNode program = new CProgramASTNode(new List <IASTNode>()
            {
                new CAssignmentASTNode("t", new CValueASTNode(expectedValue))
            });

            IInputStream inputStream = new CInputStream(inputData);

            Assert.DoesNotThrow(() => {
                var result = interpreter.Eval(program, inputStream);

                Assert.AreEqual(result, expectedValue);
            });
        }
Ejemplo n.º 16
0
        public void TestPrint_PassCorrectSimpleProgram_GenerateCorrectOutputString()
        {
            var program = new CProgramASTNode(new List <IASTNode>()
            {
                new CAssignmentASTNode("x", new CReadInputASTNode(new CValueASTNode(new int[] { 0 }))),
                new CAssignmentASTNode("y", new CReadInputASTNode(new CValueASTNode(new int[] { 1 }))),
                new CAssignmentASTNode("z", new CCallASTNode(E_INTRINSIC_FUNC_TYPE.IFT_VECOP,
                                                             new List <IASTNode>()
                {
                    new CBinaryLambdaFuncASTNode(E_OPERATION_TYPE.OT_SUB),
                    new CIdentifierASTNode("x"),
                    new CIdentifierASTNode("y")
                }))
            });

            CASTPrinter printer = new CASTPrinter();

            Assert.DoesNotThrow(() =>
            {
                string result = printer.Print(program);

                Assert.IsFalse(result.Length < 1);
            });
        }
Ejemplo n.º 17
0
        public void TestVisitAssigmentNode_SequentialAssigmentOfValueViaReference_ReturnsValue()
        {
            IInterpreter interpreter = new CInterpreter();

            int[][] inputData = new int[3][];

            int[] expectedValue = new int[] { 42 };

            // simple program that should assign 42 to a variable t
            // now program looks like t <- 42 \n v <- t
            IASTNode program = new CProgramASTNode(new List <IASTNode>()
            {
                new CAssignmentASTNode("t", new CValueASTNode(expectedValue)),
                new CAssignmentASTNode("v", new CIdentifierASTNode("t"))
            });

            IInputStream inputStream = new CInputStream(inputData);

            Assert.DoesNotThrow(() => {
                var result = interpreter.Eval(program, inputStream);

                Assert.AreEqual(result, expectedValue);
            });
        }