Add some tests for the Half type
authorFraser Waters <frassle@gmail.com>
Sat, 9 Sep 2017 09:39:26 +0000 (10:39 +0100)
committerFraser Waters <frassle@gmail.com>
Sun, 10 Sep 2017 18:39:47 +0000 (19:39 +0100)
tests/OpenTK.Tests/HalfTests.fs [new file with mode: 0644]
tests/OpenTK.Tests/OpenTK.Tests.fsproj

diff --git a/tests/OpenTK.Tests/HalfTests.fs b/tests/OpenTK.Tests/HalfTests.fs
new file mode 100644 (file)
index 0000000..ac2ec4e
--- /dev/null
@@ -0,0 +1,40 @@
+namespace OpenTK.Tests
+
+open Xunit
+open FsCheck
+open FsCheck.Xunit
+open System
+open System.Runtime.InteropServices
+open OpenTK
+
+module Half =
+    [<Fact>]
+    let ``Casting Half to Single and back to Half is lossless`` () =
+        for bits = int System.Int16.MinValue to int System.Int16.MaxValue do
+            let bytes = System.BitConverter.GetBytes(int16 bits)
+            let half = Half.FromBytes(bytes, 0)
+            let single = float32 half
+            let roundtrip = Half single
+
+            Assert.True((half.IsNaN && roundtrip.IsNaN) || half = roundtrip)
+
+    [<Fact>]
+    let ``Half.ToString and Single.ToString return same string for same value`` () =
+        for bits = int System.Int16.MinValue to int System.Int16.MaxValue do
+            let bytes = System.BitConverter.GetBytes(int16 bits)
+            let half = Half.FromBytes(bytes, 0)
+            let single = float32 half
+            Assert.Equal(half.ToString(), single.ToString())
+
+    [<Fact>]
+    let ``Half can represent all integers from -2048 to 2048 exactly`` () =
+        for i = -2048 to 2048 do
+            let single = float32 i
+            let half = Half single
+            Assert.Equal(single, float32 half)
+
+    [<Fact>]
+    let ``Single NaN and inifnity can be cast to Half NaN and infinity`` () =
+        Assert.True((Half System.Single.NaN).IsNaN)
+        Assert.True((Half System.Single.PositiveInfinity).IsPositiveInfinity)
+        Assert.True((Half System.Single.NegativeInfinity).IsNegativeInfinity)
index 968af0436fe385fa73efb7a1b50640215a17d66a..ee3974a9e399f8fb2cb13b280a0d09a1f2389e06 100644 (file)
@@ -69,6 +69,7 @@
     <Compile Include="Vector2Tests.fs" />
     <Compile Include="Vector3Tests.fs" />
     <Compile Include="Vector4Tests.fs" />
+    <Compile Include="HalfTests.fs" />
     <Content Include="App.config" />
     <None Include="paket.references" />
   </ItemGroup>