public static T[] Sum <T>(T[] array1, T[] array2, ISumCalculator <T> sumCalculator) { if (sumCalculator == null) { sumCalculator = new KnownTypesSumCalculator <T>(); } if (array1 == null) { array1 = new T[0]; } if (array2 == null) { array2 = new T[0]; } if (array1.Length != array2.Length) { throw new ArgumentException($"Array sizes must match, currently first array has {array1.Length} elements, while second array has {array2.Length} elements"); } int commonLength = array1.Length; T[] result = new T[commonLength]; for (int i = 0; i < commonLength; i++) { result[i] = sumCalculator.Sum(array1[i], array2[i]); } return(result); }
public Calculator(ISumCalculator sumCalculator, IReplacer replacer, IValidator validator, INumbersExtractor numbersExtractor) { this.sumCalculator = sumCalculator; this.replacer = replacer; this.validator = validator; this.numbersExtractor = numbersExtractor; }
public void Sum_Test_When_Array1_Has_Elements_And_Array2_Has_Elements_And_Calculator_Is_Null_And_Arrays_Size_Are_Matching() { int[] array1 = new int[] { 3, 2, 1 }; int[] array2 = new int[] { 1, 2, 3 }; ISumCalculator <int> calculator = null; var result = ArrayHelper.ArrayHelper.Sum(array1, array2, calculator); CollectionAssert.AreEqual(new[] { 4, 4, 4 }, result); }
public void Sum_Test_When_Array1_Is_Empty_And_Array2_Is_Empty_And_Calculator_Is_Null() { int[] array1 = new int[0]; int[] array2 = new int[0]; ISumCalculator <int> calculator = null; var result = ArrayHelper.ArrayHelper.Sum(array1, array2, calculator); Assert.IsNotNull(result); Assert.AreEqual(0, result.Length); }
public void Sum_Test_When_Array1_Is_Null_And_Array2_Has_Elements_And_Calculator_Is_Null() { int[] array1 = null; int[] array2 = new int[] { 1, 2, 3 }; ISumCalculator <int> calculator = null; Assert.ThrowsException <ArgumentException>( () => { var result = ArrayHelper.ArrayHelper.Sum(array1, array2, calculator); }); }
static void Main(string[] args) { // create an instance of the Calculator object Calculator calc = new Calculator(); // upcast to IProductCalculator and call the method IProductCalculator pcalc = calc; int productResult = pcalc.PerformCalculation(10, 10); // upcast to ISumCalculator and call the method ISumCalculator scalc = calc; int sumResult = scalc.PerformCalculation(10, 10); // print the result Console.WriteLine("Product result: {0}", productResult); Console.WriteLine("Sum Result: {0}", sumResult); // wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine(); }
static void Main(string[] args) { // create an object and upcast it to the combined interface ICombinedCalculator calc = new Calculator(); // upcast to the base interfaces and call the method that each defines IProductCalculator prodCalc = calc; int prodResult = prodCalc.CalculateProduct(10, 10); ISumCalculator sumCalc = calc; int calcResult = sumCalc.CalculateSum(10, 10); ISubtractionCalculator subCalc = calc; int subResult = subCalc.CalculateSubtraction(10, 2); // explicitly cast from one base interface to another prodCalc = (IProductCalculator)subCalc; // wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine(); }
public SumCalculatorTests() { kernel.Load(new DIRegistration()); sumCalculator = kernel.Get <ISumCalculator>(); }