2017-03-19 14:35:46 +01:00
|
|
|
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 =
|
2017-03-19 14:35:46 +01:00
|
|
|
/// 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) =
|
2017-03-19 14:35:46 +01:00
|
|
|
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
|
|
|
|
2017-03-19 14:35:46 +01: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) =
|
2017-03-19 14:35:46 +01:00
|
|
|
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
|
|
|
|
2017-03-19 14:35:46 +01:00
|
|
|
[<Fact>]
|
2017-06-01 21:12:59 +02:00
|
|
|
let ``ApproximatelyEqual correctly approximates equality``() =
|
2017-03-19 14:35:46 +01:00
|
|
|
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
|
|
|
|
2017-03-19 14:35:46 +01:00
|
|
|
[<Fact>]
|
2017-06-01 21:12:59 +02:00
|
|
|
let ``ApproximatelyEqual reports very different values as non-equal even with high bit count``() =
|
2017-03-19 14:35:46 +01:00
|
|
|
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
|
|
|
|
2017-03-19 14:35:46 +01:00
|
|
|
[<Fact>]
|
2017-06-01 21:12:59 +02:00
|
|
|
let ``ApproximatelyEqual works with single zero value``() =
|
2017-03-19 14:35:46 +01:00
|
|
|
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
|
|
|
|
2017-03-19 14:35:46 +01:00
|
|
|
[<Fact>]
|
2017-06-01 21:12:59 +02:00
|
|
|
let ``ApproximatelyEqual works with both zero values``() =
|
2017-03-19 14:35:46 +01:00
|
|
|
let a = 0.0f
|
|
|
|
let b = 0.0f
|
|
|
|
Assert.Equal(a,b)
|
|
|
|
Assert.True(MathHelper.ApproximatelyEqual(a,b,0))
|