public void ParseMethod_NotValidEqualsSymbol_NotValidEquationArgumentException(string equationString)
 {
     Should.Throw <NotValidEquationArgumentException>(() =>
     {
         var parseResult = EquationParser.Parse(equationString);
     });
 }
 public void ParseMethod_OnlyWhiteSpaceString_ThrowNullArgumentException()
 {
     Should.Throw <ArgumentNullException>(() =>
     {
         var parseResult = EquationParser.Parse("   ");
     });
 }
 public void ParseMethod_NullString_ThrowNullArgumentException()
 {
     Should.Throw <ArgumentNullException>(() =>
     {
         var parseResult = EquationParser.Parse(null);
     });
 }
        public void ToCanonical_ValidInitialString_ReturnCanonicalEquation(
            string equationString, string expectedEquation)
        {
            var equation = EquationParser.Parse(equationString).ToCanonicalEquation();

            equation.Left.ToString().ShouldBe(expectedEquation);
            equation.Right.ToString().ShouldBe("0");
        }
        public void ParseMethod_ValidInitialString_ReturnLeftRightPolynomials(
            string equationString, string leftPolynomial, string rightPolynomial)
        {
            var equation = EquationParser.Parse(equationString);

            equation.Left.ToString().ShouldBe(leftPolynomial);
            equation.Right.ToString().ShouldBe(rightPolynomial);
        }
示例#6
0
        public void parse_into_a_reaction(string equation, string[] expectedInputs, string[] expectedOutputs, int[] expectedInputCounts, int[] expectedOutputCounts)
        {
            var reaction = EquationParser.Parse(equation);

            var inputSpecies  = reaction.Inputs.Select(i => new { Species = i.Key.ToString(), Count = i.Value });
            var outputSpecies = reaction.Outputs.Select(i => new { Species = i.Key.ToString(), Count = i.Value });

            Assert.Equal(expectedInputs, inputSpecies.Select(s => s.Species).ToArray());
            Assert.Equal(expectedInputCounts, inputSpecies.Select(s => s.Count).ToArray());

            Assert.Equal(expectedOutputs, outputSpecies.Select(s => s.Species).ToArray());
            Assert.Equal(expectedOutputCounts, outputSpecies.Select(s => s.Count).ToArray());
        }
示例#7
0
        public void GetDelegateTest()
        {
            var func = EquationParser.GetDelegate("2x + 3x+ 3 ^ x + 2^x");

            Assert.AreEqual(2f, func(0));
            Assert.AreEqual(10f, func(1));
            func = EquationParser.GetDelegate("sinx + cotx + cosx");
            Assert.AreEqual(double.PositiveInfinity, func(0));
            func = EquationParser.GetDelegate("logx + |x|");
            Assert.AreEqual(1f, func(1));
            func = EquationParser.GetDelegate("sinhx + coshx");
            Assert.AreEqual(1, func(0));
            func = EquationParser.GetDelegate("-x^2+4");
            Assert.AreEqual(0, func(2));
        }
示例#8
0
        private static int RunInteractiveMode()
        {
            var isCancel = false;


            Console.CancelKeyPress += (object sender, ConsoleCancelEventArgs e) =>
            {
                var isCtrlC = e.SpecialKey == ConsoleSpecialKey.ControlC;
                if (isCtrlC)
                {
                    e.Cancel = true;
                    isCancel = true;
                }
            };

            Console.WriteLine("Interactive mode: enter equations and displays result on enter. For exit press Ctrl+C");
            Console.WriteLine();

            while (!isCancel)
            {
                Console.WriteLine();
                Console.Write("Enter equation: ");
                string line = Console.ReadLine();

                if (!isCancel)
                {
                    var parseResult = EquationParser.TryParse(line, out var equation);
                    if (parseResult.IsSucceed)
                    {
                        Console.WriteLine("Canonical equation: " + equation.ToCanonicalEquation());
                    }
                    else
                    {
                        Console.WriteLine(
                            $"Equation parse failed. See the errors: {parseResult.Errors.CollectionToStringWithSeparator(Environment.NewLine)}");
                    }
                }
            }
            return(0);
        }
示例#9
0
        public void GetVetcorTestException()
        {
            string      eq1         = "2x+3y+3z";
            List <char> orderedVars = new List <char>()
            {
                'x', 'y'
            };

            Assert.AreEqual(new Vector <double>(3)
            {
                2, 3, 3
            }, EquationParser.GetVetcor(eq1, orderedVars));
            orderedVars = new List <char>()
            {
                'y', 'z', 'x'
            };
            string eq2 = "2y+3z+x";

            Assert.AreEqual(new Vector <double>(3)
            {
                2, 3, 1
            }, EquationParser.GetVetcor(eq2, orderedVars));
        }
示例#10
0
        private static int RunFileMode(string inputFileName)
        {
            var totalLinesProcessed = 0;
            var inputLines          = File.ReadAllLines(inputFileName);

            var outputLines = inputLines.Select((inputLine, i) =>
            {
                var parseResult = EquationParser.TryParse(inputLine, out var equation);
                if (parseResult.IsSucceed)
                {
                    totalLinesProcessed++;
                    return(equation.ToCanonicalEquation().ToString());
                }
                else
                {
                    return($"Failed to parse line {i}: {inputLine}");
                }
            });

            File.WriteAllLines($"{inputFileName}.out", outputLines);
            Console.WriteLine($"{totalLinesProcessed} lines processed");

            return(0);
        }
 private IList <Reaction> GetReactions(IEnumerable <ReactionPair> reactions)
 {
     return(reactions
            .Select(kv => EquationParser.Parse(kv.equation, kv.rate))
            .ToList());
 }
示例#12
0
 public void GetDelegateTestException()
 {
     var func = EquationParser.GetDelegate("2x + 3x+ 3 ^ x + 2^x+");
 }
示例#13
0
        public void Initialize()
        {
            var equationParser = new EquationParser();

            this.equationParser = new EquationEvaluator(equationParser);
        }
示例#14
0
        public void Parse_ValidEquation_CorrectAmountOfSummands(string input, int expected)
        {
            var equation = EquationParser.Parse(input);

            Assert.AreEqual(expected, equation.Summands.Count);
        }
示例#15
0
 public static Equation Parse(string input) => EquationParser.Parse(input);