public void Test_DoCalc() { //arrange OhmValuesModel ohmValues = new OhmValuesModel(); OhmValuesModel ohmValues_check = new OhmValuesModel { ohms = 126000, tolerance = 0.05M, low = 119700, high = 132300 }; OhmCalcModel ohmCalc = new OhmCalcModel(); List <int> digits = new List <int>(); digits.Add(1); digits.Add(2); digits.Add(6); ohmCalc.significantFigures = digits; ohmCalc.multiplier = 1000; ohmCalc.tolerance = 0.05M; //act ohmValues = Utilities.DoCalc(ohmCalc); //assert Assert.AreEqual(ohmValues_check.ohms, ohmValues.ohms); Assert.AreEqual(ohmValues_check.tolerance, ohmValues.tolerance); Assert.AreEqual(ohmValues_check.low, ohmValues.low); Assert.AreEqual(ohmValues_check.high, ohmValues.high); }
/// <summary> /// Does the actual calculation of resistance, tolerance, low and high values for a resistor, given the derived band values. /// </summary> /// <param name="ocm_input"></param> /// <returns>Returns an object containing calc'ed values</returns> public static OhmValuesModel DoCalc(OhmCalcModel ocm_input) { //init returning object OhmValuesModel ovm_output = new OhmValuesModel(); ovm_output.ohms = 0; ovm_output.tolerance = 0; ovm_output.low = 0; ovm_output.high = 0; //use all significant figures and turn into a concat'ed string string sigfigs = string.Empty; foreach (var sigfig in ocm_input.significantFigures) { sigfigs += sigfig.ToString(); } //convert concated significant figures into a calculable number and calc it against multiplier and tolerance values. int iOhms; if (int.TryParse(sigfigs, out iOhms) && (iOhms > 0)) { //sigfig bands converted to number, multiply parsed number with multiplier value, set ohms value as decimal to retain numbers on each side of decimal place. ovm_output.ohms = iOhms; if (ocm_input.significantFigures.Count() > 1) { ovm_output.ohms = iOhms * ocm_input.multiplier; } //now calculate the low and high tolerace ovm_output.low = ovm_output.ohms - (ovm_output.ohms * ocm_input.tolerance); ovm_output.high = ovm_output.ohms + (ovm_output.ohms * ocm_input.tolerance); //set output tolerance val ovm_output.tolerance = ocm_input.tolerance; } return(ovm_output); }
/// <summary> /// BETA FEATURE: Resistors can have a variable amount of bands, this method converts color band codes to Ohms resistance value. /// Currently does not account for Fail Rate, nor Temperature Coefficient which is relevant for resistors with color band count greater than 5. /// </summary> /// <param name="bandColors">A collection of colors.</param> /// <param name="resistance"></param> /// <param name="low"></param> /// <param name="high"></param> public void CalculateOhmValue(List <string> bandColors, out decimal resistance, out decimal tolerance, out decimal low, out decimal high) { string sigfigs = string.Empty; resistance = 0; tolerance = 0; low = 0; high = 0; OhmCalcModel ocm = new OhmCalcModel(); //populate ocm //single banded resistors only indicate value of resistance with single significant figure if (bandColors.Count == 1) { //populate the list of sigfigs foreach (var color in bandColors) { ocm.significantFigures.Add(_repository.Get(color).SignificantFigure ?? 0); } //calculate the actual values. OhmValuesModel ovm = Utilities.DoCalc(ocm); resistance = ovm.ohms; low = (ovm.low ?? 0); high = (ovm.high ?? 0); tolerance = (ovm.tolerance ?? 0); } //banded resistors are not manufactured with band counts of 2 or greater than 6 else if ((bandColors.Count >= 3) && (bandColors.Count < 6)) { //the last 2 bands (for resistors with 3 to 5 bands), represent multiplier and tolerance respectively. ocm.multiplier = (_repository.Get(bandColors[bandColors.Count - 2]).Multiplier ?? 0); ocm.tolerance = (_repository.Get(bandColors[bandColors.Count - 1]).Tolerance ?? 0); //now remove the last 2 from List so that just significant figures bands remain bandColors.RemoveAt(bandColors.Count - 1); bandColors.RemoveAt(bandColors.Count - 1); //populate the list of sigfigs foreach (var color in bandColors) { ocm.significantFigures.Add(_repository.Get(color).SignificantFigure ?? 0); } //calculate the actual values. OhmValuesModel ovm = Utilities.DoCalc(ocm); resistance = ovm.ohms; low = (ovm.low ?? 0); high = (ovm.high ?? 0); tolerance = (ovm.tolerance ?? 0); } else if ((bandColors.Count == 6)) { //band count of 6 currently not supported. } }