Skip to content

Commit

Permalink
Added Limit method
Browse files Browse the repository at this point in the history
  • Loading branch information
petermorlion committed May 12, 2016
1 parent 7998538 commit 91ee59a
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 13 deletions.
13 changes: 0 additions & 13 deletions RedStar.Amounts.Tests/AmountMathTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,5 @@ public void TestAbs()
var zeroAmount = new Amount(0, LengthUnits.CentiMeter);
Assert.Equal(new Amount(0, LengthUnits.CentiMeter), AmountMath.Abs(zeroAmount));
}

[Fact]
public void TestAverage()
{
var amount1 = new Amount(10, TemperatureUnits.DegreeCelcius);
var amount2 = new Amount(20, TemperatureUnits.DegreeCelcius);
var amount3 = new Amount(25, TemperatureUnits.DegreeCelcius);
var amount4 = new Amount(18, TemperatureUnits.DegreeCelcius);

var list = new[] {amount1, amount2, amount3, amount4};

Assert.Equal(new Amount(18.25, TemperatureUnits.DegreeCelcius), list.Average());
}
}
}
71 changes: 71 additions & 0 deletions RedStar.Amounts.Tests/ExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using RedStar.Amounts.StandardUnits;
using Xunit;

namespace RedStar.Amounts.Tests
{
public class ExtensionsTests
{
private class MyClass
{
public Amount MyAmount { get; set; }
}

[Fact]
public void SumTest()
{
var a = new Amount(3000.0, LengthUnits.Meter);
var b = new Amount(3500.0, LengthUnits.Meter);
var c = new Amount(1000.0, LengthUnits.Meter);
var amounts = new[] { a, b, c };

var expected = new Amount(7500.0, LengthUnits.Meter);

var sum = amounts.Sum();

Assert.Equal(expected, sum);
}

[Fact]
public void SumWithSelectorTest()
{
var a = new MyClass { MyAmount = new Amount(3000.0, LengthUnits.Meter) };
var b = new MyClass { MyAmount = new Amount(3500.0, LengthUnits.Meter) };
var c = new MyClass { MyAmount = new Amount(1000.0, LengthUnits.Meter) };
var amounts = new[] { a, b, c };

var expected = new Amount(7500.0, LengthUnits.Meter);

var sum = amounts.Sum(x => x.MyAmount);

Assert.Equal(expected, sum);
}

[Fact]
public void TestAverage()
{
var amount1 = new Amount(10, TemperatureUnits.DegreeCelcius);
var amount2 = new Amount(20, TemperatureUnits.DegreeCelcius);
var amount3 = new Amount(25, TemperatureUnits.DegreeCelcius);
var amount4 = new Amount(18, TemperatureUnits.DegreeCelcius);

var list = new[] { amount1, amount2, amount3, amount4 };

Assert.Equal(new Amount(18.25, TemperatureUnits.DegreeCelcius), list.Average());
}

[Fact]
public void TestLimit()
{
var tooLowAmount = new Amount(10, TemperatureUnits.DegreeCelcius);
var tooHighAmount = new Amount(100, TemperatureUnits.DegreeCelcius);
var inBetweenAmount = new Amount(50, TemperatureUnits.DegreeCelcius);

var minimum = new Amount(40, TemperatureUnits.DegreeCelcius);
var maximum = new Amount(60, TemperatureUnits.DegreeCelcius);

Assert.Equal(new Amount(40, TemperatureUnits.DegreeCelcius), tooLowAmount.Limit(minimum, maximum));
Assert.Equal(new Amount(60, TemperatureUnits.DegreeCelcius), tooHighAmount.Limit(minimum, maximum));
Assert.Equal(new Amount(50, TemperatureUnits.DegreeCelcius), inBetweenAmount.Limit(minimum, maximum));
}
}
}
1 change: 1 addition & 0 deletions RedStar.Amounts.Tests/RedStar.Amounts.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<ItemGroup>
<Compile Include="AmountMathTests.cs" />
<Compile Include="AmountTests.cs" />
<Compile Include="ExtensionsTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UnitManagerTests.cs" />
<Compile Include="UnitTests.cs" />
Expand Down
9 changes: 9 additions & 0 deletions RedStar.Amounts/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,14 @@ public static Amount Average(this IEnumerable<Amount> source)
var count = source.Count();
return sum / count;
}

/// <summary>
/// Limits a given Amount to be between the minimum and maximum provided. If the given Amount is lower than the minimum, the minimum will be returned.
/// If it is higher than the maximum, the maximum will be returned. If it is between the minimum and maximum, the value will be returned unchanged.
/// </summary>
public static Amount Limit(this Amount amount, Amount minimum, Amount maximum)
{
return AmountMath.Max(minimum, AmountMath.Min(amount, maximum));
}
}
}

0 comments on commit 91ee59a

Please sign in to comment.