Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Average extension method and tests #105

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions ShittyLINQ/Average.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;

namespace ShittyLINQ
{
public static partial class Extensions
{
/// <summary>
/// Calculates the average nummeric value
/// of an IEnumerable collection.
/// </summary>
private static R Average<R, T>(this IEnumerable<T> self)
{
if (self == null)
{
throw new ArgumentNullException();
}

dynamic sum = default(R);
int count = 0;

foreach (var number in self)
{
sum += number;
count++;
}

if (count == 0)
{
throw new InvalidOperationException("The sequence is empty.");
}

return sum / count;
}

public static double Average(this IEnumerable<int> self)
{
return self.Average<double, int>();
}

public static float Average(this IEnumerable<float> self)
{
return self.Average<float, float>();
}

public static double Average(this IEnumerable<double> self)
{
return self.Average<double, double>();
}

public static double Average(this IEnumerable<long> self)
{
return self.Average<double, long>();
}
}
}
66 changes: 66 additions & 0 deletions ShittyLinqTests/AverageTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ShittyLINQ;
using System;

namespace ShittyTests
{
[TestClass]
public class AverageTests
{
[TestMethod]
public void Ints()
{
int[] numbers = new int[] { 0, 7, 4, 1 };
double expectedResult = 3d;
double actualResult = numbers.Average();

Assert.AreEqual(expectedResult, actualResult);
}

[TestMethod]
public void Floats()
{
float[] numbers = new float[] { 3.5f, -4f, 14f, 7f };
float expectedResult = 5.125f;
float actualResult = numbers.Average();

Assert.AreEqual(expectedResult, actualResult);
}

[TestMethod]
public void Doubles()
{
double[] numbers = new double[] { -1.25d, 2.75d, 7.5d, -4d };
double expectedResult = 1.25d;
double actualResult = numbers.Average();

Assert.AreEqual(expectedResult, actualResult);
}

[TestMethod]
public void Longs()
{
long[] numbers = new long[] { 5, 3, 17, 1 };
double expectedResult = 6.5d;
double actualResult = numbers.Average();

Assert.AreEqual(expectedResult, actualResult);
}

[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void CollectionIsEmpty()
{
int[] numbers = new int[] { };
numbers.Average();
}

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void CollectionIsNull()
{
int[] numbers = null;
numbers.Average();
}
}
}