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); } }
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); } }
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); } }
public int CalculateComposition(out string errorMsg) { int retVal = Constants.Failure; errorMsg = string.Empty; try { Composition.Clear(); IDictionary <string, double> calculatedComposition = parser.ParseElements(out retVal); if (retVal == Constants.Success) { foreach (var element in calculatedComposition) { string elementName = element.Key; double massContent = element.Value; Composition.Add(new Element() { element = elementName, relativeMassContent = massContent, Brush = elementColors[elementName] }); } } else { errorMsg = parser.GetLastError(); } } catch (Exception ex) { retVal = Constants.Failure; errorMsg = string.Format("An unexpected error occurred: {0}", ex.Message); } return(retVal); }
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); } }