{
public partial class DoubleTests : RemoteExecutorTestBase
{
+ [Theory]
+ [InlineData("a")]
+ [InlineData((float)234)]
+ public void CompareTo_ObjectNotDouble_ThrowsArgumentException(object value)
+ {
+ AssertExtensions.Throws<ArgumentException>(null, () => ((double)123).CompareTo(value));
+ }
+
+ [Theory]
+ [InlineData((double)234, (double)234, 0)]
+ [InlineData((double)234, double.MinValue, 1)]
+ [InlineData((double)234, (double)-123, 1)]
+ [InlineData((double)234, (double)0, 1)]
+ [InlineData((double)234, (double)123, 1)]
+ [InlineData((double)234, (double)456, -1)]
+ [InlineData((double)234, double.MaxValue, -1)]
+ [InlineData((double)234, double.NaN, 1)]
+ [InlineData(double.NaN, double.NaN, 0)]
+ [InlineData(double.NaN, (double)0, -1)]
+ [InlineData((double)234, null, 1)]
+ public void CompareTo_Other_ReturnsExpected(double d1, object value, int expected)
+ {
+ if (value is double d2)
+ {
+ Assert.Equal(expected, Math.Sign(d1.CompareTo(d2)));
+ if (double.IsNaN(d1) || double.IsNaN(d2))
+ {
+ Assert.False(d1 >= d2);
+ Assert.False(d1 > d2);
+ Assert.False(d1 <= d2);
+ Assert.False(d1 < d2);
+ }
+ else
+ {
+ if (expected >= 0)
+ {
+ Assert.True(d1 >= d2);
+ Assert.False(d1 < d2);
+ }
+ if (expected > 0)
+ {
+ Assert.True(d1 > d2);
+ Assert.False(d1 <= d2);
+ }
+ if (expected <= 0)
+ {
+ Assert.True(d1 <= d2);
+ Assert.False(d1 > d2);
+ }
+ if (expected < 0)
+ {
+ Assert.True(d1 < d2);
+ Assert.False(d1 >= d2);
+ }
+ }
+ }
+
+ Assert.Equal(expected, Math.Sign(d1.CompareTo(value)));
+ }
+
[Fact]
public static void Ctor_Empty()
{
}
[Fact]
- public static void MaxValue()
+ public static void Epsilon()
{
- Assert.Equal(1.7976931348623157E+308, double.MaxValue);
+ Assert.Equal(4.9406564584124654E-324, double.Epsilon);
}
- [Fact]
- public static void MinValue()
+ [Theory]
+ [InlineData((double)789, (double)789, true)]
+ [InlineData((double)789, (double)-789, false)]
+ [InlineData((double)789, (double)0, false)]
+ [InlineData(double.NaN, double.NaN, true)]
+ [InlineData(double.NaN, -double.NaN, true)]
+ [InlineData((double)789, (float)789, false)]
+ [InlineData((double)789, "789", false)]
+ [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "The fix was made in coreclr that is not in netfx. See https://github.com/dotnet/coreclr/issues/6237")]
+ public static void Equals(double d1, object value, bool expected)
{
- Assert.Equal(-1.7976931348623157E+308, double.MinValue);
+ if (value is double d2)
+ {
+ Assert.Equal(expected, d1.Equals(d2));
+
+ if (double.IsNaN(d1) && double.IsNaN(d2))
+ {
+ Assert.Equal(!expected, d1 == d2);
+ Assert.Equal(expected, d1 != d2);
+ }
+ else
+ {
+ Assert.Equal(expected, d1 == d2);
+ Assert.Equal(!expected, d1 != d2);
+ }
+ Assert.Equal(expected, d1.GetHashCode().Equals(d2.GetHashCode()));
+ }
+ Assert.Equal(expected, d1.Equals(value));
}
[Fact]
- public static void Epsilon()
+ public void GetTypeCode_Invoke_ReturnsDouble()
{
- Assert.Equal(4.9406564584124654E-324, double.Epsilon);
+ Assert.Equal(TypeCode.Double, 0.0.GetTypeCode());
}
[Theory]
Assert.Equal(expected, double.IsInfinity(d));
}
- [Fact]
- public static void NaN()
- {
- Assert.Equal(0.0 / 0.0, double.NaN);
- }
-
[Theory]
[InlineData(double.NegativeInfinity, false)] // Negative Infinity
[InlineData(double.MinValue, false)] // Min Negative Normal
Assert.Equal(expected, double.IsNaN(d));
}
- [Fact]
- public static void NegativeInfinity()
- {
- Assert.Equal(-1.0 / 0.0, double.NegativeInfinity);
- }
-
[Theory]
[InlineData(double.NegativeInfinity, true)] // Negative Infinity
[InlineData(double.MinValue, false)] // Min Negative Normal
Assert.Equal(expected, double.IsNegativeInfinity(d));
}
- [Fact]
- public static void PositiveInfinity()
- {
- Assert.Equal(1.0 / 0.0, double.PositiveInfinity);
- }
-
[Theory]
[InlineData(double.NegativeInfinity, false)] // Negative Infinity
[InlineData(double.MinValue, false)] // Min Negative Normal
Assert.Equal(expected, double.IsPositiveInfinity(d));
}
- [Theory]
- [InlineData((double)234, (double)234, 0)]
- [InlineData((double)234, double.MinValue, 1)]
- [InlineData((double)234, (double)-123, 1)]
- [InlineData((double)234, (double)0, 1)]
- [InlineData((double)234, (double)123, 1)]
- [InlineData((double)234, (double)456, -1)]
- [InlineData((double)234, double.MaxValue, -1)]
- [InlineData((double)234, double.NaN, 1)]
- [InlineData(double.NaN, double.NaN, 0)]
- [InlineData(double.NaN, (double)0, -1)]
- [InlineData((double)234, null, 1)]
- public void CompareTo_Other_ReturnsExpected(double d1, object value, int expected)
- {
- if (value is double d2)
- {
- Assert.Equal(expected, Math.Sign(d1.CompareTo(d2)));
- if (double.IsNaN(d1) || double.IsNaN(d2))
- {
- Assert.False(d1 >= d2);
- Assert.False(d1 > d2);
- Assert.False(d1 <= d2);
- Assert.False(d1 < d2);
- }
- else
- {
- if (expected >= 0)
- {
- Assert.True(d1 >= d2);
- Assert.False(d1 < d2);
- }
- if (expected > 0)
- {
- Assert.True(d1 > d2);
- Assert.False(d1 <= d2);
- }
- if (expected <= 0)
- {
- Assert.True(d1 <= d2);
- Assert.False(d1 > d2);
- }
- if (expected < 0)
- {
- Assert.True(d1 < d2);
- Assert.False(d1 >= d2);
- }
- }
- }
-
- Assert.Equal(expected, Math.Sign(d1.CompareTo(value)));
- }
-
- [Theory]
- [InlineData("a")]
- [InlineData((float)234)]
- public void CompareTo_ObjectNotDouble_ThrowsArgumentException(object value)
- {
- AssertExtensions.Throws<ArgumentException>(null, () => ((double)123).CompareTo(value));
- }
-
- [Theory]
- [InlineData((double)789, (double)789, true)]
- [InlineData((double)789, (double)-789, false)]
- [InlineData((double)789, (double)0, false)]
- [InlineData(double.NaN, double.NaN, true)]
- [InlineData(double.NaN, -double.NaN, true)]
- [InlineData((double)789, (float)789, false)]
- [InlineData((double)789, "789", false)]
- [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "The fix was made in coreclr that is not in netfx. See https://github.com/dotnet/coreclr/issues/6237")]
- public static void Equals(double d1, object value, bool expected)
- {
- if (value is double d2)
- {
- Assert.Equal(expected, d1.Equals(d2));
-
- if (double.IsNaN(d1) && double.IsNaN(d2))
- {
- Assert.Equal(!expected, d1 == d2);
- Assert.Equal(expected, d1 != d2);
- }
- else
- {
- Assert.Equal(expected, d1 == d2);
- Assert.Equal(!expected, d1 != d2);
- }
- Assert.Equal(expected, d1.GetHashCode().Equals(d2.GetHashCode()));
- }
- Assert.Equal(expected, d1.Equals(value));
- }
-
[Fact]
- public void GetTypeCode_Invoke_ReturnsDouble()
+ public static void MaxValue()
{
- Assert.Equal(TypeCode.Double, 0.0.GetTypeCode());
+ Assert.Equal(1.7976931348623157E+308, double.MaxValue);
}
- public static IEnumerable<object[]> ToString_TestData()
+ [Fact]
+ public static void MinValue()
{
- yield return new object[] { double.MinValue, "G", null, "-1.79769313486232E+308" };
- yield return new object[] { (double)-4567, "G", null, "-4567" };
- yield return new object[] { -4567.89101, "G", null, "-4567.89101" };
- yield return new object[] { (double)0, "G", null, "0" };
- yield return new object[] { (double)4567, "G", null, "4567" };
- yield return new object[] { 4567.89101, "G", null, "4567.89101" };
- yield return new object[] { double.MaxValue, "G", null, "1.79769313486232E+308" };
-
- yield return new object[] { double.Epsilon, "G", null, "4.94065645841247E-324" };
- yield return new object[] { double.NaN, "G", null, "NaN" };
-
- yield return new object[] { (double)2468, "N", null, "2,468.00" };
-
- // Changing the negative pattern doesn't do anything without also passing in a format string
- var customNegativePattern = new NumberFormatInfo() { NumberNegativePattern = 0 };
- yield return new object[] { (double)-6310, "G", customNegativePattern, "-6310" };
-
- var customNegativeSignDecimalGroupSeparator = new NumberFormatInfo()
- {
- NegativeSign = "#",
- NumberDecimalSeparator = "~",
- NumberGroupSeparator = "*"
- };
- yield return new object[] { (double)-2468, "N", customNegativeSignDecimalGroupSeparator, "#2*468~00" };
- yield return new object[] { (double)2468, "N", customNegativeSignDecimalGroupSeparator, "2*468~00" };
-
- var customNegativeSignGroupSeparatorNegativePattern = new NumberFormatInfo()
- {
- NegativeSign = "xx", // Set to trash to make sure it doesn't show up
- NumberGroupSeparator = "*",
- NumberNegativePattern = 0,
- };
- yield return new object[] { (double)-2468, "N", customNegativeSignGroupSeparatorNegativePattern, "(2*468.00)" };
-
- NumberFormatInfo invariantFormat = NumberFormatInfo.InvariantInfo;
- yield return new object[] { double.Epsilon, "G", invariantFormat, "4.94065645841247E-324" };
- yield return new object[] { double.NaN, "G", invariantFormat, "NaN" };
- yield return new object[] { double.PositiveInfinity, "G", invariantFormat, "Infinity" };
- yield return new object[] { double.NegativeInfinity, "G", invariantFormat, "-Infinity" };
+ Assert.Equal(-1.7976931348623157E+308, double.MinValue);
}
-
[Fact]
- public static void Test_ToString()
- {
- RemoteInvoke(() =>
- {
- CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
-
- foreach (var testdata in ToString_TestData())
- {
- ToString((double)testdata[0], (string)testdata[1], (IFormatProvider)testdata[2], (string)testdata[3]);
- }
- return SuccessExitCode;
- }).Dispose();
- }
-
- private static void ToString(double d, string format, IFormatProvider provider, string expected)
+ public static void NaN()
{
- bool isDefaultProvider = (provider == null || provider == NumberFormatInfo.CurrentInfo);
- if (string.IsNullOrEmpty(format) || format.ToUpperInvariant() == "G")
- {
- if (isDefaultProvider)
- {
- Assert.Equal(expected, d.ToString());
- Assert.Equal(expected, d.ToString((IFormatProvider)null));
- }
- Assert.Equal(expected, d.ToString(provider));
- }
- if (isDefaultProvider)
- {
- Assert.Equal(expected.Replace('e', 'E'), d.ToString(format.ToUpperInvariant())); // If format is upper case, then exponents are printed in upper case
- Assert.Equal(expected.Replace('E', 'e'), d.ToString(format.ToLowerInvariant())); // If format is lower case, then exponents are printed in upper case
- Assert.Equal(expected.Replace('e', 'E'), d.ToString(format.ToUpperInvariant(), null));
- Assert.Equal(expected.Replace('E', 'e'), d.ToString(format.ToLowerInvariant(), null));
- }
- Assert.Equal(expected.Replace('e', 'E'), d.ToString(format.ToUpperInvariant(), provider));
- Assert.Equal(expected.Replace('E', 'e'), d.ToString(format.ToLowerInvariant(), provider));
+ Assert.Equal(0.0 / 0.0, double.NaN);
}
[Fact]
- public static void ToString_InvalidFormat_ThrowsFormatException()
+ public static void NegativeInfinity()
{
- double d = 123;
- Assert.Throws<FormatException>(() => d.ToString("Y")); // Invalid format
- Assert.Throws<FormatException>(() => d.ToString("Y", null)); // Invalid format
+ Assert.Equal(-1.0 / 0.0, double.NegativeInfinity);
}
public static IEnumerable<object[]> Parse_Valid_TestData()
Assert.Throws(exceptionType, () => double.Parse(value, style, NumberFormatInfo.CurrentInfo));
}
}
+
+ [Fact]
+ public static void PositiveInfinity()
+ {
+ Assert.Equal(1.0 / 0.0, double.PositiveInfinity);
+ }
+
+ public static IEnumerable<object[]> ToString_TestData()
+ {
+ yield return new object[] { double.MinValue, "G", null, "-1.79769313486232E+308" };
+ yield return new object[] { (double)-4567, "G", null, "-4567" };
+ yield return new object[] { -4567.89101, "G", null, "-4567.89101" };
+ yield return new object[] { (double)0, "G", null, "0" };
+ yield return new object[] { (double)4567, "G", null, "4567" };
+ yield return new object[] { 4567.89101, "G", null, "4567.89101" };
+ yield return new object[] { double.MaxValue, "G", null, "1.79769313486232E+308" };
+
+ yield return new object[] { double.Epsilon, "G", null, "4.94065645841247E-324" };
+ yield return new object[] { double.NaN, "G", null, "NaN" };
+
+ yield return new object[] { (double)2468, "N", null, "2,468.00" };
+
+ // Changing the negative pattern doesn't do anything without also passing in a format string
+ var customNegativePattern = new NumberFormatInfo() { NumberNegativePattern = 0 };
+ yield return new object[] { (double)-6310, "G", customNegativePattern, "-6310" };
+
+ var customNegativeSignDecimalGroupSeparator = new NumberFormatInfo()
+ {
+ NegativeSign = "#",
+ NumberDecimalSeparator = "~",
+ NumberGroupSeparator = "*"
+ };
+ yield return new object[] { (double)-2468, "N", customNegativeSignDecimalGroupSeparator, "#2*468~00" };
+ yield return new object[] { (double)2468, "N", customNegativeSignDecimalGroupSeparator, "2*468~00" };
+
+ var customNegativeSignGroupSeparatorNegativePattern = new NumberFormatInfo()
+ {
+ NegativeSign = "xx", // Set to trash to make sure it doesn't show up
+ NumberGroupSeparator = "*",
+ NumberNegativePattern = 0,
+ };
+ yield return new object[] { (double)-2468, "N", customNegativeSignGroupSeparatorNegativePattern, "(2*468.00)" };
+
+ NumberFormatInfo invariantFormat = NumberFormatInfo.InvariantInfo;
+ yield return new object[] { double.Epsilon, "G", invariantFormat, "4.94065645841247E-324" };
+ yield return new object[] { double.NaN, "G", invariantFormat, "NaN" };
+ yield return new object[] { double.PositiveInfinity, "G", invariantFormat, "Infinity" };
+ yield return new object[] { double.NegativeInfinity, "G", invariantFormat, "-Infinity" };
+ }
+
+ [Fact]
+ public static void Test_ToString()
+ {
+ RemoteInvoke(() =>
+ {
+ CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
+
+ foreach (var testdata in ToString_TestData())
+ {
+ ToString((double)testdata[0], (string)testdata[1], (IFormatProvider)testdata[2], (string)testdata[3]);
+ }
+ return SuccessExitCode;
+ }).Dispose();
+ }
+
+ private static void ToString(double d, string format, IFormatProvider provider, string expected)
+ {
+ bool isDefaultProvider = (provider == null || provider == NumberFormatInfo.CurrentInfo);
+ if (string.IsNullOrEmpty(format) || format.ToUpperInvariant() == "G")
+ {
+ if (isDefaultProvider)
+ {
+ Assert.Equal(expected, d.ToString());
+ Assert.Equal(expected, d.ToString((IFormatProvider)null));
+ }
+ Assert.Equal(expected, d.ToString(provider));
+ }
+ if (isDefaultProvider)
+ {
+ Assert.Equal(expected.Replace('e', 'E'), d.ToString(format.ToUpperInvariant())); // If format is upper case, then exponents are printed in upper case
+ Assert.Equal(expected.Replace('E', 'e'), d.ToString(format.ToLowerInvariant())); // If format is lower case, then exponents are printed in upper case
+ Assert.Equal(expected.Replace('e', 'E'), d.ToString(format.ToUpperInvariant(), null));
+ Assert.Equal(expected.Replace('E', 'e'), d.ToString(format.ToLowerInvariant(), null));
+ }
+ Assert.Equal(expected.Replace('e', 'E'), d.ToString(format.ToUpperInvariant(), provider));
+ Assert.Equal(expected.Replace('E', 'e'), d.ToString(format.ToLowerInvariant(), provider));
+ }
+
+ [Fact]
+ public static void ToString_InvalidFormat_ThrowsFormatException()
+ {
+ double d = 123;
+ Assert.Throws<FormatException>(() => d.ToString("Y")); // Invalid format
+ Assert.Throws<FormatException>(() => d.ToString("Y", null)); // Invalid format
+ }
}
}
{
public partial class SingleTests : RemoteExecutorTestBase
{
+ [Theory]
+ [InlineData("a")]
+ [InlineData((double)234)]
+ public void CompareTo_ObjectNotFloat_ThrowsArgumentException(object value)
+ {
+ AssertExtensions.Throws<ArgumentException>(null, () => ((float)123).CompareTo(value));
+ }
+
+ [Theory]
+ [InlineData((float)234, (float)234, 0)]
+ [InlineData((float)234, float.MinValue, 1)]
+ [InlineData((float)234, (float)-123, 1)]
+ [InlineData((float)234, (float)0, 1)]
+ [InlineData((float)234, (float)123, 1)]
+ [InlineData((float)234, (float)456, -1)]
+ [InlineData((float)234, float.MaxValue, -1)]
+ [InlineData((float)234, float.NaN, 1)]
+ [InlineData(float.NaN, float.NaN, 0)]
+ [InlineData(float.NaN, (float)0, -1)]
+ [InlineData((float)234, null, 1)]
+ public void CompareTo_Other_ReturnsExpected(float f1, object value, int expected)
+ {
+ if (value is float f2)
+ {
+ Assert.Equal(expected, Math.Sign(f1.CompareTo(f2)));
+ if (float.IsNaN(f1) || float.IsNaN(f2))
+ {
+ Assert.False(f1 >= f2);
+ Assert.False(f1 > f2);
+ Assert.False(f1 <= f2);
+ Assert.False(f1 < f2);
+ }
+ else
+ {
+ if (expected >= 0)
+ {
+ Assert.True(f1 >= f2);
+ Assert.False(f1 < f2);
+ }
+ if (expected > 0)
+ {
+ Assert.True(f1 > f2);
+ Assert.False(f1 <= f2);
+ }
+ if (expected <= 0)
+ {
+ Assert.True(f1 <= f2);
+ Assert.False(f1 > f2);
+ }
+ if (expected < 0)
+ {
+ Assert.True(f1 < f2);
+ Assert.False(f1 >= f2);
+ }
+ }
+ }
+
+ Assert.Equal(expected, Math.Sign(f1.CompareTo(value)));
+ }
+
[Fact]
public static void Ctor_Empty()
{
}
[Fact]
- public static void MaxValue()
+ public static void Epsilon()
{
- Assert.Equal((float)3.40282346638528859e+38, float.MaxValue);
+ Assert.Equal((float)1.4e-45, float.Epsilon);
}
- [Fact]
- public static void MinValue()
+ [Theory]
+ [InlineData((float)789, (float)789, true)]
+ [InlineData((float)789, (float)-789, false)]
+ [InlineData((float)789, (float)0, false)]
+ [InlineData(float.NaN, float.NaN, true)]
+ [InlineData(float.NaN, -float.NaN, true)]
+ [InlineData((float)789, (double)789, false)]
+ [InlineData((float)789, "789", false)]
+ [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "The fix was made in coreclr that is not in netfx. See https://github.com/dotnet/coreclr/issues/6237")]
+ public static void Equals(float f1, object value, bool expected)
{
- Assert.Equal((float)-3.40282346638528859e+38, float.MinValue);
+ if (value is float f2)
+ {
+ Assert.Equal(expected, f1.Equals(f2));
+
+ if (float.IsNaN(f1) && float.IsNaN(f2))
+ {
+ Assert.Equal(!expected, f1 == f2);
+ Assert.Equal(expected, f1 != f2);
+ }
+ else
+ {
+ Assert.Equal(expected, f1 == f2);
+ Assert.Equal(!expected, f1 != f2);
+ }
+ Assert.Equal(expected, f1.GetHashCode().Equals(f2.GetHashCode()));
+ }
+ Assert.Equal(expected, f1.Equals(value));
}
[Fact]
- public static void Epsilon()
+ public void GetTypeCode_Invoke_ReturnsSingle()
{
- Assert.Equal((float)1.4e-45, float.Epsilon);
+ Assert.Equal(TypeCode.Single, 0.0f.GetTypeCode());
}
[Theory]
Assert.Equal(expected, float.IsInfinity(d));
}
- [Fact]
- public void GetTypeCode_Invoke_ReturnsSingle()
- {
- Assert.Equal(TypeCode.Single, 0.0f.GetTypeCode());
- }
-
- [Fact]
- public static void NaN()
- {
- Assert.Equal((float)0.0 / (float)0.0, float.NaN);
- }
-
[Theory]
[InlineData(float.NegativeInfinity, false)] // Negative Infinity
[InlineData(float.MinValue, false)] // Min Negative Normal
Assert.Equal(expected, float.IsNaN(d));
}
- [Fact]
- public static void NegativeInfinity()
- {
- Assert.Equal((float)-1.0 / (float)0.0, float.NegativeInfinity);
- }
-
[Theory]
[InlineData(float.NegativeInfinity, true)] // Negative Infinity
[InlineData(float.MinValue, false)] // Min Negative Normal
Assert.Equal(expected, float.IsNegativeInfinity(d));
}
- [Fact]
- public static void PositiveInfinity()
- {
- Assert.Equal((float)1.0 / (float)0.0, float.PositiveInfinity);
- }
-
[Theory]
[InlineData(float.NegativeInfinity, false)] // Negative Infinity
[InlineData(float.MinValue, false)] // Min Negative Normal
Assert.Equal(expected, float.IsPositiveInfinity(d));
}
- [Theory]
- [InlineData((float)234, (float)234, 0)]
- [InlineData((float)234, float.MinValue, 1)]
- [InlineData((float)234, (float)-123, 1)]
- [InlineData((float)234, (float)0, 1)]
- [InlineData((float)234, (float)123, 1)]
- [InlineData((float)234, (float)456, -1)]
- [InlineData((float)234, float.MaxValue, -1)]
- [InlineData((float)234, float.NaN, 1)]
- [InlineData(float.NaN, float.NaN, 0)]
- [InlineData(float.NaN, (float)0, -1)]
- [InlineData((float)234, null, 1)]
- public void CompareTo_Other_ReturnsExpected(float f1, object value, int expected)
- {
- if (value is float f2)
- {
- Assert.Equal(expected, Math.Sign(f1.CompareTo(f2)));
- if (float.IsNaN(f1) || float.IsNaN(f2))
- {
- Assert.False(f1 >= f2);
- Assert.False(f1 > f2);
- Assert.False(f1 <= f2);
- Assert.False(f1 < f2);
- }
- else
- {
- if (expected >= 0)
- {
- Assert.True(f1 >= f2);
- Assert.False(f1 < f2);
- }
- if (expected > 0)
- {
- Assert.True(f1 > f2);
- Assert.False(f1 <= f2);
- }
- if (expected <= 0)
- {
- Assert.True(f1 <= f2);
- Assert.False(f1 > f2);
- }
- if (expected < 0)
- {
- Assert.True(f1 < f2);
- Assert.False(f1 >= f2);
- }
- }
- }
-
- Assert.Equal(expected, Math.Sign(f1.CompareTo(value)));
- }
-
- [Theory]
- [InlineData("a")]
- [InlineData((double)234)]
- public void CompareTo_ObjectNotFloat_ThrowsArgumentException(object value)
- {
- AssertExtensions.Throws<ArgumentException>(null, () => ((float)123).CompareTo(value));
- }
-
- [Theory]
- [InlineData((float)789, (float)789, true)]
- [InlineData((float)789, (float)-789, false)]
- [InlineData((float)789, (float)0, false)]
- [InlineData(float.NaN, float.NaN, true)]
- [InlineData(float.NaN, -float.NaN, true)]
- [InlineData((float)789, (double)789, false)]
- [InlineData((float)789, "789", false)]
- [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "The fix was made in coreclr that is not in netfx. See https://github.com/dotnet/coreclr/issues/6237")]
- public static void Equals(float f1, object value, bool expected)
+ [Fact]
+ public static void MaxValue()
{
- if (value is float f2)
- {
- Assert.Equal(expected, f1.Equals(f2));
-
- if (float.IsNaN(f1) && float.IsNaN(f2))
- {
- Assert.Equal(!expected, f1 == f2);
- Assert.Equal(expected, f1 != f2);
- }
- else
- {
- Assert.Equal(expected, f1 == f2);
- Assert.Equal(!expected, f1 != f2);
- }
- Assert.Equal(expected, f1.GetHashCode().Equals(f2.GetHashCode()));
- }
- Assert.Equal(expected, f1.Equals(value));
+ Assert.Equal((float)3.40282346638528859e+38, float.MaxValue);
}
- public static IEnumerable<object[]> ToString_TestData()
+ [Fact]
+ public static void MinValue()
{
- yield return new object[] { float.MinValue, "G", null, "-3.402823E+38" };
- yield return new object[] { (float)-4567, "G", null, "-4567" };
- yield return new object[] { (float)-4567.89101, "G", null, "-4567.891" };
- yield return new object[] { (float)0, "G", null, "0" };
- yield return new object[] { (float)4567, "G", null, "4567" };
- yield return new object[] { (float)4567.89101, "G", null, "4567.891" };
- yield return new object[] { float.MaxValue, "G", null, "3.402823E+38" };
-
- yield return new object[] { float.Epsilon, "G", null, "1.401298E-45" };
- yield return new object[] { float.NaN, "G", null, "NaN" };
-
- yield return new object[] { (float)2468, "N", null, "2,468.00" };
-
- // Changing the negative pattern doesn't do anything without also passing in a format string
- var customNegativePattern = new NumberFormatInfo() { NumberNegativePattern = 0 };
- yield return new object[] { (float)-6310, "G", customNegativePattern, "-6310" };
-
- var customNegativeSignDecimalGroupSeparator = new NumberFormatInfo()
- {
- NegativeSign = "#",
- NumberDecimalSeparator = "~",
- NumberGroupSeparator = "*"
- };
- yield return new object[] { (float)-2468, "N", customNegativeSignDecimalGroupSeparator, "#2*468~00" };
- yield return new object[] { (float)2468, "N", customNegativeSignDecimalGroupSeparator, "2*468~00" };
-
- var customNegativeSignGroupSeparatorNegativePattern = new NumberFormatInfo()
- {
- NegativeSign = "xx", // Set to trash to make sure it doesn't show up
- NumberGroupSeparator = "*",
- NumberNegativePattern = 0
- };
- yield return new object[] { (float)-2468, "N", customNegativeSignGroupSeparatorNegativePattern, "(2*468.00)" };
-
- NumberFormatInfo invariantFormat = NumberFormatInfo.InvariantInfo;
- yield return new object[] { float.Epsilon, "G", invariantFormat, "1.401298E-45" };
- yield return new object[] { float.NaN, "G", invariantFormat, "NaN" };
- yield return new object[] { float.PositiveInfinity, "G", invariantFormat, "Infinity" };
- yield return new object[] { float.NegativeInfinity, "G", invariantFormat, "-Infinity" };
+ Assert.Equal((float)-3.40282346638528859e+38, float.MinValue);
}
[Fact]
- public static void Test_ToString()
- {
- RemoteInvoke(() =>
- {
- CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
-
- foreach (var testdata in ToString_TestData())
- {
- ToString((float)testdata[0], (string)testdata[1], (IFormatProvider)testdata[2], (string)testdata[3]);
- }
- return SuccessExitCode;
- }).Dispose();
- }
-
- private static void ToString(float f, string format, IFormatProvider provider, string expected)
+ public static void NaN()
{
- bool isDefaultProvider = provider == null;
- if (string.IsNullOrEmpty(format) || format.ToUpperInvariant() == "G")
- {
- if (isDefaultProvider)
- {
- Assert.Equal(expected, f.ToString());
- Assert.Equal(expected, f.ToString((IFormatProvider)null));
- }
- Assert.Equal(expected, f.ToString(provider));
- }
- if (isDefaultProvider)
- {
- Assert.Equal(expected.Replace('e', 'E'), f.ToString(format.ToUpperInvariant())); // If format is upper case, then exponents are printed in upper case
- Assert.Equal(expected.Replace('E', 'e'), f.ToString(format.ToLowerInvariant())); // If format is lower case, then exponents are printed in lower case
- Assert.Equal(expected.Replace('e', 'E'), f.ToString(format.ToUpperInvariant(), null));
- Assert.Equal(expected.Replace('E', 'e'), f.ToString(format.ToLowerInvariant(), null));
- }
- Assert.Equal(expected.Replace('e', 'E'), f.ToString(format.ToUpperInvariant(), provider));
- Assert.Equal(expected.Replace('E', 'e'), f.ToString(format.ToLowerInvariant(), provider));
+ Assert.Equal((float)0.0 / (float)0.0, float.NaN);
}
[Fact]
- public static void ToString_InvalidFormat_ThrowsFormatException()
+ public static void NegativeInfinity()
{
- float f = 123;
- Assert.Throws<FormatException>(() => f.ToString("Y")); // Invalid format
- Assert.Throws<FormatException>(() => f.ToString("Y", null)); // Invalid format
+ Assert.Equal((float)-1.0 / (float)0.0, float.NegativeInfinity);
}
public static IEnumerable<object[]> Parse_Valid_TestData()
Assert.Throws(exceptionType, () => float.Parse(value, style, NumberFormatInfo.CurrentInfo));
}
}
+
+ [Fact]
+ public static void PositiveInfinity()
+ {
+ Assert.Equal((float)1.0 / (float)0.0, float.PositiveInfinity);
+ }
+
+ public static IEnumerable<object[]> ToString_TestData()
+ {
+ yield return new object[] { float.MinValue, "G", null, "-3.402823E+38" };
+ yield return new object[] { (float)-4567, "G", null, "-4567" };
+ yield return new object[] { (float)-4567.89101, "G", null, "-4567.891" };
+ yield return new object[] { (float)0, "G", null, "0" };
+ yield return new object[] { (float)4567, "G", null, "4567" };
+ yield return new object[] { (float)4567.89101, "G", null, "4567.891" };
+ yield return new object[] { float.MaxValue, "G", null, "3.402823E+38" };
+
+ yield return new object[] { float.Epsilon, "G", null, "1.401298E-45" };
+ yield return new object[] { float.NaN, "G", null, "NaN" };
+
+ yield return new object[] { (float)2468, "N", null, "2,468.00" };
+
+ // Changing the negative pattern doesn't do anything without also passing in a format string
+ var customNegativePattern = new NumberFormatInfo() { NumberNegativePattern = 0 };
+ yield return new object[] { (float)-6310, "G", customNegativePattern, "-6310" };
+
+ var customNegativeSignDecimalGroupSeparator = new NumberFormatInfo()
+ {
+ NegativeSign = "#",
+ NumberDecimalSeparator = "~",
+ NumberGroupSeparator = "*"
+ };
+ yield return new object[] { (float)-2468, "N", customNegativeSignDecimalGroupSeparator, "#2*468~00" };
+ yield return new object[] { (float)2468, "N", customNegativeSignDecimalGroupSeparator, "2*468~00" };
+
+ var customNegativeSignGroupSeparatorNegativePattern = new NumberFormatInfo()
+ {
+ NegativeSign = "xx", // Set to trash to make sure it doesn't show up
+ NumberGroupSeparator = "*",
+ NumberNegativePattern = 0
+ };
+ yield return new object[] { (float)-2468, "N", customNegativeSignGroupSeparatorNegativePattern, "(2*468.00)" };
+
+ NumberFormatInfo invariantFormat = NumberFormatInfo.InvariantInfo;
+ yield return new object[] { float.Epsilon, "G", invariantFormat, "1.401298E-45" };
+ yield return new object[] { float.NaN, "G", invariantFormat, "NaN" };
+ yield return new object[] { float.PositiveInfinity, "G", invariantFormat, "Infinity" };
+ yield return new object[] { float.NegativeInfinity, "G", invariantFormat, "-Infinity" };
+ }
+
+ [Fact]
+ public static void Test_ToString()
+ {
+ RemoteInvoke(() =>
+ {
+ CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
+
+ foreach (var testdata in ToString_TestData())
+ {
+ ToString((float)testdata[0], (string)testdata[1], (IFormatProvider)testdata[2], (string)testdata[3]);
+ }
+ return SuccessExitCode;
+ }).Dispose();
+ }
+
+ private static void ToString(float f, string format, IFormatProvider provider, string expected)
+ {
+ bool isDefaultProvider = provider == null;
+ if (string.IsNullOrEmpty(format) || format.ToUpperInvariant() == "G")
+ {
+ if (isDefaultProvider)
+ {
+ Assert.Equal(expected, f.ToString());
+ Assert.Equal(expected, f.ToString((IFormatProvider)null));
+ }
+ Assert.Equal(expected, f.ToString(provider));
+ }
+ if (isDefaultProvider)
+ {
+ Assert.Equal(expected.Replace('e', 'E'), f.ToString(format.ToUpperInvariant())); // If format is upper case, then exponents are printed in upper case
+ Assert.Equal(expected.Replace('E', 'e'), f.ToString(format.ToLowerInvariant())); // If format is lower case, then exponents are printed in lower case
+ Assert.Equal(expected.Replace('e', 'E'), f.ToString(format.ToUpperInvariant(), null));
+ Assert.Equal(expected.Replace('E', 'e'), f.ToString(format.ToLowerInvariant(), null));
+ }
+ Assert.Equal(expected.Replace('e', 'E'), f.ToString(format.ToUpperInvariant(), provider));
+ Assert.Equal(expected.Replace('E', 'e'), f.ToString(format.ToLowerInvariant(), provider));
+ }
+
+ [Fact]
+ public static void ToString_InvalidFormat_ThrowsFormatException()
+ {
+ float f = 123;
+ Assert.Throws<FormatException>(() => f.ToString("Y")); // Invalid format
+ Assert.Throws<FormatException>(() => f.ToString("Y", null)); // Invalid format
+ }
}
}