Skip to content

Commit

Permalink
More Parse tests for double, single and Half (dotnet#50394)
Browse files Browse the repository at this point in the history
* Test ibm-fpgen locally for validation

* sq
  • Loading branch information
Prashanth Govindarajan authored Jun 22, 2021
1 parent 2a43c07 commit bea8d95
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 2 deletions.
2 changes: 1 addition & 1 deletion THIRD-PARTY-NOTICES.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ worldwide. This software is distributed without any warranty.

See <http://creativecommons.org/publicdomain/zero/1.0/>.

License for fastmod (https://github.com/lemire/fastmod)
License for fastmod (https://github.com/lemire/fastmod) and ibm-fpgen (https://github.com/nigeltao/parse-number-fxx-test-data)
--------------------------------------

Copyright 2018 Daniel Lemire
Expand Down
4 changes: 4 additions & 0 deletions eng/Version.Details.xml
Original file line number Diff line number Diff line change
Expand Up @@ -214,5 +214,9 @@
<Uri>https://github.com/dotnet/hotreload-utils</Uri>
<Sha>25b814e010cd4796cedfbcce72a274c26928f496</Sha>
</Dependency>
<Dependency Name="System.Runtime.Numerics.TestData" Version="6.0.0-beta.21314.1">
<Uri>https://github.com/dotnet/runtime-assets
<Sha>8d7b898b96cbdb868cac343e938173105287ed9e</Sha>
</Dependency>
</ToolsetDependencies>
</Dependencies>
1 change: 1 addition & 0 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
<SystemValueTupleVersion>4.5.0</SystemValueTupleVersion>
<runtimenativeSystemIOPortsVersion>6.0.0-preview.6.21314.1</runtimenativeSystemIOPortsVersion>
<!-- Runtime-Assets dependencies -->
<SystemRuntimeNumericsTestDataVersion>6.0.0-beta.21314.1</SystemRuntimeNumericsTestDataVersion>
<SystemComponentModelTypeConverterTestDataVersion>6.0.0-beta.21307.1</SystemComponentModelTypeConverterTestDataVersion>
<SystemDrawingCommonTestDataVersion>6.0.0-beta.21307.1</SystemDrawingCommonTestDataVersion>
<SystemIOCompressionTestDataVersion>6.0.0-beta.21307.1</SystemIOCompressionTestDataVersion>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<NoWarn>$(NoWarn),1718,SYSLIB0013</NoWarn>
Expand Down Expand Up @@ -282,6 +282,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Moq" Version="$(MoqVersion)" />
<PackageReference Include="System.Runtime.Numerics.TestData" Version="$(SystemRuntimeNumericsTestDataVersion)" GeneratePathProperty="true"/>
<ProjectReference Include="TestLoadAssembly\TestLoadAssembly.csproj" />
<ProjectReference Include="TestCollectibleAssembly\TestCollectibleAssembly.csproj" />
<ProjectReference Include="TestModule\System.Reflection.TestModule.ilproj" />
Expand Down
65 changes: 65 additions & 0 deletions src/libraries/System.Runtime/tests/System/DoubleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Generic;
using System.Globalization;
using System.IO;
using Xunit;

#pragma warning disable xUnit1025 // reporting duplicate test cases due to not distinguishing 0.0 from -0.0, NaN from -NaN
Expand Down Expand Up @@ -346,6 +347,70 @@ public static void Parse(string value, NumberStyles style, IFormatProvider provi
}
}

internal static string SplitPairs(string input)
{
string[] splitPairs = input.Split('-');
string ret = "";
foreach (var pair in splitPairs)
{
string reversedPair = Reverse(pair);
ret += reversedPair;
}

return ret;
}

internal static string Reverse(string s)
{
char[] charArray = s.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotBrowser))]
public static void ParsePatterns()
{
string path = Directory.GetCurrentDirectory();
using (FileStream file = new FileStream(Path.Combine(path, "ibm-fpgen.txt"), FileMode.Open))
{
using (var streamReader = new StreamReader(file))
{
string line = streamReader.ReadLine();
while (line != null)
{
string[] data = line.Split(' ');
string inputHalfBytes = data[0];
string inputFloatBytes = data[1];
string inputDoubleBytes = data[2];
string correctValue = data[3];

double doubleValue = double.Parse(correctValue, NumberFormatInfo.InvariantInfo);
string doubleBytes = BitConverter.ToString(BitConverter.GetBytes(doubleValue));
float floatValue = float.Parse(correctValue, NumberFormatInfo.InvariantInfo);
string floatBytes = BitConverter.ToString(BitConverter.GetBytes(floatValue));
Half halfValue = Half.Parse(correctValue, NumberFormatInfo.InvariantInfo);
string halfBytes = BitConverter.ToString(BitConverter.GetBytes(halfValue));

doubleBytes = SplitPairs(doubleBytes);
floatBytes = SplitPairs(floatBytes);
halfBytes = SplitPairs(halfBytes);

if (BitConverter.IsLittleEndian)
{
doubleBytes = Reverse(doubleBytes);
floatBytes = Reverse(floatBytes);
halfBytes = Reverse(halfBytes);
}

Assert.Equal(doubleBytes, inputDoubleBytes);
Assert.Equal(floatBytes, inputFloatBytes);
Assert.Equal(halfBytes, inputHalfBytes);
line = streamReader.ReadLine();
}
}
}
}

public static IEnumerable<object[]> Parse_Invalid_TestData()
{
NumberStyles defaultStyle = NumberStyles.Float;
Expand Down

0 comments on commit bea8d95

Please sign in to comment.