Opentk/tests/OpenTK.Tests/MathHelperTests.fs

56 lines
1.9 KiB
Forth
Raw Normal View History

namespace OpenTK.Tests
open Xunit
open FsCheck
open FsCheck.Xunit
open System
open OpenTK
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
2017-06-01 21:12:59 +02:00
module MathHelper =
/// This test ensures that approximately equal can never get it 'wrong' about the values.
[<Property>]
2017-06-01 21:12:59 +02:00
let ``ApproximatelyEqual is never incorrect`` (a : float32,b : float32,bits : int32) =
let clamped = max 0 (min bits 24)
let areApproxEqual = MathHelper.ApproximatelyEqual(a,b,clamped)
let areExactlyEqual = a = b
let isWrong = areExactlyEqual && not areApproxEqual
Assert.False(isWrong)
2017-06-01 21:12:59 +02:00
[<Property>]
2017-06-01 21:12:59 +02:00
let ``ApproximatelyEqual can return true if some values are not exactly equal`` (a : float32,b : float32,bits : int32) =
let clamped = max 0 (min bits 24)
let areApproxEqual = MathHelper.ApproximatelyEqual(a,b,clamped)
let areExactlyEqual = a = b
let isWrong = areExactlyEqual && not areApproxEqual
let p = new PropertyAttribute()
Assert.False(isWrong)
2017-06-01 21:12:59 +02:00
[<Fact>]
2017-06-01 21:12:59 +02:00
let ``ApproximatelyEqual correctly approximates equality``() =
let a = 0.000000001f
let b = 0.0000000010000001f
Assert.NotEqual(a,b)
[ 1..24 ] |> List.iter (fun i -> Assert.True(MathHelper.ApproximatelyEqual(a,b,i)))
2017-06-01 21:12:59 +02:00
[<Fact>]
2017-06-01 21:12:59 +02:00
let ``ApproximatelyEqual reports very different values as non-equal even with high bit count``() =
let a = 2.0f
let b = 1.0f
Assert.NotEqual(a,b)
Assert.False(MathHelper.ApproximatelyEqual(a,b,10))
2017-06-01 21:12:59 +02:00
[<Fact>]
2017-06-01 21:12:59 +02:00
let ``ApproximatelyEqual works with single zero value``() =
let a = 1.0f
let b = 0.0f
Assert.NotEqual(a,b)
Assert.False(MathHelper.ApproximatelyEqual(a,b,0))
2017-06-01 21:12:59 +02:00
[<Fact>]
2017-06-01 21:12:59 +02:00
let ``ApproximatelyEqual works with both zero values``() =
let a = 0.0f
let b = 0.0f
Assert.Equal(a,b)
Assert.True(MathHelper.ApproximatelyEqual(a,b,0))