Skip to content

Instantly share code, notes, and snippets.

@jeroenheijmans
Last active December 29, 2016 08:01
Show Gist options
  • Save jeroenheijmans/23339df68a3184f2bc3757bdb9472abf to your computer and use it in GitHub Desktop.
Save jeroenheijmans/23339df68a3184f2bc3757bdb9472abf to your computer and use it in GitHub Desktop.
DoubleVsDecimal Test-Driven-Description
namespace DoubleVsDecimal
{
using System;
using NUnit.Framework;
[TestFixture]
public class RangeTests
{
static readonly decimal[] interestingDecimals = new decimal[] {
decimal.MinValue,
decimal.MaxValue,
decimal.MinusOne,
decimal.Zero,
decimal.One,
-0.5m,
0.5m,
-1.0m / 3.0m,
1.0m / 3.0m,
};
[Test]
public void Decimal_ToDouble_gives_same_result_as_hard_cast()
{
// Source: https://referencesource.microsoft.com/#mscorlib/system/convert.cs,60ac48a8a574d890,references
foreach (decimal dec in interestingDecimals)
{
Assert.That((double)dec == Convert.ToDouble(dec));
}
}
[TestCase(0.0d)]
[TestCase(-0.5d)]
[TestCase(0.5d)]
[TestCase(1.0d / 3.0d)]
[TestCase(-1.0d / 3.0d)]
public void Double_ToDecimal_gives_same_result_as_hard_cast(double dbl)
{
// Source: https://referencesource.microsoft.com/#mscorlib/system/convert.cs,f121bf86d1ad3519,references
Assert.That((decimal)dbl == Convert.ToDecimal(dbl));
}
[Test]
public void Zero_equals_zero_after_Convert()
{
Assert.That(0.0d == Convert.ToDouble(decimal.Zero));
Assert.That(0.0m == Convert.ToDecimal(0.0d));
}
[Test]
public void Decimal_MaxValue_fits_in_double()
{
Assert.That(Convert.ToDouble(decimal.MaxValue) != 0.0d);
}
[Test]
public void Decimal_MinValue_fits_in_double()
{
Assert.That(Convert.ToDouble(decimal.MinValue) != 0.0d);
}
[Category("PossiblySurprising")]
[TestCase(double.MinValue)]
[TestCase(double.MaxValue)]
[TestCase(double.NaN)]
[TestCase(double.NegativeInfinity)]
[TestCase(double.PositiveInfinity)]
public void Double_constant_wont_fit_in_decimal(double dbl)
{
Assert.Throws<OverflowException>(() =>
{
decimal result = Convert.ToDecimal(dbl);
});
}
[Category("PossiblySurprising")]
[TestCase(double.Epsilon)]
[TestCase(1.0d / double.MaxValue)]
[TestCase(1.0d / double.MinValue)]
public void Double_small_value_equals_decimal_zero(double dbl)
{
Assert.That(Convert.ToDecimal(dbl) == decimal.Zero);
}
[Category("PossiblySurprising")]
[Test]
public void Decimal_MinValue_and_MaxValue_do_not_survive_conversion_roundtrip()
{
foreach (decimal dec in new[] { decimal.MinValue, decimal.MaxValue })
{
Assert.Throws<OverflowException>(() =>
{
decimal result = Convert.ToDecimal(Convert.ToDouble(dec));
});
}
}
[Category("PossiblySurprising")]
[Test]
public void Decimal_might_loose_precision_when_cast_to_double()
{
decimal decPi = 3.1415926535897932384626433m;
double dblPi = Convert.ToDouble(decPi); // No exception, just precision loss
Assert.That(dblPi.ToString(), Is.Not.EqualTo(decPi.ToString()));
Assert.That(dblPi.ToString(), Is.EqualTo("3.14159265358979"));
Assert.That(decPi.ToString(), Is.EqualTo("3.1415926535897932384626433"));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment