From be213a782890d0ab42a52f556bbc5295143fa982 Mon Sep 17 00:00:00 2001 From: Jarl Gullberg Date: Wed, 31 May 2017 22:28:27 +0200 Subject: [PATCH] Fixed normalization tests in Vector3 and ported some common tests to Vector2. --- tests/OpenTK.Tests/Vector2Tests.fs | 113 ++++++++++++++++++++++++++--- tests/OpenTK.Tests/Vector3Tests.fs | 4 +- 2 files changed, 105 insertions(+), 12 deletions(-) diff --git a/tests/OpenTK.Tests/Vector2Tests.fs b/tests/OpenTK.Tests/Vector2Tests.fs index e69f2321..e2947b73 100644 --- a/tests/OpenTK.Tests/Vector2Tests.fs +++ b/tests/OpenTK.Tests/Vector2Tests.fs @@ -35,6 +35,47 @@ module Vector2 = // Assert.True(a.Length >= 0.0f) + [ |])>] + module Length = + // + [] + let ``Length method works`` (a, b) = + let v = Vector2(a, b) + let l = System.Math.Sqrt((float)(a * a + b * b)) + + Assert.Equal((float32)l, v.Length) + + [] + let ``Fast length method works`` (a, b) = + let v = Vector2(a, b) + let l = 1.0f / MathHelper.InverseSqrtFast(a * a + b * b) + + Assert.Equal(l, v.LengthFast) + + [] + let ``Length squared method works`` (a, b) = + let v = Vector2(a, b) + let lsq = a * a + b * b + + Assert.Equal(lsq, v.LengthSquared) + + [ |])>] + module ``Unit vectors and perpendicularity`` = + // + [] + let ``Perpendicular vector to the right is correct`` (a, b) = + let v = Vector2(a, b) + let perp = Vector2(a, -b) + + Assert.Equal(perp, v.PerpendicularRight) + + [] + let ``Perpendicular vector to the left is correct`` (a, b) = + let v = Vector2(a, b) + let perp = Vector2(-a, b) + + Assert.Equal(perp, v.PerpendicularLeft) + [ |])>] module Indexing = // @@ -242,21 +283,73 @@ module Vector2 = Assert.Equal(dot, vRes) [ |])>] - module Normalization = + module Normalization = // [] - let ``Normalization works`` (a : Vector2) = - let scale = 1.0f / a.Length - let norm = Vector2(a.X * scale, a.Y * scale) - let vRes = Vector2.Normalize(ref a) - Assert.Equal(norm, Vector2.Normalize(a)); - Assert.Equal(norm, vRes) + let ``Normalization of instance, creating a new vector, works`` (a, b) = + let v = Vector2(a, b) + let l = v.Length + + // Dividing by zero is not supported + if not (approxEq l 0.0f) then + let norm = v.Normalized() + + Assert.ApproximatelyEqual(v.X / l, norm.X) + Assert.ApproximatelyEqual(v.Y / l, norm.Y) + + [] + let ``Normalization of instance works`` (a, b) = + let v = Vector2(a, b) + let l = v.Length + + if not (approxEq l 0.0f) then + let norm = Vector2(a, b) + norm.Normalize() + + Assert.ApproximatelyEqual(v.X / l, norm.X) + Assert.ApproximatelyEqual(v.Y / l, norm.Y) + + [] + let ``Fast approximate normalization of instance works`` (a, b) = + let v = Vector2(a, b) + let norm = Vector2(a, b) + norm.NormalizeFast() + + let scale = MathHelper.InverseSqrtFast(a * a + b * b) + + Assert.ApproximatelyEqual(v.X * scale, norm.X) + Assert.ApproximatelyEqual(v.Y * scale, norm.Y) [] - let ``Fast approximate normalization works`` (a : Vector2, b : Vector2) = + let ``Normalization by reference works`` (a : Vector2) = + if not (approxEq a.Length 0.0f) then + let scale = 1.0f / a.Length + let norm = Vector2(a.X * scale, a.Y * scale) + let vRes = Vector2.Normalize(ref a) + + Assert.ApproximatelyEqual(norm, vRes) + + [] + let ``Normalization works`` (a : Vector2) = + if not (approxEq a.Length 0.0f) then + let scale = 1.0f / a.Length + let norm = Vector2(a.X * scale, a.Y * scale) + + Assert.ApproximatelyEqual(norm, Vector3.Normalize(a)); + + [] + let ``Fast approximate normalization by reference works`` (a : Vector2) = let scale = MathHelper.InverseSqrtFast(a.X * a.X + a.Y * a.Y) + let norm = Vector2(a.X * scale, a.Y * scale) let vRes = Vector2.NormalizeFast(ref a) - Assert.Equal(norm, Vector2.NormalizeFast(a)); - Assert.Equal(norm, vRes) \ No newline at end of file + Assert.ApproximatelyEqual(norm, vRes) + + [] + let ``Fast approximate normalization works`` (a : Vector2) = + let scale = MathHelper.InverseSqrtFast(a.X * a.X + a.Y * a.Y) + + let norm = Vector2(a.X * scale, a.Y * scale) + + Assert.ApproximatelyEqual(norm, Vector2.NormalizeFast(a)); \ No newline at end of file diff --git a/tests/OpenTK.Tests/Vector3Tests.fs b/tests/OpenTK.Tests/Vector3Tests.fs index b3ece0cc..ab8fcd69 100644 --- a/tests/OpenTK.Tests/Vector3Tests.fs +++ b/tests/OpenTK.Tests/Vector3Tests.fs @@ -181,7 +181,7 @@ module Vector3 = [] let ``Fast approximate normalization by reference works`` (a : Vector3) = - let scale = MathHelper.InverseSqrtFast(a.X * a.X + a.Y * a.Y) + let scale = MathHelper.InverseSqrtFast(a.X * a.X + a.Y * a.Y + a.Z * a.Z) let norm = Vector3(a.X * scale, a.Y * scale, a.Z * scale) let vRes = Vector3.NormalizeFast(ref a) @@ -190,7 +190,7 @@ module Vector3 = [] let ``Fast approximate normalization works`` (a : Vector3) = - let scale = MathHelper.InverseSqrtFast(a.X * a.X + a.Y * a.Y) + let scale = MathHelper.InverseSqrtFast(a.X * a.X + a.Y * a.Y + a.Z * a.Z) let norm = Vector3(a.X * scale, a.Y * scale, a.Z * scale)