// The sum of all ingredients quantities is eaqual to the result sum // Q1 + Q2 + ... Qn = Qt // // The sum of product (quantity * concentration) of all ingredients is equal to the result product (quantity * concentration) // Q1*C1 + Q2*C2 + ... Qn*Cn = Qt*Ct #endregion #region Validation /// <summary> /// Validate calculation data /// </summary> /// <param name="ingredients">Ingredients</param> /// <param name="total">Total</param> /// <returns>Error message or null</returns> public static string Validate(IEnumerable <IIngredient> ingredients, IIngredient total) { // Selected unknown values for calculation UVCountValidationResult countValidationResult = GetUnknownValuesCountState(ingredients, total); switch (countValidationResult) { case UVCountValidationResult.ExcessiveSelections: return("Too many unknown values selected. Calculation is indeterminate and impossible. Expected selections are two!"); case UVCountValidationResult.InsufficientSelections: return("Only one unknown value selected. Calculation is over-determinate and incorrect. Expected selections are two!"); case UVCountValidationResult.ExcessiveConcentrationSelections: return("Too many unknown concentrations selected. Calculation is indeterminate and impossible. Only one concentration could be unknown!"); } // Ingredients minimum count if (ingredients.Count() < 2) { return("Minimum two ingredients are expected for meaningful calculation!"); } // Ingredients quantity bool invalidQuanity = ingredients.Any(i => !IsQuantityValid(i)); if (invalidQuanity) { return("Invalid ingredient quantity found!"); } // Ingredients concentration bool invalidConcentration = ingredients.Any(i => !IsConcentrationValid(i)); if (invalidConcentration) { return("Invalid ingredient concentration found!"); } // Total quantity invalidQuanity = !IsQuantityValid(total); if (invalidQuanity) { return("Invalid total quantity!"); } // Total concentration invalidConcentration = !IsConcentrationValid(total); if (invalidConcentration) { return("Invalid total concentration!"); } return(null);// validation is OK }
/// <summary> /// Validate system of equations /// Related to cooperative validation rules only. The individual values validation rules are not included. /// </summary> private void ValidateSystemOfEquations() { // Error messages string ingredientsCountMsg = (string)_view.Resources["IngredientsCountError"]; string excessiveSelectionsMsg = (string)_view.Resources["ExcessiveSelectionsError"]; string insufficientSelectionsMsg = (string)_view.Resources["InsufficientSelectionsError"]; string excessiveConcentrationSelectionsMsg = (string)_view.Resources["ExcessiveConcentrationSelectionsError"]; // Current validation error message string validationMsg = null; // Validate unknown values count UVCountValidationResult countValidationResult = Calculator.GetUnknownValuesCountState(Ingredients, Total); switch (countValidationResult) { case UVCountValidationResult.ExcessiveSelections: validationMsg = excessiveSelectionsMsg; break; case UVCountValidationResult.InsufficientSelections: validationMsg = insufficientSelectionsMsg; break; case UVCountValidationResult.ExcessiveConcentrationSelections: validationMsg = excessiveConcentrationSelectionsMsg; break; } // Validation ingredients count if (Ingredients.Count() < 2) { validationMsg = ingredientsCountMsg; } // Set the validation error if exist or clean all errors of the model if (validationMsg == null) { SystemOfEquationsValidationMessage = null; IsSystemOfEquationsValid = true; CleanErrors(true); } else { SystemOfEquationsValidationMessage = validationMsg; IsSystemOfEquationsValid = false; AddError(validationMsg, string.Empty); } }
public void OK8() { // Arrange Ingredient total = new Ingredient(160, 40, true, false); List <Ingredient> ingredients = new List <Ingredient>(); Ingredient ingredient; ingredient = new Ingredient(30, 100); ingredients.Add(ingredient); ingredient = new Ingredient(40, 20, false, true); ingredients.Add(ingredient); ingredient = new Ingredient(60, 0); ingredients.Add(ingredient); // Act UVCountValidationResult result = Calculator.GetUnknownValuesCountState(ingredients, total); // Assert Assert.Equal(UVCountValidationResult.OK, result); }
public void InsufficientSelections1() { // Arrange Ingredient total = new Ingredient(160, 40); List <Ingredient> ingredients = new List <Ingredient>(); Ingredient ingredient; ingredient = new Ingredient(30, 100); ingredients.Add(ingredient); ingredient = new Ingredient(40, 20); ingredients.Add(ingredient); ingredient = new Ingredient(60, 0); ingredients.Add(ingredient); // Act UVCountValidationResult result = Calculator.GetUnknownValuesCountState(ingredients, total); // Assert Assert.Equal(UVCountValidationResult.InsufficientSelections, result); }