Strip trailing spaces.

This commit is contained in:
Jarl Gullberg 2017-06-01 21:12:59 +02:00
parent c7298d8784
commit 7d846c4459
No known key found for this signature in database
GPG key ID: 750FF6F6BDA72D23
7 changed files with 752 additions and 752 deletions

View file

@ -7,25 +7,25 @@ open System
open OpenTK open OpenTK
[<AutoOpen>] [<AutoOpen>]
module private AssertHelpers = module private AssertHelpers =
[<Literal>] [<Literal>]
let private BitAccuracy = 6 let private BitAccuracy = 6
let approxEq a b = MathHelper.ApproximatelyEqual(a,b,BitAccuracy) let approxEq a b = MathHelper.ApproximatelyEqual(a,b,BitAccuracy)
/// We use a full type here instead of a module, as the overloading semantics are more suitable for our desired goal. /// We use a full type here instead of a module, as the overloading semantics are more suitable for our desired goal.
[<Sealed>] [<Sealed>]
type internal Assert = type internal Assert =
static member ApproximatelyEqual(a : Vector2,b : Vector2) = static member ApproximatelyEqual(a : Vector2,b : Vector2) =
if not <| approxEq a.X b.X && approxEq a.Y b.Y then raise <| new Xunit.Sdk.EqualException(a,b) if not <| approxEq a.X b.X && approxEq a.Y b.Y then raise <| new Xunit.Sdk.EqualException(a,b)
static member ApproximatelyEqual(a : Vector3,b : Vector3) = static member ApproximatelyEqual(a : Vector3,b : Vector3) =
if not <| approxEq a.X b.X && approxEq a.Y b.Y && approxEq a.Z b.Z then raise <| new Xunit.Sdk.EqualException(a,b) if not <| approxEq a.X b.X && approxEq a.Y b.Y && approxEq a.Z b.Z then raise <| new Xunit.Sdk.EqualException(a,b)
static member ApproximatelyEqual(a : Vector4,b : Vector4) = static member ApproximatelyEqual(a : Vector4,b : Vector4) =
if not <| approxEq a.X b.X && approxEq a.Y b.Y && approxEq a.Z b.Z && approxEq a.W b.W then if not <| approxEq a.X b.X && approxEq a.Y b.Y && approxEq a.Z b.Z && approxEq a.W b.W then
raise <| new Xunit.Sdk.EqualException(a,b) raise <| new Xunit.Sdk.EqualException(a,b)
static member ApproximatelyEqual(a : float32,b : float32) = static member ApproximatelyEqual(a : float32,b : float32) =
if not <| approxEq a b then raise <| new Xunit.Sdk.EqualException(a,b) if not <| approxEq a b then raise <| new Xunit.Sdk.EqualException(a,b)

View file

@ -7,62 +7,62 @@ open System
open OpenTK open OpenTK
[<AutoOpen>] [<AutoOpen>]
module private Generators = module private Generators =
let private isValidFloat f = not (Single.IsNaN f || Single.IsInfinity f || Single.IsInfinity (f * f) || f = Single.MinValue || f = Single.MaxValue ) let private isValidFloat f = not (Single.IsNaN f || Single.IsInfinity f || Single.IsInfinity (f * f) || f = Single.MinValue || f = Single.MaxValue )
let private isValidDouble d = not (Double.IsNaN d || Double.IsInfinity d || Double.IsInfinity (d * d)|| d = Double.MinValue || d = Double.MaxValue) let private isValidDouble d = not (Double.IsNaN d || Double.IsInfinity d || Double.IsInfinity (d * d)|| d = Double.MinValue || d = Double.MaxValue)
let singleArb = Arb.Default.Float32() |> Arb.toGen |> Gen.filter isValidFloat let singleArb = Arb.Default.Float32() |> Arb.toGen |> Gen.filter isValidFloat
let single = singleArb |> Arb.fromGen let single = singleArb |> Arb.fromGen
let double = let double =
Arb.Default.Float() |> Arb.toGen Arb.Default.Float() |> Arb.toGen
|> Gen.filter isValidDouble |> Gen.filter isValidDouble
|> Arb.fromGen |> Arb.fromGen
let vec2 = let vec2 =
singleArb singleArb
|> Gen.two |> Gen.two
|> Gen.map Vector2 |> Gen.map Vector2
|> Arb.fromGen |> Arb.fromGen
let vec3 = let vec3 =
singleArb singleArb
|> Gen.three |> Gen.three
|> Gen.map Vector3 |> Gen.map Vector3
|> Arb.fromGen |> Arb.fromGen
let vec4 = let vec4 =
singleArb singleArb
|> Gen.four |> Gen.four
|> Gen.map Vector4 |> Gen.map Vector4
|> Arb.fromGen |> Arb.fromGen
let quat = let quat =
singleArb singleArb
|> Gen.four |> Gen.four
|> Gen.map Quaternion |> Gen.map Quaternion
|> Arb.fromGen |> Arb.fromGen
let mat2 = let mat2 =
singleArb singleArb
|> Gen.four |> Gen.four
|> Gen.map Matrix2 |> Gen.map Matrix2
|> Arb.fromGen |> Arb.fromGen
let mat3 = let mat3 =
vec3 vec3
|> Arb.toGen |> Arb.toGen
|> Gen.three |> Gen.three
|> Gen.map Matrix3 |> Gen.map Matrix3
|> Arb.fromGen |> Arb.fromGen
let mat4 = let mat4 =
vec4 vec4
|> Arb.toGen |> Arb.toGen
|> Gen.four |> Gen.four
|> Gen.map Matrix4 |> Gen.map Matrix4
|> Arb.fromGen |> Arb.fromGen
type OpenTKGen = type OpenTKGen =
static member Single() = single static member Single() = single
static member float32() = single static member float32() = single
static member Double() = double static member Double() = double

View file

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

View file

@ -6,14 +6,14 @@ open FsCheck.Xunit
open System open System
open OpenTK open OpenTK
module Matrix4 = module Matrix4 =
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Constructors = module Constructors =
// //
[<Property>] [<Property>]
let ``Sixteen value constructor sets all components to the correct values`` (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) = let ``Sixteen value constructor sets all components to the correct values`` (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) =
let A = Matrix4(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) let A = Matrix4(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)
Assert.Equal(a, A.M11) Assert.Equal(a, A.M11)
Assert.Equal(b, A.M12) Assert.Equal(b, A.M12)
Assert.Equal(c, A.M13) Assert.Equal(c, A.M13)
@ -33,12 +33,12 @@ module Matrix4 =
Assert.Equal(n, A.M42) Assert.Equal(n, A.M42)
Assert.Equal(o, A.M43) Assert.Equal(o, A.M43)
Assert.Equal(p, A.M44) Assert.Equal(p, A.M44)
[<Property>] [<Property>]
let ``Matrix3 partial constructor sets all components to the correct values`` (a, b, c, d, e, f, g, h, i) = let ``Matrix3 partial constructor sets all components to the correct values`` (a, b, c, d, e, f, g, h, i) =
let B = Matrix3(a, b, c, d, e, f, g, h, i) let B = Matrix3(a, b, c, d, e, f, g, h, i)
let A = Matrix4(B) let A = Matrix4(B)
Assert.Equal(a, A.M11) Assert.Equal(a, A.M11)
Assert.Equal(b, A.M12) Assert.Equal(b, A.M12)
Assert.Equal(c, A.M13) Assert.Equal(c, A.M13)
@ -58,16 +58,16 @@ module Matrix4 =
Assert.Equal((float32)0, A.M42) Assert.Equal((float32)0, A.M42)
Assert.Equal((float32)0, A.M43) Assert.Equal((float32)0, A.M43)
Assert.Equal((float32)1, A.M44) Assert.Equal((float32)1, A.M44)
[<Property>] [<Property>]
let ``Four-vector4 constructor sets all components to the correct values`` (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) = let ``Four-vector4 constructor sets all components to the correct values`` (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) =
let v1 = Vector4(a, b, c, d) let v1 = Vector4(a, b, c, d)
let v2 = Vector4(e, f, g, h) let v2 = Vector4(e, f, g, h)
let v3 = Vector4(i, j, k, l) let v3 = Vector4(i, j, k, l)
let v4 = Vector4(m, n, o, p) let v4 = Vector4(m, n, o, p)
let A = Matrix4(v1, v2, v3, v4) let A = Matrix4(v1, v2, v3, v4)
Assert.Equal(a, A.M11) Assert.Equal(a, A.M11)
Assert.Equal(b, A.M12) Assert.Equal(b, A.M12)
Assert.Equal(c, A.M13) Assert.Equal(c, A.M13)
@ -87,52 +87,52 @@ module Matrix4 =
Assert.Equal(n, A.M42) Assert.Equal(n, A.M42)
Assert.Equal(o, A.M43) Assert.Equal(o, A.M43)
Assert.Equal(p, A.M44) Assert.Equal(p, A.M44)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Equality = module Equality =
// //
[<Property>] [<Property>]
let ``Two matrices with identical values are equal`` (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) = let ``Two matrices with identical values are equal`` (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) =
let A = Matrix4(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) let A = Matrix4(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)
let B = Matrix4(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) let B = Matrix4(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)
let equality = A = B let equality = A = B
Assert.True(equality) Assert.True(equality)
[<Property>] [<Property>]
let ``A matrix is not equal to an object which is not a matrix`` (a : Matrix4, b : Vector3) = let ``A matrix is not equal to an object which is not a matrix`` (a : Matrix4, b : Vector3) =
Assert.False(a.Equals(b)) Assert.False(a.Equals(b))
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Multiplication = module Multiplication =
// //
[<Property>] [<Property>]
let ``Matrix multiplication is done by row/column multiplication and summation`` (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) = let ``Matrix multiplication is done by row/column multiplication and summation`` (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) =
let A = Matrix4(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) let A = Matrix4(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)
let B = Matrix4(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) let B = Matrix4(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)
let R11 = a*a + b*e + c*i + d*m let R11 = a*a + b*e + c*i + d*m
let R12 = a*b + b*f + c*j + d*n let R12 = a*b + b*f + c*j + d*n
let R13 = a*c + b*g + c*k + d*o let R13 = a*c + b*g + c*k + d*o
let R14 = a*d + b*h + c*l + d*p let R14 = a*d + b*h + c*l + d*p
let R21 = e*a + f*e + g*i + h*m let R21 = e*a + f*e + g*i + h*m
let R22 = e*b + f*f + g*j + h*n let R22 = e*b + f*f + g*j + h*n
let R23 = e*c + f*g + g*k + h*o let R23 = e*c + f*g + g*k + h*o
let R24 = e*d + f*h + g*l + h*p let R24 = e*d + f*h + g*l + h*p
let R31 = i*a + j*e + k*i + l*m let R31 = i*a + j*e + k*i + l*m
let R32 = i*b + j*f + k*j + l*n let R32 = i*b + j*f + k*j + l*n
let R33 = i*c + j*g + k*k + l*o let R33 = i*c + j*g + k*k + l*o
let R34 = i*d + j*h + k*l + l*p let R34 = i*d + j*h + k*l + l*p
let R41 = m*a + n*e + o*i + p*m let R41 = m*a + n*e + o*i + p*m
let R42 = m*b + n*f + o*j + p*n let R42 = m*b + n*f + o*j + p*n
let R43 = m*c + n*g + o*k + p*o let R43 = m*c + n*g + o*k + p*o
let R44 = m*d + n*h + o*l + p*p let R44 = m*d + n*h + o*l + p*p
let AB = A*B let AB = A*B
Assert.Equal(R11, AB.M11) Assert.Equal(R11, AB.M11)
Assert.Equal(R12, AB.M12) Assert.Equal(R12, AB.M12)
Assert.Equal(R13, AB.M13) Assert.Equal(R13, AB.M13)
@ -152,66 +152,66 @@ module Matrix4 =
Assert.Equal(R42, AB.M42) Assert.Equal(R42, AB.M42)
Assert.Equal(R43, AB.M43) Assert.Equal(R43, AB.M43)
Assert.Equal(R44, AB.M44) Assert.Equal(R44, AB.M44)
[<Property>] [<Property>]
let ``Matrix multiplication by scalar is the same as row multiplication by scalar`` (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, scalar : float32) = let ``Matrix multiplication by scalar is the same as row multiplication by scalar`` (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, scalar : float32) =
let A = Matrix4(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) let A = Matrix4(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)
let R1 = Vector4(a, b, c, d) * scalar let R1 = Vector4(a, b, c, d) * scalar
let R2 = Vector4(e, f, g, h) * scalar let R2 = Vector4(e, f, g, h) * scalar
let R3 = Vector4(i, j, k, l) * scalar let R3 = Vector4(i, j, k, l) * scalar
let R4 = Vector4(m, n, o, p) * scalar let R4 = Vector4(m, n, o, p) * scalar
let AScaled = A * scalar let AScaled = A * scalar
Assert.Equal(R1, AScaled.Row0) Assert.Equal(R1, AScaled.Row0)
Assert.Equal(R2, AScaled.Row1) Assert.Equal(R2, AScaled.Row1)
Assert.Equal(R3, AScaled.Row2) Assert.Equal(R3, AScaled.Row2)
Assert.Equal(R4, AScaled.Row3) Assert.Equal(R4, AScaled.Row3)
[<Property>] [<Property>]
let ``Static method matrix multiplication by scalar is the same as row multiplication by scalar`` (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, scalar : float32) = let ``Static method matrix multiplication by scalar is the same as row multiplication by scalar`` (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, scalar : float32) =
let A = Matrix4(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) let A = Matrix4(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)
let R1 = Vector4(a, b, c, d) * scalar let R1 = Vector4(a, b, c, d) * scalar
let R2 = Vector4(e, f, g, h) * scalar let R2 = Vector4(e, f, g, h) * scalar
let R3 = Vector4(i, j, k, l) * scalar let R3 = Vector4(i, j, k, l) * scalar
let R4 = Vector4(m, n, o, p) * scalar let R4 = Vector4(m, n, o, p) * scalar
let AScaled = Matrix4.Mult(A, scalar) let AScaled = Matrix4.Mult(A, scalar)
Assert.Equal(R1, AScaled.Row0) Assert.Equal(R1, AScaled.Row0)
Assert.Equal(R2, AScaled.Row1) Assert.Equal(R2, AScaled.Row1)
Assert.Equal(R3, AScaled.Row2) Assert.Equal(R3, AScaled.Row2)
Assert.Equal(R4, AScaled.Row3) Assert.Equal(R4, AScaled.Row3)
[<Property>] [<Property>]
let ``Static method matrix multiplication by reference by scalar is the same as row multiplication by scalar`` (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, scalar : float32) = let ``Static method matrix multiplication by reference by scalar is the same as row multiplication by scalar`` (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, scalar : float32) =
let A = Matrix4(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) let A = Matrix4(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)
let R1 = Vector4(a, b, c, d) * scalar let R1 = Vector4(a, b, c, d) * scalar
let R2 = Vector4(e, f, g, h) * scalar let R2 = Vector4(e, f, g, h) * scalar
let R3 = Vector4(i, j, k, l) * scalar let R3 = Vector4(i, j, k, l) * scalar
let R4 = Vector4(m, n, o, p) * scalar let R4 = Vector4(m, n, o, p) * scalar
let AScaled = Matrix4.Mult(ref A, scalar) let AScaled = Matrix4.Mult(ref A, scalar)
Assert.Equal(R1, AScaled.Row0) Assert.Equal(R1, AScaled.Row0)
Assert.Equal(R2, AScaled.Row1) Assert.Equal(R2, AScaled.Row1)
Assert.Equal(R3, AScaled.Row2) Assert.Equal(R3, AScaled.Row2)
Assert.Equal(R4, AScaled.Row3) Assert.Equal(R4, AScaled.Row3)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Addition = module Addition =
// //
[<Property>] [<Property>]
let ``Matrix addition adds corresponding components`` (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) = let ``Matrix addition adds corresponding components`` (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) =
let A = Matrix4(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) let A = Matrix4(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)
let B = Matrix4(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) let B = Matrix4(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)
let sum = A + B let sum = A + B
Assert.Equal(a + a, sum.M11) Assert.Equal(a + a, sum.M11)
Assert.Equal(b + b, sum.M12) Assert.Equal(b + b, sum.M12)
Assert.Equal(c + c, sum.M13) Assert.Equal(c + c, sum.M13)
@ -231,17 +231,17 @@ module Matrix4 =
Assert.Equal(n + n, sum.M42) Assert.Equal(n + n, sum.M42)
Assert.Equal(o + o, sum.M43) Assert.Equal(o + o, sum.M43)
Assert.Equal(p + p, sum.M44) Assert.Equal(p + p, sum.M44)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Subtraction = module Subtraction =
// //
[<Property>] [<Property>]
let ``Matrix subtraction subtracts corresponding components`` (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) = let ``Matrix subtraction subtracts corresponding components`` (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) =
let A = Matrix4(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) let A = Matrix4(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)
let B = Matrix4(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) let B = Matrix4(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)
let sub = A - B let sub = A - B
Assert.Equal(a - a, sub.M11) Assert.Equal(a - a, sub.M11)
Assert.Equal(b - b, sub.M12) Assert.Equal(b - b, sub.M12)
Assert.Equal(c - c, sub.M13) Assert.Equal(c - c, sub.M13)
@ -261,34 +261,34 @@ module Matrix4 =
Assert.Equal(n - n, sub.M42) Assert.Equal(n - n, sub.M42)
Assert.Equal(o - o, sub.M43) Assert.Equal(o - o, sub.M43)
Assert.Equal(p - p, sub.M44) Assert.Equal(p - p, sub.M44)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Indexing = module Indexing =
// //
[<Property>] [<Property>]
let ``Matrix set indexing sets correct components`` (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) = let ``Matrix set indexing sets correct components`` (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) =
let mutable A = Matrix4() let mutable A = Matrix4()
A.[0, 0] <- a A.[0, 0] <- a
A.[0, 1] <- b A.[0, 1] <- b
A.[0, 2] <- c A.[0, 2] <- c
A.[0, 3] <- d A.[0, 3] <- d
A.[1, 0] <- e A.[1, 0] <- e
A.[1, 1] <- f A.[1, 1] <- f
A.[1, 2] <- g A.[1, 2] <- g
A.[1, 3] <- h A.[1, 3] <- h
A.[2, 0] <- i A.[2, 0] <- i
A.[2, 1] <- j A.[2, 1] <- j
A.[2, 2] <- k A.[2, 2] <- k
A.[2, 3] <- l A.[2, 3] <- l
A.[3, 0] <- m A.[3, 0] <- m
A.[3, 1] <- n A.[3, 1] <- n
A.[3, 2] <- o A.[3, 2] <- o
A.[3, 3] <- p A.[3, 3] <- p
Assert.Equal(a, A.M11) Assert.Equal(a, A.M11)
Assert.Equal(b, A.M12) Assert.Equal(b, A.M12)
Assert.Equal(c, A.M13) Assert.Equal(c, A.M13)
@ -308,11 +308,11 @@ module Matrix4 =
Assert.Equal(n, A.M42) Assert.Equal(n, A.M42)
Assert.Equal(o, A.M43) Assert.Equal(o, A.M43)
Assert.Equal(p, A.M44) Assert.Equal(p, A.M44)
[<Property>] [<Property>]
let ``Matrix get indexing accesses the correct components`` (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) = let ``Matrix get indexing accesses the correct components`` (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) =
let A = Matrix4(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) let A = Matrix4(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)
Assert.Equal(a, A.[0, 0]) Assert.Equal(a, A.[0, 0])
Assert.Equal(b, A.[0, 1]) Assert.Equal(b, A.[0, 1])
Assert.Equal(c, A.[0, 2]) Assert.Equal(c, A.[0, 2])
@ -341,7 +341,7 @@ module Matrix4 =
(fun() -> a.[-1, -2] <- x) |> Assert.Throws<IndexOutOfRangeException> |> ignore (fun() -> a.[-1, -2] <- x) |> Assert.Throws<IndexOutOfRangeException> |> ignore
[<Property>] [<Property>]
let ``Indexed get operator throws exception for negative indices`` (a : Matrix4) = let ``Indexed get operator throws exception for negative indices`` (a : Matrix4) =
(fun() -> a.[-1, 2] |> ignore) |> Assert.Throws<IndexOutOfRangeException> |> ignore (fun() -> a.[-1, 2] |> ignore) |> Assert.Throws<IndexOutOfRangeException> |> ignore
(fun() -> a.[1, -2] |> ignore) |> Assert.Throws<IndexOutOfRangeException> |> ignore (fun() -> a.[1, -2] |> ignore) |> Assert.Throws<IndexOutOfRangeException> |> ignore
(fun() -> a.[-1, -2] |> ignore) |> Assert.Throws<IndexOutOfRangeException> |> ignore (fun() -> a.[-1, -2] |> ignore) |> Assert.Throws<IndexOutOfRangeException> |> ignore
@ -352,25 +352,25 @@ module Matrix4 =
(fun() -> b.[5, 2] <- x) |> Assert.Throws<IndexOutOfRangeException> |> ignore (fun() -> b.[5, 2] <- x) |> Assert.Throws<IndexOutOfRangeException> |> ignore
(fun() -> b.[1, 6] <- x) |> Assert.Throws<IndexOutOfRangeException> |> ignore (fun() -> b.[1, 6] <- x) |> Assert.Throws<IndexOutOfRangeException> |> ignore
(fun() -> b.[7, 12] <- x) |> Assert.Throws<IndexOutOfRangeException> |> ignore (fun() -> b.[7, 12] <- x) |> Assert.Throws<IndexOutOfRangeException> |> ignore
[<Property>] [<Property>]
let ``Indexed get operator throws exception for large indices`` (a : Matrix4) = let ``Indexed get operator throws exception for large indices`` (a : Matrix4) =
(fun() -> a.[5, 2] |> ignore) |> Assert.Throws<IndexOutOfRangeException> |> ignore (fun() -> a.[5, 2] |> ignore) |> Assert.Throws<IndexOutOfRangeException> |> ignore
(fun() -> a.[1, 6] |> ignore) |> Assert.Throws<IndexOutOfRangeException> |> ignore (fun() -> a.[1, 6] |> ignore) |> Assert.Throws<IndexOutOfRangeException> |> ignore
(fun() -> a.[7, 12] |> ignore) |> Assert.Throws<IndexOutOfRangeException> |> ignore (fun() -> a.[7, 12] |> ignore) |> Assert.Throws<IndexOutOfRangeException> |> ignore
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module ``Row and column properties`` = module ``Row and column properties`` =
// //
[<Property>] [<Property>]
let ``Matrix row properties return the correct components`` (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) = let ``Matrix row properties return the correct components`` (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) =
let A = Matrix4(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) let A = Matrix4(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)
let R0 = A.Row0 let R0 = A.Row0
let R1 = A.Row1 let R1 = A.Row1
let R2 = A.Row2 let R2 = A.Row2
let R3 = A.Row3 let R3 = A.Row3
Assert.Equal(a, R0.X) Assert.Equal(a, R0.X)
Assert.Equal(b, R0.Y) Assert.Equal(b, R0.Y)
Assert.Equal(c, R0.Z) Assert.Equal(c, R0.Z)

View file

@ -7,18 +7,18 @@ open System
open System.Runtime.InteropServices open System.Runtime.InteropServices
open OpenTK open OpenTK
module Vector2 = module Vector2 =
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Constructors = module Constructors =
// //
[<Property>] [<Property>]
let ``Single value constructor sets all components to the same value`` (f : float32) = let ``Single value constructor sets all components to the same value`` (f : float32) =
let v = Vector2(f) let v = Vector2(f)
Assert.Equal(f,v.X) Assert.Equal(f,v.X)
Assert.Equal(f,v.Y) Assert.Equal(f,v.Y)
[<Property>] [<Property>]
let ``Two value constructor sets all components correctly`` (x,y) = let ``Two value constructor sets all components correctly`` (x,y) =
let v = Vector2(x,y) let v = Vector2(x,y)
Assert.Equal(x,v.X) Assert.Equal(x,v.X)
Assert.Equal(y,v.Y) Assert.Equal(y,v.Y)
@ -29,269 +29,269 @@ module Vector2 =
[<Property>] [<Property>]
let ``Clamping one vector between two other vectors clamps all components between corresponding components`` (a : Vector2, b : Vector2, w : Vector2) = let ``Clamping one vector between two other vectors clamps all components between corresponding components`` (a : Vector2, b : Vector2, w : Vector2) =
let res = Vector2.Clamp(w, a, b) let res = Vector2.Clamp(w, a, b)
let expX = if w.X < a.X then a.X else if w.X > b.X then b.X else w.X let expX = if w.X < a.X then a.X else if w.X > b.X then b.X else w.X
let expY = if w.Y < a.Y then a.Y else if w.Y > b.Y then b.Y else w.Y let expY = if w.Y < a.Y then a.Y else if w.Y > b.Y then b.Y else w.Y
Assert.Equal(expX, res.X) Assert.Equal(expX, res.X)
Assert.Equal(expY, res.Y) Assert.Equal(expY, res.Y)
[<Property>] [<Property>]
let ``Clamping one vector between two other vectors by reference clamps all components`` (a : Vector2, b : Vector2, w : Vector2) = let ``Clamping one vector between two other vectors by reference clamps all components`` (a : Vector2, b : Vector2, w : Vector2) =
let res = Vector2.Clamp(ref w, ref a, ref b) let res = Vector2.Clamp(ref w, ref a, ref b)
let expX = if w.X < a.X then a.X else if w.X > b.X then b.X else w.X let expX = if w.X < a.X then a.X else if w.X > b.X then b.X else w.X
let expY = if w.Y < a.Y then a.Y else if w.Y > b.Y then b.Y else w.Y let expY = if w.Y < a.Y then a.Y else if w.Y > b.Y then b.Y else w.Y
Assert.Equal(expX, res.X) Assert.Equal(expX, res.X)
Assert.Equal(expY, res.Y) Assert.Equal(expY, res.Y)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Length = module Length =
// //
[<Property>] [<Property>]
let ``Length is always >= 0`` (a : Vector2) = let ``Length is always >= 0`` (a : Vector2) =
// //
Assert.True(a.Length >= 0.0f) Assert.True(a.Length >= 0.0f)
[<Property>] [<Property>]
let ``Length follows the pythagorean theorem`` (a, b) = let ``Length follows the pythagorean theorem`` (a, b) =
let v = Vector2(a, b) let v = Vector2(a, b)
let l = System.Math.Sqrt((float)(a * a + b * b)) let l = System.Math.Sqrt((float)(a * a + b * b))
Assert.Equal((float32)l, v.Length) Assert.Equal((float32)l, v.Length)
[<Property>] [<Property>]
let ``Fast length method works`` (a, b) = let ``Fast length method works`` (a, b) =
let v = Vector2(a, b) let v = Vector2(a, b)
let l = 1.0f / MathHelper.InverseSqrtFast(a * a + b * b) let l = 1.0f / MathHelper.InverseSqrtFast(a * a + b * b)
Assert.Equal(l, v.LengthFast) Assert.Equal(l, v.LengthFast)
[<Property>] [<Property>]
let ``Length squared method works`` (a, b) = let ``Length squared method works`` (a, b) =
let v = Vector2(a, b) let v = Vector2(a, b)
let lsq = a * a + b * b let lsq = a * a + b * b
Assert.Equal(lsq, v.LengthSquared) Assert.Equal(lsq, v.LengthSquared)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module ``Unit vectors and perpendicularity`` = module ``Unit vectors and perpendicularity`` =
// //
[<Property>] [<Property>]
let ``Perpendicular vector to the right is correct`` (a, b) = let ``Perpendicular vector to the right is correct`` (a, b) =
let v = Vector2(a, b) let v = Vector2(a, b)
let perp = Vector2(b, -a) let perp = Vector2(b, -a)
Assert.Equal(perp, v.PerpendicularRight) Assert.Equal(perp, v.PerpendicularRight)
[<Property>] [<Property>]
let ``Perpendicular vector to the left is correct`` (a, b) = let ``Perpendicular vector to the left is correct`` (a, b) =
let v = Vector2(a, b) let v = Vector2(a, b)
let perp = Vector2(-b, a) let perp = Vector2(-b, a)
Assert.Equal(perp, v.PerpendicularLeft) Assert.Equal(perp, v.PerpendicularLeft)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Indexing = module Indexing =
// //
[<Property>] [<Property>]
let ``Index operator accesses the correct components`` (x, y) = let ``Index operator accesses the correct components`` (x, y) =
let v = Vector2(x, y) let v = Vector2(x, y)
Assert.Equal(x, v.[0]) Assert.Equal(x, v.[0])
Assert.Equal(y, v.[1]) Assert.Equal(y, v.[1])
[<Property>] [<Property>]
let ``Indexed set operator throws exception for negative indices`` (x, y) = let ``Indexed set operator throws exception for negative indices`` (x, y) =
let mutable v = Vector2(x, y) let mutable v = Vector2(x, y)
(fun() -> v.[-1] <- x) |> Assert.Throws<IndexOutOfRangeException> |> ignore (fun() -> v.[-1] <- x) |> Assert.Throws<IndexOutOfRangeException> |> ignore
[<Property>] [<Property>]
let ``Indexed get operator throws exception for negative indices`` (x, y) = let ``Indexed get operator throws exception for negative indices`` (x, y) =
let mutable v = Vector2(x, y) let mutable v = Vector2(x, y)
(fun() -> v.[-1] |> ignore) |> Assert.Throws<IndexOutOfRangeException> |> ignore (fun() -> v.[-1] |> ignore) |> Assert.Throws<IndexOutOfRangeException> |> ignore
[<Property>] [<Property>]
let ``Indexed set operator throws exception for large indices`` (x, y) = let ``Indexed set operator throws exception for large indices`` (x, y) =
let mutable v = Vector2(x, y) let mutable v = Vector2(x, y)
(fun() -> v.[2] <- x) |> Assert.Throws<IndexOutOfRangeException> |> ignore (fun() -> v.[2] <- x) |> Assert.Throws<IndexOutOfRangeException> |> ignore
[<Property>] [<Property>]
let ``Indexed get operator throws exception for large indices`` (x, y) = let ``Indexed get operator throws exception for large indices`` (x, y) =
let mutable v = Vector2(x, y) let mutable v = Vector2(x, y)
(fun() -> v.[2] |> ignore) |> Assert.Throws<IndexOutOfRangeException> |> ignore (fun() -> v.[2] |> ignore) |> Assert.Throws<IndexOutOfRangeException> |> ignore
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module ``Simple Properties`` = module ``Simple Properties`` =
// //
[<Property>] [<Property>]
let ``Vector equality is by component`` (a : Vector2,b : Vector2) = let ``Vector equality is by component`` (a : Vector2,b : Vector2) =
// //
Assert.Equal((a.X = b.X && a.Y = b.Y),(a = b)) Assert.Equal((a.X = b.X && a.Y = b.Y),(a = b))
[<Property>] [<Property>]
let ``Vector length is always >= 0`` (a : Vector2) = let ``Vector length is always >= 0`` (a : Vector2) =
// //
Assert.True(a.Length >= 0.0f) Assert.True(a.Length >= 0.0f)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Addition = module Addition =
// //
[<Property>] [<Property>]
let ``Vector addition is the same as component addition`` (a : Vector2,b : Vector2) = let ``Vector addition is the same as component addition`` (a : Vector2,b : Vector2) =
let c = a + b let c = a + b
Assert.ApproximatelyEqual(a.X + b.X,c.X) Assert.ApproximatelyEqual(a.X + b.X,c.X)
Assert.ApproximatelyEqual(a.Y + b.Y,c.Y) Assert.ApproximatelyEqual(a.Y + b.Y,c.Y)
[<Property>] [<Property>]
let ``Vector addition is commutative`` (a : Vector2,b : Vector2) = let ``Vector addition is commutative`` (a : Vector2,b : Vector2) =
let c = a + b let c = a + b
let c2 = b + a let c2 = b + a
Assert.ApproximatelyEqual(c,c2) Assert.ApproximatelyEqual(c,c2)
[<Property>] [<Property>]
let ``Vector addition is associative`` (a : Vector2,b : Vector2,c : Vector2) = let ``Vector addition is associative`` (a : Vector2,b : Vector2,c : Vector2) =
let r1 = (a + b) + c let r1 = (a + b) + c
let r2 = a + (b + c) let r2 = a + (b + c)
Assert.ApproximatelyEqual(r1,r2) Assert.ApproximatelyEqual(r1,r2)
[<Property>] [<Property>]
let ``Static Vector2 addition method is the same as component addition`` (a : Vector2, b : Vector2) = let ``Static Vector2 addition method is the same as component addition`` (a : Vector2, b : Vector2) =
let v1 = Vector2(a.X + b.X, a.Y + b.Y) let v1 = Vector2(a.X + b.X, a.Y + b.Y)
let sum = Vector2.Add(a, b) let sum = Vector2.Add(a, b)
Assert.ApproximatelyEqual(v1, sum) Assert.ApproximatelyEqual(v1, sum)
[<Property>] [<Property>]
let ``Static Vector2 addition method by reference is the same as component addition`` (a : Vector2, b : Vector2) = let ``Static Vector2 addition method by reference is the same as component addition`` (a : Vector2, b : Vector2) =
let v1 = Vector2(a.X + b.X, a.Y + b.Y) let v1 = Vector2(a.X + b.X, a.Y + b.Y)
let sum = Vector2.Add(ref a, ref b) let sum = Vector2.Add(ref a, ref b)
Assert.ApproximatelyEqual(v1, sum) Assert.ApproximatelyEqual(v1, sum)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Multiplication = module Multiplication =
// //
[<Property>] [<Property>]
let ``Vector2 multiplication is the same as component multiplication`` (a : Vector2, b : Vector2) = let ``Vector2 multiplication is the same as component multiplication`` (a : Vector2, b : Vector2) =
let c = a * b let c = a * b
Assert.Equal(a.X * b.X,c.X) Assert.Equal(a.X * b.X,c.X)
Assert.Equal(a.Y * b.Y,c.Y) Assert.Equal(a.Y * b.Y,c.Y)
[<Property>] [<Property>]
let ``Vector2 multiplication is commutative`` (a : Vector2, b : Vector2) = let ``Vector2 multiplication is commutative`` (a : Vector2, b : Vector2) =
let r1 = a * b let r1 = a * b
let r2 = b * a let r2 = b * a
Assert.Equal(r1,r2) Assert.Equal(r1,r2)
[<Property>] [<Property>]
let ``Left-handed Vector2-scalar multiplication is the same as component-scalar multiplication`` (a : Vector2, f : float32) = let ``Left-handed Vector2-scalar multiplication is the same as component-scalar multiplication`` (a : Vector2, f : float32) =
let r = a * f let r = a * f
Assert.Equal(a.X * f,r.X) Assert.Equal(a.X * f,r.X)
Assert.Equal(a.Y * f,r.Y) Assert.Equal(a.Y * f,r.Y)
[<Property>] [<Property>]
let ``Right-handed Vector2-scalar multiplication is the same as component-scalar multiplication`` (a : Vector2, f : float32) = let ``Right-handed Vector2-scalar multiplication is the same as component-scalar multiplication`` (a : Vector2, f : float32) =
let r = f * a let r = f * a
Assert.Equal(a.X * f,r.X) Assert.Equal(a.X * f,r.X)
Assert.Equal(a.Y * f,r.Y) Assert.Equal(a.Y * f,r.Y)
[<Property>] [<Property>]
let ``Static Vector2 multiplication method is the same as component multiplication`` (a : Vector2, b : Vector2) = let ``Static Vector2 multiplication method is the same as component multiplication`` (a : Vector2, b : Vector2) =
let v1 = Vector2(a.X * b.X, a.Y * b.Y) let v1 = Vector2(a.X * b.X, a.Y * b.Y)
let sum = Vector2.Multiply(a, b) let sum = Vector2.Multiply(a, b)
Assert.ApproximatelyEqual(v1, sum) Assert.ApproximatelyEqual(v1, sum)
[<Property>] [<Property>]
let ``Static Vector2 multiplication method by reference is the same as component multiplication`` (a : Vector2, b : Vector2) = let ``Static Vector2 multiplication method by reference is the same as component multiplication`` (a : Vector2, b : Vector2) =
let v1 = Vector2(a.X * b.X, a.Y * b.Y) let v1 = Vector2(a.X * b.X, a.Y * b.Y)
let sum = Vector2.Multiply(ref a, ref b) let sum = Vector2.Multiply(ref a, ref b)
Assert.ApproximatelyEqual(v1, sum) Assert.ApproximatelyEqual(v1, sum)
[<Property>] [<Property>]
let ``Static method Vector2-scalar multiplication is the same as component-scalar multiplication`` (a : Vector2, f : float32) = let ``Static method Vector2-scalar multiplication is the same as component-scalar multiplication`` (a : Vector2, f : float32) =
let r = Vector2.Multiply(a, f) let r = Vector2.Multiply(a, f)
Assert.Equal(a.X * f,r.X) Assert.Equal(a.X * f,r.X)
Assert.Equal(a.Y * f,r.Y) Assert.Equal(a.Y * f,r.Y)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Subtraction = module Subtraction =
// //
[<Property>] [<Property>]
let ``Vector2 subtraction is the same as component subtraction`` (a : Vector2, b : Vector2) = let ``Vector2 subtraction is the same as component subtraction`` (a : Vector2, b : Vector2) =
let c = a - b let c = a - b
Assert.Equal(a.X - b.X,c.X) Assert.Equal(a.X - b.X,c.X)
Assert.Equal(a.Y - b.Y,c.Y) Assert.Equal(a.Y - b.Y,c.Y)
[<Property>] [<Property>]
let ``Static Vector2 subtraction method is the same as component addition`` (a : Vector2, b : Vector2) = let ``Static Vector2 subtraction method is the same as component addition`` (a : Vector2, b : Vector2) =
let v1 = Vector2(a.X - b.X, a.Y - b.Y) let v1 = Vector2(a.X - b.X, a.Y - b.Y)
let sum = Vector2.Subtract(a, b) let sum = Vector2.Subtract(a, b)
Assert.ApproximatelyEqual(v1, sum) Assert.ApproximatelyEqual(v1, sum)
[<Property>] [<Property>]
let ``Static Vector2 subtraction method by reference is the same as component addition`` (a : Vector2, b : Vector2) = let ``Static Vector2 subtraction method by reference is the same as component addition`` (a : Vector2, b : Vector2) =
let v1 = Vector2(a.X - b.X, a.Y - b.Y) let v1 = Vector2(a.X - b.X, a.Y - b.Y)
let sum = Vector2.Subtract(ref a, ref b) let sum = Vector2.Subtract(ref a, ref b)
Assert.ApproximatelyEqual(v1, sum) Assert.ApproximatelyEqual(v1, sum)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Division = module Division =
// //
[<Property>] [<Property>]
let ``Vector2-float division is the same as component-float division`` (a : Vector2, f : float32) = let ``Vector2-float division is the same as component-float division`` (a : Vector2, f : float32) =
let r = a / f let r = a / f
Assert.ApproximatelyEqual(a.X / f,r.X) Assert.ApproximatelyEqual(a.X / f,r.X)
Assert.ApproximatelyEqual(a.Y / f,r.Y) Assert.ApproximatelyEqual(a.Y / f,r.Y)
[<Property>] [<Property>]
let ``Static Vector2-Vector2 division method is the same as component division`` (a : Vector2, b : Vector2) = let ``Static Vector2-Vector2 division method is the same as component division`` (a : Vector2, b : Vector2) =
let v1 = Vector2(a.X / b.X, a.Y / b.Y) let v1 = Vector2(a.X / b.X, a.Y / b.Y)
let sum = Vector2.Divide(a, b) let sum = Vector2.Divide(a, b)
Assert.ApproximatelyEqual(v1, sum) Assert.ApproximatelyEqual(v1, sum)
[<Property>] [<Property>]
let ``Static Vector2-Vector2 divison method by reference `` (a : Vector2, b : Vector2) = let ``Static Vector2-Vector2 divison method by reference `` (a : Vector2, b : Vector2) =
let v1 = Vector2(a.X / b.X, a.Y / b.Y) let v1 = Vector2(a.X / b.X, a.Y / b.Y)
let sum = Vector2.Divide(ref a, ref b) let sum = Vector2.Divide(ref a, ref b)
Assert.ApproximatelyEqual(v1, sum) Assert.ApproximatelyEqual(v1, sum)
[<Property>] [<Property>]
let ``Static Vector2-scalar division method is the same as component division`` (a : Vector2, b : float32) = let ``Static Vector2-scalar division method is the same as component division`` (a : Vector2, b : float32) =
let v1 = Vector2(a.X / b, a.Y / b) let v1 = Vector2(a.X / b, a.Y / b)
let sum = Vector2.Divide(a, b) let sum = Vector2.Divide(a, b)
Assert.ApproximatelyEqual(v1, sum) Assert.ApproximatelyEqual(v1, sum)
[<Property>] [<Property>]
let ``Static Vector2-scalar divison method by reference is the same as component division`` (a : Vector2, b : float32) = let ``Static Vector2-scalar divison method by reference is the same as component division`` (a : Vector2, b : float32) =
let v1 = Vector2(a.X / b, a.Y / b) let v1 = Vector2(a.X / b, a.Y / b)
let sum = Vector2.Divide(ref a, b) let sum = Vector2.Divide(ref a, b)
Assert.ApproximatelyEqual(v1, sum) Assert.ApproximatelyEqual(v1, sum)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
@ -303,7 +303,7 @@ module Vector2 =
let vNeg = -v let vNeg = -v
Assert.Equal(-x, vNeg.X) Assert.Equal(-x, vNeg.X)
Assert.Equal(-y, vNeg.Y) Assert.Equal(-y, vNeg.Y)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Equality = module Equality =
// //
@ -312,29 +312,29 @@ module Vector2 =
let v1 = Vector2(x, y) let v1 = Vector2(x, y)
let v2 = Vector2(x, y) let v2 = Vector2(x, y)
let equality = v1 = v2 let equality = v1 = v2
Assert.True(equality) Assert.True(equality)
[<Property>] [<Property>]
let ``Vector inequality operator is by component`` (x, y) = let ``Vector inequality operator is by component`` (x, y) =
let v1 = Vector2(x, y) let v1 = Vector2(x, y)
let v2 = Vector2(x + (float32)1 , y + (float32)1) let v2 = Vector2(x + (float32)1 , y + (float32)1)
let inequality = v1 <> v2 let inequality = v1 <> v2
Assert.True(inequality) Assert.True(inequality)
[<Property>] [<Property>]
let ``Vector equality method is by component`` (x, y) = let ``Vector equality method is by component`` (x, y) =
let v1 = Vector2(x, y) let v1 = Vector2(x, y)
let v2 = Vector2(x, y) let v2 = Vector2(x, y)
let notVector = Matrix2() let notVector = Matrix2()
let equality = v1.Equals(v2) let equality = v1.Equals(v2)
let inequalityByOtherType = v1.Equals(notVector) let inequalityByOtherType = v1.Equals(notVector)
Assert.True(equality) Assert.True(equality)
Assert.False(inequalityByOtherType) Assert.False(inequalityByOtherType)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Swizzling = module Swizzling =
// //
@ -342,7 +342,7 @@ module Vector2 =
let ``Vector swizzling returns the correct composites`` (x, y) = let ``Vector swizzling returns the correct composites`` (x, y) =
let v1 = Vector2(x, y) let v1 = Vector2(x, y)
let v2 = Vector2(y, x) let v2 = Vector2(y, x)
let v1yx = v1.Yx; let v1yx = v1.Yx;
Assert.Equal(v2, v1yx); Assert.Equal(v2, v1yx);
@ -353,76 +353,76 @@ module Vector2 =
let ``Linear interpolation is by component`` (a : Vector2, b : Vector2, q) = let ``Linear interpolation is by component`` (a : Vector2, b : Vector2, q) =
let blend = q let blend = q
let rX = blend * (b.X - a.X) + a.X let rX = blend * (b.X - a.X) + a.X
let rY = blend * (b.Y - a.Y) + a.Y let rY = blend * (b.Y - a.Y) + a.Y
let vExp = Vector2(rX, rY) let vExp = Vector2(rX, rY)
Assert.Equal(vExp, Vector2.Lerp(a, b, q)) Assert.Equal(vExp, Vector2.Lerp(a, b, q))
let vRes = Vector2.Lerp(ref a, ref b, q) let vRes = Vector2.Lerp(ref a, ref b, q)
Assert.Equal(vExp, vRes) Assert.Equal(vExp, vRes)
[<Property>] [<Property>]
let ``Barycentric interpolation follows the barycentric formula`` (a : Vector2, b : Vector2, c : Vector2, u, v) = let ``Barycentric interpolation follows the barycentric formula`` (a : Vector2, b : Vector2, c : Vector2, u, v) =
let r = a + u * (b - a) + v * (c - a) let r = a + u * (b - a) + v * (c - a)
Assert.Equal(r, Vector2.BaryCentric(a, b, c, u, v)) Assert.Equal(r, Vector2.BaryCentric(a, b, c, u, v))
let vRes = Vector2.BaryCentric(ref a, ref b, ref c, u, v) let vRes = Vector2.BaryCentric(ref a, ref b, ref c, u, v)
Assert.Equal(r, vRes) Assert.Equal(r, vRes)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module ``Vector products`` = module ``Vector products`` =
// //
[<Property>] [<Property>]
let ``Dot product follows the dot product formula`` (a : Vector2, b : Vector2) = let ``Dot product follows the dot product formula`` (a : Vector2, b : Vector2) =
let dot = a.X * b.X + a.Y * b.Y let dot = a.X * b.X + a.Y * b.Y
Assert.Equal(dot, Vector2.Dot(a, b)); Assert.Equal(dot, Vector2.Dot(a, b));
let vRes = Vector2.Dot(ref a, ref b) let vRes = Vector2.Dot(ref a, ref b)
Assert.Equal(dot, vRes) Assert.Equal(dot, vRes)
[<Property>] [<Property>]
let ``Perpendicular dot product follows the perpendicular dot product formula`` (a : Vector2, b : Vector2) = let ``Perpendicular dot product follows the perpendicular dot product formula`` (a : Vector2, b : Vector2) =
let perpDot = a.X * b.Y - a.Y * b.X let perpDot = a.X * b.Y - a.Y * b.X
Assert.Equal(perpDot, Vector2.PerpDot(a, b)); Assert.Equal(perpDot, Vector2.PerpDot(a, b));
let vRes = Vector2.PerpDot(ref a, ref b) let vRes = Vector2.PerpDot(ref a, ref b)
Assert.Equal(perpDot, vRes) Assert.Equal(perpDot, vRes)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Normalization = module Normalization =
// //
[<Property>] [<Property>]
let ``Normalization creates a new unit length vector with the correct components`` (a, b) = let ``Normalization creates a new unit length vector with the correct components`` (a, b) =
let v = Vector2(a, b) let v = Vector2(a, b)
let l = v.Length let l = v.Length
// Dividing by zero is not supported // Dividing by zero is not supported
if not (approxEq l 0.0f) then if not (approxEq l 0.0f) then
let norm = v.Normalized() let norm = v.Normalized()
Assert.ApproximatelyEqual(v.X / l, norm.X) Assert.ApproximatelyEqual(v.X / l, norm.X)
Assert.ApproximatelyEqual(v.Y / l, norm.Y) Assert.ApproximatelyEqual(v.Y / l, norm.Y)
[<Property>] [<Property>]
let ``Normalization of instance transforms the instance into a unit length vector with the correct components`` (a, b) = let ``Normalization of instance transforms the instance into a unit length vector with the correct components`` (a, b) =
let v = Vector2(a, b) let v = Vector2(a, b)
let l = v.Length let l = v.Length
if not (approxEq l 0.0f) then if not (approxEq l 0.0f) then
let norm = Vector2(a, b) let norm = Vector2(a, b)
norm.Normalize() norm.Normalize()
Assert.ApproximatelyEqual(v.X / l, norm.X) Assert.ApproximatelyEqual(v.X / l, norm.X)
Assert.ApproximatelyEqual(v.Y / l, norm.Y) Assert.ApproximatelyEqual(v.Y / l, norm.Y)
[<Property>] [<Property>]
let ``Fast approximate normalization of instance transforms the instance into a unit length vector with the correct components`` (a, b) = let ``Fast approximate normalization of instance transforms the instance into a unit length vector with the correct components`` (a, b) =
let v = Vector2(a, b) let v = Vector2(a, b)
let norm = Vector2(a, b) let norm = Vector2(a, b)
norm.NormalizeFast() norm.NormalizeFast()
@ -431,37 +431,37 @@ module Vector2 =
Assert.ApproximatelyEqual(v.X * scale, norm.X) Assert.ApproximatelyEqual(v.X * scale, norm.X)
Assert.ApproximatelyEqual(v.Y * scale, norm.Y) Assert.ApproximatelyEqual(v.Y * scale, norm.Y)
[<Property>] [<Property>]
let ``Normalization by reference is the same as division by magnitude`` (a : Vector2) = let ``Normalization by reference is the same as division by magnitude`` (a : Vector2) =
let norm = a / a.Length let norm = a / a.Length
let vRes = Vector2.Normalize(ref a) let vRes = Vector2.Normalize(ref a)
Assert.ApproximatelyEqual(norm, vRes) Assert.ApproximatelyEqual(norm, vRes)
[<Property>] [<Property>]
let ``Normalization is the same as division by magnitude`` (a : Vector2) = let ``Normalization is the same as division by magnitude`` (a : Vector2) =
let norm = a / a.Length let norm = a / a.Length
Assert.ApproximatelyEqual(norm, Vector2.Normalize(a)); Assert.ApproximatelyEqual(norm, Vector2.Normalize(a));
[<Property>] [<Property>]
let ``Fast approximate normalization by reference is the same as multiplication by the fast inverse square`` (a : Vector2) = let ``Fast approximate normalization by reference is the same as multiplication by the fast inverse square`` (a : Vector2) =
let scale = MathHelper.InverseSqrtFast(a.X * a.X + a.Y * a.Y) let scale = MathHelper.InverseSqrtFast(a.X * a.X + a.Y * a.Y)
let norm = a * scale let norm = a * scale
let vRes = Vector2.NormalizeFast(ref a) let vRes = Vector2.NormalizeFast(ref a)
Assert.ApproximatelyEqual(norm, vRes) Assert.ApproximatelyEqual(norm, vRes)
[<Property>] [<Property>]
let ``Fast approximate normalization is the same as multiplication by the fast inverse square`` (a : Vector2) = let ``Fast approximate normalization is the same as multiplication by the fast inverse square`` (a : Vector2) =
let scale = MathHelper.InverseSqrtFast(a.X * a.X + a.Y * a.Y) let scale = MathHelper.InverseSqrtFast(a.X * a.X + a.Y * a.Y)
let norm = a * scale let norm = a * scale
Assert.ApproximatelyEqual(norm, Vector2.NormalizeFast(a)); Assert.ApproximatelyEqual(norm, Vector2.NormalizeFast(a));
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module ``Component min and max`` = module ``Component min and max`` =
// //
@ -469,117 +469,117 @@ module Vector2 =
let ``ComponentMin produces a new vector from the smallest components of the given vectors`` (x, y, u, w) = let ``ComponentMin produces a new vector from the smallest components of the given vectors`` (x, y, u, w) =
let v1 = Vector2(x, y) let v1 = Vector2(x, y)
let v2 = Vector2(u, w) let v2 = Vector2(u, w)
let vMin = Vector2.ComponentMin(v1, v2) let vMin = Vector2.ComponentMin(v1, v2)
Assert.True(vMin.X <= v1.X) Assert.True(vMin.X <= v1.X)
Assert.True(vMin.X <= v2.X) Assert.True(vMin.X <= v2.X)
Assert.True(vMin.Y <= v1.Y) Assert.True(vMin.Y <= v1.Y)
Assert.True(vMin.Y <= v2.Y) Assert.True(vMin.Y <= v2.Y)
[<Property>] [<Property>]
let ``ComponentMax produces a new vector from the largest components of the given vectors`` (x, y, u, w) = let ``ComponentMax produces a new vector from the largest components of the given vectors`` (x, y, u, w) =
let v1 = Vector2(x, y) let v1 = Vector2(x, y)
let v2 = Vector2(u, w) let v2 = Vector2(u, w)
let vMax = Vector2.ComponentMax(v1, v2) let vMax = Vector2.ComponentMax(v1, v2)
Assert.True(vMax.X >= v1.X) Assert.True(vMax.X >= v1.X)
Assert.True(vMax.X >= v2.X) Assert.True(vMax.X >= v2.X)
Assert.True(vMax.Y >= v1.Y) Assert.True(vMax.Y >= v1.Y)
Assert.True(vMax.Y >= v2.Y) Assert.True(vMax.Y >= v2.Y)
[<Property>] [<Property>]
let ``ComponentMin by reference produces a new vector from the smallest components of the given vectors`` (x, y, u, w) = let ``ComponentMin by reference produces a new vector from the smallest components of the given vectors`` (x, y, u, w) =
let v1 = Vector2(x, y) let v1 = Vector2(x, y)
let v2 = Vector2(u, w) let v2 = Vector2(u, w)
let vMin = Vector2.ComponentMin(ref v1, ref v2) let vMin = Vector2.ComponentMin(ref v1, ref v2)
Assert.True(vMin.X <= v1.X) Assert.True(vMin.X <= v1.X)
Assert.True(vMin.X <= v2.X) Assert.True(vMin.X <= v2.X)
Assert.True(vMin.Y <= v1.Y) Assert.True(vMin.Y <= v1.Y)
Assert.True(vMin.Y <= v2.Y) Assert.True(vMin.Y <= v2.Y)
[<Property>] [<Property>]
let ``ComponentMax by reference produces a new vector from the largest components of the given vectors`` (x, y, u, w) = let ``ComponentMax by reference produces a new vector from the largest components of the given vectors`` (x, y, u, w) =
let v1 = Vector2(x, y) let v1 = Vector2(x, y)
let v2 = Vector2(u, w) let v2 = Vector2(u, w)
let vMax = Vector2.ComponentMax(ref v1, ref v2) let vMax = Vector2.ComponentMax(ref v1, ref v2)
Assert.True(vMax.X >= v1.X) Assert.True(vMax.X >= v1.X)
Assert.True(vMax.X >= v2.X) Assert.True(vMax.X >= v2.X)
Assert.True(vMax.Y >= v1.Y) Assert.True(vMax.Y >= v1.Y)
Assert.True(vMax.Y >= v2.Y) Assert.True(vMax.Y >= v2.Y)
[<Property>] [<Property>]
let ``Min selects the vector with lesser magnitude given two vectors`` (x, y, u, w) = let ``Min selects the vector with lesser magnitude given two vectors`` (x, y, u, w) =
let v1 = Vector2(x, y) let v1 = Vector2(x, y)
let v2 = Vector2(u, w) let v2 = Vector2(u, w)
let l1 = v1.LengthSquared let l1 = v1.LengthSquared
let l2 = v2.LengthSquared let l2 = v2.LengthSquared
let vMin = Vector2.Min(v1, v2) let vMin = Vector2.Min(v1, v2)
if l1 < l2 then if l1 < l2 then
let equalsFirst = vMin = v1 let equalsFirst = vMin = v1
Assert.True(equalsFirst) Assert.True(equalsFirst)
else else
let equalsLast = vMin = v2 let equalsLast = vMin = v2
Assert.True(equalsLast) Assert.True(equalsLast)
[<Property>] [<Property>]
let ``Max selects the vector with greater magnitude given two vectors`` (x, y, u, w) = let ``Max selects the vector with greater magnitude given two vectors`` (x, y, u, w) =
let v1 = Vector2(x, y) let v1 = Vector2(x, y)
let v2 = Vector2(u, w) let v2 = Vector2(u, w)
let l1 = v1.LengthSquared let l1 = v1.LengthSquared
let l2 = v2.LengthSquared let l2 = v2.LengthSquared
let vMin = Vector2.Max(v1, v2) let vMin = Vector2.Max(v1, v2)
if l1 >= l2 then if l1 >= l2 then
let equalsFirst = vMin = v1 let equalsFirst = vMin = v1
Assert.True(equalsFirst) Assert.True(equalsFirst)
else else
let equalsLast = vMin = v2 let equalsLast = vMin = v2
Assert.True(equalsLast) Assert.True(equalsLast)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Transformation = module Transformation =
// //
[<Property>] [<Property>]
let ``Transformation by quaternion is the same as multiplication by quaternion and its conjugate`` (v : Vector2, q : Quaternion) = let ``Transformation by quaternion is the same as multiplication by quaternion and its conjugate`` (v : Vector2, q : Quaternion) =
let vectorQuat = Quaternion(v.X, v.Y, (float32)0, (float32)0) let vectorQuat = Quaternion(v.X, v.Y, (float32)0, (float32)0)
let inverse = Quaternion.Invert(q) let inverse = Quaternion.Invert(q)
let transformedQuat = q * vectorQuat * inverse let transformedQuat = q * vectorQuat * inverse
let transformedVector = Vector2(transformedQuat.X, transformedQuat.Y) let transformedVector = Vector2(transformedQuat.X, transformedQuat.Y)
Assert.Equal(transformedVector, Vector2.Transform(v, q)) Assert.Equal(transformedVector, Vector2.Transform(v, q))
[<Property>] [<Property>]
let ``Transformation by quaternion by reference is the same as multiplication by quaternion and its conjugate`` (v : Vector2, q : Quaternion) = let ``Transformation by quaternion by reference is the same as multiplication by quaternion and its conjugate`` (v : Vector2, q : Quaternion) =
let vectorQuat = Quaternion(v.X, v.Y, (float32)0, (float32)0) let vectorQuat = Quaternion(v.X, v.Y, (float32)0, (float32)0)
let inverse = Quaternion.Invert(q) let inverse = Quaternion.Invert(q)
let transformedQuat = q * vectorQuat * inverse let transformedQuat = q * vectorQuat * inverse
let transformedVector = Vector2(transformedQuat.X, transformedQuat.Y) let transformedVector = Vector2(transformedQuat.X, transformedQuat.Y)
Assert.Equal(transformedVector, Vector2.Transform(ref v, ref q)) Assert.Equal(transformedVector, Vector2.Transform(ref v, ref q))
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Serialization = module Serialization =
// //
[<Property>] [<Property>]
let ``The absolute size of a Vector2 is always the size of its components`` (v : Vector2) = let ``The absolute size of a Vector2 is always the size of its components`` (v : Vector2) =
let expectedSize = sizeof<float32> * 2 let expectedSize = sizeof<float32> * 2
Assert.Equal(expectedSize, Vector2.SizeInBytes) Assert.Equal(expectedSize, Vector2.SizeInBytes)
Assert.Equal(expectedSize, Marshal.SizeOf(Vector2())) Assert.Equal(expectedSize, Marshal.SizeOf(Vector2()))

View file

@ -7,28 +7,28 @@ open System
open System.Runtime.InteropServices open System.Runtime.InteropServices
open OpenTK open OpenTK
module Vector3 = module Vector3 =
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Constructors = module Constructors =
// //
[<Property>] [<Property>]
let ``Triple value constructor sets all components to the correct values`` (a, b, c) = let ``Triple value constructor sets all components to the correct values`` (a, b, c) =
let v = Vector3(a, b, c) let v = Vector3(a, b, c)
Assert.Equal(a, v.X) Assert.Equal(a, v.X)
Assert.Equal(b, v.Y) Assert.Equal(b, v.Y)
Assert.Equal(c, v.Z) Assert.Equal(c, v.Z)
[<Property>] [<Property>]
let ``Single value constructor sets all components to the correct values`` (a : float32) = let ``Single value constructor sets all components to the correct values`` (a : float32) =
let v = Vector3(a) let v = Vector3(a)
Assert.Equal(a, v.X) Assert.Equal(a, v.X)
Assert.Equal(a, v.Y) Assert.Equal(a, v.Y)
Assert.Equal(a, v.Z) Assert.Equal(a, v.Z)
[<Property>] [<Property>]
let ``Vector2 value constructor sets all components to the correct values`` (a, b) = let ``Vector2 value constructor sets all components to the correct values`` (a, b) =
let v1 = Vector2(a, b) let v1 = Vector2(a, b)
let v2 = Vector3(v1) let v2 = Vector3(v1)
@ -38,9 +38,9 @@ module Vector3 =
Assert.Equal(a, v2.X) Assert.Equal(a, v2.X)
Assert.Equal(b, v2.Y) Assert.Equal(b, v2.Y)
Assert.Equal((float32)0, v2.Z) Assert.Equal((float32)0, v2.Z)
[<Property>] [<Property>]
let ``Vector3 value constructor sets all components to the correct values`` (a, b, c) = let ``Vector3 value constructor sets all components to the correct values`` (a, b, c) =
let v1 = Vector3(a, b, c) let v1 = Vector3(a, b, c)
let v2 = Vector3(v1) let v2 = Vector3(v1)
@ -51,9 +51,9 @@ module Vector3 =
Assert.Equal(a, v2.X) Assert.Equal(a, v2.X)
Assert.Equal(b, v2.Y) Assert.Equal(b, v2.Y)
Assert.Equal(c, v2.Z) Assert.Equal(c, v2.Z)
[<Property>] [<Property>]
let ``Vector4 value constructor sets all components to the correct values`` (a, b, c, d) = let ``Vector4 value constructor sets all components to the correct values`` (a, b, c, d) =
let v1 = Vector4(a, b, c, d) let v1 = Vector4(a, b, c, d)
let v2 = Vector3(v1) let v2 = Vector3(v1)
@ -64,87 +64,87 @@ module Vector3 =
Assert.Equal(a, v2.X) Assert.Equal(a, v2.X)
Assert.Equal(b, v2.Y) Assert.Equal(b, v2.Y)
Assert.Equal(c, v2.Z) Assert.Equal(c, v2.Z)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Indexing = module Indexing =
// //
[<Property>] [<Property>]
let ``Index operator accesses the correct components`` (x, y, z) = let ``Index operator accesses the correct components`` (x, y, z) =
let v = Vector3(x, y, z) let v = Vector3(x, y, z)
Assert.Equal(x, v.[0]) Assert.Equal(x, v.[0])
Assert.Equal(y, v.[1]) Assert.Equal(y, v.[1])
Assert.Equal(z, v.[2]) Assert.Equal(z, v.[2])
[<Property>] [<Property>]
let ``Indexed set operator throws exception for negative indices`` (x, y, z) = let ``Indexed set operator throws exception for negative indices`` (x, y, z) =
let mutable v = Vector3(x, y, z) let mutable v = Vector3(x, y, z)
(fun() -> v.[-1] <- x) |> Assert.Throws<IndexOutOfRangeException> |> ignore (fun() -> v.[-1] <- x) |> Assert.Throws<IndexOutOfRangeException> |> ignore
[<Property>] [<Property>]
let ``Indexed get operator throws exception for negative indices`` (x, y, z) = let ``Indexed get operator throws exception for negative indices`` (x, y, z) =
let mutable v = Vector3(x, y, z) let mutable v = Vector3(x, y, z)
(fun() -> v.[-1] |> ignore) |> Assert.Throws<IndexOutOfRangeException> |> ignore (fun() -> v.[-1] |> ignore) |> Assert.Throws<IndexOutOfRangeException> |> ignore
[<Property>] [<Property>]
let ``Indexed set operator throws exception for large indices`` (x, y, z) = let ``Indexed set operator throws exception for large indices`` (x, y, z) =
let mutable v = Vector3(x, y, z) let mutable v = Vector3(x, y, z)
(fun() -> v.[4] <- x) |> Assert.Throws<IndexOutOfRangeException> |> ignore (fun() -> v.[4] <- x) |> Assert.Throws<IndexOutOfRangeException> |> ignore
[<Property>] [<Property>]
let ``Indexed get operator throws exception for large indices`` (x, y, z) = let ``Indexed get operator throws exception for large indices`` (x, y, z) =
let mutable v = Vector3(x, y, z) let mutable v = Vector3(x, y, z)
(fun() -> v.[4] |> ignore) |> Assert.Throws<IndexOutOfRangeException> |> ignore (fun() -> v.[4] |> ignore) |> Assert.Throws<IndexOutOfRangeException> |> ignore
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Length = module Length =
// //
[<Property>] [<Property>]
let ``Length method follows the pythagorean theorem`` (a, b, c) = let ``Length method follows the pythagorean theorem`` (a, b, c) =
let v = Vector3(a, b, c) let v = Vector3(a, b, c)
let l = System.Math.Sqrt((float)(a * a + b * b + c * c)) let l = System.Math.Sqrt((float)(a * a + b * b + c * c))
Assert.Equal((float32)l, v.Length) Assert.Equal((float32)l, v.Length)
[<Property>] [<Property>]
let ``Fast length method is the same as one divided by the fast inverse square`` (a, b, c) = let ``Fast length method is the same as one divided by the fast inverse square`` (a, b, c) =
let v = Vector3(a, b, c) let v = Vector3(a, b, c)
let l = 1.0f / MathHelper.InverseSqrtFast(a * a + b * b + c * c) let l = 1.0f / MathHelper.InverseSqrtFast(a * a + b * b + c * c)
Assert.Equal(l, v.LengthFast) Assert.Equal(l, v.LengthFast)
[<Property>] [<Property>]
let ``Length squared method returns each component squared and summed`` (a, b, c) = let ``Length squared method returns each component squared and summed`` (a, b, c) =
let v = Vector3(a, b, c) let v = Vector3(a, b, c)
let lsq = a * a + b * b + c * c let lsq = a * a + b * b + c * c
Assert.Equal(lsq, v.LengthSquared) Assert.Equal(lsq, v.LengthSquared)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Normalization = module Normalization =
// //
[<Property>] [<Property>]
let ``Normalization creates a new unit length vector with the correct components`` (a, b, c) = let ``Normalization creates a new unit length vector with the correct components`` (a, b, c) =
let v = Vector3(a, b, c) let v = Vector3(a, b, c)
let l = v.Length let l = v.Length
// Dividing by zero is not supported // Dividing by zero is not supported
if not (approxEq l 0.0f) then if not (approxEq l 0.0f) then
let norm = v.Normalized() let norm = v.Normalized()
Assert.ApproximatelyEqual(v.X / l, norm.X) Assert.ApproximatelyEqual(v.X / l, norm.X)
Assert.ApproximatelyEqual(v.Y / l, norm.Y) Assert.ApproximatelyEqual(v.Y / l, norm.Y)
Assert.ApproximatelyEqual(v.Z / l, norm.Z) Assert.ApproximatelyEqual(v.Z / l, norm.Z)
[<Property>] [<Property>]
let ``Normalization of instance transforms the instance into a unit length vector with the correct components`` (a, b, c) = let ``Normalization of instance transforms the instance into a unit length vector with the correct components`` (a, b, c) =
let v = Vector3(a, b, c) let v = Vector3(a, b, c)
let l = v.Length let l = v.Length
if not (approxEq l 0.0f) then if not (approxEq l 0.0f) then
let norm = Vector3(a, b, c) let norm = Vector3(a, b, c)
norm.Normalize() norm.Normalize()
@ -154,7 +154,7 @@ module Vector3 =
Assert.ApproximatelyEqual(v.Z / l, norm.Z) Assert.ApproximatelyEqual(v.Z / l, norm.Z)
[<Property>] [<Property>]
let ``Fast approximate normalization of instance transforms the instance into a unit length vector with the correct components`` (a, b, c) = let ``Fast approximate normalization of instance transforms the instance into a unit length vector with the correct components`` (a, b, c) =
let v = Vector3(a, b, c) let v = Vector3(a, b, c)
let norm = Vector3(a, b, c) let norm = Vector3(a, b, c)
norm.NormalizeFast() norm.NormalizeFast()
@ -164,230 +164,230 @@ module Vector3 =
Assert.ApproximatelyEqual(v.X * scale, norm.X) Assert.ApproximatelyEqual(v.X * scale, norm.X)
Assert.ApproximatelyEqual(v.Y * scale, norm.Y) Assert.ApproximatelyEqual(v.Y * scale, norm.Y)
Assert.ApproximatelyEqual(v.Z * scale, norm.Z) Assert.ApproximatelyEqual(v.Z * scale, norm.Z)
[<Property>] [<Property>]
let ``Normalization by reference is the same as division by magnitude`` (a : Vector3) = let ``Normalization by reference is the same as division by magnitude`` (a : Vector3) =
let norm = a / a.Length let norm = a / a.Length
let vRes = Vector3.Normalize(ref a) let vRes = Vector3.Normalize(ref a)
Assert.ApproximatelyEqual(norm, vRes) Assert.ApproximatelyEqual(norm, vRes)
[<Property>] [<Property>]
let ``Normalization is the same as division by magnitude`` (a : Vector3) = let ``Normalization is the same as division by magnitude`` (a : Vector3) =
let norm = a / a.Length let norm = a / a.Length
Assert.ApproximatelyEqual(norm, Vector3.Normalize(a)); Assert.ApproximatelyEqual(norm, Vector3.Normalize(a));
[<Property>] [<Property>]
let ``Fast approximate normalization by reference is the same as multiplication by the fast inverse square`` (a : Vector3) = let ``Fast approximate normalization by reference is the same as multiplication by the fast inverse square`` (a : Vector3) =
let scale = MathHelper.InverseSqrtFast(a.X * a.X + a.Y * a.Y + a.Z * a.Z) let scale = MathHelper.InverseSqrtFast(a.X * a.X + a.Y * a.Y + a.Z * a.Z)
let norm = a * scale let norm = a * scale
let vRes = Vector3.NormalizeFast(ref a) let vRes = Vector3.NormalizeFast(ref a)
Assert.ApproximatelyEqual(norm, vRes) Assert.ApproximatelyEqual(norm, vRes)
[<Property>] [<Property>]
let ``Fast approximate normalization is the same as multiplication by fast inverse square`` (a : Vector3) = let ``Fast approximate normalization is the same as multiplication by fast inverse square`` (a : Vector3) =
let scale = MathHelper.InverseSqrtFast(a.X * a.X + a.Y * a.Y + a.Z * a.Z) let scale = MathHelper.InverseSqrtFast(a.X * a.X + a.Y * a.Y + a.Z * a.Z)
let norm = a * scale let norm = a * scale
Assert.ApproximatelyEqual(norm, Vector3.NormalizeFast(a)); Assert.ApproximatelyEqual(norm, Vector3.NormalizeFast(a));
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Addition = module Addition =
// //
[<Property>] [<Property>]
let ``Vector3 addition is the same as component addition`` (a : Vector3, b : Vector3) = let ``Vector3 addition is the same as component addition`` (a : Vector3, b : Vector3) =
let c = a + b let c = a + b
Assert.ApproximatelyEqual(a.X + b.X,c.X) Assert.ApproximatelyEqual(a.X + b.X,c.X)
Assert.ApproximatelyEqual(a.Y + b.Y,c.Y) Assert.ApproximatelyEqual(a.Y + b.Y,c.Y)
Assert.ApproximatelyEqual(a.Z + b.Z,c.Z) Assert.ApproximatelyEqual(a.Z + b.Z,c.Z)
[<Property>] [<Property>]
let ``Vector3 addition is commutative`` (a : Vector3, b : Vector3) = let ``Vector3 addition is commutative`` (a : Vector3, b : Vector3) =
let c = a + b let c = a + b
let c2 = b + a let c2 = b + a
Assert.ApproximatelyEqual(c, c2) Assert.ApproximatelyEqual(c, c2)
[<Property>] [<Property>]
let ``Vector3 addition is associative`` (a : Vector3, b : Vector3, c : Vector3) = let ``Vector3 addition is associative`` (a : Vector3, b : Vector3, c : Vector3) =
let r1 = (a + b) + c let r1 = (a + b) + c
let r2 = a + (b + c) let r2 = a + (b + c)
Assert.ApproximatelyEqual(r1, r2) Assert.ApproximatelyEqual(r1, r2)
[<Property>] [<Property>]
let ``Static Vector3 addition method is the same as component addition`` (a : Vector3, b : Vector3) = let ``Static Vector3 addition method is the same as component addition`` (a : Vector3, b : Vector3) =
let v1 = Vector3(a.X + b.X, a.Y + b.Y, a.Z + b.Z) let v1 = Vector3(a.X + b.X, a.Y + b.Y, a.Z + b.Z)
let sum = Vector3.Add(a, b) let sum = Vector3.Add(a, b)
Assert.ApproximatelyEqual(v1, sum) Assert.ApproximatelyEqual(v1, sum)
[<Property>] [<Property>]
let ``Static Vector3 addition method by reference is the same as component addition`` (a : Vector3, b : Vector3) = let ``Static Vector3 addition method by reference is the same as component addition`` (a : Vector3, b : Vector3) =
let v1 = Vector3(a.X + b.X, a.Y + b.Y, a.Z + b.Z) let v1 = Vector3(a.X + b.X, a.Y + b.Y, a.Z + b.Z)
let sum = Vector3.Add(ref a, ref b) let sum = Vector3.Add(ref a, ref b)
Assert.ApproximatelyEqual(v1, sum) Assert.ApproximatelyEqual(v1, sum)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Subtraction = module Subtraction =
// //
[<Property>] [<Property>]
let ``Vector3 subtraction is the same as component subtraction`` (a : Vector3, b : Vector3) = let ``Vector3 subtraction is the same as component subtraction`` (a : Vector3, b : Vector3) =
let c = a - b let c = a - b
Assert.Equal(a.X - b.X,c.X) Assert.Equal(a.X - b.X,c.X)
Assert.Equal(a.Y - b.Y,c.Y) Assert.Equal(a.Y - b.Y,c.Y)
Assert.Equal(a.Z - b.Z,c.Z) Assert.Equal(a.Z - b.Z,c.Z)
[<Property>] [<Property>]
let ``Static Vector3 subtraction method is the same as component addition`` (a : Vector3, b : Vector3) = let ``Static Vector3 subtraction method is the same as component addition`` (a : Vector3, b : Vector3) =
let v1 = Vector3(a.X - b.X, a.Y - b.Y, a.Z - b.Z) let v1 = Vector3(a.X - b.X, a.Y - b.Y, a.Z - b.Z)
let sum = Vector3.Subtract(a, b) let sum = Vector3.Subtract(a, b)
Assert.ApproximatelyEqual(v1, sum) Assert.ApproximatelyEqual(v1, sum)
[<Property>] [<Property>]
let ``Static Vector3 subtraction method by reference is the same as component addition`` (a : Vector3, b : Vector3) = let ``Static Vector3 subtraction method by reference is the same as component addition`` (a : Vector3, b : Vector3) =
let v1 = Vector3(a.X - b.X, a.Y - b.Y, a.Z - b.Z) let v1 = Vector3(a.X - b.X, a.Y - b.Y, a.Z - b.Z)
let sum = Vector3.Subtract(ref a, ref b) let sum = Vector3.Subtract(ref a, ref b)
Assert.ApproximatelyEqual(v1, sum) Assert.ApproximatelyEqual(v1, sum)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Multiplication = module Multiplication =
// //
[<Property>] [<Property>]
let ``Vector3 multiplication is the same as component multiplication`` (a : Vector3, b : Vector3) = let ``Vector3 multiplication is the same as component multiplication`` (a : Vector3, b : Vector3) =
let c = a * b let c = a * b
Assert.Equal(a.X * b.X,c.X) Assert.Equal(a.X * b.X,c.X)
Assert.Equal(a.Y * b.Y,c.Y) Assert.Equal(a.Y * b.Y,c.Y)
Assert.Equal(a.Z * b.Z,c.Z) Assert.Equal(a.Z * b.Z,c.Z)
[<Property>] [<Property>]
let ``Vector3 multiplication is commutative`` (a : Vector3, b : Vector3) = let ``Vector3 multiplication is commutative`` (a : Vector3, b : Vector3) =
let r1 = a * b let r1 = a * b
let r2 = b * a let r2 = b * a
Assert.Equal(r1, r2) Assert.Equal(r1, r2)
[<Property>] [<Property>]
let ``Left-handed Vector3-scalar multiplication is the same as component-scalar multiplication`` (a : Vector3, f : float32) = let ``Left-handed Vector3-scalar multiplication is the same as component-scalar multiplication`` (a : Vector3, f : float32) =
let r = a * f let r = a * f
Assert.Equal(a.X * f,r.X) Assert.Equal(a.X * f,r.X)
Assert.Equal(a.Y * f,r.Y) Assert.Equal(a.Y * f,r.Y)
Assert.Equal(a.Z * f,r.Z) Assert.Equal(a.Z * f,r.Z)
[<Property>] [<Property>]
let ``Right-handed Vector3-scalar multiplication is the same as component-scalar multiplication`` (a : Vector3, f : float32) = let ``Right-handed Vector3-scalar multiplication is the same as component-scalar multiplication`` (a : Vector3, f : float32) =
let r = f * a let r = f * a
Assert.Equal(a.X * f,r.X) Assert.Equal(a.X * f,r.X)
Assert.Equal(a.Y * f,r.Y) Assert.Equal(a.Y * f,r.Y)
Assert.Equal(a.Z * f,r.Z) Assert.Equal(a.Z * f,r.Z)
[<Property>] [<Property>]
let ``Static method Vector3-scalar multiplication is the same as component-scalar multiplication`` (a : Vector3, f : float32) = let ``Static method Vector3-scalar multiplication is the same as component-scalar multiplication`` (a : Vector3, f : float32) =
let r = Vector3.Multiply(a, f) let r = Vector3.Multiply(a, f)
Assert.Equal(a.X * f,r.X) Assert.Equal(a.X * f,r.X)
Assert.Equal(a.Y * f,r.Y) Assert.Equal(a.Y * f,r.Y)
Assert.Equal(a.Z * f,r.Z) Assert.Equal(a.Z * f,r.Z)
[<Property>] [<Property>]
let ``Vector3-Matrix3 multiplication using right-handed notation is the same as vector/row multiplication and summation`` (a : Matrix3, b : Vector3) = let ``Vector3-Matrix3 multiplication using right-handed notation is the same as vector/row multiplication and summation`` (a : Matrix3, b : Vector3) =
let res = a*b let res = a*b
let c1 = b.X * a.M11 + b.Y * a.M12 + b.Z * a.M13 let c1 = b.X * a.M11 + b.Y * a.M12 + b.Z * a.M13
let c2 = b.X * a.M21 + b.Y * a.M22 + b.Z * a.M23 let c2 = b.X * a.M21 + b.Y * a.M22 + b.Z * a.M23
let c3 = b.X * a.M31 + b.Y * a.M32 + b.Z * a.M33 let c3 = b.X * a.M31 + b.Y * a.M32 + b.Z * a.M33
let exp = Vector3(c1, c2, c3) let exp = Vector3(c1, c2, c3)
Assert.Equal(exp, res) Assert.Equal(exp, res)
[<Property>] [<Property>]
let ``Vector3-Matrix3 multiplication using left-handed notation is the same as vector/column multiplication and summation`` (a : Matrix3, b : Vector3) = let ``Vector3-Matrix3 multiplication using left-handed notation is the same as vector/column multiplication and summation`` (a : Matrix3, b : Vector3) =
let res = b*a let res = b*a
let c1 = b.X * a.M11 + b.Y * a.M21 + b.Z * a.M31 let c1 = b.X * a.M11 + b.Y * a.M21 + b.Z * a.M31
let c2 = b.X * a.M12 + b.Y * a.M22 + b.Z * a.M32 let c2 = b.X * a.M12 + b.Y * a.M22 + b.Z * a.M32
let c3 = b.X * a.M13 + b.Y * a.M23 + b.Z * a.M33 let c3 = b.X * a.M13 + b.Y * a.M23 + b.Z * a.M33
let exp = Vector3(c1, c2, c3) let exp = Vector3(c1, c2, c3)
Assert.Equal(exp, res) Assert.Equal(exp, res)
[<Property>] [<Property>]
let ``Static Vector3 multiplication method is the same as component multiplication`` (a : Vector3, b : Vector3) = let ``Static Vector3 multiplication method is the same as component multiplication`` (a : Vector3, b : Vector3) =
let v1 = Vector3(a.X * b.X, a.Y * b.Y, a.Z * b.Z) let v1 = Vector3(a.X * b.X, a.Y * b.Y, a.Z * b.Z)
let sum = Vector3.Multiply(a, b) let sum = Vector3.Multiply(a, b)
Assert.ApproximatelyEqual(v1, sum) Assert.ApproximatelyEqual(v1, sum)
[<Property>] [<Property>]
let ``Static Vector3 multiplication method by reference is the same as component multiplication`` (a : Vector3, b : Vector3) = let ``Static Vector3 multiplication method by reference is the same as component multiplication`` (a : Vector3, b : Vector3) =
let v1 = Vector3(a.X * b.X, a.Y * b.Y, a.Z * b.Z) let v1 = Vector3(a.X * b.X, a.Y * b.Y, a.Z * b.Z)
let sum = Vector3.Multiply(ref a, ref b) let sum = Vector3.Multiply(ref a, ref b)
Assert.ApproximatelyEqual(v1, sum) Assert.ApproximatelyEqual(v1, sum)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Division = module Division =
// //
[<Property>] [<Property>]
let ``Vector3-float division is the same as component-float division`` (a : Vector3, f : float32) = let ``Vector3-float division is the same as component-float division`` (a : Vector3, f : float32) =
if not (approxEq f 0.0f) then // we don't support diving by zero. if not (approxEq f 0.0f) then // we don't support diving by zero.
let r = a / f let r = a / f
Assert.ApproximatelyEqual(a.X / f,r.X) Assert.ApproximatelyEqual(a.X / f,r.X)
Assert.ApproximatelyEqual(a.Y / f,r.Y) Assert.ApproximatelyEqual(a.Y / f,r.Y)
Assert.ApproximatelyEqual(a.Z / f,r.Z) Assert.ApproximatelyEqual(a.Z / f,r.Z)
[<Property>] [<Property>]
let ``Static Vector3-Vector3 division method is the same as component division`` (a : Vector3, b : Vector3) = let ``Static Vector3-Vector3 division method is the same as component division`` (a : Vector3, b : Vector3) =
let v1 = Vector3(a.X / b.X, a.Y / b.Y, a.Z / b.Z) let v1 = Vector3(a.X / b.X, a.Y / b.Y, a.Z / b.Z)
let sum = Vector3.Divide(a, b) let sum = Vector3.Divide(a, b)
Assert.ApproximatelyEqual(v1, sum) Assert.ApproximatelyEqual(v1, sum)
[<Property>] [<Property>]
let ``Static Vector3-Vector3 divison method by reference is the same as component division`` (a : Vector3, b : Vector3) = let ``Static Vector3-Vector3 divison method by reference is the same as component division`` (a : Vector3, b : Vector3) =
let v1 = Vector3(a.X / b.X, a.Y / b.Y, a.Z / b.Z) let v1 = Vector3(a.X / b.X, a.Y / b.Y, a.Z / b.Z)
let sum = Vector3.Divide(ref a, ref b) let sum = Vector3.Divide(ref a, ref b)
Assert.ApproximatelyEqual(v1, sum) Assert.ApproximatelyEqual(v1, sum)
[<Property>] [<Property>]
let ``Static Vector3-scalar division method is the same as component division`` (a : Vector3, b : float32) = let ``Static Vector3-scalar division method is the same as component division`` (a : Vector3, b : float32) =
let v1 = Vector3(a.X / b, a.Y / b, a.Z / b) let v1 = Vector3(a.X / b, a.Y / b, a.Z / b)
let sum = Vector3.Divide(a, b) let sum = Vector3.Divide(a, b)
Assert.ApproximatelyEqual(v1, sum) Assert.ApproximatelyEqual(v1, sum)
[<Property>] [<Property>]
let ``Static Vector3-scalar divison method by reference is the same as component division`` (a : Vector3, b : float32) = let ``Static Vector3-scalar divison method by reference is the same as component division`` (a : Vector3, b : float32) =
let v1 = Vector3(a.X / b, a.Y / b, a.Z / b) let v1 = Vector3(a.X / b, a.Y / b, a.Z / b)
let sum = Vector3.Divide(ref a, b) let sum = Vector3.Divide(ref a, b)
Assert.ApproximatelyEqual(v1, sum) Assert.ApproximatelyEqual(v1, sum)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Negation = module Negation =
// //
@ -398,7 +398,7 @@ module Vector3 =
Assert.Equal(-x, vNeg.X) Assert.Equal(-x, vNeg.X)
Assert.Equal(-y, vNeg.Y) Assert.Equal(-y, vNeg.Y)
Assert.Equal(-z, vNeg.Z) Assert.Equal(-z, vNeg.Z)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Equality = module Equality =
// //
@ -407,74 +407,74 @@ module Vector3 =
let v1 = Vector3(x, y, z) let v1 = Vector3(x, y, z)
let v2 = Vector3(x, y, z) let v2 = Vector3(x, y, z)
let equality = v1 = v2 let equality = v1 = v2
Assert.True(equality) Assert.True(equality)
[<Property>] [<Property>]
let ``Vector inequality operator is by component`` (x, y, z) = let ``Vector inequality operator is by component`` (x, y, z) =
let v1 = Vector3(x, y, z) let v1 = Vector3(x, y, z)
let v2 = Vector3(x + (float32)1 , y + (float32)1, z + (float32)1) let v2 = Vector3(x + (float32)1 , y + (float32)1, z + (float32)1)
let inequality = v1 <> v2 let inequality = v1 <> v2
Assert.True(inequality) Assert.True(inequality)
[<Property>] [<Property>]
let ``Vector equality method is by component`` (x, y, z) = let ``Vector equality method is by component`` (x, y, z) =
let v1 = Vector3(x, y, z) let v1 = Vector3(x, y, z)
let v2 = Vector3(x, y, z) let v2 = Vector3(x, y, z)
let notVector = Matrix2() let notVector = Matrix2()
let equality = v1.Equals(v2) let equality = v1.Equals(v2)
let inequalityByOtherType = v1.Equals(notVector) let inequalityByOtherType = v1.Equals(notVector)
Assert.True(equality) Assert.True(equality)
Assert.False(inequalityByOtherType) Assert.False(inequalityByOtherType)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Swizzling = module Swizzling =
// //
[<Property>] [<Property>]
let ``Vector swizzling returns the correct composite for X-primary components`` (x, y, z) = let ``Vector swizzling returns the correct composite for X-primary components`` (x, y, z) =
let v = Vector3(x, y, z) let v = Vector3(x, y, z)
let xyz = Vector3(x, y, z) let xyz = Vector3(x, y, z)
let xzy = Vector3(x, z, y) let xzy = Vector3(x, z, y)
let xy = Vector2(x, y) let xy = Vector2(x, y)
let xz = Vector2(x, z) let xz = Vector2(x, z)
Assert.Equal(xyz, v); Assert.Equal(xyz, v);
Assert.Equal(xzy, v.Xzy); Assert.Equal(xzy, v.Xzy);
Assert.Equal(xy, v.Xy); Assert.Equal(xy, v.Xy);
Assert.Equal(xz, v.Xz); Assert.Equal(xz, v.Xz);
[<Property>] [<Property>]
let ``Vector swizzling returns the correct composite for Y-primary components`` (x, y, z) = let ``Vector swizzling returns the correct composite for Y-primary components`` (x, y, z) =
let v = Vector3(x, y, z) let v = Vector3(x, y, z)
let yxz = Vector3(y, x, z) let yxz = Vector3(y, x, z)
let yzx = Vector3(y, z, x) let yzx = Vector3(y, z, x)
let yx = Vector2(y, x) let yx = Vector2(y, x)
let yz = Vector2(y, z) let yz = Vector2(y, z)
Assert.Equal(yxz, v.Yxz); Assert.Equal(yxz, v.Yxz);
Assert.Equal(yzx, v.Yzx); Assert.Equal(yzx, v.Yzx);
Assert.Equal(yx, v.Yx); Assert.Equal(yx, v.Yx);
Assert.Equal(yz, v.Yz); Assert.Equal(yz, v.Yz);
[<Property>] [<Property>]
let ``Vector swizzling returns the correct composite for Z-primary components`` (x, y, z) = let ``Vector swizzling returns the correct composite for Z-primary components`` (x, y, z) =
let v = Vector3(x, y, z) let v = Vector3(x, y, z)
let zxy = Vector3(z, x, y) let zxy = Vector3(z, x, y)
let zyx = Vector3(z, y, x) let zyx = Vector3(z, y, x)
let zx = Vector2(z, x) let zx = Vector2(z, x)
let zy = Vector2(z, y); let zy = Vector2(z, y);
Assert.Equal(zxy, v.Zxy); Assert.Equal(zxy, v.Zxy);
Assert.Equal(zyx, v.Zyx); Assert.Equal(zyx, v.Zyx);
Assert.Equal(zx, v.Zx); Assert.Equal(zx, v.Zx);
Assert.Equal(zy, v.Zy); Assert.Equal(zy, v.Zy);
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Interpolation = module Interpolation =
// //
@ -482,51 +482,51 @@ module Vector3 =
let ``Linear interpolation is by component`` (a : Vector3, b : Vector3, q) = let ``Linear interpolation is by component`` (a : Vector3, b : Vector3, q) =
let blend = q let blend = q
let rX = blend * (b.X - a.X) + a.X let rX = blend * (b.X - a.X) + a.X
let rY = blend * (b.Y - a.Y) + a.Y let rY = blend * (b.Y - a.Y) + a.Y
let rZ = blend * (b.Z - a.Z) + a.Z let rZ = blend * (b.Z - a.Z) + a.Z
let vExp = Vector3(rX, rY, rZ) let vExp = Vector3(rX, rY, rZ)
Assert.Equal(vExp, Vector3.Lerp(a, b, q)) Assert.Equal(vExp, Vector3.Lerp(a, b, q))
let vRes = Vector3.Lerp(ref a, ref b, q) let vRes = Vector3.Lerp(ref a, ref b, q)
Assert.Equal(vExp, vRes) Assert.Equal(vExp, vRes)
[<Property>] [<Property>]
let ``Barycentric interpolation follows the barycentric formula`` (a : Vector3, b : Vector3, c : Vector3, u, v) = let ``Barycentric interpolation follows the barycentric formula`` (a : Vector3, b : Vector3, c : Vector3, u, v) =
let r = a + u * (b - a) + v * (c - a) let r = a + u * (b - a) + v * (c - a)
Assert.Equal(r, Vector3.BaryCentric(a, b, c, u, v)) Assert.Equal(r, Vector3.BaryCentric(a, b, c, u, v))
let vRes = Vector3.BaryCentric(ref a, ref b, ref c, u, v) let vRes = Vector3.BaryCentric(ref a, ref b, ref c, u, v)
Assert.Equal(r, vRes) Assert.Equal(r, vRes)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module ``Vector products`` = module ``Vector products`` =
// //
[<Property>] [<Property>]
let ``Dot product follows the dot product formula`` (a : Vector3, b : Vector3) = let ``Dot product follows the dot product formula`` (a : Vector3, b : Vector3) =
let dot = a.X * b.X + a.Y * b.Y + a.Z * b.Z let dot = a.X * b.X + a.Y * b.Y + a.Z * b.Z
Assert.Equal(dot, Vector3.Dot(a, b)); Assert.Equal(dot, Vector3.Dot(a, b));
let vRes = Vector3.Dot(ref a, ref b) let vRes = Vector3.Dot(ref a, ref b)
Assert.Equal(dot, vRes) Assert.Equal(dot, vRes)
[<Property>] [<Property>]
let ``Cross product follows the cross product formula`` (a : Vector3, b : Vector3) = let ``Cross product follows the cross product formula`` (a : Vector3, b : Vector3) =
let crossX = a.Y * b.Z - a.Z * b.Y let crossX = a.Y * b.Z - a.Z * b.Y
let crossY = a.Z * b.X - a.X * b.Z let crossY = a.Z * b.X - a.X * b.Z
let crossZ = a.X * b.Y - a.Y * b.X let crossZ = a.X * b.Y - a.Y * b.X
let cross = Vector3(crossX, crossY, crossZ) let cross = Vector3(crossX, crossY, crossZ)
Assert.Equal(cross, Vector3.Cross(a, b)); Assert.Equal(cross, Vector3.Cross(a, b));
let vRes = Vector3.Cross(ref a, ref b) let vRes = Vector3.Cross(ref a, ref b)
Assert.Equal(cross, vRes) Assert.Equal(cross, vRes)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module ``Component min and max`` = module ``Component min and max`` =
// //
@ -534,166 +534,166 @@ module Vector3 =
let ``ComponentMin produces a new vector from the smallest components of the given vectors`` (x, y, z, u, w, q) = let ``ComponentMin produces a new vector from the smallest components of the given vectors`` (x, y, z, u, w, q) =
let v1 = Vector3(x, y, z) let v1 = Vector3(x, y, z)
let v2 = Vector3(u, w, q) let v2 = Vector3(u, w, q)
let vMin = Vector3.ComponentMin(v1, v2) let vMin = Vector3.ComponentMin(v1, v2)
Assert.True(vMin.X <= v1.X) Assert.True(vMin.X <= v1.X)
Assert.True(vMin.X <= v2.X) Assert.True(vMin.X <= v2.X)
Assert.True(vMin.Y <= v1.Y) Assert.True(vMin.Y <= v1.Y)
Assert.True(vMin.Y <= v2.Y) Assert.True(vMin.Y <= v2.Y)
Assert.True(vMin.Z <= v1.Z) Assert.True(vMin.Z <= v1.Z)
Assert.True(vMin.Z <= v2.Z) Assert.True(vMin.Z <= v2.Z)
[<Property>] [<Property>]
let ``ComponentMax producing a new vector from the largest components of the given vectors`` (x, y, z, u, w, q) = let ``ComponentMax producing a new vector from the largest components of the given vectors`` (x, y, z, u, w, q) =
let v1 = Vector3(x, y, z) let v1 = Vector3(x, y, z)
let v2 = Vector3(u, w, q) let v2 = Vector3(u, w, q)
let vMax = Vector3.ComponentMax(v1, v2) let vMax = Vector3.ComponentMax(v1, v2)
Assert.True(vMax.X >= v1.X) Assert.True(vMax.X >= v1.X)
Assert.True(vMax.X >= v2.X) Assert.True(vMax.X >= v2.X)
Assert.True(vMax.Y >= v1.Y) Assert.True(vMax.Y >= v1.Y)
Assert.True(vMax.Y >= v2.Y) Assert.True(vMax.Y >= v2.Y)
Assert.True(vMax.Z >= v1.Z) Assert.True(vMax.Z >= v1.Z)
Assert.True(vMax.Z >= v2.Z) Assert.True(vMax.Z >= v2.Z)
[<Property>] [<Property>]
let ``ComponentMin by reference produces a new vector from the smallest components of the given vectors`` (x, y, z, u, w, q) = let ``ComponentMin by reference produces a new vector from the smallest components of the given vectors`` (x, y, z, u, w, q) =
let v1 = Vector3(x, y, z) let v1 = Vector3(x, y, z)
let v2 = Vector3(u, w, q) let v2 = Vector3(u, w, q)
let vMin = Vector3.ComponentMin(ref v1, ref v2) let vMin = Vector3.ComponentMin(ref v1, ref v2)
Assert.True(vMin.X <= v1.X) Assert.True(vMin.X <= v1.X)
Assert.True(vMin.X <= v2.X) Assert.True(vMin.X <= v2.X)
Assert.True(vMin.Y <= v1.Y) Assert.True(vMin.Y <= v1.Y)
Assert.True(vMin.Y <= v2.Y) Assert.True(vMin.Y <= v2.Y)
Assert.True(vMin.Z <= v1.Z) Assert.True(vMin.Z <= v1.Z)
Assert.True(vMin.Z <= v2.Z) Assert.True(vMin.Z <= v2.Z)
[<Property>] [<Property>]
let ``ComponentMax produces a new vector from the smallest components of the given vectors`` (x, y, z, u, w, q) = let ``ComponentMax produces a new vector from the smallest components of the given vectors`` (x, y, z, u, w, q) =
let v1 = Vector3(x, y, z) let v1 = Vector3(x, y, z)
let v2 = Vector3(u, w, q) let v2 = Vector3(u, w, q)
let vMax = Vector3.ComponentMax(ref v1, ref v2) let vMax = Vector3.ComponentMax(ref v1, ref v2)
Assert.True(vMax.X >= v1.X) Assert.True(vMax.X >= v1.X)
Assert.True(vMax.X >= v2.X) Assert.True(vMax.X >= v2.X)
Assert.True(vMax.Y >= v1.Y) Assert.True(vMax.Y >= v1.Y)
Assert.True(vMax.Y >= v2.Y) Assert.True(vMax.Y >= v2.Y)
Assert.True(vMax.Z >= v1.Z) Assert.True(vMax.Z >= v1.Z)
Assert.True(vMax.Z >= v2.Z) Assert.True(vMax.Z >= v2.Z)
[<Property>] [<Property>]
let ``Min selects the vector with lesser magnitude given two vectors`` (x, y, z, u, w, q) = let ``Min selects the vector with lesser magnitude given two vectors`` (x, y, z, u, w, q) =
let v1 = Vector3(x, y, z) let v1 = Vector3(x, y, z)
let v2 = Vector3(u, w, q) let v2 = Vector3(u, w, q)
let l1 = v1.LengthSquared let l1 = v1.LengthSquared
let l2 = v2.LengthSquared let l2 = v2.LengthSquared
let vMin = Vector3.Min(v1, v2) let vMin = Vector3.Min(v1, v2)
if l1 < l2 then if l1 < l2 then
let equalsFirst = vMin = v1 let equalsFirst = vMin = v1
Assert.True(equalsFirst) Assert.True(equalsFirst)
else else
let equalsLast = vMin = v2 let equalsLast = vMin = v2
Assert.True(equalsLast) Assert.True(equalsLast)
[<Property>] [<Property>]
let ``Max selects the vector with greater magnitude given two vectors`` (x, y, z, u, w, q) = let ``Max selects the vector with greater magnitude given two vectors`` (x, y, z, u, w, q) =
let v1 = Vector3(x, y, z) let v1 = Vector3(x, y, z)
let v2 = Vector3(u, w, q) let v2 = Vector3(u, w, q)
let l1 = v1.LengthSquared let l1 = v1.LengthSquared
let l2 = v2.LengthSquared let l2 = v2.LengthSquared
let vMin = Vector3.Max(v1, v2) let vMin = Vector3.Max(v1, v2)
if l1 >= l2 then if l1 >= l2 then
let equalsFirst = vMin = v1 let equalsFirst = vMin = v1
Assert.True(equalsFirst) Assert.True(equalsFirst)
else else
let equalsLast = vMin = v2 let equalsLast = vMin = v2
Assert.True(equalsLast) Assert.True(equalsLast)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Clamping = module Clamping =
// //
[<Property>] [<Property>]
let ``Clamping one vector between two other vectors clamps all components between corresponding components`` (a : Vector3, b : Vector3, w : Vector3) = let ``Clamping one vector between two other vectors clamps all components between corresponding components`` (a : Vector3, b : Vector3, w : Vector3) =
let res = Vector3.Clamp(w, a, b) let res = Vector3.Clamp(w, a, b)
let expX = if w.X < a.X then a.X else if w.X > b.X then b.X else w.X let expX = if w.X < a.X then a.X else if w.X > b.X then b.X else w.X
let expY = if w.Y < a.Y then a.Y else if w.Y > b.Y then b.Y else w.Y let expY = if w.Y < a.Y then a.Y else if w.Y > b.Y then b.Y else w.Y
let expZ = if w.Z < a.Z then a.Z else if w.Z > b.Z then b.Z else w.Z let expZ = if w.Z < a.Z then a.Z else if w.Z > b.Z then b.Z else w.Z
Assert.Equal(expX, res.X) Assert.Equal(expX, res.X)
Assert.Equal(expY, res.Y) Assert.Equal(expY, res.Y)
Assert.Equal(expZ, res.Z) Assert.Equal(expZ, res.Z)
[<Property>] [<Property>]
let ``Clamping one vector between two other vectors by reference clamps all components between corresponding components`` (a : Vector3, b : Vector3, w : Vector3) = let ``Clamping one vector between two other vectors by reference clamps all components between corresponding components`` (a : Vector3, b : Vector3, w : Vector3) =
let res = Vector3.Clamp(ref w, ref a, ref b) let res = Vector3.Clamp(ref w, ref a, ref b)
let expX = if w.X < a.X then a.X else if w.X > b.X then b.X else w.X let expX = if w.X < a.X then a.X else if w.X > b.X then b.X else w.X
let expY = if w.Y < a.Y then a.Y else if w.Y > b.Y then b.Y else w.Y let expY = if w.Y < a.Y then a.Y else if w.Y > b.Y then b.Y else w.Y
let expZ = if w.Z < a.Z then a.Z else if w.Z > b.Z then b.Z else w.Z let expZ = if w.Z < a.Z then a.Z else if w.Z > b.Z then b.Z else w.Z
Assert.Equal(expX, res.X) Assert.Equal(expX, res.X)
Assert.Equal(expY, res.Y) Assert.Equal(expY, res.Y)
Assert.Equal(expZ, res.Z) Assert.Equal(expZ, res.Z)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module ``Unit vectors``= module ``Unit vectors``=
// //
[<Property>] [<Property>]
let ``Unit X is correct`` = let ``Unit X is correct`` =
let unitX = Vector3((float32)1, (float32)0, (float32)0) let unitX = Vector3((float32)1, (float32)0, (float32)0)
Assert.Equal(Vector3.UnitX, unitX) Assert.Equal(Vector3.UnitX, unitX)
[<Property>] [<Property>]
let ``Unit Y is correct`` = let ``Unit Y is correct`` =
let unitY = Vector3((float32)0, (float32)1, (float32)0) let unitY = Vector3((float32)0, (float32)1, (float32)0)
Assert.Equal(Vector3.UnitY, unitY) Assert.Equal(Vector3.UnitY, unitY)
[<Property>] [<Property>]
let ``Unit Z is correct`` = let ``Unit Z is correct`` =
let unitZ = Vector3((float32)0, (float32)0, (float32)1) let unitZ = Vector3((float32)0, (float32)0, (float32)1)
Assert.Equal(Vector3.UnitZ, unitZ) Assert.Equal(Vector3.UnitZ, unitZ)
[<Property>] [<Property>]
let ``Unit zero is correct`` = let ``Unit zero is correct`` =
let unitZero = Vector3((float32)0, (float32)0, (float32)0) let unitZero = Vector3((float32)0, (float32)0, (float32)0)
Assert.Equal(Vector3.Zero, unitZero) Assert.Equal(Vector3.Zero, unitZero)
[<Property>] [<Property>]
let ``Unit one is correct`` = let ``Unit one is correct`` =
let unitOne = Vector3((float32)1, (float32)1, (float32)1) let unitOne = Vector3((float32)1, (float32)1, (float32)1)
Assert.Equal(Vector3.One, unitOne) Assert.Equal(Vector3.One, unitOne)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Serialization = module Serialization =
// //
[<Property>] [<Property>]
let ``The absolute size of a Vector3 is always the size of its components`` (v : Vector3) = let ``The absolute size of a Vector3 is always the size of its components`` (v : Vector3) =
let expectedSize = sizeof<float32> * 3 let expectedSize = sizeof<float32> * 3
Assert.Equal(expectedSize, Vector3.SizeInBytes) Assert.Equal(expectedSize, Vector3.SizeInBytes)
Assert.Equal(expectedSize, Marshal.SizeOf(Vector3())) Assert.Equal(expectedSize, Marshal.SizeOf(Vector3()))

View file

@ -7,30 +7,30 @@ open System
open System.Runtime.InteropServices open System.Runtime.InteropServices
open OpenTK open OpenTK
module Vector4 = module Vector4 =
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Constructors = module Constructors =
// //
[<Property>] [<Property>]
let ``Triple value constructor sets all components to the correct values`` (x, y, z, w) = let ``Triple value constructor sets all components to the correct values`` (x, y, z, w) =
let v = Vector4(x, y, z, w) let v = Vector4(x, y, z, w)
Assert.Equal(x, v.X) Assert.Equal(x, v.X)
Assert.Equal(y, v.Y) Assert.Equal(y, v.Y)
Assert.Equal(z, v.Z) Assert.Equal(z, v.Z)
Assert.Equal(w, v.W) Assert.Equal(w, v.W)
[<Property>] [<Property>]
let ``Single value constructor sets all components to the correct values`` (a : float32) = let ``Single value constructor sets all components to the correct values`` (a : float32) =
let v = Vector4(a) let v = Vector4(a)
Assert.Equal(a, v.X) Assert.Equal(a, v.X)
Assert.Equal(a, v.Y) Assert.Equal(a, v.Y)
Assert.Equal(a, v.Z) Assert.Equal(a, v.Z)
Assert.Equal(a, v.W) Assert.Equal(a, v.W)
[<Property>] [<Property>]
let ``Vector2 value constructor sets all components to the correct values`` (x, y) = let ``Vector2 value constructor sets all components to the correct values`` (x, y) =
let v1 = Vector2(x, y) let v1 = Vector2(x, y)
let v2 = Vector4(v1) let v2 = Vector4(v1)
@ -41,9 +41,9 @@ module Vector4 =
Assert.Equal(y, v2.Y) Assert.Equal(y, v2.Y)
Assert.Equal((float32)0, v2.Z) Assert.Equal((float32)0, v2.Z)
Assert.Equal((float32)0, v2.W) Assert.Equal((float32)0, v2.W)
[<Property>] [<Property>]
let ``Vector3 value constructor sets all components to the correct values`` (x, y, z) = let ``Vector3 value constructor sets all components to the correct values`` (x, y, z) =
let v1 = Vector3(x, y, z) let v1 = Vector3(x, y, z)
let v2 = Vector4(v1) let v2 = Vector4(v1)
@ -55,9 +55,9 @@ module Vector4 =
Assert.Equal(y, v2.Y) Assert.Equal(y, v2.Y)
Assert.Equal(z, v2.Z) Assert.Equal(z, v2.Z)
Assert.Equal((float32)0, v2.W) Assert.Equal((float32)0, v2.W)
[<Property>] [<Property>]
let ``Vector3 value and scalar constructor sets all components to the correct values`` (x, y, z, w) = let ``Vector3 value and scalar constructor sets all components to the correct values`` (x, y, z, w) =
let v1 = Vector3(x, y, z) let v1 = Vector3(x, y, z)
let v2 = Vector4(v1, w) let v2 = Vector4(v1, w)
@ -69,9 +69,9 @@ module Vector4 =
Assert.Equal(y, v2.Y) Assert.Equal(y, v2.Y)
Assert.Equal(z, v2.Z) Assert.Equal(z, v2.Z)
Assert.Equal(w, v2.W) Assert.Equal(w, v2.W)
[<Property>] [<Property>]
let ``Vector4 value constructor sets all components to the correct values`` (x, y, z, w) = let ``Vector4 value constructor sets all components to the correct values`` (x, y, z, w) =
let v1 = Vector4(x, y, z, w) let v1 = Vector4(x, y, z, w)
let v2 = Vector4(v1) let v2 = Vector4(v1)
@ -84,72 +84,72 @@ module Vector4 =
Assert.Equal(y, v2.Y) Assert.Equal(y, v2.Y)
Assert.Equal(z, v2.Z) Assert.Equal(z, v2.Z)
Assert.Equal(w, v2.W) Assert.Equal(w, v2.W)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Indexing = module Indexing =
// //
[<Property>] [<Property>]
let ``Index operator accesses the correct components`` (x, y, z, w) = let ``Index operator accesses the correct components`` (x, y, z, w) =
let v = Vector4(x, y, z, w) let v = Vector4(x, y, z, w)
Assert.Equal(x, v.[0]) Assert.Equal(x, v.[0])
Assert.Equal(y, v.[1]) Assert.Equal(y, v.[1])
Assert.Equal(z, v.[2]) Assert.Equal(z, v.[2])
Assert.Equal(w, v.[3]) Assert.Equal(w, v.[3])
[<Property>] [<Property>]
let ``Indexed set operator throws exception for negative indices`` (x, y, z, w) = let ``Indexed set operator throws exception for negative indices`` (x, y, z, w) =
let mutable v = Vector4(x, y, z, w) let mutable v = Vector4(x, y, z, w)
(fun() -> v.[-1] <- x) |> Assert.Throws<IndexOutOfRangeException> |> ignore (fun() -> v.[-1] <- x) |> Assert.Throws<IndexOutOfRangeException> |> ignore
[<Property>] [<Property>]
let ``Indexed get operator throws exception for negative indices`` (x, y, z, w) = let ``Indexed get operator throws exception for negative indices`` (x, y, z, w) =
let mutable v = Vector4(x, y, z, w) let mutable v = Vector4(x, y, z, w)
(fun() -> v.[-1] |> ignore) |> Assert.Throws<IndexOutOfRangeException> |> ignore (fun() -> v.[-1] |> ignore) |> Assert.Throws<IndexOutOfRangeException> |> ignore
[<Property>] [<Property>]
let ``Indexed set operator throws exception for large indices`` (x, y, z, w) = let ``Indexed set operator throws exception for large indices`` (x, y, z, w) =
let mutable v = Vector4(x, y, z, w) let mutable v = Vector4(x, y, z, w)
(fun() -> v.[4] <- x) |> Assert.Throws<IndexOutOfRangeException> |> ignore (fun() -> v.[4] <- x) |> Assert.Throws<IndexOutOfRangeException> |> ignore
[<Property>] [<Property>]
let ``Indexed get operator throws exception for large indices`` (x, y, z, w) = let ``Indexed get operator throws exception for large indices`` (x, y, z, w) =
let mutable v = Vector4(x, y, z, w) let mutable v = Vector4(x, y, z, w)
(fun() -> v.[4] |> ignore) |> Assert.Throws<IndexOutOfRangeException> |> ignore (fun() -> v.[4] |> ignore) |> Assert.Throws<IndexOutOfRangeException> |> ignore
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Length = module Length =
// //
[<Property>] [<Property>]
let ``Length method follows the pythagorean theorem`` (x, y, z, w) = let ``Length method follows the pythagorean theorem`` (x, y, z, w) =
let v = Vector4(x, y, z, w) let v = Vector4(x, y, z, w)
let l = System.Math.Sqrt((float)(x * x + y * y + z * z + w * w)) let l = System.Math.Sqrt((float)(x * x + y * y + z * z + w * w))
Assert.Equal((float32)l, v.Length) Assert.Equal((float32)l, v.Length)
[<Property>] [<Property>]
let ``Fast length method is the same as one divided by the fast inverse square`` (x, y, z, w) = let ``Fast length method is the same as one divided by the fast inverse square`` (x, y, z, w) =
let v = Vector4(x, y, z, w) let v = Vector4(x, y, z, w)
let l = 1.0f / MathHelper.InverseSqrtFast(x * x + y * y + z * z + w * w) let l = 1.0f / MathHelper.InverseSqrtFast(x * x + y * y + z * z + w * w)
Assert.Equal(l, v.LengthFast) Assert.Equal(l, v.LengthFast)
[<Property>] [<Property>]
let ``Length squared method returns each component squared and summed`` (x, y, z, w) = let ``Length squared method returns each component squared and summed`` (x, y, z, w) =
let v = Vector4(x, y, z, w) let v = Vector4(x, y, z, w)
let lsq = x * x + y * y + z * z + w * w let lsq = x * x + y * y + z * z + w * w
Assert.Equal(lsq, v.LengthSquared) Assert.Equal(lsq, v.LengthSquared)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Normalization = module Normalization =
// //
[<Property>] [<Property>]
let ``Normalization creates a new unit length vector with the correct components`` (x, y, z, w) = let ``Normalization creates a new unit length vector with the correct components`` (x, y, z, w) =
let v = Vector4(x, y, z, w) let v = Vector4(x, y, z, w)
let l = v.Length let l = v.Length
@ -161,7 +161,7 @@ module Vector4 =
Assert.ApproximatelyEqual(v.W / l, norm.W) Assert.ApproximatelyEqual(v.W / l, norm.W)
[<Property>] [<Property>]
let ``Normalization of instance transforms the instance into a unit length vector with the correct components`` (x, y, z, w) = let ``Normalization of instance transforms the instance into a unit length vector with the correct components`` (x, y, z, w) =
let v = Vector4(x, y, z, w) let v = Vector4(x, y, z, w)
let l = v.Length let l = v.Length
@ -174,7 +174,7 @@ module Vector4 =
Assert.ApproximatelyEqual(v.W / l, norm.W) Assert.ApproximatelyEqual(v.W / l, norm.W)
[<Property>] [<Property>]
let ``Fast approximate normalization of instance transforms the instance into a unit length vector with the correct components`` (x, y, z, w) = let ``Fast approximate normalization of instance transforms the instance into a unit length vector with the correct components`` (x, y, z, w) =
let v = Vector4(x, y, z, w) let v = Vector4(x, y, z, w)
let norm = Vector4(x, y, z, w) let norm = Vector4(x, y, z, w)
norm.NormalizeFast() norm.NormalizeFast()
@ -185,240 +185,240 @@ module Vector4 =
Assert.ApproximatelyEqual(v.Y * scale, norm.Y) Assert.ApproximatelyEqual(v.Y * scale, norm.Y)
Assert.ApproximatelyEqual(v.Z * scale, norm.Z) Assert.ApproximatelyEqual(v.Z * scale, norm.Z)
Assert.ApproximatelyEqual(v.W * scale, norm.W) Assert.ApproximatelyEqual(v.W * scale, norm.W)
[<Property>] [<Property>]
let ``Normalization by reference is the same as division by magnitude`` (a : Vector4) = let ``Normalization by reference is the same as division by magnitude`` (a : Vector4) =
let norm = a / a.Length let norm = a / a.Length
let vRes = Vector4.Normalize(ref a) let vRes = Vector4.Normalize(ref a)
Assert.ApproximatelyEqual(norm, vRes) Assert.ApproximatelyEqual(norm, vRes)
[<Property>] [<Property>]
let ``Normalization is the same as division by magnitude`` (a : Vector4) = let ``Normalization is the same as division by magnitude`` (a : Vector4) =
let norm = a / a.Length let norm = a / a.Length
Assert.ApproximatelyEqual(norm, Vector4.Normalize(a)); Assert.ApproximatelyEqual(norm, Vector4.Normalize(a));
[<Property>] [<Property>]
let ``Fast approximate normalization by reference is the same as multiplication by the fast inverse square`` (a : Vector4) = let ``Fast approximate normalization by reference is the same as multiplication by the fast inverse square`` (a : Vector4) =
let scale = MathHelper.InverseSqrtFast(a.X * a.X + a.Y * a.Y + a.Z * a.Z + a.W * a.W) let scale = MathHelper.InverseSqrtFast(a.X * a.X + a.Y * a.Y + a.Z * a.Z + a.W * a.W)
let norm = a * scale let norm = a * scale
let vRes = Vector4.NormalizeFast(ref a) let vRes = Vector4.NormalizeFast(ref a)
Assert.ApproximatelyEqual(norm, vRes) Assert.ApproximatelyEqual(norm, vRes)
[<Property>] [<Property>]
let ``Fast approximate normalization is the same as multiplication by the fast inverse square`` (a : Vector4) = let ``Fast approximate normalization is the same as multiplication by the fast inverse square`` (a : Vector4) =
let scale = MathHelper.InverseSqrtFast(a.X * a.X + a.Y * a.Y + a.Z * a.Z + a.W * a.W) let scale = MathHelper.InverseSqrtFast(a.X * a.X + a.Y * a.Y + a.Z * a.Z + a.W * a.W)
let norm = a * scale let norm = a * scale
Assert.ApproximatelyEqual(norm, Vector4.NormalizeFast(a)); Assert.ApproximatelyEqual(norm, Vector4.NormalizeFast(a));
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Addition = module Addition =
// //
[<Property>] [<Property>]
let ``Vector4 addition is the same as component addition`` (a : Vector4, b : Vector4) = let ``Vector4 addition is the same as component addition`` (a : Vector4, b : Vector4) =
let c = a + b let c = a + b
Assert.ApproximatelyEqual(a.X + b.X,c.X) Assert.ApproximatelyEqual(a.X + b.X,c.X)
Assert.ApproximatelyEqual(a.Y + b.Y,c.Y) Assert.ApproximatelyEqual(a.Y + b.Y,c.Y)
Assert.ApproximatelyEqual(a.Z + b.Z,c.Z) Assert.ApproximatelyEqual(a.Z + b.Z,c.Z)
Assert.ApproximatelyEqual(a.W + b.W,c.W) Assert.ApproximatelyEqual(a.W + b.W,c.W)
[<Property>] [<Property>]
let ``Vector4 addition is commutative`` (a : Vector4, b : Vector4) = let ``Vector4 addition is commutative`` (a : Vector4, b : Vector4) =
let c = a + b let c = a + b
let c2 = b + a let c2 = b + a
Assert.ApproximatelyEqual(c, c2) Assert.ApproximatelyEqual(c, c2)
[<Property>] [<Property>]
let ``Vector4 addition is associative`` (a : Vector4, b : Vector4, c : Vector4) = let ``Vector4 addition is associative`` (a : Vector4, b : Vector4, c : Vector4) =
let r1 = (a + b) + c let r1 = (a + b) + c
let r2 = a + (b + c) let r2 = a + (b + c)
Assert.ApproximatelyEqual(r1, r2) Assert.ApproximatelyEqual(r1, r2)
[<Property>] [<Property>]
let ``Static Vector4 addition method is the same as component addition`` (a : Vector4, b : Vector4) = let ``Static Vector4 addition method is the same as component addition`` (a : Vector4, b : Vector4) =
let v1 = Vector4(a.X + b.X, a.Y + b.Y, a.Z + b.Z, a.W + b.W) let v1 = Vector4(a.X + b.X, a.Y + b.Y, a.Z + b.Z, a.W + b.W)
let sum = Vector4.Add(a, b) let sum = Vector4.Add(a, b)
Assert.ApproximatelyEqual(v1, sum) Assert.ApproximatelyEqual(v1, sum)
[<Property>] [<Property>]
let ``Static Vector4 addition method by reference is the same as component addition`` (a : Vector4, b : Vector4) = let ``Static Vector4 addition method by reference is the same as component addition`` (a : Vector4, b : Vector4) =
let v1 = Vector4(a.X + b.X, a.Y + b.Y, a.Z + b.Z, a.W + b.W) let v1 = Vector4(a.X + b.X, a.Y + b.Y, a.Z + b.Z, a.W + b.W)
let sum = Vector4.Add(ref a, ref b) let sum = Vector4.Add(ref a, ref b)
Assert.ApproximatelyEqual(v1, sum) Assert.ApproximatelyEqual(v1, sum)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Subtraction = module Subtraction =
// //
[<Property>] [<Property>]
let ``Vector4 subtraction is the same as component subtraction`` (a : Vector4, b : Vector4) = let ``Vector4 subtraction is the same as component subtraction`` (a : Vector4, b : Vector4) =
let c = a - b let c = a - b
Assert.Equal(a.X - b.X,c.X) Assert.Equal(a.X - b.X,c.X)
Assert.Equal(a.Y - b.Y,c.Y) Assert.Equal(a.Y - b.Y,c.Y)
Assert.Equal(a.Z - b.Z,c.Z) Assert.Equal(a.Z - b.Z,c.Z)
Assert.Equal(a.W - b.W,c.W) Assert.Equal(a.W - b.W,c.W)
[<Property>] [<Property>]
let ``Static Vector4 subtraction method is the same as component addition`` (a : Vector4, b : Vector4) = let ``Static Vector4 subtraction method is the same as component addition`` (a : Vector4, b : Vector4) =
let v1 = Vector4(a.X - b.X, a.Y - b.Y, a.Z - b.Z, a.W - b.W) let v1 = Vector4(a.X - b.X, a.Y - b.Y, a.Z - b.Z, a.W - b.W)
let sum = Vector4.Subtract(a, b) let sum = Vector4.Subtract(a, b)
Assert.ApproximatelyEqual(v1, sum) Assert.ApproximatelyEqual(v1, sum)
[<Property>] [<Property>]
let ``Static Vector4 subtraction method by reference is the same as component addition`` (a : Vector4, b : Vector4) = let ``Static Vector4 subtraction method by reference is the same as component addition`` (a : Vector4, b : Vector4) =
let v1 = Vector4(a.X - b.X, a.Y - b.Y, a.Z - b.Z, a.W - b.W) let v1 = Vector4(a.X - b.X, a.Y - b.Y, a.Z - b.Z, a.W - b.W)
let sum = Vector4.Subtract(ref a, ref b) let sum = Vector4.Subtract(ref a, ref b)
Assert.ApproximatelyEqual(v1, sum) Assert.ApproximatelyEqual(v1, sum)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Multiplication = module Multiplication =
// //
[<Property>] [<Property>]
let ``Vector4 multiplication is the same as component multiplication`` (a : Vector4, b : Vector4) = let ``Vector4 multiplication is the same as component multiplication`` (a : Vector4, b : Vector4) =
let c = a * b let c = a * b
Assert.Equal(a.X * b.X,c.X) Assert.Equal(a.X * b.X,c.X)
Assert.Equal(a.Y * b.Y,c.Y) Assert.Equal(a.Y * b.Y,c.Y)
Assert.Equal(a.Z * b.Z,c.Z) Assert.Equal(a.Z * b.Z,c.Z)
Assert.Equal(a.W * b.W,c.W) Assert.Equal(a.W * b.W,c.W)
[<Property>] [<Property>]
let ``Vector4 multiplication is commutative`` (a : Vector4, b : Vector4) = let ``Vector4 multiplication is commutative`` (a : Vector4, b : Vector4) =
let r1 = a * b let r1 = a * b
let r2 = b * a let r2 = b * a
Assert.Equal(r1, r2) Assert.Equal(r1, r2)
[<Property>] [<Property>]
let ``Left-handed Vector4-scalar multiplication is the same as component-scalar multiplication`` (a : Vector4, f : float32) = let ``Left-handed Vector4-scalar multiplication is the same as component-scalar multiplication`` (a : Vector4, f : float32) =
let r = a * f let r = a * f
Assert.Equal(a.X * f,r.X) Assert.Equal(a.X * f,r.X)
Assert.Equal(a.Y * f,r.Y) Assert.Equal(a.Y * f,r.Y)
Assert.Equal(a.Z * f,r.Z) Assert.Equal(a.Z * f,r.Z)
Assert.Equal(a.W * f,r.W) Assert.Equal(a.W * f,r.W)
[<Property>] [<Property>]
let ``Right-handed Vector4-scalar multiplication is the same as component-scalar multiplication`` (a : Vector4, f : float32) = let ``Right-handed Vector4-scalar multiplication is the same as component-scalar multiplication`` (a : Vector4, f : float32) =
let r = f * a let r = f * a
Assert.Equal(a.X * f,r.X) Assert.Equal(a.X * f,r.X)
Assert.Equal(a.Y * f,r.Y) Assert.Equal(a.Y * f,r.Y)
Assert.Equal(a.Z * f,r.Z) Assert.Equal(a.Z * f,r.Z)
Assert.Equal(a.W * f,r.W) Assert.Equal(a.W * f,r.W)
[<Property>] [<Property>]
let ``Static method Vector4-scalar multiplication is the same as component-scalar multiplication`` (a : Vector4, f : float32) = let ``Static method Vector4-scalar multiplication is the same as component-scalar multiplication`` (a : Vector4, f : float32) =
let r = Vector4.Multiply(a, f) let r = Vector4.Multiply(a, f)
Assert.Equal(a.X * f,r.X) Assert.Equal(a.X * f,r.X)
Assert.Equal(a.Y * f,r.Y) Assert.Equal(a.Y * f,r.Y)
Assert.Equal(a.Z * f,r.Z) Assert.Equal(a.Z * f,r.Z)
Assert.Equal(a.W * f,r.W) Assert.Equal(a.W * f,r.W)
[<Property>] [<Property>]
let ``Vector4-Matrix4 multiplication using right-handed notation is the same as vector/row multiplication and summation`` (a : Matrix4, b : Vector4) = let ``Vector4-Matrix4 multiplication using right-handed notation is the same as vector/row multiplication and summation`` (a : Matrix4, b : Vector4) =
let res = a*b let res = a*b
let c1 = b.X * a.M11 + b.Y * a.M12 + b.Z * a.M13 + b.W * a.M14 let c1 = b.X * a.M11 + b.Y * a.M12 + b.Z * a.M13 + b.W * a.M14
let c2 = b.X * a.M21 + b.Y * a.M22 + b.Z * a.M23 + b.W * a.M24 let c2 = b.X * a.M21 + b.Y * a.M22 + b.Z * a.M23 + b.W * a.M24
let c3 = b.X * a.M31 + b.Y * a.M32 + b.Z * a.M33 + b.W * a.M34 let c3 = b.X * a.M31 + b.Y * a.M32 + b.Z * a.M33 + b.W * a.M34
let c4 = b.X * a.M41 + b.Y * a.M42 + b.Z * a.M43 + b.W * a.M44 let c4 = b.X * a.M41 + b.Y * a.M42 + b.Z * a.M43 + b.W * a.M44
let exp = Vector4(c1, c2, c3, c4) let exp = Vector4(c1, c2, c3, c4)
Assert.Equal(exp, res) Assert.Equal(exp, res)
[<Property>] [<Property>]
let ``Vector4-Matrix4 multiplication using left-handed notation is the same as vector/column multiplication and summation`` (a : Matrix4, b : Vector4) = let ``Vector4-Matrix4 multiplication using left-handed notation is the same as vector/column multiplication and summation`` (a : Matrix4, b : Vector4) =
let res = b*a let res = b*a
let c1 = b.X * a.M11 + b.Y * a.M21 + b.Z * a.M31 + b.W * a.M41 let c1 = b.X * a.M11 + b.Y * a.M21 + b.Z * a.M31 + b.W * a.M41
let c2 = b.X * a.M12 + b.Y * a.M22 + b.Z * a.M32 + b.W * a.M42 let c2 = b.X * a.M12 + b.Y * a.M22 + b.Z * a.M32 + b.W * a.M42
let c3 = b.X * a.M13 + b.Y * a.M23 + b.Z * a.M33 + b.W * a.M43 let c3 = b.X * a.M13 + b.Y * a.M23 + b.Z * a.M33 + b.W * a.M43
let c4 = b.X * a.M14 + b.Y * a.M24 + b.Z * a.M34 + b.W * a.M44 let c4 = b.X * a.M14 + b.Y * a.M24 + b.Z * a.M34 + b.W * a.M44
let exp = Vector4(c1, c2, c3, c4) let exp = Vector4(c1, c2, c3, c4)
Assert.Equal(exp, res) Assert.Equal(exp, res)
[<Property>] [<Property>]
let ``Static Vector4 multiplication method is the same as component multiplication`` (a : Vector4, b : Vector4) = let ``Static Vector4 multiplication method is the same as component multiplication`` (a : Vector4, b : Vector4) =
let v1 = Vector4(a.X * b.X, a.Y * b.Y, a.Z * b.Z, a.W * b.W) let v1 = Vector4(a.X * b.X, a.Y * b.Y, a.Z * b.Z, a.W * b.W)
let sum = Vector4.Multiply(a, b) let sum = Vector4.Multiply(a, b)
Assert.ApproximatelyEqual(v1, sum) Assert.ApproximatelyEqual(v1, sum)
[<Property>] [<Property>]
let ``Static Vector4 multiplication method by reference is the same as component multiplication`` (a : Vector4, b : Vector4) = let ``Static Vector4 multiplication method by reference is the same as component multiplication`` (a : Vector4, b : Vector4) =
let v1 = Vector4(a.X * b.X, a.Y * b.Y, a.Z * b.Z, a.W * b.W) let v1 = Vector4(a.X * b.X, a.Y * b.Y, a.Z * b.Z, a.W * b.W)
let sum = Vector4.Multiply(ref a, ref b) let sum = Vector4.Multiply(ref a, ref b)
Assert.ApproximatelyEqual(v1, sum) Assert.ApproximatelyEqual(v1, sum)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Division = module Division =
// //
[<Property>] [<Property>]
let ``Vector4-float division is the same as component-float division`` (a : Vector4, f : float32) = let ``Vector4-float division is the same as component-float division`` (a : Vector4, f : float32) =
if not (approxEq f 0.0f) then // we don't support diving by zero. if not (approxEq f 0.0f) then // we don't support diving by zero.
let r = a / f let r = a / f
Assert.ApproximatelyEqual(a.X / f, r.X) Assert.ApproximatelyEqual(a.X / f, r.X)
Assert.ApproximatelyEqual(a.Y / f, r.Y) Assert.ApproximatelyEqual(a.Y / f, r.Y)
Assert.ApproximatelyEqual(a.Z / f, r.Z) Assert.ApproximatelyEqual(a.Z / f, r.Z)
Assert.ApproximatelyEqual(a.W / f, r.W) Assert.ApproximatelyEqual(a.W / f, r.W)
[<Property>] [<Property>]
let ``Static Vector4-Vector4 division method is the same as component division`` (a : Vector4, b : Vector4) = let ``Static Vector4-Vector4 division method is the same as component division`` (a : Vector4, b : Vector4) =
let v1 = Vector4(a.X / b.X, a.Y / b.Y, a.Z / b.Z, a.W / b.W) let v1 = Vector4(a.X / b.X, a.Y / b.Y, a.Z / b.Z, a.W / b.W)
let sum = Vector4.Divide(a, b) let sum = Vector4.Divide(a, b)
Assert.ApproximatelyEqual(v1, sum) Assert.ApproximatelyEqual(v1, sum)
[<Property>] [<Property>]
let ``Static Vector4-Vector4 divison method by reference is the same as component division`` (a : Vector4, b : Vector4) = let ``Static Vector4-Vector4 divison method by reference is the same as component division`` (a : Vector4, b : Vector4) =
let v1 = Vector4(a.X / b.X, a.Y / b.Y, a.Z / b.Z, a.W / b.W) let v1 = Vector4(a.X / b.X, a.Y / b.Y, a.Z / b.Z, a.W / b.W)
let sum = Vector4.Divide(ref a, ref b) let sum = Vector4.Divide(ref a, ref b)
Assert.ApproximatelyEqual(v1, sum) Assert.ApproximatelyEqual(v1, sum)
[<Property>] [<Property>]
let ``Static Vector4-scalar division method is the same as component division`` (a : Vector4, b : float32) = let ``Static Vector4-scalar division method is the same as component division`` (a : Vector4, b : float32) =
let v1 = Vector4(a.X / b, a.Y / b, a.Z / b, a.W / b) let v1 = Vector4(a.X / b, a.Y / b, a.Z / b, a.W / b)
let sum = Vector4.Divide(a, b) let sum = Vector4.Divide(a, b)
Assert.ApproximatelyEqual(v1, sum) Assert.ApproximatelyEqual(v1, sum)
[<Property>] [<Property>]
let ``Static Vector4-scalar divison method by reference is the same as component division`` (a : Vector4, b : float32) = let ``Static Vector4-scalar divison method by reference is the same as component division`` (a : Vector4, b : float32) =
let v1 = Vector4(a.X / b, a.Y / b, a.Z / b, a.W / b) let v1 = Vector4(a.X / b, a.Y / b, a.Z / b, a.W / b)
let sum = Vector4.Divide(ref a, b) let sum = Vector4.Divide(ref a, b)
Assert.ApproximatelyEqual(v1, sum) Assert.ApproximatelyEqual(v1, sum)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Negation = module Negation =
// //
@ -430,7 +430,7 @@ module Vector4 =
Assert.Equal(-y, vNeg.Y) Assert.Equal(-y, vNeg.Y)
Assert.Equal(-z, vNeg.Z) Assert.Equal(-z, vNeg.Z)
Assert.Equal(-w, vNeg.W) Assert.Equal(-w, vNeg.W)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Equality = module Equality =
// //
@ -439,64 +439,64 @@ module Vector4 =
let v1 = Vector4(x, y, z, w) let v1 = Vector4(x, y, z, w)
let v2 = Vector4(x, y, z, w) let v2 = Vector4(x, y, z, w)
let equality = v1 = v2 let equality = v1 = v2
Assert.True(equality) Assert.True(equality)
[<Property>] [<Property>]
let ``Vector inequality operator is by component`` (x, y, z, w) = let ``Vector inequality operator is by component`` (x, y, z, w) =
let v1 = Vector4(x, y, z, w) let v1 = Vector4(x, y, z, w)
let v2 = Vector4(x + (float32)1 , y + (float32)1, z + (float32)1, w + (float32)1) let v2 = Vector4(x + (float32)1 , y + (float32)1, z + (float32)1, w + (float32)1)
let inequality = v1 <> v2 let inequality = v1 <> v2
Assert.True(inequality) Assert.True(inequality)
[<Property>] [<Property>]
let ``Vector equality method is by component`` (x, y, z, w) = let ``Vector equality method is by component`` (x, y, z, w) =
let v1 = Vector4(x, y, z, w) let v1 = Vector4(x, y, z, w)
let v2 = Vector4(x, y, z, w) let v2 = Vector4(x, y, z, w)
let notVector = Matrix2() let notVector = Matrix2()
let equality = v1.Equals(v2) let equality = v1.Equals(v2)
let inequalityByOtherType = v1.Equals(notVector) let inequalityByOtherType = v1.Equals(notVector)
Assert.True(equality) Assert.True(equality)
Assert.False(inequalityByOtherType) Assert.False(inequalityByOtherType)
[<Property>] [<Property>]
let ``Vector equality method returns false for other classes`` (x, y, z, w) = let ``Vector equality method returns false for other classes`` (x, y, z, w) =
let v1 = Vector4(x, y, z, w) let v1 = Vector4(x, y, z, w)
let notVector = Matrix2() let notVector = Matrix2()
let inequalityByOtherType = v1.Equals(notVector) let inequalityByOtherType = v1.Equals(notVector)
Assert.False(inequalityByOtherType) Assert.False(inequalityByOtherType)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Swizzling = module Swizzling =
// //
[<Property>] [<Property>]
let ``Vector swizzling returns the correct composite for X-primary components`` (x, y, z, w) = let ``Vector swizzling returns the correct composite for X-primary components`` (x, y, z, w) =
let v = Vector4(x, y, z, w) let v = Vector4(x, y, z, w)
let xyzw = v let xyzw = v
let xywz = Vector4(x, y, w, z) let xywz = Vector4(x, y, w, z)
let xzyw = Vector4(x, z, y, w) let xzyw = Vector4(x, z, y, w)
let xzwy = Vector4(x, z, w, y) let xzwy = Vector4(x, z, w, y)
let xwyz = Vector4(x, w, y, z) let xwyz = Vector4(x, w, y, z)
let xwzy = Vector4(x, w, z, y) let xwzy = Vector4(x, w, z, y)
let xyz = Vector3(x, y, z) let xyz = Vector3(x, y, z)
let xyw = Vector3(x, y, w) let xyw = Vector3(x, y, w)
let xzy = Vector3(x, z, y) let xzy = Vector3(x, z, y)
let xzw = Vector3(x, z, w) let xzw = Vector3(x, z, w)
let xwy = Vector3(x, w, y) let xwy = Vector3(x, w, y)
let xwz = Vector3(x, w, z) let xwz = Vector3(x, w, z)
let xy = Vector2(x, y) let xy = Vector2(x, y)
let xz = Vector2(x, z) let xz = Vector2(x, z)
let xw = Vector2(x, w) let xw = Vector2(x, w)
// X primary // X primary
Assert.Equal(xyzw, v) Assert.Equal(xyzw, v)
Assert.Equal(xywz, v.Xywz) Assert.Equal(xywz, v.Xywz)
@ -504,23 +504,23 @@ module Vector4 =
Assert.Equal(xzwy, v.Xzwy) Assert.Equal(xzwy, v.Xzwy)
Assert.Equal(xwyz, v.Xwyz) Assert.Equal(xwyz, v.Xwyz)
Assert.Equal(xwzy, v.Xwzy) Assert.Equal(xwzy, v.Xwzy)
Assert.Equal(xyz, v.Xyz) Assert.Equal(xyz, v.Xyz)
Assert.Equal(xyw, v.Xyw) Assert.Equal(xyw, v.Xyw)
Assert.Equal(xzy, v.Xzy) Assert.Equal(xzy, v.Xzy)
Assert.Equal(xzw, v.Xzw) Assert.Equal(xzw, v.Xzw)
Assert.Equal(xwy, v.Xwy) Assert.Equal(xwy, v.Xwy)
Assert.Equal(xwz, v.Xwz) Assert.Equal(xwz, v.Xwz)
Assert.Equal(xy, v.Xy) Assert.Equal(xy, v.Xy)
Assert.Equal(xz, v.Xz) Assert.Equal(xz, v.Xz)
Assert.Equal(xw, v.Xw) Assert.Equal(xw, v.Xw)
[<Property>] [<Property>]
let ``Vector swizzling returns the correct composite for Y-primary components`` (x, y, z, w) = let ``Vector swizzling returns the correct composite for Y-primary components`` (x, y, z, w) =
let v = Vector4(x, y, z, w) let v = Vector4(x, y, z, w)
let yxzw = Vector4(y, x, z, w) let yxzw = Vector4(y, x, z, w)
let yxwz = Vector4(y, x, w, z) let yxwz = Vector4(y, x, w, z)
let yyzw = Vector4(y, y, z, w) let yyzw = Vector4(y, y, z, w)
@ -529,18 +529,18 @@ module Vector4 =
let yzwx = Vector4(y, z, w, x) let yzwx = Vector4(y, z, w, x)
let ywxz = Vector4(y, w, x, z) let ywxz = Vector4(y, w, x, z)
let ywzx = Vector4(y, w, z, x) let ywzx = Vector4(y, w, z, x)
let yxz = Vector3(y, x, z) let yxz = Vector3(y, x, z)
let yxw = Vector3(y, x, w) let yxw = Vector3(y, x, w)
let yzx = Vector3(y, z, x) let yzx = Vector3(y, z, x)
let yzw = Vector3(y, z, w) let yzw = Vector3(y, z, w)
let ywx = Vector3(y, w, x) let ywx = Vector3(y, w, x)
let ywz = Vector3(y, w, z) let ywz = Vector3(y, w, z)
let yx = Vector2(y, x) let yx = Vector2(y, x)
let yz = Vector2(y, z) let yz = Vector2(y, z)
let yw = Vector2(y, w) let yw = Vector2(y, w)
// Y primary // Y primary
Assert.Equal(yxzw, v.Yxzw) Assert.Equal(yxzw, v.Yxzw)
Assert.Equal(yxwz, v.Yxwz) Assert.Equal(yxwz, v.Yxwz)
@ -550,23 +550,23 @@ module Vector4 =
Assert.Equal(yzwx, v.Yzwx) Assert.Equal(yzwx, v.Yzwx)
Assert.Equal(ywxz, v.Ywxz) Assert.Equal(ywxz, v.Ywxz)
Assert.Equal(ywzx, v.Ywzx) Assert.Equal(ywzx, v.Ywzx)
Assert.Equal(yxz, v.Yxz) Assert.Equal(yxz, v.Yxz)
Assert.Equal(yxw, v.Yxw) Assert.Equal(yxw, v.Yxw)
Assert.Equal(yzx, v.Yzx) Assert.Equal(yzx, v.Yzx)
Assert.Equal(yzw, v.Yzw) Assert.Equal(yzw, v.Yzw)
Assert.Equal(ywx, v.Ywx) Assert.Equal(ywx, v.Ywx)
Assert.Equal(ywz, v.Ywz) Assert.Equal(ywz, v.Ywz)
Assert.Equal(yx, v.Yx) Assert.Equal(yx, v.Yx)
Assert.Equal(yz, v.Yz) Assert.Equal(yz, v.Yz)
Assert.Equal(yw, v.Yw) Assert.Equal(yw, v.Yw)
[<Property>] [<Property>]
let ``Vector swizzling returns the correct composite for Z-primary components`` (x, y, z, w) = let ``Vector swizzling returns the correct composite for Z-primary components`` (x, y, z, w) =
let v = Vector4(x, y, z, w) let v = Vector4(x, y, z, w)
let zxyw = Vector4(z, x, y, w) let zxyw = Vector4(z, x, y, w)
let zxwy = Vector4(z, x, w, y) let zxwy = Vector4(z, x, w, y)
let zyxw = Vector4(z, y, x, w) let zyxw = Vector4(z, y, x, w)
@ -574,18 +574,18 @@ module Vector4 =
let zwxy = Vector4(z, w, x, y) let zwxy = Vector4(z, w, x, y)
let zwyx = Vector4(z, w, y, x) let zwyx = Vector4(z, w, y, x)
let zwzy = Vector4(z, w, z, y) let zwzy = Vector4(z, w, z, y)
let zxy = Vector3(z, x, y) let zxy = Vector3(z, x, y)
let zxw = Vector3(z, x, w) let zxw = Vector3(z, x, w)
let zyx = Vector3(z, y, x) let zyx = Vector3(z, y, x)
let zyw = Vector3(z, y, w) let zyw = Vector3(z, y, w)
let zwx = Vector3(z, w, x) let zwx = Vector3(z, w, x)
let zwy = Vector3(z, w, y) let zwy = Vector3(z, w, y)
let zx = Vector2(z, x) let zx = Vector2(z, x)
let zy = Vector2(z, y) let zy = Vector2(z, y)
let zw = Vector2(z, w) let zw = Vector2(z, w)
// Z primary // Z primary
Assert.Equal(zxyw, v.Zxyw) Assert.Equal(zxyw, v.Zxyw)
Assert.Equal(zxwy, v.Zxwy) Assert.Equal(zxwy, v.Zxwy)
@ -594,21 +594,21 @@ module Vector4 =
Assert.Equal(zwxy, v.Zwxy) Assert.Equal(zwxy, v.Zwxy)
Assert.Equal(zwyx, v.Zwyx) Assert.Equal(zwyx, v.Zwyx)
Assert.Equal(zwzy, v.Zwzy) Assert.Equal(zwzy, v.Zwzy)
Assert.Equal(zxy, v.Zxy) Assert.Equal(zxy, v.Zxy)
Assert.Equal(zxw, v.Zxw) Assert.Equal(zxw, v.Zxw)
Assert.Equal(zyx, v.Zyx) Assert.Equal(zyx, v.Zyx)
Assert.Equal(zyw, v.Zyw) Assert.Equal(zyw, v.Zyw)
Assert.Equal(zwx, v.Zwx) Assert.Equal(zwx, v.Zwx)
Assert.Equal(zwy, v.Zwy) Assert.Equal(zwy, v.Zwy)
Assert.Equal(zx, v.Zx) Assert.Equal(zx, v.Zx)
Assert.Equal(zy, v.Zy) Assert.Equal(zy, v.Zy)
Assert.Equal(zw, v.Zw) Assert.Equal(zw, v.Zw)
[<Property>] [<Property>]
let ``Vector swizzling returns the correct composite for W-primary components`` (x, y, z, w) = let ``Vector swizzling returns the correct composite for W-primary components`` (x, y, z, w) =
let v = Vector4(x, y, z, w) let v = Vector4(x, y, z, w)
let wxyz = Vector4(w, x, y, z) let wxyz = Vector4(w, x, y, z)
@ -618,14 +618,14 @@ module Vector4 =
let wzxy = Vector4(w, z, x, y) let wzxy = Vector4(w, z, x, y)
let wzyx = Vector4(w, z, y, x) let wzyx = Vector4(w, z, y, x)
let wzyw = Vector4(w, z, y, w) let wzyw = Vector4(w, z, y, w)
let wxy = Vector3(w, x, y) let wxy = Vector3(w, x, y)
let wxz = Vector3(w, x, z) let wxz = Vector3(w, x, z)
let wyx = Vector3(w, y, x) let wyx = Vector3(w, y, x)
let wyz = Vector3(w, y, z) let wyz = Vector3(w, y, z)
let wzx = Vector3(w, z, x) let wzx = Vector3(w, z, x)
let wzy = Vector3(w, z, y) let wzy = Vector3(w, z, y)
let wx = Vector2(w, x) let wx = Vector2(w, x)
let wy = Vector2(w, y) let wy = Vector2(w, y)
let wz = Vector2(w, z) let wz = Vector2(w, z)
@ -638,18 +638,18 @@ module Vector4 =
Assert.Equal(wzxy, v.Wzxy) Assert.Equal(wzxy, v.Wzxy)
Assert.Equal(wzyx, v.Wzyx) Assert.Equal(wzyx, v.Wzyx)
Assert.Equal(wzyw, v.Wzyw) Assert.Equal(wzyw, v.Wzyw)
Assert.Equal(wxy, v.Wxy) Assert.Equal(wxy, v.Wxy)
Assert.Equal(wxz, v.Wxz) Assert.Equal(wxz, v.Wxz)
Assert.Equal(wyx, v.Wyx) Assert.Equal(wyx, v.Wyx)
Assert.Equal(wyz, v.Wyz) Assert.Equal(wyz, v.Wyz)
Assert.Equal(wzx, v.Wzx) Assert.Equal(wzx, v.Wzx)
Assert.Equal(wzy, v.Wzy) Assert.Equal(wzy, v.Wzy)
Assert.Equal(wx, v.Wx) Assert.Equal(wx, v.Wx)
Assert.Equal(wy, v.Wy) Assert.Equal(wy, v.Wy)
Assert.Equal(wz, v.Wz) Assert.Equal(wz, v.Wz)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Interpolation = module Interpolation =
// //
@ -657,40 +657,40 @@ module Vector4 =
let ``Linear interpolation is by component`` (a : Vector4, b : Vector4, q) = let ``Linear interpolation is by component`` (a : Vector4, b : Vector4, q) =
let blend = q let blend = q
let rX = blend * (b.X - a.X) + a.X let rX = blend * (b.X - a.X) + a.X
let rY = blend * (b.Y - a.Y) + a.Y let rY = blend * (b.Y - a.Y) + a.Y
let rZ = blend * (b.Z - a.Z) + a.Z let rZ = blend * (b.Z - a.Z) + a.Z
let rW = blend * (b.W - a.W) + a.W let rW = blend * (b.W - a.W) + a.W
let vExp = Vector4(rX, rY, rZ, rW) let vExp = Vector4(rX, rY, rZ, rW)
Assert.Equal(vExp, Vector4.Lerp(a, b, q)) Assert.Equal(vExp, Vector4.Lerp(a, b, q))
let vRes = Vector4.Lerp(ref a, ref b, q) let vRes = Vector4.Lerp(ref a, ref b, q)
Assert.Equal(vExp, vRes) Assert.Equal(vExp, vRes)
[<Property>] [<Property>]
let ``Barycentric interpolation follows the barycentric formula`` (a : Vector4, b : Vector4, c : Vector4, u, v) = let ``Barycentric interpolation follows the barycentric formula`` (a : Vector4, b : Vector4, c : Vector4, u, v) =
let r = a + u * (b - a) + v * (c - a) let r = a + u * (b - a) + v * (c - a)
Assert.Equal(r, Vector4.BaryCentric(a, b, c, u, v)) Assert.Equal(r, Vector4.BaryCentric(a, b, c, u, v))
let vRes = Vector4.BaryCentric(ref a, ref b, ref c, u, v) let vRes = Vector4.BaryCentric(ref a, ref b, ref c, u, v)
Assert.Equal(r, vRes) Assert.Equal(r, vRes)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module ``Vector products`` = module ``Vector products`` =
// //
[<Property>] [<Property>]
let ``Dot product method follows the dot product formula`` (a : Vector4, b : Vector4) = let ``Dot product method follows the dot product formula`` (a : Vector4, b : Vector4) =
let dot = a.X * b.X + a.Y * b.Y + a.Z * b.Z + a.W * b.W let dot = a.X * b.X + a.Y * b.Y + a.Z * b.Z + a.W * b.W
Assert.Equal(dot, Vector4.Dot(a, b)); Assert.Equal(dot, Vector4.Dot(a, b));
let vRes = Vector4.Dot(ref a, ref b) let vRes = Vector4.Dot(ref a, ref b)
Assert.Equal(dot, vRes) Assert.Equal(dot, vRes)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module ``Component min and max`` = module ``Component min and max`` =
// //
@ -698,112 +698,112 @@ module Vector4 =
let ``Min selects the vector with lesser magnitude given two vectors`` (x, y, z, w, a, b, c, d) = let ``Min selects the vector with lesser magnitude given two vectors`` (x, y, z, w, a, b, c, d) =
let v1 = Vector4(x, y, z, w) let v1 = Vector4(x, y, z, w)
let v2 = Vector4(a, b, c, d) let v2 = Vector4(a, b, c, d)
let l1 = v1.LengthSquared let l1 = v1.LengthSquared
let l2 = v2.LengthSquared let l2 = v2.LengthSquared
let vMin = Vector4.Min(v1, v2) let vMin = Vector4.Min(v1, v2)
if vMin = v1 then if vMin = v1 then
let v1ShorterThanv2 = l1 < l2 let v1ShorterThanv2 = l1 < l2
Assert.True(v1ShorterThanv2) Assert.True(v1ShorterThanv2)
else else
let v2ShorterThanv1 = l2 < l1 let v2ShorterThanv1 = l2 < l1
Assert.True(v2ShorterThanv1) Assert.True(v2ShorterThanv1)
[<Property>] [<Property>]
let ``Max selects the vector with greater magnitude given two vectors`` (x, y, z, w, a, b, c, d) = let ``Max selects the vector with greater magnitude given two vectors`` (x, y, z, w, a, b, c, d) =
let v1 = Vector4(x, y, z, w) let v1 = Vector4(x, y, z, w)
let v2 = Vector4(a, b, c, d) let v2 = Vector4(a, b, c, d)
let l1 = v1.LengthSquared let l1 = v1.LengthSquared
let l2 = v2.LengthSquared let l2 = v2.LengthSquared
let vMin = Vector4.Max(v1, v2) let vMin = Vector4.Max(v1, v2)
if vMin = v1 then if vMin = v1 then
let v1LongerThanOrEqualTov2 = l1 >= l2 let v1LongerThanOrEqualTov2 = l1 >= l2
Assert.True(v1LongerThanOrEqualTov2) Assert.True(v1LongerThanOrEqualTov2)
else else
let v2LongerThanv1 = l2 > l1 let v2LongerThanv1 = l2 > l1
Assert.True(v2LongerThanv1) Assert.True(v2LongerThanv1)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Clamping = module Clamping =
// //
[<Property>] [<Property>]
let ``Clamping one vector between two other vectors clamps all components between corresponding components`` (a : Vector4, b : Vector4, w : Vector4) = let ``Clamping one vector between two other vectors clamps all components between corresponding components`` (a : Vector4, b : Vector4, w : Vector4) =
let res = Vector4.Clamp(w, a, b) let res = Vector4.Clamp(w, a, b)
let expX = if w.X < a.X then a.X else if w.X > b.X then b.X else w.X let expX = if w.X < a.X then a.X else if w.X > b.X then b.X else w.X
let expY = if w.Y < a.Y then a.Y else if w.Y > b.Y then b.Y else w.Y let expY = if w.Y < a.Y then a.Y else if w.Y > b.Y then b.Y else w.Y
let expZ = if w.Z < a.Z then a.Z else if w.Z > b.Z then b.Z else w.Z let expZ = if w.Z < a.Z then a.Z else if w.Z > b.Z then b.Z else w.Z
let expW = if w.W < a.W then a.W else if w.W > b.W then b.W else w.W let expW = if w.W < a.W then a.W else if w.W > b.W then b.W else w.W
Assert.Equal(expX, res.X) Assert.Equal(expX, res.X)
Assert.Equal(expY, res.Y) Assert.Equal(expY, res.Y)
Assert.Equal(expZ, res.Z) Assert.Equal(expZ, res.Z)
Assert.Equal(expW, res.W) Assert.Equal(expW, res.W)
[<Property>] [<Property>]
let ``Clamping one vector between two other vectors by reference clamps all components`` (a : Vector4, b : Vector4, w : Vector4) = let ``Clamping one vector between two other vectors by reference clamps all components`` (a : Vector4, b : Vector4, w : Vector4) =
let res = Vector4.Clamp(ref w, ref a, ref b) let res = Vector4.Clamp(ref w, ref a, ref b)
let expX = if w.X < a.X then a.X else if w.X > b.X then b.X else w.X let expX = if w.X < a.X then a.X else if w.X > b.X then b.X else w.X
let expY = if w.Y < a.Y then a.Y else if w.Y > b.Y then b.Y else w.Y let expY = if w.Y < a.Y then a.Y else if w.Y > b.Y then b.Y else w.Y
let expZ = if w.Z < a.Z then a.Z else if w.Z > b.Z then b.Z else w.Z let expZ = if w.Z < a.Z then a.Z else if w.Z > b.Z then b.Z else w.Z
let expW = if w.W < a.W then a.W else if w.W > b.W then b.W else w.W let expW = if w.W < a.W then a.W else if w.W > b.W then b.W else w.W
Assert.Equal(expX, res.X) Assert.Equal(expX, res.X)
Assert.Equal(expY, res.Y) Assert.Equal(expY, res.Y)
Assert.Equal(expZ, res.Z) Assert.Equal(expZ, res.Z)
Assert.Equal(expW, res.W) Assert.Equal(expW, res.W)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module ``Unit vectors``= module ``Unit vectors``=
// //
[<Property>] [<Property>]
let ``Unit X is correct`` = let ``Unit X is correct`` =
let unitX = Vector4((float32)1, (float32)0, (float32)0, (float32)0) let unitX = Vector4((float32)1, (float32)0, (float32)0, (float32)0)
Assert.Equal(Vector4.UnitX, unitX) Assert.Equal(Vector4.UnitX, unitX)
[<Property>] [<Property>]
let ``Unit Y is correct`` = let ``Unit Y is correct`` =
let unitY = Vector4((float32)0, (float32)1, (float32)0, (float32)0) let unitY = Vector4((float32)0, (float32)1, (float32)0, (float32)0)
Assert.Equal(Vector4.UnitY, unitY) Assert.Equal(Vector4.UnitY, unitY)
[<Property>] [<Property>]
let ``Unit Z is correct`` = let ``Unit Z is correct`` =
let unitZ = Vector4((float32)0, (float32)0, (float32)1, (float32)0) let unitZ = Vector4((float32)0, (float32)0, (float32)1, (float32)0)
Assert.Equal(Vector4.UnitZ, unitZ) Assert.Equal(Vector4.UnitZ, unitZ)
[<Property>] [<Property>]
let ``Unit W is correct`` = let ``Unit W is correct`` =
let unitW = Vector4((float32)0, (float32)0, (float32)0, (float32)1) let unitW = Vector4((float32)0, (float32)0, (float32)0, (float32)1)
Assert.Equal(Vector4.UnitW, unitW) Assert.Equal(Vector4.UnitW, unitW)
[<Property>] [<Property>]
let ``Unit zero is correct`` = let ``Unit zero is correct`` =
let unitZero = Vector4((float32)0, (float32)0, (float32)0, (float32)0) let unitZero = Vector4((float32)0, (float32)0, (float32)0, (float32)0)
Assert.Equal(Vector4.Zero, unitZero) Assert.Equal(Vector4.Zero, unitZero)
[<Property>] [<Property>]
let ``Unit one is correct`` = let ``Unit one is correct`` =
let unitOne = Vector4((float32)1, (float32)1, (float32)1, (float32)1) let unitOne = Vector4((float32)1, (float32)1, (float32)1, (float32)1)
Assert.Equal(Vector4.One, unitOne) Assert.Equal(Vector4.One, unitOne)
[<Properties(Arbitrary = [| typeof<OpenTKGen> |])>] [<Properties(Arbitrary = [| typeof<OpenTKGen> |])>]
module Serialization = module Serialization =
// //
[<Property>] [<Property>]
let ``The absolute size of a Vector4 is always the size of its components`` (v : Vector4) = let ``The absolute size of a Vector4 is always the size of its components`` (v : Vector4) =
let expectedSize = sizeof<float32> * 4 let expectedSize = sizeof<float32> * 4
Assert.Equal(expectedSize, Vector4.SizeInBytes) Assert.Equal(expectedSize, Vector4.SizeInBytes)
Assert.Equal(expectedSize, Marshal.SizeOf(Vector4())) Assert.Equal(expectedSize, Marshal.SizeOf(Vector4()))