Exemplo n.º 1
0
        public void index_visitor_can_access_nodes_at_given_index()
        {
            var analyzer     = new NodeAnalyzer();
            var tree         = analyzer.Parse("x + y");
            var lookupIndex  = 0;
            var indexVisitor = new IndexVisitor(() => lookupIndex);

            lookupIndex = -1;
            indexVisitor
            .Invoking(sut => sut.VisitNode(tree))
            .Should()
            .Throw <ArgumentException>();

            lookupIndex = 10;
            indexVisitor
            .Invoking(sut => sut.VisitNode(tree))
            .Should()
            .Throw <ArgumentException>();

            lookupIndex = 3;
            indexVisitor.VisitNode(tree);
            var expected = indexVisitor.NodeAtIndex as IdentifierNode;

            expected.Should().NotBeNull();
            expected?.Identifier.Should().Be("y");
        }
Exemplo n.º 2
0
        public void compute_visitor(string expression, double expected)
        {
            var analyzer       = new NodeAnalyzer();
            var tree           = analyzer.Parse(expression);
            var computeVisitor = new ComputeVisitor();

            computeVisitor.VisitNode(tree);
            computeVisitor.ComputedResult.Should().Be(expected);
        }
Exemplo n.º 3
0
        public void plus_to_minus_mutator(string expression, double expected)
        {
            var analyzer = new NodeAnalyzer();
            var tree     = analyzer.Parse(expression);
            var mutator  = new PlusToMinusMutator();

            tree = mutator.MutateNode(tree);
            var computeVisitor = new ComputeVisitor();

            computeVisitor.VisitNode(tree);
            computeVisitor.ComputedResult.Should().Be(expected);
        }
Exemplo n.º 4
0
        public void pretty_print_visitor(string expression)
        {
            var analyzer       = new NodeAnalyzer();
            var tree           = analyzer.Parse(expression);
            var computeVisitor = new PrettyPrintVisitor();

            computeVisitor.VisitNode(tree);
            var output = computeVisitor.Output;

            computeVisitor.Output.Should().NotBeNullOrEmpty();
            Console.WriteLine(output);
        }
        public void ComputeExpressionForBounds(double lowerBound, double upperBound, int stepCount)
        {
            var bound    = new ComputingBound(lowerBound, upperBound, stepCount);
            var expected = new double[bound.StepCount + 1, bound.StepCount + 1];
            var sut      = new double[bound.StepCount + 1, bound.StepCount + 1];

            for (var i = 0; i <= bound.StepCount; ++i)
            {
                for (var j = 0; j <= bound.StepCount; ++j)
                {
                    expected[i, j] = bound.LowerBound + 1.0 * i * bound.Step + bound.LowerBound + 1.0 * j * bound.Step;
                }
            }

            _computer.Compute(_nodeAnalyzer.Parse("x + y"), sut, bound);

            for (var i = 0; i <= bound.StepCount; ++i)
            {
                for (var j = 0; j <= bound.StepCount; ++j)
                {
                    sut[i, j].Should().BeApproximately(expected[i, j], Precision);
                }
            }
        }
Exemplo n.º 6
0
        public void parsing_simple_expression(string toParse, string toString)
        {
            var a = new NodeAnalyzer();

            a.Parse(toParse).ToString().Should().Be(toString);
        }