Skip to content

Commit

Permalink
Add tests for math methods
Browse files Browse the repository at this point in the history
  • Loading branch information
petermorlion committed Feb 21, 2018
1 parent 66a071f commit df98c65
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 21 deletions.
77 changes: 77 additions & 0 deletions RedStar.Amounts.Tests/AmountTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -615,5 +615,82 @@ public void Zero_WithUnitName_ShouldReturnAmountWithZeroAndCorrectUnit()

Assert.Equal(new Amount(0, LengthUnits.Meter), amount);
}

[Fact]
public void Add_ShouldAddGivenAmount()
{
var amount = new Amount(3, LengthUnits.Meter);

var result = amount.Add(new Amount(4, LengthUnits.Meter));

Assert.Equal(new Amount(7, LengthUnits.Meter), result);
Assert.NotSame(amount, result);
}

[Fact]
public void Negate_WithPositiveAmount_ShouldReturnEquivalentNegativeAmount()
{
var amount = new Amount(3, LengthUnits.Meter);

var result = amount.Negate();

Assert.Equal(new Amount(-3, LengthUnits.Meter), result);
Assert.NotSame(amount, result);
}

[Fact]
public void Negate_WithNegativeAmount_ShouldReturnEquivalentPositiveAmount()
{
var amount = new Amount(-3, LengthUnits.Meter);

var result = amount.Negate();

Assert.Equal(new Amount(3, LengthUnits.Meter), result);
Assert.NotSame(amount, result);
}

[Fact]
public void Multiply_ByAmount_ShouldReturnMultipliedValueWithNewUnit()
{
var amount = new Amount(3, LengthUnits.Meter);

var result = amount.Multiply(new Amount(4, LengthUnits.Meter));

Assert.Equal(new Amount(12, SurfaceUnits.Meter2), result);
Assert.NotSame(amount, result);
}

[Fact]
public void Multiply_ByDouble_ShouldReturnMultipliedValueWithSameUnit()
{
var amount = new Amount(3, LengthUnits.Meter);

var result = amount.Multiply(2);

Assert.Equal(new Amount(6, LengthUnits.Meter), result);
Assert.NotSame(amount, result);
}

[Fact]
public void DivideBy_Amount_ShouldReturnDividedValueWithNewUnit()
{
var amount = new Amount(12, SurfaceUnits.Meter2);

var result = amount.DivideBy(new Amount(4, LengthUnits.Meter));

Assert.Equal(new Amount(3, LengthUnits.Meter), result);
Assert.NotSame(amount, result);
}

[Fact]
public void DivideBy_Double_ShouldReturnDividedValueWithSameUnit()
{
var amount = new Amount(12, LengthUnits.Meter);

var result = amount.DivideBy(2);

Assert.Equal(new Amount(6, LengthUnits.Meter), result);
Assert.NotSame(amount, result);
}
}
}
35 changes: 14 additions & 21 deletions RedStar.Amounts/Amount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,19 @@ public static Amount Zero(string unitName)
/// </summary>
public static int EqualityPrecision
{
get { return Amount._equalityPrecision; }
set { Amount._equalityPrecision = value; }
get => Amount._equalityPrecision;
set => Amount._equalityPrecision = value;
}

/// <summary>
/// Gets the raw value of the amount.
/// </summary>
public double Value
{
get { return _value; }
}
public double Value => _value;

/// <summary>
/// Gets the unit of the amount.
/// </summary>
public Unit Unit
{
get { return _unit; }
}
public Unit Unit => _unit;

/// <summary>
/// Returns a unit that matches this amount.
Expand Down Expand Up @@ -141,7 +135,7 @@ public Amount[] Split(Unit[] units, int decimals)
var rest = this;

// Truncate for all but the last unit:
for (var i = 0; i < (units.Length - 1); i++)
for (var i = 0; i < units.Length - 1; i++)
{
amounts[i] = (Amount)rest.ConvertedTo(units[i]).MemberwiseClone();
amounts[i]._value = Math.Truncate(amounts[i]._value);
Expand Down Expand Up @@ -204,12 +198,12 @@ public static Amount Parse(string s, IFormatProvider formatProvider)

public override bool Equals(object obj)
{
return (this == (obj as Amount));
return this == obj as Amount;
}

public bool Equals(Amount amount)
{
return (this == amount);
return this == amount;
}

public override int GetHashCode()
Expand Down Expand Up @@ -322,8 +316,7 @@ public static string ToString(Amount amount, IFormatProvider formatProvider)
/// </summary>
public static string ToString(Amount amount, string format, IFormatProvider formatProvider)
{
if (amount == null) return string.Empty;
else return amount.ToString(format, formatProvider);
return amount == null ? string.Empty : amount.ToString(format, formatProvider);
}

#endregion Public implementation
Expand All @@ -343,47 +336,47 @@ public Amount Add(Amount amount)
/// </summary>
public Amount Negate()
{
return (-this);
return -this;
}

/// <summary>
/// Multiply this with amount (= this * amount).
/// </summary>
public Amount Multiply(Amount amount)
{
return (this * amount);
return this * amount;
}

/// <summary>
/// Multiply this with value (= this * value).
/// </summary>
public Amount Multiply(double value)
{
return (this * value);
return this * value;
}

/// <summary>
/// Divides this by amount (= this / amount).
/// </summary>
public Amount DivideBy(Amount amount)
{
return (this / amount);
return this / amount;
}

/// <summary>
/// Divides this by value (= this / value).
/// </summary>
public Amount DivideBy(double value)
{
return (this / value);
return this / value;
}

/// <summary>
/// Returns 1 over this amount (= 1 / this).
/// </summary>
public Amount Inverse()
{
return (1.0 / this);
return 1.0 / this;
}

/// <summary>
Expand Down

0 comments on commit df98c65

Please sign in to comment.