More Parse tests for double, single and Half (#50394)
authorPrashanth Govindarajan <prgovi@microsoft.com>
Tue, 22 Jun 2021 01:22:30 +0000 (18:22 -0700)
committerGitHub <noreply@github.com>
Tue, 22 Jun 2021 01:22:30 +0000 (01:22 +0000)
* Test ibm-fpgen locally for validation

* sq

THIRD-PARTY-NOTICES.TXT
eng/Version.Details.xml
eng/Versions.props
src/libraries/System.Runtime/tests/System.Runtime.Tests.csproj
src/libraries/System.Runtime/tests/System/DoubleTests.cs

index a877e8f..14c806c 100644 (file)
@@ -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
index 2485b69..f1ef354 100644 (file)
       <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>
index 493ee88..b62b9b4 100644 (file)
     <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>
index 2a68cc1..2ca6739 100644 (file)
@@ -1,4 +1,4 @@
-<Project Sdk="Microsoft.NET.Sdk">
+<Project Sdk="Microsoft.NET.Sdk">
   <PropertyGroup>
     <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
     <NoWarn>$(NoWarn),1718,SYSLIB0013</NoWarn>
   </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" />
index cfe3690..a80eb05 100644 (file)
@@ -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
@@ -346,6 +347,70 @@ namespace System.Tests
             }
         }
 
+        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;