コード例 #1
0
        public void TestCase010()
        {
            bool   equal;
            int    retVal   = Constants.Failure;
            string errorMsg = string.Empty;
            double expectedElementComposition;
            string Formula = "Ba(OH)2", formulaSummary;
            double expectedMolarMass = 171.38, actualMolarMass;
            Dictionary <string, double> expectedComposition = new Dictionary <string, double>()
            {
                { "H", 1.18 },
                { "O", 18.67 },
                { "Ba", 80.15 }
            };

            FormulaParser parser = new FormulaParser();

            retVal = parser.SetFormula(Formula, out formulaSummary, out actualMolarMass);
            Assert.AreEqual(Constants.Success, retVal);
            equal = Math.Abs(expectedMolarMass - actualMolarMass) <= allowedDifference;
            Assert.IsTrue(equal);

            IDictionary <string, double> actualComposition = parser.ParseElements(out retVal);

            Assert.AreEqual(Constants.Success, retVal);
            Assert.AreEqual(expectedComposition.Count, actualComposition.Count);

            foreach (KeyValuePair <string, double> kvp in actualComposition)
            {
                expectedElementComposition = expectedComposition[kvp.Key];
                equal = Math.Abs(expectedElementComposition - kvp.Value) <= allowedDifference;
                Assert.IsTrue(equal);
            }
        }
コード例 #2
0
        public void TestCase006()
        {
            bool   equal;
            int    retVal   = Constants.Failure;
            string errorMsg = string.Empty;
            double expectedElementComposition;
            string Formula = "AlO8RbS2", formulaSummary;
            double expectedMolarMass = 304.58, actualMolarMass;
            Dictionary <string, double> expectedComposition = new Dictionary <string, double>()
            {
                { "Al", 8.86 },
                { "O", 42.03 },
                { "S", 21.05 },
                { "Rb", 28.06 }
            };

            FormulaParser parser = new FormulaParser();

            retVal = parser.SetFormula(Formula, out formulaSummary, out actualMolarMass);
            Assert.AreEqual(Constants.Success, retVal);
            equal = Math.Abs(expectedMolarMass - actualMolarMass) <= allowedDifference;
            Assert.IsTrue(equal);

            IDictionary <string, double> actualComposition = parser.ParseElements(out retVal);

            Assert.AreEqual(Constants.Success, retVal);
            Assert.AreEqual(expectedComposition.Count, actualComposition.Count);

            foreach (KeyValuePair <string, double> kvp in actualComposition)
            {
                expectedElementComposition = expectedComposition[kvp.Key];
                equal = Math.Abs(expectedElementComposition - kvp.Value) <= allowedDifference;
                Assert.IsTrue(equal);
            }
        }
コード例 #3
0
        public void TestCase009()
        {
            bool   equal;
            int    retVal   = Constants.Failure;
            string errorMsg = string.Empty;
            double expectedElementComposition;
            string Formula = "(C6H5CNN)2Hg", formulaSummary;
            double expectedMolarMass = 434.87, actualMolarMass;
            Dictionary <string, double> expectedComposition = new Dictionary <string, double>()
            {
                { "C", 38.67 },
                { "H", 2.32 },
                { "Hg", 46.13 },
                { "N", 12.88 }
            };

            FormulaParser parser = new FormulaParser();

            retVal = parser.SetFormula(Formula, out formulaSummary, out actualMolarMass);
            Assert.AreEqual(Constants.Success, retVal);
            equal = Math.Abs(expectedMolarMass - actualMolarMass) <= allowedDifference;
            Assert.IsTrue(equal);

            IDictionary <string, double> actualComposition = parser.ParseElements(out retVal);

            Assert.AreEqual(Constants.Success, retVal);
            Assert.AreEqual(expectedComposition.Count, actualComposition.Count);

            foreach (KeyValuePair <string, double> kvp in actualComposition)
            {
                expectedElementComposition = expectedComposition[kvp.Key];
                equal = Math.Abs(expectedElementComposition - kvp.Value) <= allowedDifference;
                Assert.IsTrue(equal);
            }
        }
コード例 #4
0
        public int TryParseFormula(out string errorMsg)
        {
            string formulaSummary = string.Empty;;
            int    retVal         = Constants.Success;

            FormulaSummary = string.Empty;
            errorMsg       = string.Empty;
            double molarMass = 0;

            MolarMass = 0;

            try
            {
                if (!Regex.IsMatch(formula, allowedElements,
                                   RegexOptions.CultureInvariant | RegexOptions.Compiled | RegexOptions.Singleline))
                {
                    retVal   = Constants.Failure;
                    errorMsg = "Check formula, only elements and parentheses () allowed.";
                }

                if (retVal == Constants.Success)
                {
                    retVal = parser.SetFormula(Formula, out formulaSummary, out molarMass);

                    if (retVal == Constants.Success)
                    {
                        MolarMass      = molarMass;
                        FormulaSummary = formulaSummary;
                    }
                    else
                    {
                        errorMsg = parser.GetLastError();
                    }
                }
            }
            catch (Exception ex)
            {
                retVal   = Constants.Failure;
                errorMsg = string.Format("An unexpected error occurred: {0}", ex.Message);
            }

            return(retVal);
        }
コード例 #5
0
        public void TestCase002()
        {
            bool   equal;
            int    retVal   = Constants.Failure;
            string errorMsg = string.Empty;
            double expectedElementComposition;
            string Formula = "C8H8ClNO3S", formulaSummary;
            double expectedMolarMass = 233.68, actualMolarMass;
            Dictionary <string, double> expectedComposition = new Dictionary <string, double>()
            {
                { "C", 41.12 },
                { "H", 3.45 },
                { "Cl", 15.17 },
                { "N", 5.99 },
                { "O", 20.45 },
                { "S", 13.72 },
            };

            FormulaParser parser = new FormulaParser();

            retVal = parser.SetFormula(Formula, out formulaSummary, out actualMolarMass);
            Assert.AreEqual(Constants.Success, retVal);
            equal = Math.Abs(expectedMolarMass - actualMolarMass) <= allowedDifference;
            Assert.IsTrue(equal);

            IDictionary <string, double> actualComposition = parser.ParseElements(out retVal);

            Assert.AreEqual(Constants.Success, retVal);
            Assert.AreEqual(expectedComposition.Count, actualComposition.Count);

            foreach (KeyValuePair <string, double> kvp in actualComposition)
            {
                expectedElementComposition = expectedComposition[kvp.Key];
                equal = Math.Abs(expectedElementComposition - kvp.Value) <= allowedDifference;
                Assert.IsTrue(equal);
            }
        }