diff --git a/ShittyLINQ/Sum.cs b/ShittyLINQ/Sum.cs new file mode 100644 index 0000000..7ccfa92 --- /dev/null +++ b/ShittyLINQ/Sum.cs @@ -0,0 +1,110 @@ +using System; +using System.Collections.Generic; + +namespace ShittyLINQ +{ + public static partial class Extensions + { + public static int Sum(this IEnumerable self) + { + if (self == null) + { + throw new ArgumentNullException(); + } + + var sum = 0; + + foreach (var element in self) + { + sum += element; + } + + return sum; + } + + public static double Sum(this IEnumerable self) + { + if (self == null) + { + throw new ArgumentNullException(); + } + + var sum = 0d; + + foreach (var element in self) + { + sum += element; + } + + return sum; + } + + public static float Sum(this IEnumerable self) + { + if (self == null) + { + throw new ArgumentNullException(); + } + + var sum = 0f; + + foreach (var element in self) + { + sum += element; + } + + return sum; + } + + public static int Sum(this IEnumerable self, Func selector) + { + if (self == null || selector == null) + { + throw new ArgumentNullException(); + } + + var sum = 0; + + foreach (var element in self) + { + sum += selector(element).GetValueOrDefault(); + } + + return sum; + } + + public static double Sum(this IEnumerable self, Func selector) + { + if (self == null || selector == null) + { + throw new ArgumentNullException(); + } + + var sum = 0d; + + foreach (var element in self) + { + sum += selector(element).GetValueOrDefault(); + } + + return sum; + } + + public static float Sum(this IEnumerable self, Func selector) + { + if (self == null || selector == null) + { + throw new ArgumentNullException(); + } + + var sum = 0f; + + foreach (var element in self) + { + sum += selector(element).GetValueOrDefault(); + } + + return sum; + } + } +} \ No newline at end of file diff --git a/ShittyLinqTests/SumTests.cs b/ShittyLinqTests/SumTests.cs new file mode 100644 index 0000000..de21aa1 --- /dev/null +++ b/ShittyLinqTests/SumTests.cs @@ -0,0 +1,105 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using ShittyLINQ; +using System.Collections.Generic; + +namespace ShittyTests +{ + [TestClass] + public class SumTests + { + [DataRow(new int[] { 1, 2, 3, 4, 5 }, 15)] + [DataRow(new int[] { 100, 2, 3, 4, 5 }, 114)] + [DataRow(new int[] { 0, 0, 0 }, 0)] + [DataRow(new int[] { -1, -2, -3 }, -6)] + [DataTestMethod] + public void Sum_IntNumbers(IEnumerable testData, int expectedResult) + { + var result = testData.Sum(); + + Assert.AreEqual(expectedResult, result); + } + + [DataRow(new int[] { 1, 2, 3, 4, 5 }, 15)] + [DataRow(new int[] { 100, 2, 3, 4, 5 }, 114)] + [DataRow(new int[] { 0, 0, 0 }, 0)] + [DataRow(new int[] { -1, -2, -3 }, -6)] + [DataTestMethod] + public void Sum_IntNumbers_WithSelector(IEnumerable testData, int expectedResult) + { + var result = testData.Sum(x => x); + + Assert.AreEqual(expectedResult, result, 0.02); + } + + [DataRow(new double[] { 1.2, 2.5, 3.6, 4.8, 5.5 }, 17.6)] + [DataRow(new double[] { 100.223, 2.523, 3.696, 4.8345, 5.5435 }, 116.82)] + [DataRow(new double[] { 0, 0, 0 }, 0)] + [DataRow(new double[] { -1, -2, -3 }, -6)] + [DataTestMethod] + public void Sum_DoubleNumbers(IEnumerable testData, double expectedResult) + { + var result = testData.Sum(); + + Assert.AreEqual(expectedResult, result, 0.02); + } + + [DataRow(new double[] { 1.2, 2.5, 3.6, 4.8, 5.5 }, 17.6)] + [DataRow(new double[] { 100.223, 2.523, 3.696, 4.8345, 5.5435 }, 116.82)] + [DataRow(new double[] { 0, 0, 0 }, 0)] + [DataRow(new double[] { -1, -2, -3 }, -6)] + [DataTestMethod] + public void Sum_DoubleNumbers_WithSelector(IEnumerable testData, double expectedResult) + { + var result = testData.Sum(x => x); + + Assert.AreEqual(expectedResult, result, 0.02); + } + + [DataRow(new double[] { 1.2, 2.5, 3.6, 4.8, 5.5 }, 15)] + [DataRow(new double[] { 100.223, 2.523, 3.696, 4.8345, 5.5435 }, 114)] + [DataRow(new double[] { 0, 0, 0 }, 0)] + [DataRow(new double[] { -1, -2, -3 }, -6)] + [DataTestMethod] + public void Sum_DoubleNumbers_WithSelectorToInt(IEnumerable testData, int expectedResult) + { + var result = testData.Sum(x => (int)x); + + Assert.AreEqual(expectedResult, result); + } + + [DataRow(new float[] { 1.2f, 2.5f, 3.6f, 4.8f, 5.5f }, 17.6f)] + [DataRow(new float[] { 100.223f, 2.523f, 3.696f, 4.8345f, 5.5435f }, 116.82f)] + [DataRow(new float[] { 0, 0, 0 }, 0)] + [DataRow(new float[] { -1, -2, -3 }, -6)] + [DataTestMethod] + public void Sum_FloatNumbers(IEnumerable testData, float expectedResult) + { + var result = testData.Sum(); + + Assert.AreEqual(expectedResult, result, 0.02f); + } + + [DataRow(new float[] { 1.2f, 2.5f, 3.6f, 4.8f, 5.5f }, 17.6f)] + [DataRow(new float[] { 100.223f, 2.523f, 3.696f, 4.8345f, 5.5435f }, 116.82f)] + [DataRow(new float[] { 0f, 0f, 0f }, 0f)] + [DataTestMethod] + public void Sum_FloatNumbers_WithSelector(IEnumerable testData, float expectedResult) + { + var result = testData.Sum(x => x); + + Assert.AreEqual(expectedResult, result, 0.02); + } + + [DataRow(new float[] { 1.2f, 2.5f, 3.6f, 4.8f, 5.5f }, 15)] + [DataRow(new float[] { 100.223f, 2.523f, 3.696f, 4.8345f, 5.5435f }, 114)] + [DataRow(new float[] { 0f, 0f, 0f }, 0)] + [DataTestMethod] + public void Sum_FloatNumbers_WithSelectorToInt(IEnumerable testData, int expectedResult) + { + var result = testData.Sum(x => (int)x); + + Assert.AreEqual(expectedResult, result); + } + + } +} \ No newline at end of file