From de4ff51c86061fe54ebf6378230e8347fc5054a4 Mon Sep 17 00:00:00 2001 From: Vicente Penades Date: Fri, 12 Jul 2019 18:45:29 +0200 Subject: [PATCH] Asserts to own file --- tests/SharpGLTF.Tests/NumericsAssert.cs | 203 ++++++++++++++++++++++++ tests/SharpGLTF.Tests/Utils.cs | 107 ++----------- 2 files changed, 212 insertions(+), 98 deletions(-) create mode 100644 tests/SharpGLTF.Tests/NumericsAssert.cs diff --git a/tests/SharpGLTF.Tests/NumericsAssert.cs b/tests/SharpGLTF.Tests/NumericsAssert.cs new file mode 100644 index 00000000..3231f6b0 --- /dev/null +++ b/tests/SharpGLTF.Tests/NumericsAssert.cs @@ -0,0 +1,203 @@ +using System; +using System.Collections.Generic; +using System.Numerics; +using System.Text; + +using NUnit.Framework; + +namespace SharpGLTF +{ + static class NumericsAssert + { + public static void IsFinite(Vector2 vector) + { + Assert.IsTrue(float.IsFinite(vector.X), "X"); + Assert.IsTrue(float.IsFinite(vector.Y), "Y"); + } + + public static void IsFinite(Vector3 vector) + { + Assert.IsTrue(float.IsFinite(vector.X), "X"); + Assert.IsTrue(float.IsFinite(vector.Y), "Y"); + Assert.IsTrue(float.IsFinite(vector.Z), "Z"); + } + + public static void IsFinite(Vector4 vector) + { + Assert.IsTrue(float.IsFinite(vector.X), "X"); + Assert.IsTrue(float.IsFinite(vector.Y), "Y"); + Assert.IsTrue(float.IsFinite(vector.Z), "Z"); + Assert.IsTrue(float.IsFinite(vector.W), "W"); + } + + public static void IsFinite(Quaternion quaternion) + { + Assert.IsTrue(float.IsFinite(quaternion.X), "X"); + Assert.IsTrue(float.IsFinite(quaternion.Y), "Y"); + Assert.IsTrue(float.IsFinite(quaternion.Z), "Z"); + Assert.IsTrue(float.IsFinite(quaternion.W), "W"); + } + + public static void AreEqual(Vector2 expected, Vector2 actual, double delta = 0) + { + Assert.AreEqual(expected.X, actual.X, delta, "X"); + Assert.AreEqual(expected.Y, actual.Y, delta, "Y"); + } + + public static void AreEqual(Vector3 expected, Vector3 actual, double delta = 0) + { + Assert.AreEqual(expected.X, actual.X, delta, "X"); + Assert.AreEqual(expected.Y, actual.Y, delta, "Y"); + Assert.AreEqual(expected.Z, actual.Z, delta, "Z"); + } + + public static void AreEqual(Vector4 expected, Vector4 actual, double delta = 0) + { + Assert.AreEqual(expected.X, actual.X, delta, "X"); + Assert.AreEqual(expected.Y, actual.Y, delta, "Y"); + Assert.AreEqual(expected.Z, actual.Z, delta, "Z"); + Assert.AreEqual(expected.W, actual.W, delta, "W"); + } + + public static void AreEqual(Quaternion expected, Quaternion actual, double delta = 0) + { + Assert.AreEqual(expected.X, actual.X, delta, "X"); + Assert.AreEqual(expected.Y, actual.Y, delta, "Y"); + Assert.AreEqual(expected.Z, actual.Z, delta, "Z"); + Assert.AreEqual(expected.W, actual.W, delta, "W"); + } + + public static void LengthIsOne(Vector2 vector, double delta = 0) + { + var lenSquared = vector.X * vector.X + vector.Y * vector.Y; + + Assert.AreEqual(1, lenSquared, delta * delta, "Length"); + } + + public static void LengthIsOne(Vector3 vector, double delta = 0) + { + var lenSquared = vector.X * vector.X + vector.Y * vector.Y + vector.Z * vector.Z; + + Assert.AreEqual(1, lenSquared, delta * delta, "Length"); + } + + public static void LengthIsOne(Vector4 vector, double delta = 0) + { + var lenSquared = vector.X * vector.X + vector.Y * vector.Y + vector.Z * vector.Z + vector.W * vector.W; + + Assert.AreEqual(1, lenSquared, delta * delta, "Length"); + } + + public static void LengthIsOne(Quaternion vector, double delta = 0) + { + var lenSquared = vector.X * vector.X + vector.Y * vector.Y + vector.Z * vector.Z + vector.W * vector.W; + + Assert.AreEqual(1, lenSquared, delta * delta, "Length"); + } + + public static void Less(Vector2 arg1, Vector2 arg2) + { + Assert.Less(arg1.X, arg2.X, "X"); + Assert.Less(arg1.Y, arg2.Y, "Y"); + } + + public static void Less(Vector3 arg1, Vector3 arg2) + { + Assert.Less(arg1.X, arg2.X, "X"); + Assert.Less(arg1.Y, arg2.Y, "Y"); + Assert.Less(arg1.Z, arg2.Z, "Z"); + } + + public static void Less(Vector4 arg1, Vector4 arg2) + { + Assert.Less(arg1.X, arg2.X, "X"); + Assert.Less(arg1.Y, arg2.Y, "Y"); + Assert.Less(arg1.Z, arg2.Z, "Z"); + Assert.Less(arg1.W, arg2.W, "W"); + } + + public static void LessOrEqual(Vector2 arg1, Vector2 arg2) + { + Assert.LessOrEqual(arg1.X, arg2.X, "X"); + Assert.LessOrEqual(arg1.Y, arg2.Y, "Y"); + } + + public static void LessOrEqual(Vector3 arg1, Vector3 arg2) + { + Assert.LessOrEqual(arg1.X, arg2.X, "X"); + Assert.LessOrEqual(arg1.Y, arg2.Y, "Y"); + Assert.LessOrEqual(arg1.Z, arg2.Z, "Z"); + } + + public static void LessOrEqual(Vector4 arg1, Vector4 arg2) + { + Assert.LessOrEqual(arg1.X, arg2.X, "X"); + Assert.LessOrEqual(arg1.Y, arg2.Y, "Y"); + Assert.LessOrEqual(arg1.Z, arg2.Z, "Z"); + Assert.LessOrEqual(arg1.W, arg2.W, "W"); + } + + public static void Greater(Vector2 arg1, Vector2 arg2) + { + Assert.Greater(arg1.X, arg2.X, "X"); + Assert.Greater(arg1.Y, arg2.Y, "Y"); + } + + public static void Greater(Vector3 arg1, Vector3 arg2) + { + Assert.Greater(arg1.X, arg2.X, "X"); + Assert.Greater(arg1.Y, arg2.Y, "Y"); + Assert.Greater(arg1.Z, arg2.Z, "Z"); + } + + public static void Greater(Vector4 arg1, Vector4 arg2) + { + Assert.Greater(arg1.X, arg2.X, "X"); + Assert.Greater(arg1.Y, arg2.Y, "Y"); + Assert.Greater(arg1.Z, arg2.Z, "Z"); + Assert.Greater(arg1.W, arg2.W, "W"); + } + + public static void GreaterOrEqual(Vector2 arg1, Vector2 arg2) + { + Assert.GreaterOrEqual(arg1.X, arg2.X, "X"); + Assert.GreaterOrEqual(arg1.Y, arg2.Y, "Y"); + } + + public static void GreaterOrEqual(Vector3 arg1, Vector3 arg2) + { + Assert.GreaterOrEqual(arg1.X, arg2.X, "X"); + Assert.GreaterOrEqual(arg1.Y, arg2.Y, "Y"); + Assert.GreaterOrEqual(arg1.Z, arg2.Z, "Z"); + } + + public static void GreaterOrEqual(Vector4 arg1, Vector4 arg2) + { + Assert.GreaterOrEqual(arg1.X, arg2.X, "X"); + Assert.GreaterOrEqual(arg1.Y, arg2.Y, "Y"); + Assert.GreaterOrEqual(arg1.Z, arg2.Z, "Z"); + Assert.GreaterOrEqual(arg1.W, arg2.W, "W"); + } + + public static void AngleLessOrEqual(Vector2 a, Vector2 b, float radians) + { + var angle = VectorsUtils.GetAngle(a, b); + + Assert.LessOrEqual(angle, radians, "Angle"); + } + + public static void AngleLessOrEqual(Vector3 a, Vector3 b, float radians) + { + var angle = VectorsUtils.GetAngle(a, b); + + Assert.LessOrEqual(angle, radians, "Angle"); + } + + public static void AngleLessOrEqual(Quaternion a, Quaternion b, float radians) + { + var angle = VectorsUtils.GetAngle(a, b); + + Assert.LessOrEqual(angle, radians, "Angle"); + } + } +} diff --git a/tests/SharpGLTF.Tests/Utils.cs b/tests/SharpGLTF.Tests/Utils.cs index c7af555f..65ab41bc 100644 --- a/tests/SharpGLTF.Tests/Utils.cs +++ b/tests/SharpGLTF.Tests/Utils.cs @@ -279,108 +279,19 @@ public static float GetAngle(Vector3 a, Vector3 b) return (float)Math.Acos(c); } - } - static class NumericsAssert - { - public static void IsFinite(Vector2 vector) + public static float GetAngle(Vector2 a, Vector2 b) { - Assert.IsTrue(float.IsFinite(vector.X), "X"); - Assert.IsTrue(float.IsFinite(vector.Y), "Y"); - } + a = Vector2.Normalize(a); + b = Vector2.Normalize(b); - public static void IsFinite(Vector3 vector) - { - Assert.IsTrue(float.IsFinite(vector.X), "X"); - Assert.IsTrue(float.IsFinite(vector.Y), "Y"); - Assert.IsTrue(float.IsFinite(vector.Z), "Z"); - } - - public static void IsFinite(Vector4 vector) - { - Assert.IsTrue(float.IsFinite(vector.X), "X"); - Assert.IsTrue(float.IsFinite(vector.Y), "Y"); - Assert.IsTrue(float.IsFinite(vector.Z), "Z"); - Assert.IsTrue(float.IsFinite(vector.W), "W"); - } - - public static void IsFinite(Quaternion quaternion) - { - Assert.IsTrue(float.IsFinite(quaternion.X), "X"); - Assert.IsTrue(float.IsFinite(quaternion.Y), "Y"); - Assert.IsTrue(float.IsFinite(quaternion.Z), "Z"); - Assert.IsTrue(float.IsFinite(quaternion.W), "W"); - } - - public static void AreEqual(Vector2 expected, Vector2 actual, double delta = 0) - { - Assert.AreEqual(expected.X, actual.X, delta, "X"); - Assert.AreEqual(expected.Y, actual.Y, delta, "Y"); - } - - public static void AreEqual(Vector3 expected, Vector3 actual, double delta = 0) - { - Assert.AreEqual(expected.X, actual.X, delta, "X"); - Assert.AreEqual(expected.Y, actual.Y, delta, "Y"); - Assert.AreEqual(expected.Z, actual.Z, delta, "Z"); - } - - public static void AreEqual(Vector4 expected, Vector4 actual, double delta = 0) - { - Assert.AreEqual(expected.X, actual.X, delta, "X"); - Assert.AreEqual(expected.Y, actual.Y, delta, "Y"); - Assert.AreEqual(expected.Z, actual.Z, delta, "Z"); - Assert.AreEqual(expected.W, actual.W, delta, "W"); - } - - public static void AreEqual(Quaternion expected, Quaternion actual, double delta = 0) - { - Assert.AreEqual(expected.X, actual.X, delta, "X"); - Assert.AreEqual(expected.Y, actual.Y, delta, "Y"); - Assert.AreEqual(expected.Z, actual.Z, delta, "Z"); - Assert.AreEqual(expected.W, actual.W, delta, "W"); - } - - public static void IsNormal(Vector2 vector, double delta = 0) - { - var lenSquared = vector.X * vector.X + vector.Y * vector.Y; - - Assert.AreEqual(1, lenSquared, delta * delta, "Length"); - } - - public static void IsNormal(Vector3 vector, double delta = 0) - { - var lenSquared = vector.X * vector.X + vector.Y * vector.Y + vector.Z * vector.Z; - - Assert.AreEqual(1, lenSquared, delta * delta, "Length"); - } - - public static void IsNormal(Vector4 vector, double delta = 0) - { - var lenSquared = vector.X * vector.X + vector.Y * vector.Y + vector.Z * vector.Z + vector.W * vector.W; - - Assert.AreEqual(1, lenSquared, delta * delta, "Length"); - } - - public static void IsNormal(Quaternion vector, double delta = 0) - { - var lenSquared = vector.X * vector.X + vector.Y * vector.Y + vector.Z * vector.Z + vector.W * vector.W; - - Assert.AreEqual(1, lenSquared, delta * delta, "Length"); - } - - public static void AngleLessOrEqual(Vector3 a, Vector3 b, float radians) - { - var angle = VectorsUtils.GetAngle(a, b); - - Assert.LessOrEqual(angle, radians, "Angle"); - } - - public static void AngleLessOrEqual(Quaternion a, Quaternion b, float radians) - { - var angle = VectorsUtils.GetAngle(a, b); + var c = Vector2.Dot(a, b); + if (c > 1) c = 1; + if (c < -1) c = -1; - Assert.LessOrEqual(angle, radians, "Angle"); + return (float)Math.Acos(c); } } + + }