diff --git a/src/libraries/System.Numerics.Vectors/ref/System.Numerics.Vectors.cs b/src/libraries/System.Numerics.Vectors/ref/System.Numerics.Vectors.cs index 3c017e72c5a81d..239b9fe3b6d472 100644 --- a/src/libraries/System.Numerics.Vectors/ref/System.Numerics.Vectors.cs +++ b/src/libraries/System.Numerics.Vectors/ref/System.Numerics.Vectors.cs @@ -7,6 +7,118 @@ namespace System.Numerics { + public readonly struct Vector2 : IEquatable>, IFormattable + where T : struct + { + // Fields + + public T X { get { throw null; } } // Should this have a setter, a Vector2 WithX(T value), or be a public field like Vector2 + public T Y { get { throw null; } } // Should this have a setter, a Vector2 WithY(T value), or be a public field like Vector2 + + // Constructors + + public Vector2(T value) { throw null; } + public Vector2(T x, T y) { throw null; } + + public Vector2(T[] value) { throw null; } // Doesn't exist for Vector2 + public Vector2(T[] value, int offset) { throw null; } // Doesn't exist for Vector2 + public Vector2(ReadOnlySpan value) { throw null; } // Doesn't exist for Vector2 + + // Static Properties + + public static Vector2 One { get { throw null; } } + public static Vector2 UnitX { get { throw null; } } + public static Vector2 UnitY { get { throw null; } } + public static Vector2 Zero { get { throw null; } } + + // With methods + public Vector2 WithX(T x) { throw null; } + public Vector2 WithY(T y) { throw null; } + + // Operators + + public static bool operator ==(Vector2 left, Vector2 right) { throw null; } + public static bool operator !=(Vector2 left, Vector2 right) { throw null; } + + public static Vector2 operator +(Vector2 value) { throw null; } // Doesn't exist for Vector2 + public static Vector2 operator -(Vector2 value) { throw null; } + + public static Vector2 operator +(Vector2 left, Vector2 right) { throw null; } + public static Vector2 operator -(Vector2 left, Vector2 right) { throw null; } + + public static Vector2 operator *(Vector2 left, Vector2 right) { throw null; } + public static Vector2 operator /(Vector2 left, Vector2 right) { throw null; } + + public static Vector2 operator *(Vector2 left, T right) { throw null; } + public static Vector2 operator /(Vector2 left, T right) { throw null; } + + public static Vector2 operator *(T left, Vector2 right) { throw null; } + + // "Friendly" Operators + + public static Vector2 Plus(Vector2 value) { throw null; } // Doesn't exist for Vector2 + public static Vector2 Negate(Vector2 value) { throw null; } + + public static Vector2 Add(Vector2 left, Vector2 right) { throw null; } + public static Vector2 Subtract(Vector2 left, Vector2 right) { throw null; } + + public static Vector2 Multiply(Vector2 left, Vector2 right) { throw null; } + public static Vector2 Divide(Vector2 left, Vector2 right) { throw null; } + + public static Vector2 Multiply(Vector2 left, T right) { throw null; } + public static Vector2 Divide(Vector2 left, T right) { throw null; } + + public static Vector2 Multiply(T left, Vector2 right) { throw null; } + + // Static Methods + + public static Vector2 Abs(Vector2 value) { throw null; } + + public static Vector2 Clamp(Vector2 value, Vector2 min, Vector2 max) { throw null; } + + public static T Distance(Vector2 left, Vector2 right) { throw null; } + public static T DistanceSquared(Vector2 left, Vector2 right) { throw null; } + + public static T Dot(Vector2 left, Vector2 right) { throw null; } + + public static Vector2 Lerp(Vector2 min, Vector2 max, T amount) { throw null; } + + public static Vector2 Min(Vector2 left, Vector2 right) { throw null; } + public static Vector2 Max(Vector2 left, Vector2 right) { throw null; } + + public static Vector2 Normalize(Vector2 value) { throw null; } + + public static Vector2 Reflect(Vector2 incident, Vector2 normal) { throw null; } + + public static Vector2 SquareRoot(Vector2 value) { throw null; } + + //public static Vector2 Transform(Vector2 position, Matrix3x2 matrix) { throw null; } + //public static Vector2 Transform(Vector2 position, Matrix4x4 matrix) { throw null; } + + //public static Vector2 Transform(Vector2 position, Quaternion rotation) { throw null; } // Rotate is a better name? + + //public static Vector2 TransformNormal(Vector2 normal, Matrix3x2 matrix) { throw null; } + //public static Vector2 TransformNormal(Vector2 normal, Matrix4x4 matrix) { throw null; } + + // Methods + + public readonly void CopyTo(T[] array) { throw null; } + public readonly void CopyTo(T[] array, int index) { throw null; } + public readonly void CopyTo(Span destination) { throw null; } // Doesn't exist for Vector2 + + public override readonly bool Equals(object? obj) { throw null; } + public readonly bool Equals(Vector2 other) { throw null; } + + public override readonly int GetHashCode() { throw null; } + + public readonly T Length() { throw null; } // Better as a property? + public readonly T LengthSquared() { throw null; } // Better as a property? + + public readonly override string ToString() { throw null; } + public readonly string ToString(string? format) { throw null; } + public readonly string ToString(string? format, IFormatProvider? formatProvider) { throw null; } + } + public partial struct Matrix3x2 : System.IEquatable { public float M11; diff --git a/src/libraries/System.Numerics.Vectors/tests/MathHelper.cs b/src/libraries/System.Numerics.Vectors/tests/MathHelper.cs index bd22c6a02ea943..1f0d8b9fcc43e9 100644 --- a/src/libraries/System.Numerics.Vectors/tests/MathHelper.cs +++ b/src/libraries/System.Numerics.Vectors/tests/MathHelper.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System.Diagnostics; + namespace System.Numerics { static class MathHelper @@ -24,6 +26,21 @@ public static bool Equal(float a, float b) return (Math.Abs(a - b) < 1e-5); } + public static bool Equal(T a, T b) + { + if (typeof(T) == typeof(float)) + { + return (Math.Abs((float)(object)a - (float)(object)b) < 1e-5); + } + else if (typeof(T) == typeof(double)) + { + return (Math.Abs((double)(object)a - (double)(object)b) < 1e-5); + } + + Debug.Fail("Type has not been added"); + return false; + } + public static bool Equal(Vector2 a, Vector2 b) { return Equal(a.X, b.X) && Equal(a.Y, b.Y); @@ -39,6 +56,21 @@ public static bool Equal(Vector4 a, Vector4 b) return Equal(a.X, b.X) && Equal(a.Y, b.Y) && Equal(a.Z, b.Z) && Equal(a.W, b.W); } + public static bool Equal(Vector2 a, Vector2 b) where T : struct + { + return Equal(a.X, b.X) && Equal(a.Y, b.Y); + } + + public static bool Equal(Vector3 a, Vector3 b) where T : struct + { + return Equal(a.X, b.X) && Equal(a.Y, b.Y) && Equal(a.Z, b.Z); + } + + public static bool Equal(Vector4 a, Vector4 b) where T : struct + { + return Equal(a.X, b.X) && Equal(a.Y, b.Y) && Equal(a.Z, b.Z) && Equal(a.W, b.W); + } + public static bool Equal(Matrix4x4 a, Matrix4x4 b) { return diff --git a/src/libraries/System.Numerics.Vectors/tests/System.Numerics.Vectors.Tests.csproj b/src/libraries/System.Numerics.Vectors/tests/System.Numerics.Vectors.Tests.csproj index f96a9318bbc8d8..953c639c6ee0d0 100644 --- a/src/libraries/System.Numerics.Vectors/tests/System.Numerics.Vectors.Tests.csproj +++ b/src/libraries/System.Numerics.Vectors/tests/System.Numerics.Vectors.Tests.csproj @@ -16,9 +16,12 @@ GenericVectorTests.tt + - - + + + diff --git a/src/libraries/System.Numerics.Vectors/tests/Vector2Tests.cs b/src/libraries/System.Numerics.Vectors/tests/Vector2Tests.cs index b3ed6e2ba22ed2..a440e8e0645031 100644 --- a/src/libraries/System.Numerics.Vectors/tests/Vector2Tests.cs +++ b/src/libraries/System.Numerics.Vectors/tests/Vector2Tests.cs @@ -1,3 +1,9 @@ +/********************************************************************************* + * This file is auto-generated from a template file by the GenerateTests.csx * + * script in tests\src\libraries\System.Numerics.Vectors\tests. In order to make * + * changes, please update the corresponding template and run according to the * + * directions listed in the file. * + *********************************************************************************/ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. @@ -8,1183 +14,1175 @@ namespace System.Numerics.Tests { - public class Vector2Tests + public partial class Vector2Tests { - [Fact] - public void Vector2MarshalSizeTest() - { - Assert.Equal(8, Marshal.SizeOf()); - Assert.Equal(8, Marshal.SizeOf(new Vector2())); - } - - [Fact] - public void Vector2CopyToTest() - { - Vector2 v1 = new Vector2(2.0f, 3.0f); - - float[] a = new float[3]; - float[] b = new float[2]; - - Assert.Throws(() => v1.CopyTo(null, 0)); - Assert.Throws(() => v1.CopyTo(a, -1)); - Assert.Throws(() => v1.CopyTo(a, a.Length)); - AssertExtensions.Throws(null, () => v1.CopyTo(a, 2)); - - v1.CopyTo(a, 1); - v1.CopyTo(b); - Assert.Equal(0.0, a[0]); - Assert.Equal(2.0, a[1]); - Assert.Equal(3.0, a[2]); - Assert.Equal(2.0, b[0]); - Assert.Equal(3.0, b[1]); - } - - [Fact] - public void Vector2GetHashCodeTest() - { - Vector2 v1 = new Vector2(2.0f, 3.0f); - Vector2 v2 = new Vector2(2.0f, 3.0f); - Vector2 v3 = new Vector2(3.0f, 2.0f); - Assert.Equal(v1.GetHashCode(), v1.GetHashCode()); - Assert.Equal(v1.GetHashCode(), v2.GetHashCode()); - Assert.NotEqual(v1.GetHashCode(), v3.GetHashCode()); - Vector2 v4 = new Vector2(0.0f, 0.0f); - Vector2 v6 = new Vector2(1.0f, 0.0f); - Vector2 v7 = new Vector2(0.0f, 1.0f); - Vector2 v8 = new Vector2(1.0f, 1.0f); - Assert.NotEqual(v4.GetHashCode(), v6.GetHashCode()); - Assert.NotEqual(v4.GetHashCode(), v7.GetHashCode()); - Assert.NotEqual(v4.GetHashCode(), v8.GetHashCode()); - Assert.NotEqual(v7.GetHashCode(), v6.GetHashCode()); - Assert.NotEqual(v8.GetHashCode(), v6.GetHashCode()); - Assert.NotEqual(v8.GetHashCode(), v7.GetHashCode()); - } - - [Fact] - public void Vector2ToStringTest() - { - string separator = CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator; - CultureInfo enUsCultureInfo = new CultureInfo("en-US"); - - Vector2 v1 = new Vector2(2.0f, 3.0f); - - string v1str = v1.ToString(); - string expectedv1 = string.Format(CultureInfo.CurrentCulture - , "<{1:G}{0} {2:G}>" - , new object[] { separator, 2, 3 }); - Assert.Equal(expectedv1, v1str); - - string v1strformatted = v1.ToString("c", CultureInfo.CurrentCulture); - string expectedv1formatted = string.Format(CultureInfo.CurrentCulture - , "<{1:c}{0} {2:c}>" - , new object[] { separator, 2, 3 }); - Assert.Equal(expectedv1formatted, v1strformatted); - - string v2strformatted = v1.ToString("c", enUsCultureInfo); - string expectedv2formatted = string.Format(enUsCultureInfo - , "<{1:c}{0} {2:c}>" - , new object[] { enUsCultureInfo.NumberFormat.NumberGroupSeparator, 2, 3 }); - Assert.Equal(expectedv2formatted, v2strformatted); - - string v3strformatted = v1.ToString("c"); - string expectedv3formatted = string.Format(CultureInfo.CurrentCulture - , "<{1:c}{0} {2:c}>" - , new object[] { separator, 2, 3 }); - Assert.Equal(expectedv3formatted, v3strformatted); - } - - // A test for Distance (Vector2f, Vector2f) - [Fact] - public void Vector2DistanceTest() - { - Vector2 a = new Vector2(1.0f, 2.0f); - Vector2 b = new Vector2(3.0f, 4.0f); - - float expected = (float)System.Math.Sqrt(8); - float actual; - - actual = Vector2.Distance(a, b); - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.Distance did not return the expected value."); - } - - // A test for Distance (Vector2f, Vector2f) - // Distance from the same point - [Fact] - public void Vector2DistanceTest2() - { - Vector2 a = new Vector2(1.051f, 2.05f); - Vector2 b = new Vector2(1.051f, 2.05f); - - float actual = Vector2.Distance(a, b); - Assert.Equal(0.0f, actual); - } - - // A test for DistanceSquared (Vector2f, Vector2f) - [Fact] - public void Vector2DistanceSquaredTest() - { - Vector2 a = new Vector2(1.0f, 2.0f); - Vector2 b = new Vector2(3.0f, 4.0f); - - float expected = 8.0f; - float actual; - - actual = Vector2.DistanceSquared(a, b); - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.DistanceSquared did not return the expected value."); - } - - // A test for Dot (Vector2f, Vector2f) - [Fact] - public void Vector2DotTest() - { - Vector2 a = new Vector2(1.0f, 2.0f); - Vector2 b = new Vector2(3.0f, 4.0f); - - float expected = 11.0f; - float actual; - - actual = Vector2.Dot(a, b); - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.Dot did not return the expected value."); - } - - // A test for Dot (Vector2f, Vector2f) - // Dot test for perpendicular vector - [Fact] - public void Vector2DotTest1() - { - Vector2 a = new Vector2(1.55f, 1.55f); - Vector2 b = new Vector2(-1.55f, 1.55f); - - float expected = 0.0f; - float actual = Vector2.Dot(a, b); - Assert.Equal(expected, actual); - } - - // A test for Dot (Vector2f, Vector2f) - // Dot test with specail float values - [Fact] - public void Vector2DotTest2() - { - Vector2 a = new Vector2(float.MinValue, float.MinValue); - Vector2 b = new Vector2(float.MaxValue, float.MaxValue); - - float actual = Vector2.Dot(a, b); - Assert.True(float.IsNegativeInfinity(actual), "Vector2f.Dot did not return the expected value."); - } - - // A test for Length () - [Fact] - public void Vector2LengthTest() - { - Vector2 a = new Vector2(2.0f, 4.0f); - - Vector2 target = a; - - float expected = (float)System.Math.Sqrt(20); - float actual; - - actual = target.Length(); - - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.Length did not return the expected value."); - } - - // A test for Length () - // Length test where length is zero - [Fact] - public void Vector2LengthTest1() - { - Vector2 target = new Vector2(); - target.X = 0.0f; - target.Y = 0.0f; - - float expected = 0.0f; - float actual; - - actual = target.Length(); - - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.Length did not return the expected value."); - } - - // A test for LengthSquared () - [Fact] - public void Vector2LengthSquaredTest() - { - Vector2 a = new Vector2(2.0f, 4.0f); - - Vector2 target = a; - - float expected = 20.0f; - float actual; - - actual = target.LengthSquared(); - - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.LengthSquared did not return the expected value."); - } - - // A test for LengthSquared () - // LengthSquared test where the result is zero - [Fact] - public void Vector2LengthSquaredTest1() - { - Vector2 a = new Vector2(0.0f, 0.0f); - - float expected = 0.0f; - float actual = a.LengthSquared(); - - Assert.Equal(expected, actual); - } - - // A test for Min (Vector2f, Vector2f) - [Fact] - public void Vector2MinTest() - { - Vector2 a = new Vector2(-1.0f, 4.0f); - Vector2 b = new Vector2(2.0f, 1.0f); - - Vector2 expected = new Vector2(-1.0f, 1.0f); - Vector2 actual; - actual = Vector2.Min(a, b); - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.Min did not return the expected value."); - } - - [Fact] - public void Vector2MinMaxCodeCoverageTest() - { - Vector2 min = new Vector2(0, 0); - Vector2 max = new Vector2(1, 1); - Vector2 actual; - - // Min. - actual = Vector2.Min(min, max); - Assert.Equal(actual, min); - - actual = Vector2.Min(max, min); - Assert.Equal(actual, min); - - // Max. - actual = Vector2.Max(min, max); - Assert.Equal(actual, max); - - actual = Vector2.Max(max, min); - Assert.Equal(actual, max); - } - - // A test for Max (Vector2f, Vector2f) - [Fact] - public void Vector2MaxTest() - { - Vector2 a = new Vector2(-1.0f, 4.0f); - Vector2 b = new Vector2(2.0f, 1.0f); - - Vector2 expected = new Vector2(2.0f, 4.0f); - Vector2 actual; - actual = Vector2.Max(a, b); - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.Max did not return the expected value."); - } - - // A test for Clamp (Vector2f, Vector2f, Vector2f) - [Fact] - public void Vector2ClampTest() - { - Vector2 a = new Vector2(0.5f, 0.3f); - Vector2 min = new Vector2(0.0f, 0.1f); - Vector2 max = new Vector2(1.0f, 1.1f); - - // Normal case. - // Case N1: specified value is in the range. - Vector2 expected = new Vector2(0.5f, 0.3f); - Vector2 actual = Vector2.Clamp(a, min, max); - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.Clamp did not return the expected value."); - // Normal case. - // Case N2: specified value is bigger than max value. - a = new Vector2(2.0f, 3.0f); - expected = max; - actual = Vector2.Clamp(a, min, max); - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.Clamp did not return the expected value."); - // Case N3: specified value is smaller than max value. - a = new Vector2(-1.0f, -2.0f); - expected = min; - actual = Vector2.Clamp(a, min, max); - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.Clamp did not return the expected value."); - // Case N4: combination case. - a = new Vector2(-2.0f, 4.0f); - expected = new Vector2(min.X, max.Y); - actual = Vector2.Clamp(a, min, max); - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.Clamp did not return the expected value."); - // User specified min value is bigger than max value. - max = new Vector2(0.0f, 0.1f); - min = new Vector2(1.0f, 1.1f); - - // Case W1: specified value is in the range. - a = new Vector2(0.5f, 0.3f); - expected = max; - actual = Vector2.Clamp(a, min, max); - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.Clamp did not return the expected value."); - - // Normal case. - // Case W2: specified value is bigger than max and min value. - a = new Vector2(2.0f, 3.0f); - expected = max; - actual = Vector2.Clamp(a, min, max); - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.Clamp did not return the expected value."); - - // Case W3: specified value is smaller than min and max value. - a = new Vector2(-1.0f, -2.0f); - expected = max; - actual = Vector2.Clamp(a, min, max); - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.Clamp did not return the expected value."); - } - - // A test for Lerp (Vector2f, Vector2f, float) - [Fact] - public void Vector2LerpTest() - { - Vector2 a = new Vector2(1.0f, 2.0f); - Vector2 b = new Vector2(3.0f, 4.0f); - - float t = 0.5f; - - Vector2 expected = new Vector2(2.0f, 3.0f); - Vector2 actual; - actual = Vector2.Lerp(a, b, t); - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.Lerp did not return the expected value."); - } - - // A test for Lerp (Vector2f, Vector2f, float) - // Lerp test with factor zero - [Fact] - public void Vector2LerpTest1() - { - Vector2 a = new Vector2(0.0f, 0.0f); - Vector2 b = new Vector2(3.18f, 4.25f); - - float t = 0.0f; - Vector2 expected = Vector2.Zero; - Vector2 actual = Vector2.Lerp(a, b, t); - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.Lerp did not return the expected value."); - } - - // A test for Lerp (Vector2f, Vector2f, float) - // Lerp test with factor one - [Fact] - public void Vector2LerpTest2() - { - Vector2 a = new Vector2(0.0f, 0.0f); - Vector2 b = new Vector2(3.18f, 4.25f); - - float t = 1.0f; - Vector2 expected = new Vector2(3.18f, 4.25f); - Vector2 actual = Vector2.Lerp(a, b, t); - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.Lerp did not return the expected value."); - } - - // A test for Lerp (Vector2f, Vector2f, float) - // Lerp test with factor > 1 - [Fact] - public void Vector2LerpTest3() - { - Vector2 a = new Vector2(0.0f, 0.0f); - Vector2 b = new Vector2(3.18f, 4.25f); - - float t = 2.0f; - Vector2 expected = b * 2.0f; - Vector2 actual = Vector2.Lerp(a, b, t); - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.Lerp did not return the expected value."); - } - - // A test for Lerp (Vector2f, Vector2f, float) - // Lerp test with factor < 0 - [Fact] - public void Vector2LerpTest4() - { - Vector2 a = new Vector2(0.0f, 0.0f); - Vector2 b = new Vector2(3.18f, 4.25f); - - float t = -2.0f; - Vector2 expected = -(b * 2.0f); - Vector2 actual = Vector2.Lerp(a, b, t); - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.Lerp did not return the expected value."); - } - - // A test for Lerp (Vector2f, Vector2f, float) - // Lerp test with special float value - [Fact] - public void Vector2LerpTest5() - { - Vector2 a = new Vector2(45.67f, 90.0f); - Vector2 b = new Vector2(float.PositiveInfinity, float.NegativeInfinity); - - float t = 0.408f; - Vector2 actual = Vector2.Lerp(a, b, t); - Assert.True(float.IsPositiveInfinity(actual.X), "Vector2f.Lerp did not return the expected value."); - Assert.True(float.IsNegativeInfinity(actual.Y), "Vector2f.Lerp did not return the expected value."); - } - - // A test for Lerp (Vector2f, Vector2f, float) - // Lerp test from the same point - [Fact] - public void Vector2LerpTest6() - { - Vector2 a = new Vector2(1.0f, 2.0f); - Vector2 b = new Vector2(1.0f, 2.0f); - - float t = 0.5f; - - Vector2 expected = new Vector2(1.0f, 2.0f); - Vector2 actual = Vector2.Lerp(a, b, t); - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.Lerp did not return the expected value."); - } - - // A test for Transform(Vector2f, Matrix4x4) - [Fact] - public void Vector2TransformTest() - { - Vector2 v = new Vector2(1.0f, 2.0f); - Matrix4x4 m = - Matrix4x4.CreateRotationX(MathHelper.ToRadians(30.0f)) * - Matrix4x4.CreateRotationY(MathHelper.ToRadians(30.0f)) * - Matrix4x4.CreateRotationZ(MathHelper.ToRadians(30.0f)); - m.M41 = 10.0f; - m.M42 = 20.0f; - m.M43 = 30.0f; - - Vector2 expected = new Vector2(10.316987f, 22.183012f); - Vector2 actual; - - actual = Vector2.Transform(v, m); - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.Transform did not return the expected value."); - } - - // A test for Transform(Vector2f, Matrix3x2) - [Fact] - public void Vector2Transform3x2Test() - { - Vector2 v = new Vector2(1.0f, 2.0f); - Matrix3x2 m = Matrix3x2.CreateRotation(MathHelper.ToRadians(30.0f)); - m.M31 = 10.0f; - m.M32 = 20.0f; - - Vector2 expected = new Vector2(9.866025f, 22.23205f); - Vector2 actual; - - actual = Vector2.Transform(v, m); - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.Transform did not return the expected value."); - } - - // A test for TransformNormal (Vector2f, Matrix4x4) - [Fact] - public void Vector2TransformNormalTest() - { - Vector2 v = new Vector2(1.0f, 2.0f); - Matrix4x4 m = - Matrix4x4.CreateRotationX(MathHelper.ToRadians(30.0f)) * - Matrix4x4.CreateRotationY(MathHelper.ToRadians(30.0f)) * - Matrix4x4.CreateRotationZ(MathHelper.ToRadians(30.0f)); - m.M41 = 10.0f; - m.M42 = 20.0f; - m.M43 = 30.0f; - - Vector2 expected = new Vector2(0.3169873f, 2.18301272f); - Vector2 actual; - - actual = Vector2.TransformNormal(v, m); - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.Tranform did not return the expected value."); - } - - // A test for TransformNormal (Vector2f, Matrix3x2) - [Fact] - public void Vector2TransformNormal3x2Test() - { - Vector2 v = new Vector2(1.0f, 2.0f); - Matrix3x2 m = Matrix3x2.CreateRotation(MathHelper.ToRadians(30.0f)); - m.M31 = 10.0f; - m.M32 = 20.0f; - - Vector2 expected = new Vector2(-0.133974612f, 2.232051f); - Vector2 actual; - - actual = Vector2.TransformNormal(v, m); - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.Transform did not return the expected value."); - } - - // A test for Transform (Vector2f, Quaternion) - [Fact] - public void Vector2TransformByQuaternionTest() - { - Vector2 v = new Vector2(1.0f, 2.0f); - - Matrix4x4 m = - Matrix4x4.CreateRotationX(MathHelper.ToRadians(30.0f)) * - Matrix4x4.CreateRotationY(MathHelper.ToRadians(30.0f)) * - Matrix4x4.CreateRotationZ(MathHelper.ToRadians(30.0f)); - Quaternion q = Quaternion.CreateFromRotationMatrix(m); - - Vector2 expected = Vector2.Transform(v, m); - Vector2 actual = Vector2.Transform(v, q); - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.Transform did not return the expected value."); - } - - // A test for Transform (Vector2f, Quaternion) - // Transform Vector2f with zero quaternion - [Fact] - public void Vector2TransformByQuaternionTest1() - { - Vector2 v = new Vector2(1.0f, 2.0f); - Quaternion q = new Quaternion(); - Vector2 expected = v; - - Vector2 actual = Vector2.Transform(v, q); - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.Transform did not return the expected value."); - } - - // A test for Transform (Vector2f, Quaternion) - // Transform Vector2f with identity quaternion - [Fact] - public void Vector2TransformByQuaternionTest2() - { - Vector2 v = new Vector2(1.0f, 2.0f); - Quaternion q = Quaternion.Identity; - Vector2 expected = v; - - Vector2 actual = Vector2.Transform(v, q); - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.Transform did not return the expected value."); - } - - // A test for Normalize (Vector2f) - [Fact] - public void Vector2NormalizeTest() - { - Vector2 a = new Vector2(2.0f, 3.0f); - Vector2 expected = new Vector2(0.554700196225229122018341733457f, 0.8320502943378436830275126001855f); - Vector2 actual; - - actual = Vector2.Normalize(a); - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.Normalize did not return the expected value."); - } - - // A test for Normalize (Vector2f) - // Normalize zero length vector - [Fact] - public void Vector2NormalizeTest1() - { - Vector2 a = new Vector2(); // no parameter, default to 0.0f - Vector2 actual = Vector2.Normalize(a); - Assert.True(float.IsNaN(actual.X) && float.IsNaN(actual.Y), "Vector2f.Normalize did not return the expected value."); - } - - // A test for Normalize (Vector2f) - // Normalize infinite length vector - [Fact] - public void Vector2NormalizeTest2() - { - Vector2 a = new Vector2(float.MaxValue, float.MaxValue); - Vector2 actual = Vector2.Normalize(a); - Vector2 expected = new Vector2(0, 0); - Assert.Equal(expected, actual); - } - - // A test for operator - (Vector2f) - [Fact] - public void Vector2UnaryNegationTest() - { - Vector2 a = new Vector2(1.0f, 2.0f); - - Vector2 expected = new Vector2(-1.0f, -2.0f); - Vector2 actual; - - actual = -a; - - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.operator - did not return the expected value."); - } - - - - // A test for operator - (Vector2f) - // Negate test with special float value - [Fact] - public void Vector2UnaryNegationTest1() - { - Vector2 a = new Vector2(float.PositiveInfinity, float.NegativeInfinity); - - Vector2 actual = -a; - - Assert.True(float.IsNegativeInfinity(actual.X), "Vector2f.operator - did not return the expected value."); - Assert.True(float.IsPositiveInfinity(actual.Y), "Vector2f.operator - did not return the expected value."); - } - - // A test for operator - (Vector2f) - // Negate test with special float value - [Fact] - public void Vector2UnaryNegationTest2() - { - Vector2 a = new Vector2(float.NaN, 0.0f); - Vector2 actual = -a; - - Assert.True(float.IsNaN(actual.X), "Vector2f.operator - did not return the expected value."); - Assert.True(float.Equals(0.0f, actual.Y), "Vector2f.operator - did not return the expected value."); - } - - // A test for operator - (Vector2f, Vector2f) - [Fact] - public void Vector2SubtractionTest() - { - Vector2 a = new Vector2(1.0f, 3.0f); - Vector2 b = new Vector2(2.0f, 1.5f); - - Vector2 expected = new Vector2(-1.0f, 1.5f); - Vector2 actual; - - actual = a - b; - - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.operator - did not return the expected value."); - } - - // A test for operator * (Vector2f, float) - [Fact] - public void Vector2MultiplyOperatorTest() - { - Vector2 a = new Vector2(2.0f, 3.0f); - const float factor = 2.0f; - - Vector2 expected = new Vector2(4.0f, 6.0f); - Vector2 actual; - - actual = a * factor; - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.operator * did not return the expected value."); - } - - // A test for operator * (float, Vector2f) - [Fact] - public void Vector2MultiplyOperatorTest2() - { - Vector2 a = new Vector2(2.0f, 3.0f); - const float factor = 2.0f; - - Vector2 expected = new Vector2(4.0f, 6.0f); - Vector2 actual; - - actual = factor * a; - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.operator * did not return the expected value."); - } - - // A test for operator * (Vector2f, Vector2f) - [Fact] - public void Vector2MultiplyOperatorTest3() - { - Vector2 a = new Vector2(2.0f, 3.0f); - Vector2 b = new Vector2(4.0f, 5.0f); - - Vector2 expected = new Vector2(8.0f, 15.0f); - Vector2 actual; - - actual = a * b; - - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.operator * did not return the expected value."); - } - - // A test for operator / (Vector2f, float) - [Fact] - public void Vector2DivisionTest() - { - Vector2 a = new Vector2(2.0f, 3.0f); - - float div = 2.0f; - - Vector2 expected = new Vector2(1.0f, 1.5f); - Vector2 actual; - - actual = a / div; - - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.operator / did not return the expected value."); - } - - // A test for operator / (Vector2f, Vector2f) - [Fact] - public void Vector2DivisionTest1() - { - Vector2 a = new Vector2(2.0f, 3.0f); - Vector2 b = new Vector2(4.0f, 5.0f); - - Vector2 expected = new Vector2(2.0f / 4.0f, 3.0f / 5.0f); - Vector2 actual; - - actual = a / b; - - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.operator / did not return the expected value."); - } - - // A test for operator / (Vector2f, float) - // Divide by zero - [Fact] - public void Vector2DivisionTest2() - { - Vector2 a = new Vector2(-2.0f, 3.0f); - - float div = 0.0f; - - Vector2 actual = a / div; - - Assert.True(float.IsNegativeInfinity(actual.X), "Vector2f.operator / did not return the expected value."); - Assert.True(float.IsPositiveInfinity(actual.Y), "Vector2f.operator / did not return the expected value."); - } - - // A test for operator / (Vector2f, Vector2f) - // Divide by zero - [Fact] - public void Vector2DivisionTest3() - { - Vector2 a = new Vector2(0.047f, -3.0f); - Vector2 b = new Vector2(); - - Vector2 actual = a / b; - - Assert.True(float.IsInfinity(actual.X), "Vector2f.operator / did not return the expected value."); - Assert.True(float.IsInfinity(actual.Y), "Vector2f.operator / did not return the expected value."); - } - - // A test for operator + (Vector2f, Vector2f) - [Fact] - public void Vector2AdditionTest() - { - Vector2 a = new Vector2(1.0f, 2.0f); - Vector2 b = new Vector2(3.0f, 4.0f); - - Vector2 expected = new Vector2(4.0f, 6.0f); - Vector2 actual; - - actual = a + b; - - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.operator + did not return the expected value."); - } - - // A test for Vector2f (float, float) - [Fact] - public void Vector2ConstructorTest() - { - float x = 1.0f; - float y = 2.0f; - - Vector2 target = new Vector2(x, y); - Assert.True(MathHelper.Equal(target.X, x) && MathHelper.Equal(target.Y, y), "Vector2f(x,y) constructor did not return the expected value."); - } - - // A test for Vector2f () - // Constructor with no parameter - [Fact] - public void Vector2ConstructorTest2() - { - Vector2 target = new Vector2(); - Assert.Equal(0.0f, target.X); - Assert.Equal(0.0f, target.Y); - } - - // A test for Vector2f (float, float) - // Constructor with special floating values - [Fact] - public void Vector2ConstructorTest3() - { - Vector2 target = new Vector2(float.NaN, float.MaxValue); - Assert.Equal(target.X, float.NaN); - Assert.Equal(target.Y, float.MaxValue); - } - - // A test for Vector2f (float) - [Fact] - public void Vector2ConstructorTest4() - { - float value = 1.0f; - Vector2 target = new Vector2(value); - - Vector2 expected = new Vector2(value, value); - Assert.Equal(expected, target); - - value = 2.0f; - target = new Vector2(value); - expected = new Vector2(value, value); - Assert.Equal(expected, target); - } - - // A test for Add (Vector2f, Vector2f) - [Fact] - public void Vector2AddTest() - { - Vector2 a = new Vector2(1.0f, 2.0f); - Vector2 b = new Vector2(5.0f, 6.0f); - - Vector2 expected = new Vector2(6.0f, 8.0f); - Vector2 actual; - - actual = Vector2.Add(a, b); - Assert.Equal(expected, actual); - } - - // A test for Divide (Vector2f, float) - [Fact] - public void Vector2DivideTest() - { - Vector2 a = new Vector2(1.0f, 2.0f); - float div = 2.0f; - Vector2 expected = new Vector2(0.5f, 1.0f); - Vector2 actual; - actual = Vector2.Divide(a, div); - Assert.Equal(expected, actual); - } - - // A test for Divide (Vector2f, Vector2f) - [Fact] - public void Vector2DivideTest1() - { - Vector2 a = new Vector2(1.0f, 6.0f); - Vector2 b = new Vector2(5.0f, 2.0f); - - Vector2 expected = new Vector2(1.0f / 5.0f, 6.0f / 2.0f); - Vector2 actual; - - actual = Vector2.Divide(a, b); - Assert.Equal(expected, actual); - } - - // A test for Equals (object) - [Fact] - public void Vector2EqualsTest() - { - Vector2 a = new Vector2(1.0f, 2.0f); - Vector2 b = new Vector2(1.0f, 2.0f); - - // case 1: compare between same values - object obj = b; - - bool expected = true; - bool actual = a.Equals(obj); - Assert.Equal(expected, actual); - - // case 2: compare between different values - b.X = 10.0f; - obj = b; - expected = false; - actual = a.Equals(obj); - Assert.Equal(expected, actual); - - // case 3: compare between different types. - obj = new Quaternion(); - expected = false; - actual = a.Equals(obj); - Assert.Equal(expected, actual); - - // case 3: compare against null. - obj = null; - expected = false; - actual = a.Equals(obj); - Assert.Equal(expected, actual); - } - - // A test for Multiply (Vector2f, float) - [Fact] - public void Vector2MultiplyTest() - { - Vector2 a = new Vector2(1.0f, 2.0f); - const float factor = 2.0f; - Vector2 expected = new Vector2(2.0f, 4.0f); - Vector2 actual = Vector2.Multiply(a, factor); - Assert.Equal(expected, actual); - } - - // A test for Multiply (float, Vector2f) - [Fact] - public void Vector2MultiplyTest2() - { - Vector2 a = new Vector2(1.0f, 2.0f); - const float factor = 2.0f; - Vector2 expected = new Vector2(2.0f, 4.0f); - Vector2 actual = Vector2.Multiply(factor, a); - Assert.Equal(expected, actual); - } - - // A test for Multiply (Vector2f, Vector2f) - [Fact] - public void Vector2MultiplyTest3() - { - Vector2 a = new Vector2(1.0f, 2.0f); - Vector2 b = new Vector2(5.0f, 6.0f); - - Vector2 expected = new Vector2(5.0f, 12.0f); - Vector2 actual; - - actual = Vector2.Multiply(a, b); - Assert.Equal(expected, actual); - } - - // A test for Negate (Vector2f) - [Fact] - public void Vector2NegateTest() - { - Vector2 a = new Vector2(1.0f, 2.0f); - - Vector2 expected = new Vector2(-1.0f, -2.0f); - Vector2 actual; - - actual = Vector2.Negate(a); - Assert.Equal(expected, actual); - } - - // A test for operator != (Vector2f, Vector2f) - [Fact] - public void Vector2InequalityTest() - { - Vector2 a = new Vector2(1.0f, 2.0f); - Vector2 b = new Vector2(1.0f, 2.0f); - - // case 1: compare between same values - bool expected = false; - bool actual = a != b; - Assert.Equal(expected, actual); - - // case 2: compare between different values - b.X = 10.0f; - expected = true; - actual = a != b; - Assert.Equal(expected, actual); - } - - // A test for operator == (Vector2f, Vector2f) - [Fact] - public void Vector2EqualityTest() - { - Vector2 a = new Vector2(1.0f, 2.0f); - Vector2 b = new Vector2(1.0f, 2.0f); - - // case 1: compare between same values - bool expected = true; - bool actual = a == b; - Assert.Equal(expected, actual); - - // case 2: compare between different values - b.X = 10.0f; - expected = false; - actual = a == b; - Assert.Equal(expected, actual); - } - - // A test for Subtract (Vector2f, Vector2f) - [Fact] - public void Vector2SubtractTest() - { - Vector2 a = new Vector2(1.0f, 6.0f); - Vector2 b = new Vector2(5.0f, 2.0f); - - Vector2 expected = new Vector2(-4.0f, 4.0f); - Vector2 actual; - - actual = Vector2.Subtract(a, b); - Assert.Equal(expected, actual); - } - - // A test for UnitX - [Fact] - public void Vector2UnitXTest() - { - Vector2 val = new Vector2(1.0f, 0.0f); - Assert.Equal(val, Vector2.UnitX); - } - - // A test for UnitY - [Fact] - public void Vector2UnitYTest() - { - Vector2 val = new Vector2(0.0f, 1.0f); - Assert.Equal(val, Vector2.UnitY); - } - - // A test for One - [Fact] - public void Vector2OneTest() - { - Vector2 val = new Vector2(1.0f, 1.0f); - Assert.Equal(val, Vector2.One); - } - - // A test for Zero - [Fact] - public void Vector2ZeroTest() - { - Vector2 val = new Vector2(0.0f, 0.0f); - Assert.Equal(val, Vector2.Zero); - } - - // A test for Equals (Vector2f) - [Fact] - public void Vector2EqualsTest1() - { - Vector2 a = new Vector2(1.0f, 2.0f); - Vector2 b = new Vector2(1.0f, 2.0f); - - // case 1: compare between same values - bool expected = true; - bool actual = a.Equals(b); - Assert.Equal(expected, actual); - - // case 2: compare between different values - b.X = 10.0f; - expected = false; - actual = a.Equals(b); - Assert.Equal(expected, actual); - } - - // A test for Vector2f comparison involving NaN values - [Fact] - public void Vector2EqualsNanTest() - { - Vector2 a = new Vector2(float.NaN, 0); - Vector2 b = new Vector2(0, float.NaN); - - Assert.False(a == Vector2.Zero); - Assert.False(b == Vector2.Zero); - - Assert.True(a != Vector2.Zero); - Assert.True(b != Vector2.Zero); - - Assert.False(a.Equals(Vector2.Zero)); - Assert.False(b.Equals(Vector2.Zero)); - - // Counterintuitive result - IEEE rules for NaN comparison are weird! - Assert.False(a.Equals(a)); - Assert.False(b.Equals(b)); - } - - // A test for Reflect (Vector2f, Vector2f) - [Fact] - public void Vector2ReflectTest() - { - Vector2 a = Vector2.Normalize(new Vector2(1.0f, 1.0f)); - - // Reflect on XZ plane. - Vector2 n = new Vector2(0.0f, 1.0f); - Vector2 expected = new Vector2(a.X, -a.Y); - Vector2 actual = Vector2.Reflect(a, n); - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.Reflect did not return the expected value."); - - // Reflect on XY plane. - n = new Vector2(0.0f, 0.0f); - expected = new Vector2(a.X, a.Y); - actual = Vector2.Reflect(a, n); - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.Reflect did not return the expected value."); - - // Reflect on YZ plane. - n = new Vector2(1.0f, 0.0f); - expected = new Vector2(-a.X, a.Y); - actual = Vector2.Reflect(a, n); - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.Reflect did not return the expected value."); - } - - // A test for Reflect (Vector2f, Vector2f) - // Reflection when normal and source are the same - [Fact] - public void Vector2ReflectTest1() - { - Vector2 n = new Vector2(0.45f, 1.28f); - n = Vector2.Normalize(n); - Vector2 a = n; - - Vector2 expected = -n; - Vector2 actual = Vector2.Reflect(a, n); - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.Reflect did not return the expected value."); - } - - // A test for Reflect (Vector2f, Vector2f) - // Reflection when normal and source are negation - [Fact] - public void Vector2ReflectTest2() - { - Vector2 n = new Vector2(0.45f, 1.28f); - n = Vector2.Normalize(n); - Vector2 a = -n; - - Vector2 expected = n; - Vector2 actual = Vector2.Reflect(a, n); - Assert.True(MathHelper.Equal(expected, actual), "Vector2f.Reflect did not return the expected value."); - } - - [Fact] - public void Vector2AbsTest() - { - Vector2 v1 = new Vector2(-2.5f, 2.0f); - Vector2 v3 = Vector2.Abs(new Vector2(0.0f, float.NegativeInfinity)); - Vector2 v = Vector2.Abs(v1); - Assert.Equal(2.5f, v.X); - Assert.Equal(2.0f, v.Y); - Assert.Equal(0.0f, v3.X); - Assert.Equal(float.PositiveInfinity, v3.Y); - } - - [Fact] - public void Vector2SqrtTest() - { - Vector2 v1 = new Vector2(-2.5f, 2.0f); - Vector2 v2 = new Vector2(5.5f, 4.5f); - Assert.Equal(2, (int)Vector2.SquareRoot(v2).X); - Assert.Equal(2, (int)Vector2.SquareRoot(v2).Y); - Assert.Equal(float.NaN, Vector2.SquareRoot(v1).X); - } - - // A test to make sure these types are blittable directly into GPU buffer memory layouts - [Fact] - public unsafe void Vector2SizeofTest() - { - Assert.Equal(8, sizeof(Vector2)); - Assert.Equal(16, sizeof(Vector2_2x)); - Assert.Equal(12, sizeof(Vector2PlusFloat)); - Assert.Equal(24, sizeof(Vector2PlusFloat_2x)); - } - - [StructLayout(LayoutKind.Sequential)] - struct Vector2_2x - { - private Vector2 _a; - private Vector2 _b; - } - - [StructLayout(LayoutKind.Sequential)] - struct Vector2PlusFloat - { - private Vector2 _v; - private float _f; - } - - [StructLayout(LayoutKind.Sequential)] - struct Vector2PlusFloat_2x - { - private Vector2PlusFloat _a; - private Vector2PlusFloat _b; - } - - [Fact] - public void SetFieldsTest() - { - Vector2 v3 = new Vector2(4f, 5f); - v3.X = 1.0f; - v3.Y = 2.0f; - Assert.Equal(1.0f, v3.X); - Assert.Equal(2.0f, v3.Y); - Vector2 v4 = v3; - v4.Y = 0.5f; - Assert.Equal(1.0f, v4.X); - Assert.Equal(0.5f, v4.Y); - Assert.Equal(2.0f, v3.Y); - } - - [Fact] - public void EmbeddedVectorSetFields() - { - EmbeddedVectorObject evo = new EmbeddedVectorObject(); - evo.FieldVector.X = 5.0f; - evo.FieldVector.Y = 5.0f; - Assert.Equal(5.0f, evo.FieldVector.X); - Assert.Equal(5.0f, evo.FieldVector.Y); - } - - private class EmbeddedVectorObject - { - public Vector2 FieldVector; - } + [Fact] + public void Vector2CopyToTest() + { + Vector2 v1 = new Vector2(2.0f, 3.0f); + + var a = new Single[3]; + var b = new Single[2]; + + Assert.Throws(() => v1.CopyTo(a, -1)); + Assert.Throws(() => v1.CopyTo(a, a.Length)); + + v1.CopyTo(a, 1); + v1.CopyTo(b); + Assert.Equal(0.0, a[0]); + Assert.Equal(2.0, a[1]); + Assert.Equal(3.0, a[2]); + Assert.Equal(2.0, b[0]); + Assert.Equal(3.0, b[1]); + } + + [Fact] + public void Vector2GetHashCodeTest() + { + Vector2 v1 = new Vector2(2.0f, 3.0f); + Vector2 v2 = new Vector2(2.0f, 3.0f); + Vector2 v3 = new Vector2(3.0f, 2.0f); + Assert.Equal(v1.GetHashCode(), v1.GetHashCode()); + Assert.Equal(v1.GetHashCode(), v2.GetHashCode()); + Assert.NotEqual(v1.GetHashCode(), v3.GetHashCode()); + Vector2 v4 = new Vector2(0.0f, 0.0f); + Vector2 v6 = new Vector2(1.0f, 0.0f); + Vector2 v7 = new Vector2(0.0f, 1.0f); + Vector2 v8 = new Vector2(1.0f, 1.0f); + Assert.NotEqual(v4.GetHashCode(), v6.GetHashCode()); + Assert.NotEqual(v4.GetHashCode(), v7.GetHashCode()); + Assert.NotEqual(v4.GetHashCode(), v8.GetHashCode()); + Assert.NotEqual(v7.GetHashCode(), v6.GetHashCode()); + Assert.NotEqual(v8.GetHashCode(), v6.GetHashCode()); + Assert.NotEqual(v8.GetHashCode(), v7.GetHashCode()); + } + + [Fact] + public void Vector2ToStringTest() + { + string separator = CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator; + CultureInfo enUsCultureInfo = new CultureInfo("en-US"); + + Vector2 v1 = new Vector2(2.0f, 3.0f); + + string v1str = v1.ToString(); + string expectedv1 = string.Format(CultureInfo.CurrentCulture + , "<{1:G}{0} {2:G}>" + , new object[] { separator, 2, 3 }); + Assert.Equal(expectedv1, v1str); + + string v1strformatted = v1.ToString("c", CultureInfo.CurrentCulture); + string expectedv1formatted = string.Format(CultureInfo.CurrentCulture + , "<{1:c}{0} {2:c}>" + , new object[] { separator, 2, 3 }); + Assert.Equal(expectedv1formatted, v1strformatted); + + string v2strformatted = v1.ToString("c", enUsCultureInfo); + string expectedv2formatted = string.Format(enUsCultureInfo + , "<{1:c}{0} {2:c}>" + , new object[] { enUsCultureInfo.NumberFormat.NumberGroupSeparator, 2, 3 }); + Assert.Equal(expectedv2formatted, v2strformatted); + + string v3strformatted = v1.ToString("c"); + string expectedv3formatted = string.Format(CultureInfo.CurrentCulture + , "<{1:c}{0} {2:c}>" + , new object[] { separator, 2, 3 }); + Assert.Equal(expectedv3formatted, v3strformatted); + } + + // A test for Distance (Vector2, Vector2) + [Fact] + public void Vector2DistanceTest() + { + Vector2 a = new Vector2(1.0f, 2.0f); + Vector2 b = new Vector2(3.0f, 4.0f); + + Single expected = (Single)System.Math.Sqrt(8); + Single actual; + + actual = Vector2.Distance(a, b); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Distance did not return the expected value."); + } + + // A test for Distance (Vector2, Vector2) + // Distance from the same point + [Fact] + public void Vector2DistanceTest2() + { + Vector2 a = new Vector2(1.051f, 2.05f); + Vector2 b = new Vector2(1.051f, 2.05f); + + Single actual = Vector2.Distance(a, b); + Assert.Equal(0.0f, actual); + } + + // A test for DistanceSquared (Vector2, Vector2) + [Fact] + public void Vector2DistanceSquaredTest() + { + Vector2 a = new Vector2(1.0f, 2.0f); + Vector2 b = new Vector2(3.0f, 4.0f); + + Single expected = 8.0f; + Single actual; + + actual = Vector2.DistanceSquared(a, b); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.DistanceSquared did not return the expected value."); + } + + // A test for Dot (Vector2, Vector2) + [Fact] + public void Vector2DotTest() + { + Vector2 a = new Vector2(1.0f, 2.0f); + Vector2 b = new Vector2(3.0f, 4.0f); + + Single expected = 11.0f; + Single actual; + + actual = Vector2.Dot(a, b); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Dot did not return the expected value."); + } + + // A test for Dot (Vector2, Vector2) + // Dot test for perpendicular vector + [Fact] + public void Vector2DotTest1() + { + Vector2 a = new Vector2(1.55f, 1.55f); + Vector2 b = new Vector2(-1.55f, 1.55f); + + Single expected = 0.0f; + Single actual = Vector2.Dot(a, b); + Assert.Equal(expected, actual); + } + + // A test for Dot (Vector2, Vector2) + // Dot test with special Single values + [Fact] + public void Vector2DotTest2() + { + Vector2 a = new Vector2(Single.MinValue, Single.MinValue); + Vector2 b = new Vector2(Single.MaxValue, Single.MaxValue); + + Single actual = Vector2.Dot(a, b); + Assert.True(Single.IsNegativeInfinity(actual), "Vector2.Dot did not return the expected value."); + } + + // A test for Length () + [Fact] + public void Vector2LengthTest() + { + Vector2 a = new Vector2(2.0f, 4.0f); + + Vector2 target = a; + + Single expected = (Single)System.Math.Sqrt(20); + Single actual; + + actual = target.Length(); + + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Length did not return the expected value."); + } + + // A test for Length () + // Length test where length is zero + [Fact] + public void Vector2LengthTest1() + { + Vector2 target = Vector2.Zero; + + Single expected = 0.0f; + Single actual; + + actual = target.Length(); + + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Length did not return the expected value."); + } + + // A test for LengthSquared () + [Fact] + public void Vector2LengthSquaredTest() + { + Vector2 a = new Vector2(2.0f, 4.0f); + + Vector2 target = a; + + Single expected = 20.0f; + Single actual; + + actual = target.LengthSquared(); + + Assert.True(MathHelper.Equal(expected, actual), "Vector2.LengthSquared did not return the expected value."); + } + + // A test for LengthSquared () + // LengthSquared test where the result is zero + [Fact] + public void Vector2LengthSquaredTest1() + { + Vector2 a = new Vector2(0.0f, 0.0f); + + Single expected = 0.0f; + Single actual = a.LengthSquared(); + + Assert.Equal(expected, actual); + } + + // A test for Min (Vector2, Vector2) + [Fact] + public void Vector2MinTest() + { + Vector2 a = new Vector2(-1.0f, 4.0f); + Vector2 b = new Vector2(2.0f, 1.0f); + + Vector2 expected = new Vector2(-1.0f, 1.0f); + Vector2 actual; + actual = Vector2.Min(a, b); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Min did not return the expected value."); + } + + [Fact] + public void Vector2MinMaxCodeCoverageTest() + { + Vector2 min = new Vector2(0, 0); + Vector2 max = new Vector2(1, 1); + Vector2 actual; + + // Min. + actual = Vector2.Min(min, max); + Assert.Equal(actual, min); + + actual = Vector2.Min(max, min); + Assert.Equal(actual, min); + + // Max. + actual = Vector2.Max(min, max); + Assert.Equal(actual, max); + + actual = Vector2.Max(max, min); + Assert.Equal(actual, max); + } + + // A test for Max (Vector2, Vector2) + [Fact] + public void Vector2MaxTest() + { + Vector2 a = new Vector2(-1.0f, 4.0f); + Vector2 b = new Vector2(2.0f, 1.0f); + + Vector2 expected = new Vector2(2.0f, 4.0f); + Vector2 actual; + actual = Vector2.Max(a, b); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Max did not return the expected value."); + } + + // A test for Clamp (Vector2, Vector2, Vector2) + [Fact] + public void Vector2ClampTest() + { + Vector2 a = new Vector2(0.5f, 0.3f); + Vector2 min = new Vector2(0.0f, 0.1f); + Vector2 max = new Vector2(1.0f, 1.1f); + + // Normal case. + // Case N1: specified value is in the range. + Vector2 expected = new Vector2(0.5f, 0.3f); + Vector2 actual = Vector2.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Clamp did not return the expected value."); + // Normal case. + // Case N2: specified value is bigger than max value. + a = new Vector2(2.0f, 3.0f); + expected = max; + actual = Vector2.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Clamp did not return the expected value."); + // Case N3: specified value is smaller than max value. + a = new Vector2(-1.0f, -2.0f); + expected = min; + actual = Vector2.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Clamp did not return the expected value."); + // Case N4: combination case. + a = new Vector2(-2.0f, 4.0f); + expected = new Vector2(min.X, max.Y); + actual = Vector2.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Clamp did not return the expected value."); + // User specified min value is bigger than max value. + max = new Vector2(0.0f, 0.1f); + min = new Vector2(1.0f, 1.1f); + + // Case W1: specified value is in the range. + a = new Vector2(0.5f, 0.3f); + expected = max; + actual = Vector2.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Clamp did not return the expected value."); + + // Normal case. + // Case W2: specified value is bigger than max and min value. + a = new Vector2(2.0f, 3.0f); + expected = max; + actual = Vector2.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Clamp did not return the expected value."); + + // Case W3: specified value is smaller than min and max value. + a = new Vector2(-1.0f, -2.0f); + expected = max; + actual = Vector2.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Clamp did not return the expected value."); + } + + // A test for Lerp (Vector2, Vector2, Single) + [Fact] + public void Vector2LerpTest() + { + Vector2 a = new Vector2(1.0f, 2.0f); + Vector2 b = new Vector2(3.0f, 4.0f); + + Single t = 0.5f; + + Vector2 expected = new Vector2(2.0f, 3.0f); + Vector2 actual; + actual = Vector2.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector2, Vector2, Single) + // Lerp test with factor zero + [Fact] + public void Vector2LerpTest1() + { + Vector2 a = new Vector2(0.0f, 0.0f); + Vector2 b = new Vector2(3.18f, 4.25f); + + Single t = 0.0f; + Vector2 expected = Vector2.Zero; + Vector2 actual = Vector2.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector2, Vector2, Single) + // Lerp test with factor one + [Fact] + public void Vector2LerpTest2() + { + Vector2 a = new Vector2(0.0f, 0.0f); + Vector2 b = new Vector2(3.18f, 4.25f); + + Single t = 1.0f; + Vector2 expected = new Vector2(3.18f, 4.25f); + Vector2 actual = Vector2.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector2, Vector2, Single) + // Lerp test with factor > 1 + [Fact] + public void Vector2LerpTest3() + { + Vector2 a = new Vector2(0.0f, 0.0f); + Vector2 b = new Vector2(3.18f, 4.25f); + + Single t = 2.0f; + Vector2 expected = b * 2.0f; + Vector2 actual = Vector2.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector2, Vector2, Single) + // Lerp test with factor < 0 + [Fact] + public void Vector2LerpTest4() + { + Vector2 a = new Vector2(0.0f, 0.0f); + Vector2 b = new Vector2(3.18f, 4.25f); + + Single t = -2.0f; + Vector2 expected = -(b * 2.0f); + Vector2 actual = Vector2.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector2, Vector2, Single) + // Lerp test with special Single value + [Fact] + public void Vector2LerpTest5() + { + Vector2 a = new Vector2(45.67f, 90.0f); + Vector2 b = new Vector2(Single.PositiveInfinity, Single.NegativeInfinity); + + Single t = 0.408f; + Vector2 actual = Vector2.Lerp(a, b, t); + Assert.True(Single.IsPositiveInfinity(actual.X), "Vector2.Lerp did not return the expected value."); + Assert.True(Single.IsNegativeInfinity(actual.Y), "Vector2.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector2, Vector2, Single) + // Lerp test from the same point + [Fact] + public void Vector2LerpTest6() + { + Vector2 a = new Vector2(1.0f, 2.0f); + Vector2 b = new Vector2(1.0f, 2.0f); + + Single t = 0.5f; + + Vector2 expected = new Vector2(1.0f, 2.0f); + Vector2 actual = Vector2.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector2, Vector2, Single) + // Lerp test with values known to be innacurate with the old lerp impl + [Fact] + public void Vector2LerpTest7() + { + Vector2 a = new Vector2(0.44728136f); + Vector2 b = new Vector2(0.46345946f); + + Single t = 0.26402435f; + + Vector2 expected = new Vector2(0.45155275f); + Vector2 actual = Vector2.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector2, Vector2, Single) + // Lerp test with values known to be innacurate with the old lerp impl + // (Old code incorrectly gets 0.33333588) + [Fact] + public void Vector2LerpTest8() + { + Vector2 a = new Vector2(-100); + Vector2 b = new Vector2(0.33333334f); + + Single t = 1f; + + Vector2 expected = new Vector2(0.33333334f); + Vector2 actual = Vector2.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Lerp did not return the expected value."); + } + + // // A test for Transform(Vector2, Matrix4x4) + // [Fact] + // public void Vector2TransformTest() + // { + // Vector2 v = new Vector2(1.0f, 2.0f); + // Matrix4x4 m = + // Matrix4x4.CreateRotationX(MathHelper.ToRadians(30.0f)) * + // Matrix4x4.CreateRotationY(MathHelper.ToRadians(30.0f)) * + // Matrix4x4.CreateRotationZ(MathHelper.ToRadians(30.0f)); + // m.M41 = 10.0f; + // m.M42 = 20.0f; + // m.M43 = 30.0f; + + // Vector2 expected = new Vector2(10.316987f, 22.183012f); + // Vector2 actual; + + // actual = Vector2.Transform(v, m); + // Assert.True(MathHelper.Equal(expected, actual), "Vector2.Transform did not return the expected value."); + // } + + // // A test for Transform(Vector2, Matrix3x2) + // [Fact] + // public void Vector2Transform3x2Test() + // { + // Vector2 v = new Vector2(1.0f, 2.0f); + // Matrix3x2 m = Matrix3x2.CreateRotation(MathHelper.ToRadians(30.0f)); + // m.M31 = 10.0f; + // m.M32 = 20.0f; + + // Vector2 expected = new Vector2(9.866025f, 22.23205f); + // Vector2 actual; + + // actual = Vector2.Transform(v, m); + // Assert.True(MathHelper.Equal(expected, actual), "Vector2.Transform did not return the expected value."); + // } + + // // A test for TransformNormal (Vector2, Matrix4x4) + // [Fact] + // public void Vector2TransformNormalTest() + // { + // Vector2 v = new Vector2(1.0f, 2.0f); + // Matrix4x4 m = + // Matrix4x4.CreateRotationX(MathHelper.ToRadians(30.0f)) * + // Matrix4x4.CreateRotationY(MathHelper.ToRadians(30.0f)) * + // Matrix4x4.CreateRotationZ(MathHelper.ToRadians(30.0f)); + // m.M41 = 10.0f; + // m.M42 = 20.0f; + // m.M43 = 30.0f; + + // Vector2 expected = new Vector2(0.3169873f, 2.18301272f); + // Vector2 actual; + + // actual = Vector2.TransformNormal(v, m); + // Assert.True(MathHelper.Equal(expected, actual), "Vector2.Tranform did not return the expected value."); + // } + + // // A test for TransformNormal (Vector2, Matrix3x2) + // [Fact] + // public void Vector2TransformNormal3x2Test() + // { + // Vector2 v = new Vector2(1.0f, 2.0f); + // Matrix3x2 m = Matrix3x2.CreateRotation(MathHelper.ToRadians(30.0f)); + // m.M31 = 10.0f; + // m.M32 = 20.0f; + + // Vector2 expected = new Vector2(-0.133974612f, 2.232051f); + // Vector2 actual; + + // actual = Vector2.TransformNormal(v, m); + // Assert.True(MathHelper.Equal(expected, actual), "Vector2.Transform did not return the expected value."); + // } + + // // A test for Transform (Vector2, Quaternion) + // [Fact] + // public void Vector2TransformByQuaternionTest() + // { + // Vector2 v = new Vector2(1.0f, 2.0f); + + // Matrix4x4 m = + // Matrix4x4.CreateRotationX(MathHelper.ToRadians(30.0f)) * + // Matrix4x4.CreateRotationY(MathHelper.ToRadians(30.0f)) * + // Matrix4x4.CreateRotationZ(MathHelper.ToRadians(30.0f)); + // Quaternion q = Quaternion.CreateFromRotationMatrix(m); + + // Vector2 expected = Vector2.Transform(v, m); + // Vector2 actual = Vector2.Transform(v, q); + // Assert.True(MathHelper.Equal(expected, actual), "Vector2.Transform did not return the expected value."); + // } + + // // A test for Transform (Vector2, Quaternion) + // // Transform Vector2 with zero quaternion + // [Fact] + // public void Vector2TransformByQuaternionTest1() + // { + // Vector2 v = new Vector2(1.0f, 2.0f); + // Quaternion q = new Quaternion(); + // Vector2 expected = v; + + // Vector2 actual = Vector2.Transform(v, q); + // Assert.True(MathHelper.Equal(expected, actual), "Vector2.Transform did not return the expected value."); + // } + + // // A test for Transform (Vector2, Quaternion) + // // Transform Vector2 with identity quaternion + // [Fact] + // public void Vector2TransformByQuaternionTest2() + // { + // Vector2 v = new Vector2(1.0f, 2.0f); + // Quaternion q = Quaternion.Identity; + // Vector2 expected = v; + + // Vector2 actual = Vector2.Transform(v, q); + // Assert.True(MathHelper.Equal(expected, actual), "Vector2.Transform did not return the expected value."); + // } + + // A test for Normalize (Vector2) + [Fact] + public void Vector2NormalizeTest() + { + Vector2 a = new Vector2(2.0f, 3.0f); + Vector2 expected = new Vector2(0.554700196225229122018341733457f, 0.8320502943378436830275126001855f); + Vector2 actual; + + actual = Vector2.Normalize(a); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Normalize did not return the expected value."); + } + + // A test for Normalize (Vector2) + // Normalize zero length vector + [Fact] + public void Vector2NormalizeTest1() + { + Vector2 a = new Vector2(); // no parameter, default to 0.0f + Vector2 actual = Vector2.Normalize(a); + Assert.True(Single.IsNaN(actual.X) && Single.IsNaN(actual.Y), "Vector2.Normalize did not return the expected value."); + } + + // A test for Normalize (Vector2) + // Normalize infinite length vector + [Fact] + public void Vector2NormalizeTest2() + { + Vector2 a = new Vector2(Single.MaxValue, Single.MaxValue); + Vector2 actual = Vector2.Normalize(a); + Vector2 expected = new Vector2(0, 0); + Assert.Equal(expected, actual); + } + + // A test for operator - (Vector2) + [Fact] + public void Vector2UnaryNegationTest() + { + Vector2 a = new Vector2(1.0f, 2.0f); + + Vector2 expected = new Vector2(-1.0f, -2.0f); + Vector2 actual; + + actual = -a; + + Assert.True(MathHelper.Equal(expected, actual), "Vector2.operator - did not return the expected value."); + } + + + + // A test for operator - (Vector2) + // Negate test with special Single value + [Fact] + public void Vector2UnaryNegationTest1() + { + Vector2 a = new Vector2(Single.PositiveInfinity, Single.NegativeInfinity); + + Vector2 actual = -a; + + Assert.True(Single.IsNegativeInfinity(actual.X), "Vector2.operator - did not return the expected value."); + Assert.True(Single.IsPositiveInfinity(actual.Y), "Vector2.operator - did not return the expected value."); + } + + // A test for operator - (Vector2) + // Negate test with special Single value + [Fact] + public void Vector2UnaryNegationTest2() + { + Vector2 a = new Vector2(Single.NaN, 0.0f); + Vector2 actual = -a; + + Assert.True(Single.IsNaN(actual.X), "Vector2.operator - did not return the expected value."); + Assert.True(Single.Equals(0.0f, actual.Y), "Vector2.operator - did not return the expected value."); + } + + // A test for operator - (Vector2, Vector2) + [Fact] + public void Vector2SubtractionTest() + { + Vector2 a = new Vector2(1.0f, 3.0f); + Vector2 b = new Vector2(2.0f, 1.5f); + + Vector2 expected = new Vector2(-1.0f, 1.5f); + Vector2 actual; + + actual = a - b; + + Assert.True(MathHelper.Equal(expected, actual), "Vector2.operator - did not return the expected value."); + } + + // A test for operator * (Vector2, Single) + [Fact] + public void Vector2MultiplyOperatorTest() + { + Vector2 a = new Vector2(2.0f, 3.0f); + const Single factor = 2.0f; + + Vector2 expected = new Vector2(4.0f, 6.0f); + Vector2 actual; + + actual = a * factor; + Assert.True(MathHelper.Equal(expected, actual), "Vector2.operator * did not return the expected value."); + } + + // A test for operator * (Single, Vector2) + [Fact] + public void Vector2MultiplyOperatorTest2() + { + Vector2 a = new Vector2(2.0f, 3.0f); + const Single factor = 2.0f; + + Vector2 expected = new Vector2(4.0f, 6.0f); + Vector2 actual; + + actual = factor * a; + Assert.True(MathHelper.Equal(expected, actual), "Vector2.operator * did not return the expected value."); + } + + // A test for operator * (Vector2, Vector2) + [Fact] + public void Vector2MultiplyOperatorTest3() + { + Vector2 a = new Vector2(2.0f, 3.0f); + Vector2 b = new Vector2(4.0f, 5.0f); + + Vector2 expected = new Vector2(8.0f, 15.0f); + Vector2 actual; + + actual = a * b; + + Assert.True(MathHelper.Equal(expected, actual), "Vector2.operator * did not return the expected value."); + } + + // A test for operator / (Vector2, Single) + [Fact] + public void Vector2DivisionTest() + { + Vector2 a = new Vector2(2.0f, 3.0f); + + Single div = 2.0f; + + Vector2 expected = new Vector2(1.0f, 1.5f); + Vector2 actual; + + actual = a / div; + + Assert.True(MathHelper.Equal(expected, actual), "Vector2.operator / did not return the expected value."); + } + + // A test for operator / (Vector2, Vector2) + [Fact] + public void Vector2DivisionTest1() + { + Vector2 a = new Vector2(2.0f, 3.0f); + Vector2 b = new Vector2(4.0f, 5.0f); + + Vector2 expected = new Vector2(2.0f / 4.0f, 3.0f / 5.0f); + Vector2 actual; + + actual = a / b; + + Assert.True(MathHelper.Equal(expected, actual), "Vector2.operator / did not return the expected value."); + } + + // A test for operator / (Vector2, Single) + // Divide by zero + [Fact] + public void Vector2DivisionTest2() + { + Vector2 a = new Vector2(-2.0f, 3.0f); + + Single div = 0.0f; + + Vector2 actual = a / div; + + Assert.True(Single.IsNegativeInfinity(actual.X), "Vector2.operator / did not return the expected value."); + Assert.True(Single.IsPositiveInfinity(actual.Y), "Vector2.operator / did not return the expected value."); + } + + // A test for operator / (Vector2, Vector2) + // Divide by zero + [Fact] + public void Vector2DivisionTest3() + { + Vector2 a = new Vector2(0.047f, -3.0f); + Vector2 b = new Vector2(); + + Vector2 actual = a / b; + + Assert.True(Single.IsInfinity(actual.X), "Vector2.operator / did not return the expected value."); + Assert.True(Single.IsInfinity(actual.Y), "Vector2.operator / did not return the expected value."); + } + + // A test for operator + (Vector2, Vector2) + [Fact] + public void Vector2AdditionTest() + { + Vector2 a = new Vector2(1.0f, 2.0f); + Vector2 b = new Vector2(3.0f, 4.0f); + + Vector2 expected = new Vector2(4.0f, 6.0f); + Vector2 actual; + + actual = a + b; + + Assert.True(MathHelper.Equal(expected, actual), "Vector2.operator + did not return the expected value."); + } + + // A test for Vector2 (Single, Single) + [Fact] + public void Vector2ConstructorTest() + { + Single x = 1.0f; + Single y = 2.0f; + + Vector2 target = new Vector2(x, y); + Assert.True(MathHelper.Equal(target.X, x) && MathHelper.Equal(target.Y, y), "Vector2(x,y) constructor did not return the expected value."); + } + + // A test for Vector2 () + // Constructor with no parameter + [Fact] + public void Vector2ConstructorTest2() + { + Vector2 target = new Vector2(); + Assert.Equal(0.0f, target.X); + Assert.Equal(0.0f, target.Y); + } + + // A test for Vector2 (Single, Single) + // Constructor with special Singleing values + [Fact] + public void Vector2ConstructorTest3() + { + Vector2 target = new Vector2(Single.NaN, Single.MaxValue); + Assert.Equal(target.X, Single.NaN); + Assert.Equal(target.Y, Single.MaxValue); + } + + // A test for Vector2 (Single) + [Fact] + public void Vector2ConstructorTest4() + { + Single value = 1.0f; + Vector2 target = new Vector2(value); + + Vector2 expected = new Vector2(value, value); + Assert.Equal(expected, target); + + value = 2.0f; + target = new Vector2(value); + expected = new Vector2(value, value); + Assert.Equal(expected, target); + } + + // A test for Add (Vector2, Vector2) + [Fact] + public void Vector2AddTest() + { + Vector2 a = new Vector2(1.0f, 2.0f); + Vector2 b = new Vector2(5.0f, 6.0f); + + Vector2 expected = new Vector2(6.0f, 8.0f); + Vector2 actual; + + actual = Vector2.Add(a, b); + Assert.Equal(expected, actual); + } + + // A test for Divide (Vector2, Single) + [Fact] + public void Vector2DivideTest() + { + Vector2 a = new Vector2(1.0f, 2.0f); + Single div = 2.0f; + Vector2 expected = new Vector2(0.5f, 1.0f); + Vector2 actual; + actual = Vector2.Divide(a, div); + Assert.Equal(expected, actual); + } + + // A test for Divide (Vector2, Vector2) + [Fact] + public void Vector2DivideTest1() + { + Vector2 a = new Vector2(1.0f, 6.0f); + Vector2 b = new Vector2(5.0f, 2.0f); + + Vector2 expected = new Vector2(1.0f / 5.0f, 6.0f / 2.0f); + Vector2 actual; + + actual = Vector2.Divide(a, b); + Assert.Equal(expected, actual); + } + + // A test for Equals (object) + [Fact] + public void Vector2EqualsTest() + { + Vector2 a = new Vector2(1.0f, 2.0f); + Vector2 b = new Vector2(1.0f, 2.0f); + + // case 1: compare between same values + object obj = b; + + bool expected = true; + bool actual = a.Equals(obj); + Assert.Equal(expected, actual); + + // case 2: compare between different values + b = new Vector2(b.X, 10); + obj = b; + expected = false; + actual = a.Equals(obj); + Assert.Equal(expected, actual); + + // case 3: compare between different types. + obj = new Quaternion(); + expected = false; + actual = a.Equals(obj); + Assert.Equal(expected, actual); + + // case 3: compare against null. + obj = null; + expected = false; + actual = a.Equals(obj); + Assert.Equal(expected, actual); + } + + // A test for Multiply (Vector2, Single) + [Fact] + public void Vector2MultiplyTest() + { + Vector2 a = new Vector2(1.0f, 2.0f); + const Single factor = 2.0f; + Vector2 expected = new Vector2(2.0f, 4.0f); + Vector2 actual = Vector2.Multiply(a, factor); + Assert.Equal(expected, actual); + } + + // A test for Multiply (Single, Vector2) + [Fact] + public void Vector2MultiplyTest2() + { + Vector2 a = new Vector2(1.0f, 2.0f); + const Single factor = 2.0f; + Vector2 expected = new Vector2(2.0f, 4.0f); + Vector2 actual = Vector2.Multiply(factor, a); + Assert.Equal(expected, actual); + } + + // A test for Multiply (Vector2, Vector2) + [Fact] + public void Vector2MultiplyTest3() + { + Vector2 a = new Vector2(1.0f, 2.0f); + Vector2 b = new Vector2(5.0f, 6.0f); + + Vector2 expected = new Vector2(5.0f, 12.0f); + Vector2 actual; + + actual = Vector2.Multiply(a, b); + Assert.Equal(expected, actual); + } + + // A test for Negate (Vector2) + [Fact] + public void Vector2NegateTest() + { + Vector2 a = new Vector2(1.0f, 2.0f); + + Vector2 expected = new Vector2(-1.0f, -2.0f); + Vector2 actual; + + actual = Vector2.Negate(a); + Assert.Equal(expected, actual); + } + + // A test for operator != (Vector2, Vector2) + [Fact] + public void Vector2InequalityTest() + { + Vector2 a = new Vector2(1.0f, 2.0f); + Vector2 b = new Vector2(1.0f, 2.0f); + + // case 1: compare between same values + bool expected = false; + bool actual = a != b; + Assert.Equal(expected, actual); + + // case 2: compare between different values + b = new Vector2(b.X, 10); + expected = true; + actual = a != b; + Assert.Equal(expected, actual); + } + + // A test for operator == (Vector2, Vector2) + [Fact] + public void Vector2EqualityTest() + { + Vector2 a = new Vector2(1.0f, 2.0f); + Vector2 b = new Vector2(1.0f, 2.0f); + + // case 1: compare between same values + bool expected = true; + bool actual = a == b; + Assert.Equal(expected, actual); + + // case 2: compare between different values + b = new Vector2(b.X, 10); + expected = false; + actual = a == b; + Assert.Equal(expected, actual); + } + + // A test for Subtract (Vector2, Vector2) + [Fact] + public void Vector2SubtractTest() + { + Vector2 a = new Vector2(1.0f, 6.0f); + Vector2 b = new Vector2(5.0f, 2.0f); + + Vector2 expected = new Vector2(-4.0f, 4.0f); + Vector2 actual; + + actual = Vector2.Subtract(a, b); + Assert.Equal(expected, actual); + } + + // A test for UnitX + [Fact] + public void Vector2UnitXTest() + { + Vector2 val = new Vector2(1.0f, 0.0f); + Assert.Equal(val, Vector2.UnitX); + } + + // A test for UnitY + [Fact] + public void Vector2UnitYTest() + { + Vector2 val = new Vector2(0.0f, 1.0f); + Assert.Equal(val, Vector2.UnitY); + } + + // A test for One + [Fact] + public void Vector2OneTest() + { + Vector2 val = new Vector2(1.0f, 1.0f); + Assert.Equal(val, Vector2.One); + } + + // A test for Zero + [Fact] + public void Vector2ZeroTest() + { + Vector2 val = new Vector2(0.0f, 0.0f); + Assert.Equal(val, Vector2.Zero); + } + + // A test for Equals (Vector2) + [Fact] + public void Vector2EqualsTest1() + { + Vector2 a = new Vector2(1.0f, 2.0f); + Vector2 b = new Vector2(1.0f, 2.0f); + + // case 1: compare between same values + bool expected = true; + bool actual = a.Equals(b); + Assert.Equal(expected, actual); + + // case 2: compare between different values + b = new Vector2(b.X, 10); + expected = false; + actual = a.Equals(b); + Assert.Equal(expected, actual); + } + + // A test for Vector2 comparison involving NaN values + [Fact] + public void Vector2EqualsNanTest() + { + Vector2 a = new Vector2(Single.NaN, 0); + Vector2 b = new Vector2(0, Single.NaN); + + Assert.False(a == Vector2.Zero); + Assert.False(b == Vector2.Zero); + + Assert.True(a != Vector2.Zero); + Assert.True(b != Vector2.Zero); + + Assert.False(a.Equals(Vector2.Zero)); + Assert.False(b.Equals(Vector2.Zero)); + + // Counterintuitive result - IEEE rules for NaN comparison are weird! + Assert.False(a.Equals(a)); + Assert.False(b.Equals(b)); + } + + // A test for Reflect (Vector2, Vector2) + [Fact] + public void Vector2ReflectTest() + { + Vector2 a = Vector2.Normalize(new Vector2(1.0f, 1.0f)); + + // Reflect on XZ plane. + Vector2 n = new Vector2(0.0f, 1.0f); + Vector2 expected = new Vector2(a.X, -a.Y); + Vector2 actual = Vector2.Reflect(a, n); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Reflect did not return the expected value."); + + // Reflect on XY plane. + n = new Vector2(0.0f, 0.0f); + expected = new Vector2(a.X, a.Y); + actual = Vector2.Reflect(a, n); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Reflect did not return the expected value."); + + // Reflect on YZ plane. + n = new Vector2(1.0f, 0.0f); + expected = new Vector2(-a.X, a.Y); + actual = Vector2.Reflect(a, n); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Reflect did not return the expected value."); + } + + // A test for Reflect (Vector2, Vector2) + // Reflection when normal and source are the same + [Fact] + public void Vector2ReflectTest1() + { + Vector2 n = new Vector2(0.45f, 1.28f); + n = Vector2.Normalize(n); + Vector2 a = n; + + Vector2 expected = -n; + Vector2 actual = Vector2.Reflect(a, n); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Reflect did not return the expected value."); + } + + // A test for Reflect (Vector2, Vector2) + // Reflection when normal and source are negation + [Fact] + public void Vector2ReflectTest2() + { + Vector2 n = new Vector2(0.45f, 1.28f); + n = Vector2.Normalize(n); + Vector2 a = -n; + + Vector2 expected = n; + Vector2 actual = Vector2.Reflect(a, n); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Reflect did not return the expected value."); + } + + [Fact] + public void Vector2AbsTest() + { + Vector2 v1 = new Vector2(-2.5f, 2.0f); + Vector2 v3 = Vector2.Abs(new Vector2(0.0f, Single.NegativeInfinity)); + Vector2 v = Vector2.Abs(v1); + Assert.Equal(2.5f, v.X); + Assert.Equal(2.0f, v.Y); + Assert.Equal(0.0f, v3.X); + Assert.Equal(Single.PositiveInfinity, v3.Y); + } + + [Fact] + public void Vector2SqrtTest() + { + Vector2 v1 = new Vector2(-2.5f, 2.0f); + Vector2 v2 = new Vector2(5.5f, 4.5f); + Assert.Equal(2, (int)Vector2.SquareRoot(v2).X); + Assert.Equal(2, (int)Vector2.SquareRoot(v2).Y); + Assert.Equal(Single.NaN, Vector2.SquareRoot(v1).X); + } + + #pragma warning disable xUnit2000 // 'sizeof(constant) should be argument 'expected'' error + // A test to make sure these types are blittable directly into GPU buffer memory layouts + [Fact] + public unsafe void Vector2SizeofTest() + { + Assert.Equal(sizeof(Single) * 2, sizeof(Vector2)); + Assert.Equal(sizeof(Single) * 2 * 2, sizeof(Vector2_2x)); + Assert.Equal(sizeof(Single) * 2 + sizeof(Single), sizeof(Vector2PlusSingle)); + Assert.Equal((sizeof(Single) * 2 + sizeof(Single)) * 2, sizeof(Vector2PlusSingle_2x)); + } + #pragma warning restore xUnit2000 // 'sizeof(constant) should be argument 'expected'' error + + [StructLayout(LayoutKind.Sequential)] + struct Vector2_2x + { + private Vector2 _a; + private Vector2 _b; + } + + [StructLayout(LayoutKind.Sequential)] + struct Vector2PlusSingle + { + private Vector2 _v; + private Single _f; + } + + [StructLayout(LayoutKind.Sequential)] + struct Vector2PlusSingle_2x + { + private Vector2PlusSingle _a; + private Vector2PlusSingle _b; + } } -} +} \ No newline at end of file diff --git a/src/libraries/System.Numerics.Vectors/tests/Vector2Tests_NonGeneric.cs b/src/libraries/System.Numerics.Vectors/tests/Vector2Tests_NonGeneric.cs new file mode 100644 index 00000000000000..5c7b5e4b06e649 --- /dev/null +++ b/src/libraries/System.Numerics.Vectors/tests/Vector2Tests_NonGeneric.cs @@ -0,0 +1,50 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Globalization; +using System.Runtime.InteropServices; +using Xunit; + +namespace System.Numerics.Tests +{ + public partial class Vector2Tests + { + [Fact] + public void Vector2MarshalSizeTest() + { + Assert.Equal(8, Marshal.SizeOf()); + Assert.Equal(8, Marshal.SizeOf(new Vector2())); + } + + [Fact] + public void SetFieldsTest() + { + Vector2 v3 = new Vector2(4f, 5f); + v3.X = 1.0f; + v3.Y = 2.0f; + Assert.Equal(1.0f, v3.X); + Assert.Equal(2.0f, v3.Y); + Vector2 v4 = v3; + v4.Y = 0.5f; + Assert.Equal(1.0f, v4.X); + Assert.Equal(0.5f, v4.Y); + Assert.Equal(2.0f, v3.Y); + } + + [Fact] + public void EmbeddedVectorSetFields() + { + EmbeddedVectorObject evo = new EmbeddedVectorObject(); + evo.FieldVector.X = 5.0f; + evo.FieldVector.Y = 5.0f; + Assert.Equal(5.0f, evo.FieldVector.X); + Assert.Equal(5.0f, evo.FieldVector.Y); + } + + private class EmbeddedVectorObject + { + public Vector2 FieldVector; + } + } +} diff --git a/src/libraries/System.Numerics.Vectors/tests/Vector2_DoubleTests.cs b/src/libraries/System.Numerics.Vectors/tests/Vector2_DoubleTests.cs new file mode 100644 index 00000000000000..b63c70fc88c86b --- /dev/null +++ b/src/libraries/System.Numerics.Vectors/tests/Vector2_DoubleTests.cs @@ -0,0 +1,1210 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Globalization; +using System.Runtime.InteropServices; +using Xunit; + +namespace System.Numerics.Tests +{ + public partial class Vector2DoubleTests + { + [Fact] + public void Vector2DoubleCopyToTest() + { + Vector2 v1 = new Vector2(2.0d, 3.0d); + + var a = new Double[3]; + var b = new Double[2]; + + Assert.Throws(() => v1.CopyTo(a, -1)); + Assert.Throws(() => v1.CopyTo(a, a.Length)); + + v1.CopyTo(a, 1); + v1.CopyTo(b); + Assert.Equal(0.0, a[0]); + Assert.Equal(2.0, a[1]); + Assert.Equal(3.0, a[2]); + Assert.Equal(2.0, b[0]); + Assert.Equal(3.0, b[1]); + } + + [Fact] + public void Vector2DoubleGetHashCodeTest() + { + Vector2 v1 = new Vector2(2.0d, 3.0d); + Vector2 v2 = new Vector2(2.0d, 3.0d); + Vector2 v3 = new Vector2(3.0d, 2.0d); + Assert.Equal(v1.GetHashCode(), v1.GetHashCode()); + Assert.Equal(v1.GetHashCode(), v2.GetHashCode()); + Assert.NotEqual(v1.GetHashCode(), v3.GetHashCode()); + Vector2 v4 = new Vector2(0.0d, 0.0d); + Vector2 v6 = new Vector2(1.0d, 0.0d); + Vector2 v7 = new Vector2(0.0d, 1.0d); + Vector2 v8 = new Vector2(1.0d, 1.0d); + Assert.NotEqual(v4.GetHashCode(), v6.GetHashCode()); + Assert.NotEqual(v4.GetHashCode(), v7.GetHashCode()); + Assert.NotEqual(v4.GetHashCode(), v8.GetHashCode()); + Assert.NotEqual(v7.GetHashCode(), v6.GetHashCode()); + Assert.NotEqual(v8.GetHashCode(), v6.GetHashCode()); + Assert.NotEqual(v8.GetHashCode(), v7.GetHashCode()); + } + + [Fact] + public void Vector2DoubleToStringTest() + { + string separator = CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator; + CultureInfo enUsCultureInfo = new CultureInfo("en-US"); + + Vector2 v1 = new Vector2(2.0d, 3.0d); + + string v1str = v1.ToString(); + string expectedv1 = string.Format(CultureInfo.CurrentCulture + , "<{1:G}{0} {2:G}>" + , new object[] { separator, 2, 3 }); + Assert.Equal(expectedv1, v1str); + + string v1strformatted = v1.ToString("c", CultureInfo.CurrentCulture); + string expectedv1dormatted = string.Format(CultureInfo.CurrentCulture + , "<{1:c}{0} {2:c}>" + , new object[] { separator, 2, 3 }); + Assert.Equal(expectedv1dormatted, v1strformatted); + + string v2strformatted = v1.ToString("c", enUsCultureInfo); + string expectedv2dormatted = string.Format(enUsCultureInfo + , "<{1:c}{0} {2:c}>" + , new object[] { enUsCultureInfo.NumberFormat.NumberGroupSeparator, 2, 3 }); + Assert.Equal(expectedv2dormatted, v2strformatted); + + string v3strformatted = v1.ToString("c"); + string expectedv3dormatted = string.Format(CultureInfo.CurrentCulture + , "<{1:c}{0} {2:c}>" + , new object[] { separator, 2, 3 }); + Assert.Equal(expectedv3dormatted, v3strformatted); + } + + // A test for Distance (Vector2, Vector2) + [Fact] + public void Vector2DoubleDistanceTest() + { + Vector2 a = new Vector2(1.0d, 2.0d); + Vector2 b = new Vector2(3.0d, 4.0d); + + Double expected = (Double)System.Math.Sqrt(8); + Double actual; + + actual = Vector2.Distance(a, b); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Distance did not return the expected value."); + } + + // A test for Distance (Vector2, Vector2) + // Distance from the same point + [Fact] + public void Vector2DoubleDistanceTest2() + { + Vector2 a = new Vector2(1.051d, 2.05d); + Vector2 b = new Vector2(1.051d, 2.05d); + + Double actual = Vector2.Distance(a, b); + Assert.Equal(0.0d, actual); + } + + // A test for DistanceSquared (Vector2, Vector2) + [Fact] + public void Vector2DoubleDistanceSquaredTest() + { + Vector2 a = new Vector2(1.0d, 2.0d); + Vector2 b = new Vector2(3.0d, 4.0d); + + Double expected = 8.0d; + Double actual; + + actual = Vector2.DistanceSquared(a, b); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.DistanceSquared did not return the expected value."); + } + + // A test for Dot (Vector2, Vector2) + [Fact] + public void Vector2DoubleDotTest() + { + Vector2 a = new Vector2(1.0d, 2.0d); + Vector2 b = new Vector2(3.0d, 4.0d); + + Double expected = 11.0d; + Double actual; + + actual = Vector2.Dot(a, b); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Dot did not return the expected value."); + } + + // A test for Dot (Vector2, Vector2) + // Dot test for perpendicular vector + [Fact] + public void Vector2DoubleDotTest1() + { + Vector2 a = new Vector2(1.55d, 1.55d); + Vector2 b = new Vector2(-1.55d, 1.55d); + + Double expected = 0.0d; + Double actual = Vector2.Dot(a, b); + Assert.Equal(expected, actual); + } + + // A test for Dot (Vector2, Vector2) + // Dot test with special Double values + [Fact] + public void Vector2DoubleDotTest2() + { + Vector2 a = new Vector2(Double.MinValue, Double.MinValue); + Vector2 b = new Vector2(Double.MaxValue, Double.MaxValue); + + Double actual = Vector2.Dot(a, b); + Assert.True(Double.IsNegativeInfinity(actual), "Vector2.Dot did not return the expected value."); + } + + // A test for Length () + [Fact] + public void Vector2DoubleLengthTest() + { + Vector2 a = new Vector2(2.0d, 4.0d); + + Vector2 target = a; + + Double expected = (Double)System.Math.Sqrt(20); + Double actual; + + actual = target.Length(); + + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Length did not return the expected value."); + } + + // A test for Length () + // Length test where length is zero + [Fact] + public void Vector2DoubleLengthTest1() + { + Vector2 target = Vector2.Zero; + + Double expected = 0.0d; + Double actual; + + actual = target.Length(); + + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Length did not return the expected value."); + } + + // A test for LengthSquared () + [Fact] + public void Vector2DoubleLengthSquaredTest() + { + Vector2 a = new Vector2(2.0d, 4.0d); + + Vector2 target = a; + + Double expected = 20.0d; + Double actual; + + actual = target.LengthSquared(); + + Assert.True(MathHelper.Equal(expected, actual), "Vector2.LengthSquared did not return the expected value."); + } + + // A test for LengthSquared () + // LengthSquared test where the result is zero + [Fact] + public void Vector2DoubleLengthSquaredTest1() + { + Vector2 a = new Vector2(0.0d, 0.0d); + + Double expected = 0.0d; + Double actual = a.LengthSquared(); + + Assert.Equal(expected, actual); + } + + // A test for Min (Vector2, Vector2) + [Fact] + public void Vector2DoubleMinTest() + { + Vector2 a = new Vector2(-1.0d, 4.0d); + Vector2 b = new Vector2(2.0d, 1.0d); + + Vector2 expected = new Vector2(-1.0d, 1.0d); + Vector2 actual; + actual = Vector2.Min(a, b); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Min did not return the expected value."); + } + + [Fact] + public void Vector2DoubleMinMaxCodeCoverageTest() + { + Vector2 min = new Vector2(0, 0); + Vector2 max = new Vector2(1, 1); + Vector2 actual; + + // Min. + actual = Vector2.Min(min, max); + Assert.Equal(actual, min); + + actual = Vector2.Min(max, min); + Assert.Equal(actual, min); + + // Max. + actual = Vector2.Max(min, max); + Assert.Equal(actual, max); + + actual = Vector2.Max(max, min); + Assert.Equal(actual, max); + } + + // A test for Max (Vector2, Vector2) + [Fact] + public void Vector2DoubleMaxTest() + { + Vector2 a = new Vector2(-1.0d, 4.0d); + Vector2 b = new Vector2(2.0d, 1.0d); + + Vector2 expected = new Vector2(2.0d, 4.0d); + Vector2 actual; + actual = Vector2.Max(a, b); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Max did not return the expected value."); + } + + // A test for Clamp (Vector2, Vector2, Vector2) + [Fact] + public void Vector2DoubleClampTest() + { + Vector2 a = new Vector2(0.5d, 0.3d); + Vector2 min = new Vector2(0.0d, 0.1d); + Vector2 max = new Vector2(1.0d, 1.1d); + + // Normal case. + // Case N1: specified value is in the range. + Vector2 expected = new Vector2(0.5d, 0.3d); + Vector2 actual = Vector2.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Clamp did not return the expected value."); + // Normal case. + // Case N2: specified value is bigger than max value. + a = new Vector2(2.0d, 3.0d); + expected = max; + actual = Vector2.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Clamp did not return the expected value."); + // Case N3: specified value is smaller than max value. + a = new Vector2(-1.0d, -2.0d); + expected = min; + actual = Vector2.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Clamp did not return the expected value."); + // Case N4: combination case. + a = new Vector2(-2.0d, 4.0d); + expected = new Vector2(min.X, max.Y); + actual = Vector2.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Clamp did not return the expected value."); + // User specified min value is bigger than max value. + max = new Vector2(0.0d, 0.1d); + min = new Vector2(1.0d, 1.1d); + + // Case W1: specified value is in the range. + a = new Vector2(0.5d, 0.3d); + expected = max; + actual = Vector2.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Clamp did not return the expected value."); + + // Normal case. + // Case W2: specified value is bigger than max and min value. + a = new Vector2(2.0d, 3.0d); + expected = max; + actual = Vector2.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Clamp did not return the expected value."); + + // Case W3: specified value is smaller than min and max value. + a = new Vector2(-1.0d, -2.0d); + expected = max; + actual = Vector2.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Clamp did not return the expected value."); + } + + // A test for Lerp (Vector2, Vector2, Double) + [Fact] + public void Vector2DoubleLerpTest() + { + Vector2 a = new Vector2(1.0d, 2.0d); + Vector2 b = new Vector2(3.0d, 4.0d); + + Double t = 0.5d; + + Vector2 expected = new Vector2(2.0d, 3.0d); + Vector2 actual; + actual = Vector2.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector2, Vector2, Double) + // Lerp test with factor zero + [Fact] + public void Vector2DoubleLerpTest1() + { + Vector2 a = new Vector2(0.0d, 0.0d); + Vector2 b = new Vector2(3.18d, 4.25d); + + Double t = 0.0d; + Vector2 expected = Vector2.Zero; + Vector2 actual = Vector2.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector2, Vector2, Double) + // Lerp test with factor one + [Fact] + public void Vector2DoubleLerpTest2() + { + Vector2 a = new Vector2(0.0d, 0.0d); + Vector2 b = new Vector2(3.18d, 4.25d); + + Double t = 1.0d; + Vector2 expected = new Vector2(3.18d, 4.25d); + Vector2 actual = Vector2.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector2, Vector2, Double) + // Lerp test with factor > 1 + [Fact] + public void Vector2DoubleLerpTest3() + { + Vector2 a = new Vector2(0.0d, 0.0d); + Vector2 b = new Vector2(3.18d, 4.25d); + + Double t = 2.0d; + Vector2 expected = b * 2.0d; + Vector2 actual = Vector2.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector2, Vector2, Double) + // Lerp test with factor < 0 + [Fact] + public void Vector2DoubleLerpTest4() + { + Vector2 a = new Vector2(0.0d, 0.0d); + Vector2 b = new Vector2(3.18d, 4.25d); + + Double t = -2.0d; + Vector2 expected = -(b * 2.0d); + Vector2 actual = Vector2.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector2, Vector2, Double) + // Lerp test with special Double value + [Fact] + public void Vector2DoubleLerpTest5() + { + Vector2 a = new Vector2(45.67d, 90.0d); + Vector2 b = new Vector2(Double.PositiveInfinity, Double.NegativeInfinity); + + Double t = 0.408d; + Vector2 actual = Vector2.Lerp(a, b, t); + Assert.True(Double.IsPositiveInfinity(actual.X), "Vector2.Lerp did not return the expected value."); + Assert.True(Double.IsNegativeInfinity(actual.Y), "Vector2.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector2, Vector2, Double) + // Lerp test from the same point + [Fact] + public void Vector2DoubleLerpTest6() + { + Vector2 a = new Vector2(1.0d, 2.0d); + Vector2 b = new Vector2(1.0d, 2.0d); + + Double t = 0.5d; + + Vector2 expected = new Vector2(1.0d, 2.0d); + Vector2 actual = Vector2.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector2, Vector2, Double) + // Lerp test with values known to be innacurate with the old lerp impl + [Fact] + public void Vector2DoubleLerpTest7() + { + Vector2 a = new Vector2(0.44728136d); + Vector2 b = new Vector2(0.46345946d); + + Double t = 0.26402435d; + + Vector2 expected = new Vector2(0.45155275d); + Vector2 actual = Vector2.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector2, Vector2, Double) + // Lerp test with values known to be innacurate with the old lerp impl + // (Old code incorrectly gets 0.33333588) + [Fact] + public void Vector2DoubleLerpTest8() + { + Vector2 a = new Vector2(-100); + Vector2 b = new Vector2(0.33333334d); + + Double t = 1d; + + Vector2 expected = new Vector2(0.33333334d); + Vector2 actual = Vector2.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Lerp did not return the expected value."); + } + + // // A test for Transform(Vector2, Matrix4x4) + // [Fact] + // public void Vector2DoubleTransformTest() + // { + // Vector2 v = new Vector2(1.0d, 2.0d); + // Matrix4x4 m = + // Matrix4x4.CreateRotationX(MathHelper.ToRadians(30.0d)) * + // Matrix4x4.CreateRotationY(MathHelper.ToRadians(30.0d)) * + // Matrix4x4.CreateRotationZ(MathHelper.ToRadians(30.0d)); + // m.M41 = 10.0d; + // m.M42 = 20.0d; + // m.M43 = 30.0d; + + // Vector2 expected = new Vector2(10.316987d, 22.183012d); + // Vector2 actual; + + // actual = Vector2.Transform(v, m); + // Assert.True(MathHelper.Equal(expected, actual), "Vector2.Transform did not return the expected value."); + // } + + // // A test for Transform(Vector2, Matrix3x2) + // [Fact] + // public void Vector2DoubleTransform3x2Test() + // { + // Vector2 v = new Vector2(1.0d, 2.0d); + // Matrix3x2 m = Matrix3x2.CreateRotation(MathHelper.ToRadians(30.0d)); + // m.M31 = 10.0d; + // m.M32 = 20.0d; + + // Vector2 expected = new Vector2(9.866025d, 22.23205d); + // Vector2 actual; + + // actual = Vector2.Transform(v, m); + // Assert.True(MathHelper.Equal(expected, actual), "Vector2.Transform did not return the expected value."); + // } + + // // A test for TransformNormal (Vector2, Matrix4x4) + // [Fact] + // public void Vector2DoubleTransformNormalTest() + // { + // Vector2 v = new Vector2(1.0d, 2.0d); + // Matrix4x4 m = + // Matrix4x4.CreateRotationX(MathHelper.ToRadians(30.0d)) * + // Matrix4x4.CreateRotationY(MathHelper.ToRadians(30.0d)) * + // Matrix4x4.CreateRotationZ(MathHelper.ToRadians(30.0d)); + // m.M41 = 10.0d; + // m.M42 = 20.0d; + // m.M43 = 30.0d; + + // Vector2 expected = new Vector2(0.3169873d, 2.18301272d); + // Vector2 actual; + + // actual = Vector2.TransformNormal(v, m); + // Assert.True(MathHelper.Equal(expected, actual), "Vector2.Tranform did not return the expected value."); + // } + + // // A test for TransformNormal (Vector2, Matrix3x2) + // [Fact] + // public void Vector2DoubleTransformNormal3x2Test() + // { + // Vector2 v = new Vector2(1.0d, 2.0d); + // Matrix3x2 m = Matrix3x2.CreateRotation(MathHelper.ToRadians(30.0d)); + // m.M31 = 10.0d; + // m.M32 = 20.0d; + + // Vector2 expected = new Vector2(-0.133974612d, 2.232051d); + // Vector2 actual; + + // actual = Vector2.TransformNormal(v, m); + // Assert.True(MathHelper.Equal(expected, actual), "Vector2.Transform did not return the expected value."); + // } + + // // A test for Transform (Vector2, Quaternion) + // [Fact] + // public void Vector2DoubleTransformByQuaternionTest() + // { + // Vector2 v = new Vector2(1.0d, 2.0d); + + // Matrix4x4 m = + // Matrix4x4.CreateRotationX(MathHelper.ToRadians(30.0d)) * + // Matrix4x4.CreateRotationY(MathHelper.ToRadians(30.0d)) * + // Matrix4x4.CreateRotationZ(MathHelper.ToRadians(30.0d)); + // Quaternion q = Quaternion.CreateFromRotationMatrix(m); + + // Vector2 expected = Vector2.Transform(v, m); + // Vector2 actual = Vector2.Transform(v, q); + // Assert.True(MathHelper.Equal(expected, actual), "Vector2.Transform did not return the expected value."); + // } + + // // A test for Transform (Vector2, Quaternion) + // // Transform Vector2 with zero quaternion + // [Fact] + // public void Vector2DoubleTransformByQuaternionTest1() + // { + // Vector2 v = new Vector2(1.0d, 2.0d); + // Quaternion q = new Quaternion(); + // Vector2 expected = v; + + // Vector2 actual = Vector2.Transform(v, q); + // Assert.True(MathHelper.Equal(expected, actual), "Vector2.Transform did not return the expected value."); + // } + + // // A test for Transform (Vector2, Quaternion) + // // Transform Vector2 with identity quaternion + // [Fact] + // public void Vector2DoubleTransformByQuaternionTest2() + // { + // Vector2 v = new Vector2(1.0d, 2.0d); + // Quaternion q = Quaternion.Identity; + // Vector2 expected = v; + + // Vector2 actual = Vector2.Transform(v, q); + // Assert.True(MathHelper.Equal(expected, actual), "Vector2.Transform did not return the expected value."); + // } + + // A test for Normalize (Vector2) + [Fact] + public void Vector2DoubleNormalizeTest() + { + Vector2 a = new Vector2(2.0d, 3.0d); + Vector2 expected = new Vector2(0.554700196225229122018341733457d, 0.8320502943378436830275126001855d); + Vector2 actual; + + actual = Vector2.Normalize(a); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Normalize did not return the expected value."); + } + + // A test for Normalize (Vector2) + // Normalize zero length vector + [Fact] + public void Vector2DoubleNormalizeTest1() + { + Vector2 a = new Vector2(); // no parameter, default to 0.0d + Vector2 actual = Vector2.Normalize(a); + Assert.True(Double.IsNaN(actual.X) && Double.IsNaN(actual.Y), "Vector2.Normalize did not return the expected value."); + } + + // A test for Normalize (Vector2) + // Normalize infinite length vector + [Fact] + public void Vector2DoubleNormalizeTest2() + { + Vector2 a = new Vector2(Double.MaxValue, Double.MaxValue); + Vector2 actual = Vector2.Normalize(a); + Vector2 expected = new Vector2(0, 0); + Assert.Equal(expected, actual); + } + + // A test for operator - (Vector2) + [Fact] + public void Vector2DoubleUnaryNegationTest() + { + Vector2 a = new Vector2(1.0d, 2.0d); + + Vector2 expected = new Vector2(-1.0d, -2.0d); + Vector2 actual; + + actual = -a; + + Assert.True(MathHelper.Equal(expected, actual), "Vector2.operator - did not return the expected value."); + } + + // A test for operator - (Vector2) + // Negate test with special Double value + [Fact] + public void Vector2DoubleUnaryNegationTest1() + { + Vector2 a = new Vector2(Double.PositiveInfinity, Double.NegativeInfinity); + + Vector2 actual = -a; + + Assert.True(Double.IsNegativeInfinity(actual.X), "Vector2.operator - did not return the expected value."); + Assert.True(Double.IsPositiveInfinity(actual.Y), "Vector2.operator - did not return the expected value."); + } + + // A test for operator - (Vector2) + // Negate test with special Double value + [Fact] + public void Vector2DoubleUnaryNegationTest2() + { + Vector2 a = new Vector2(Double.NaN, 0.0d); + Vector2 actual = -a; + + Assert.True(Double.IsNaN(actual.X), "Vector2.operator - did not return the expected value."); + Assert.True(Double.Equals(0.0d, actual.Y), "Vector2.operator - did not return the expected value."); + } + + // A test for operator - (Vector2, Vector2) + [Fact] + public void Vector2DoubleSubtractionTest() + { + Vector2 a = new Vector2(1.0d, 3.0d); + Vector2 b = new Vector2(2.0d, 1.5d); + + Vector2 expected = new Vector2(-1.0d, 1.5d); + Vector2 actual; + + actual = a - b; + + Assert.True(MathHelper.Equal(expected, actual), "Vector2.operator - did not return the expected value."); + } + + // A test for operator * (Vector2, Double) + [Fact] + public void Vector2DoubleMultiplyOperatorTest() + { + Vector2 a = new Vector2(2.0d, 3.0d); + const Double factor = 2.0d; + + Vector2 expected = new Vector2(4.0d, 6.0d); + Vector2 actual; + + actual = a * factor; + Assert.True(MathHelper.Equal(expected, actual), "Vector2.operator * did not return the expected value."); + } + + // A test for operator * (Double, Vector2) + [Fact] + public void Vector2DoubleMultiplyOperatorTest2() + { + Vector2 a = new Vector2(2.0d, 3.0d); + const Double factor = 2.0d; + + Vector2 expected = new Vector2(4.0d, 6.0d); + Vector2 actual; + + actual = factor * a; + Assert.True(MathHelper.Equal(expected, actual), "Vector2.operator * did not return the expected value."); + } + + // A test for operator * (Vector2, Vector2) + [Fact] + public void Vector2DoubleMultiplyOperatorTest3() + { + Vector2 a = new Vector2(2.0d, 3.0d); + Vector2 b = new Vector2(4.0d, 5.0d); + + Vector2 expected = new Vector2(8.0d, 15.0d); + Vector2 actual; + + actual = a * b; + + Assert.True(MathHelper.Equal(expected, actual), "Vector2.operator * did not return the expected value."); + } + + // A test for operator / (Vector2, Double) + [Fact] + public void Vector2DoubleDivisionTest() + { + Vector2 a = new Vector2(2.0d, 3.0d); + + Double div = 2.0d; + + Vector2 expected = new Vector2(1.0d, 1.5d); + Vector2 actual; + + actual = a / div; + + Assert.True(MathHelper.Equal(expected, actual), "Vector2.operator / did not return the expected value."); + } + + // A test for operator / (Vector2, Vector2) + [Fact] + public void Vector2DoubleDivisionTest1() + { + Vector2 a = new Vector2(2.0d, 3.0d); + Vector2 b = new Vector2(4.0d, 5.0d); + + Vector2 expected = new Vector2(2.0d / 4.0d, 3.0d / 5.0d); + Vector2 actual; + + actual = a / b; + + Assert.True(MathHelper.Equal(expected, actual), "Vector2.operator / did not return the expected value."); + } + + // A test for operator / (Vector2, Double) + // Divide by zero + [Fact] + public void Vector2DoubleDivisionTest2() + { + Vector2 a = new Vector2(-2.0d, 3.0d); + + Double div = 0.0d; + + Vector2 actual = a / div; + + Assert.True(Double.IsNegativeInfinity(actual.X), "Vector2.operator / did not return the expected value."); + Assert.True(Double.IsPositiveInfinity(actual.Y), "Vector2.operator / did not return the expected value."); + } + + // A test for operator / (Vector2, Vector2) + // Divide by zero + [Fact] + public void Vector2DoubleDivisionTest3() + { + Vector2 a = new Vector2(0.047d, -3.0d); + Vector2 b = new Vector2(); + + Vector2 actual = a / b; + + Assert.True(Double.IsInfinity(actual.X), "Vector2.operator / did not return the expected value."); + Assert.True(Double.IsInfinity(actual.Y), "Vector2.operator / did not return the expected value."); + } + + // A test for operator + (Vector2, Vector2) + [Fact] + public void Vector2DoubleAdditionTest() + { + Vector2 a = new Vector2(1.0d, 2.0d); + Vector2 b = new Vector2(3.0d, 4.0d); + + Vector2 expected = new Vector2(4.0d, 6.0d); + Vector2 actual; + + actual = a + b; + + Assert.True(MathHelper.Equal(expected, actual), "Vector2.operator + did not return the expected value."); + } + + // A test for Vector2 (Double, Double) + [Fact] + public void Vector2DoubleConstructorTest() + { + Double x = 1.0d; + Double y = 2.0d; + + Vector2 target = new Vector2(x, y); + Assert.True(MathHelper.Equal(target.X, x) && MathHelper.Equal(target.Y, y), "Vector2(x,y) constructor did not return the expected value."); + } + + // A test for Vector2 () + // Constructor with no parameter + [Fact] + public void Vector2DoubleConstructorTest2() + { + Vector2 target = new Vector2(); + Assert.Equal(0.0d, target.X); + Assert.Equal(0.0d, target.Y); + } + + // A test for Vector2 (Double, Double) + // Constructor with special Doubleing values + [Fact] + public void Vector2DoubleConstructorTest3() + { + Vector2 target = new Vector2(Double.NaN, Double.MaxValue); + Assert.Equal(target.X, Double.NaN); + Assert.Equal(target.Y, Double.MaxValue); + } + + // A test for Vector2 (Double) + [Fact] + public void Vector2DoubleConstructorTest4() + { + Double value = 1.0d; + Vector2 target = new Vector2(value); + + Vector2 expected = new Vector2(value, value); + Assert.Equal(expected, target); + + value = 2.0d; + target = new Vector2(value); + expected = new Vector2(value, value); + Assert.Equal(expected, target); + } + + // A test for Add (Vector2, Vector2) + [Fact] + public void Vector2DoubleAddTest() + { + Vector2 a = new Vector2(1.0d, 2.0d); + Vector2 b = new Vector2(5.0d, 6.0d); + + Vector2 expected = new Vector2(6.0d, 8.0d); + Vector2 actual; + + actual = Vector2.Add(a, b); + Assert.Equal(expected, actual); + } + + // A test for Divide (Vector2, Double) + [Fact] + public void Vector2DoubleDivideTest() + { + Vector2 a = new Vector2(1.0d, 2.0d); + Double div = 2.0d; + Vector2 expected = new Vector2(0.5d, 1.0d); + Vector2 actual; + actual = Vector2.Divide(a, div); + Assert.Equal(expected, actual); + } + + // A test for Divide (Vector2, Vector2) + [Fact] + public void Vector2DoubleDivideTest1() + { + Vector2 a = new Vector2(1.0d, 6.0d); + Vector2 b = new Vector2(5.0d, 2.0d); + + Vector2 expected = new Vector2(1.0d / 5.0d, 6.0d / 2.0d); + Vector2 actual; + + actual = Vector2.Divide(a, b); + Assert.Equal(expected, actual); + } + + // A test for Equals (object) + [Fact] + public void Vector2DoubleEqualsTest() + { + Vector2 a = new Vector2(1.0d, 2.0d); + Vector2 b = new Vector2(1.0d, 2.0d); + + // case 1: compare between same values + object obj = b; + + bool expected = true; + bool actual = a.Equals(obj); + Assert.Equal(expected, actual); + + // case 2: compare between different values + b = new Vector2(b.X, 10); + obj = b; + expected = false; + actual = a.Equals(obj); + Assert.Equal(expected, actual); + + // case 3: compare between different types. + obj = new Quaternion(); + expected = false; + actual = a.Equals(obj); + Assert.Equal(expected, actual); + + // case 3: compare against null. + obj = null; + expected = false; + actual = a.Equals(obj); + Assert.Equal(expected, actual); + } + + // A test for Multiply (Vector2, Double) + [Fact] + public void Vector2DoubleMultiplyTest() + { + Vector2 a = new Vector2(1.0d, 2.0d); + const Double factor = 2.0d; + Vector2 expected = new Vector2(2.0d, 4.0d); + Vector2 actual = Vector2.Multiply(a, factor); + Assert.Equal(expected, actual); + } + + // A test for Multiply (Double, Vector2) + [Fact] + public void Vector2DoubleMultiplyTest2() + { + Vector2 a = new Vector2(1.0d, 2.0d); + const Double factor = 2.0d; + Vector2 expected = new Vector2(2.0d, 4.0d); + Vector2 actual = Vector2.Multiply(factor, a); + Assert.Equal(expected, actual); + } + + // A test for Multiply (Vector2, Vector2) + [Fact] + public void Vector2DoubleMultiplyTest3() + { + Vector2 a = new Vector2(1.0d, 2.0d); + Vector2 b = new Vector2(5.0d, 6.0d); + + Vector2 expected = new Vector2(5.0d, 12.0d); + Vector2 actual; + + actual = Vector2.Multiply(a, b); + Assert.Equal(expected, actual); + } + + // A test for Negate (Vector2) + [Fact] + public void Vector2DoubleNegateTest() + { + Vector2 a = new Vector2(1.0d, 2.0d); + + Vector2 expected = new Vector2(-1.0d, -2.0d); + Vector2 actual; + + actual = Vector2.Negate(a); + Assert.Equal(expected, actual); + } + + // A test for operator != (Vector2, Vector2) + [Fact] + public void Vector2DoubleInequalityTest() + { + Vector2 a = new Vector2(1.0d, 2.0d); + Vector2 b = new Vector2(1.0d, 2.0d); + + // case 1: compare between same values + bool expected = false; + bool actual = a != b; + Assert.Equal(expected, actual); + + // case 2: compare between different values + b = new Vector2(b.X, 10); + expected = true; + actual = a != b; + Assert.Equal(expected, actual); + } + + // A test for operator == (Vector2, Vector2) + [Fact] + public void Vector2DoubleEqualityTest() + { + Vector2 a = new Vector2(1.0d, 2.0d); + Vector2 b = new Vector2(1.0d, 2.0d); + + // case 1: compare between same values + bool expected = true; + bool actual = a == b; + Assert.Equal(expected, actual); + + // case 2: compare between different values + b = new Vector2(b.X, 10); + expected = false; + actual = a == b; + Assert.Equal(expected, actual); + } + + // A test for Subtract (Vector2, Vector2) + [Fact] + public void Vector2DoubleSubtractTest() + { + Vector2 a = new Vector2(1.0d, 6.0d); + Vector2 b = new Vector2(5.0d, 2.0d); + + Vector2 expected = new Vector2(-4.0d, 4.0d); + Vector2 actual; + + actual = Vector2.Subtract(a, b); + Assert.Equal(expected, actual); + } + + // A test for UnitX + [Fact] + public void Vector2DoubleUnitXTest() + { + Vector2 val = new Vector2(1.0d, 0.0d); + Assert.Equal(val, Vector2.UnitX); + } + + // A test for UnitY + [Fact] + public void Vector2DoubleUnitYTest() + { + Vector2 val = new Vector2(0.0d, 1.0d); + Assert.Equal(val, Vector2.UnitY); + } + + // A test for One + [Fact] + public void Vector2DoubleOneTest() + { + Vector2 val = new Vector2(1.0d, 1.0d); + Assert.Equal(val, Vector2.One); + } + + // A test for Zero + [Fact] + public void Vector2DoubleZeroTest() + { + Vector2 val = new Vector2(0.0d, 0.0d); + Assert.Equal(val, Vector2.Zero); + } + + // A test for Equals (Vector2) + [Fact] + public void Vector2DoubleEqualsTest1() + { + Vector2 a = new Vector2(1.0d, 2.0d); + Vector2 b = new Vector2(1.0d, 2.0d); + + // case 1: compare between same values + bool expected = true; + bool actual = a.Equals(b); + Assert.Equal(expected, actual); + + // case 2: compare between different values + b = new Vector2(b.X, 10); + expected = false; + actual = a.Equals(b); + Assert.Equal(expected, actual); + } + + // A test for Vector2 comparison involving NaN values + [Fact] + public void Vector2DoubleEqualsNanTest() + { + Vector2 a = new Vector2(Double.NaN, 0); + Vector2 b = new Vector2(0, Double.NaN); + + Assert.False(a == Vector2.Zero); + Assert.False(b == Vector2.Zero); + + Assert.True(a != Vector2.Zero); + Assert.True(b != Vector2.Zero); + + Assert.False(a.Equals(Vector2.Zero)); + Assert.False(b.Equals(Vector2.Zero)); + + // Counterintuitive result - IEEE rules for NaN comparison are weird! + Assert.False(a.Equals(a)); + Assert.False(b.Equals(b)); + } + + // A test for Reflect (Vector2, Vector2) + [Fact] + public void Vector2DoubleReflectTest() + { + Vector2 a = Vector2.Normalize(new Vector2(1.0d, 1.0d)); + + // Reflect on XZ plane. + Vector2 n = new Vector2(0.0d, 1.0d); + Vector2 expected = new Vector2(a.X, -a.Y); + Vector2 actual = Vector2.Reflect(a, n); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Reflect did not return the expected value."); + + // Reflect on XY plane. + n = new Vector2(0.0d, 0.0d); + expected = new Vector2(a.X, a.Y); + actual = Vector2.Reflect(a, n); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Reflect did not return the expected value."); + + // Reflect on YZ plane. + n = new Vector2(1.0d, 0.0d); + expected = new Vector2(-a.X, a.Y); + actual = Vector2.Reflect(a, n); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Reflect did not return the expected value."); + } + + // A test for Reflect (Vector2, Vector2) + // Reflection when normal and source are the same + [Fact] + public void Vector2DoubleReflectTest1() + { + Vector2 n = new Vector2(0.45d, 1.28d); + n = Vector2.Normalize(n); + Vector2 a = n; + + Vector2 expected = -n; + Vector2 actual = Vector2.Reflect(a, n); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Reflect did not return the expected value."); + } + + // A test for Reflect (Vector2, Vector2) + // Reflection when normal and source are negation + [Fact] + public void Vector2DoubleReflectTest2() + { + Vector2 n = new Vector2(0.45d, 1.28d); + n = Vector2.Normalize(n); + Vector2 a = -n; + + Vector2 expected = n; + Vector2 actual = Vector2.Reflect(a, n); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Reflect did not return the expected value."); + } + + [Fact] + public void Vector2DoubleAbsTest() + { + Vector2 v1 = new Vector2(-2.5d, 2.0d); + Vector2 v3 = Vector2.Abs(new Vector2(0.0d, Double.NegativeInfinity)); + Vector2 v = Vector2.Abs(v1); + Assert.Equal(2.5d, v.X); + Assert.Equal(2.0d, v.Y); + Assert.Equal(0.0d, v3.X); + Assert.Equal(Double.PositiveInfinity, v3.Y); + } + + [Fact] + public void Vector2DoubleSqrtTest() + { + Vector2 v1 = new Vector2(-2.5d, 2.0d); + Vector2 v2 = new Vector2(5.5d, 4.5d); + Assert.Equal(2, (int)Vector2.SquareRoot(v2).X); + Assert.Equal(2, (int)Vector2.SquareRoot(v2).Y); + Assert.Equal(Double.NaN, Vector2.SquareRoot(v1).X); + } + + #pragma warning disable xUnit2000 // 'sizeof(constant) should be argument 'expected'' error + // A test to make sure these types are blittable directly into GPU buffer memory layouts + [Fact] + public unsafe void Vector2DoubleSizeofTest() + { + Assert.Equal(sizeof(Double) * 2, sizeof(Vector2)); + Assert.Equal(sizeof(Double) * 2 * 2, sizeof(Vector2_2x)); + Assert.Equal(sizeof(Double) * 2 + sizeof(Double), sizeof(Vector2PlusDouble)); + Assert.Equal((sizeof(Double) * 2 + sizeof(Double)) * 2, sizeof(Vector2PlusDouble_2x)); + } + #pragma warning restore xUnit2000 // 'sizeof(constant) should be argument 'expected'' error + + [StructLayout(LayoutKind.Sequential)] + struct Vector2_2x + { + private Vector2 _a; + private Vector2 _b; + } + + [StructLayout(LayoutKind.Sequential)] + struct Vector2PlusDouble + { + private Vector2 _v; + private Double _f; + } + + [StructLayout(LayoutKind.Sequential)] + struct Vector2PlusDouble_2x + { + private Vector2PlusDouble _a; + private Vector2PlusDouble _b; + } + + [Fact] + public void SetFieldsTest() + { + Vector2 v3 = new Vector2(4f, 5f); + v3 = v3.WithX(1.0d); + v3 = v3.WithY(2.0d); + Assert.Equal(1.0f, v3.X); + Assert.Equal(2.0f, v3.Y); + Vector2 v4 = v3; + v4 = v4.WithY(0.5d); + Assert.Equal(1.0f, v4.X); + Assert.Equal(0.5f, v4.Y); + Assert.Equal(2.0f, v3.Y); + } + + [Fact] + public void EmbeddedVectorSetFields() + { + EmbeddedVectorObject evo = new EmbeddedVectorObject(); + evo.FieldVector = evo.FieldVector.WithX(5.0d); + evo.FieldVector = evo.FieldVector.WithY(5.0d); + Assert.Equal(5.0f, evo.FieldVector.X); + Assert.Equal(5.0f, evo.FieldVector.Y); + } + + private class EmbeddedVectorObject + { + public Vector2 FieldVector; + } + } +} \ No newline at end of file diff --git a/src/libraries/System.Numerics.Vectors/tests/Vector2_SingleTests.cs b/src/libraries/System.Numerics.Vectors/tests/Vector2_SingleTests.cs new file mode 100644 index 00000000000000..e2433212fa55bf --- /dev/null +++ b/src/libraries/System.Numerics.Vectors/tests/Vector2_SingleTests.cs @@ -0,0 +1,1211 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Globalization; +using System.Runtime.InteropServices; +using Xunit; + +namespace System.Numerics.Tests +{ + public partial class Vector2SingleTests + { + [Fact] + public void Vector2SingleCopyToTest() + { + Vector2 v1 = new Vector2(2.0f, 3.0f); + + var a = new Single[3]; + var b = new Single[2]; + + Assert.Throws(() => v1.CopyTo(a, -1)); + Assert.Throws(() => v1.CopyTo(a, a.Length)); + + v1.CopyTo(a, 1); + v1.CopyTo(b); + Assert.Equal(0.0, a[0]); + Assert.Equal(2.0, a[1]); + Assert.Equal(3.0, a[2]); + Assert.Equal(2.0, b[0]); + Assert.Equal(3.0, b[1]); + } + + [Fact] + public void Vector2SingleGetHashCodeTest() + { + Vector2 v1 = new Vector2(2.0f, 3.0f); + Vector2 v2 = new Vector2(2.0f, 3.0f); + Vector2 v3 = new Vector2(3.0f, 2.0f); + Assert.Equal(v1.GetHashCode(), v1.GetHashCode()); + Assert.Equal(v1.GetHashCode(), v2.GetHashCode()); + Assert.NotEqual(v1.GetHashCode(), v3.GetHashCode()); + Vector2 v4 = new Vector2(0.0f, 0.0f); + Vector2 v6 = new Vector2(1.0f, 0.0f); + Vector2 v7 = new Vector2(0.0f, 1.0f); + Vector2 v8 = new Vector2(1.0f, 1.0f); + Assert.NotEqual(v4.GetHashCode(), v6.GetHashCode()); + Assert.NotEqual(v4.GetHashCode(), v7.GetHashCode()); + Assert.NotEqual(v4.GetHashCode(), v8.GetHashCode()); + Assert.NotEqual(v7.GetHashCode(), v6.GetHashCode()); + Assert.NotEqual(v8.GetHashCode(), v6.GetHashCode()); + Assert.NotEqual(v8.GetHashCode(), v7.GetHashCode()); + } + + [Fact] + public void Vector2SingleToStringTest() + { + string separator = CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator; + CultureInfo enUsCultureInfo = new CultureInfo("en-US"); + + Vector2 v1 = new Vector2(2.0f, 3.0f); + + string v1str = v1.ToString(); + string expectedv1 = string.Format(CultureInfo.CurrentCulture + , "<{1:G}{0} {2:G}>" + , new object[] { separator, 2, 3 }); + Assert.Equal(expectedv1, v1str); + + string v1strformatted = v1.ToString("c", CultureInfo.CurrentCulture); + string expectedv1formatted = string.Format(CultureInfo.CurrentCulture + , "<{1:c}{0} {2:c}>" + , new object[] { separator, 2, 3 }); + Assert.Equal(expectedv1formatted, v1strformatted); + + string v2strformatted = v1.ToString("c", enUsCultureInfo); + string expectedv2formatted = string.Format(enUsCultureInfo + , "<{1:c}{0} {2:c}>" + , new object[] { enUsCultureInfo.NumberFormat.NumberGroupSeparator, 2, 3 }); + Assert.Equal(expectedv2formatted, v2strformatted); + + string v3strformatted = v1.ToString("c"); + string expectedv3formatted = string.Format(CultureInfo.CurrentCulture + , "<{1:c}{0} {2:c}>" + , new object[] { separator, 2, 3 }); + Assert.Equal(expectedv3formatted, v3strformatted); + } + + // A test for Distance (Vector2, Vector2) + [Fact] + public void Vector2SingleDistanceTest() + { + Vector2 a = new Vector2(1.0f, 2.0f); + Vector2 b = new Vector2(3.0f, 4.0f); + + Single expected = (Single)System.Math.Sqrt(8); + Single actual; + + actual = Vector2.Distance(a, b); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Distance did not return the expected value."); + } + + // A test for Distance (Vector2, Vector2) + // Distance from the same point + [Fact] + public void Vector2SingleDistanceTest2() + { + Vector2 a = new Vector2(1.051f, 2.05f); + Vector2 b = new Vector2(1.051f, 2.05f); + + Single actual = Vector2.Distance(a, b); + Assert.Equal(0.0f, actual); + } + + // A test for DistanceSquared (Vector2, Vector2) + [Fact] + public void Vector2SingleDistanceSquaredTest() + { + Vector2 a = new Vector2(1.0f, 2.0f); + Vector2 b = new Vector2(3.0f, 4.0f); + + Single expected = 8.0f; + Single actual; + + actual = Vector2.DistanceSquared(a, b); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.DistanceSquared did not return the expected value."); + } + + // A test for Dot (Vector2, Vector2) + [Fact] + public void Vector2SingleDotTest() + { + Vector2 a = new Vector2(1.0f, 2.0f); + Vector2 b = new Vector2(3.0f, 4.0f); + + Single expected = 11.0f; + Single actual; + + actual = Vector2.Dot(a, b); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Dot did not return the expected value."); + } + + // A test for Dot (Vector2, Vector2) + // Dot test for perpendicular vector + [Fact] + public void Vector2SingleDotTest1() + { + Vector2 a = new Vector2(1.55f, 1.55f); + Vector2 b = new Vector2(-1.55f, 1.55f); + + Single expected = 0.0f; + Single actual = Vector2.Dot(a, b); + Assert.Equal(expected, actual); + } + + // A test for Dot (Vector2, Vector2) + // Dot test with special Single values + [Fact] + public void Vector2SingleDotTest2() + { + Vector2 a = new Vector2(Single.MinValue, Single.MinValue); + Vector2 b = new Vector2(Single.MaxValue, Single.MaxValue); + + Single actual = Vector2.Dot(a, b); + Assert.True(Single.IsNegativeInfinity(actual), "Vector2.Dot did not return the expected value."); + } + + // A test for Length () + [Fact] + public void Vector2SingleLengthTest() + { + Vector2 a = new Vector2(2.0f, 4.0f); + + Vector2 target = a; + + Single expected = (Single)System.Math.Sqrt(20); + Single actual; + + actual = target.Length(); + + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Length did not return the expected value."); + } + + // A test for Length () + // Length test where length is zero + [Fact] + public void Vector2SingleLengthTest1() + { + Vector2 target = Vector2.Zero; + + Single expected = 0.0f; + Single actual; + + actual = target.Length(); + + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Length did not return the expected value."); + } + + // A test for LengthSquared () + [Fact] + public void Vector2SingleLengthSquaredTest() + { + Vector2 a = new Vector2(2.0f, 4.0f); + + Vector2 target = a; + + Single expected = 20.0f; + Single actual; + + actual = target.LengthSquared(); + + Assert.True(MathHelper.Equal(expected, actual), "Vector2.LengthSquared did not return the expected value."); + } + + // A test for LengthSquared () + // LengthSquared test where the result is zero + [Fact] + public void Vector2SingleLengthSquaredTest1() + { + Vector2 a = new Vector2(0.0f, 0.0f); + + Single expected = 0.0f; + Single actual = a.LengthSquared(); + + Assert.Equal(expected, actual); + } + + // A test for Min (Vector2, Vector2) + [Fact] + public void Vector2SingleMinTest() + { + Vector2 a = new Vector2(-1.0f, 4.0f); + Vector2 b = new Vector2(2.0f, 1.0f); + + Vector2 expected = new Vector2(-1.0f, 1.0f); + Vector2 actual; + actual = Vector2.Min(a, b); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Min did not return the expected value."); + } + + [Fact] + public void Vector2SingleMinMaxCodeCoverageTest() + { + Vector2 min = new Vector2(0, 0); + Vector2 max = new Vector2(1, 1); + Vector2 actual; + + // Min. + actual = Vector2.Min(min, max); + Assert.Equal(actual, min); + + actual = Vector2.Min(max, min); + Assert.Equal(actual, min); + + // Max. + actual = Vector2.Max(min, max); + Assert.Equal(actual, max); + + actual = Vector2.Max(max, min); + Assert.Equal(actual, max); + } + + // A test for Max (Vector2, Vector2) + [Fact] + public void Vector2SingleMaxTest() + { + Vector2 a = new Vector2(-1.0f, 4.0f); + Vector2 b = new Vector2(2.0f, 1.0f); + + Vector2 expected = new Vector2(2.0f, 4.0f); + Vector2 actual; + actual = Vector2.Max(a, b); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Max did not return the expected value."); + } + + // A test for Clamp (Vector2, Vector2, Vector2) + [Fact] + public void Vector2SingleClampTest() + { + Vector2 a = new Vector2(0.5f, 0.3f); + Vector2 min = new Vector2(0.0f, 0.1f); + Vector2 max = new Vector2(1.0f, 1.1f); + + // Normal case. + // Case N1: specified value is in the range. + Vector2 expected = new Vector2(0.5f, 0.3f); + Vector2 actual = Vector2.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Clamp did not return the expected value."); + // Normal case. + // Case N2: specified value is bigger than max value. + a = new Vector2(2.0f, 3.0f); + expected = max; + actual = Vector2.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Clamp did not return the expected value."); + // Case N3: specified value is smaller than max value. + a = new Vector2(-1.0f, -2.0f); + expected = min; + actual = Vector2.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Clamp did not return the expected value."); + // Case N4: combination case. + a = new Vector2(-2.0f, 4.0f); + expected = new Vector2(min.X, max.Y); + actual = Vector2.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Clamp did not return the expected value."); + // User specified min value is bigger than max value. + max = new Vector2(0.0f, 0.1f); + min = new Vector2(1.0f, 1.1f); + + // Case W1: specified value is in the range. + a = new Vector2(0.5f, 0.3f); + expected = max; + actual = Vector2.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Clamp did not return the expected value."); + + // Normal case. + // Case W2: specified value is bigger than max and min value. + a = new Vector2(2.0f, 3.0f); + expected = max; + actual = Vector2.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Clamp did not return the expected value."); + + // Case W3: specified value is smaller than min and max value. + a = new Vector2(-1.0f, -2.0f); + expected = max; + actual = Vector2.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Clamp did not return the expected value."); + } + + // A test for Lerp (Vector2, Vector2, Single) + [Fact] + public void Vector2SingleLerpTest() + { + Vector2 a = new Vector2(1.0f, 2.0f); + Vector2 b = new Vector2(3.0f, 4.0f); + + Single t = 0.5f; + + Vector2 expected = new Vector2(2.0f, 3.0f); + Vector2 actual; + actual = Vector2.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector2, Vector2, Single) + // Lerp test with factor zero + [Fact] + public void Vector2SingleLerpTest1() + { + Vector2 a = new Vector2(0.0f, 0.0f); + Vector2 b = new Vector2(3.18f, 4.25f); + + Single t = 0.0f; + Vector2 expected = Vector2.Zero; + Vector2 actual = Vector2.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector2, Vector2, Single) + // Lerp test with factor one + [Fact] + public void Vector2SingleLerpTest2() + { + Vector2 a = new Vector2(0.0f, 0.0f); + Vector2 b = new Vector2(3.18f, 4.25f); + + Single t = 1.0f; + Vector2 expected = new Vector2(3.18f, 4.25f); + Vector2 actual = Vector2.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector2, Vector2, Single) + // Lerp test with factor > 1 + [Fact] + public void Vector2SingleLerpTest3() + { + Vector2 a = new Vector2(0.0f, 0.0f); + Vector2 b = new Vector2(3.18f, 4.25f); + + Single t = 2.0f; + Vector2 expected = b * 2.0f; + Vector2 actual = Vector2.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector2, Vector2, Single) + // Lerp test with factor < 0 + [Fact] + public void Vector2SingleLerpTest4() + { + Vector2 a = new Vector2(0.0f, 0.0f); + Vector2 b = new Vector2(3.18f, 4.25f); + + Single t = -2.0f; + Vector2 expected = -(b * 2.0f); + Vector2 actual = Vector2.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector2, Vector2, Single) + // Lerp test with special Single value + [Fact] + public void Vector2SingleLerpTest5() + { + Vector2 a = new Vector2(45.67f, 90.0f); + Vector2 b = new Vector2(Single.PositiveInfinity, Single.NegativeInfinity); + + Single t = 0.408f; + Vector2 actual = Vector2.Lerp(a, b, t); + Assert.True(Single.IsPositiveInfinity(actual.X), "Vector2.Lerp did not return the expected value."); + Assert.True(Single.IsNegativeInfinity(actual.Y), "Vector2.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector2, Vector2, Single) + // Lerp test from the same point + [Fact] + public void Vector2SingleLerpTest6() + { + Vector2 a = new Vector2(1.0f, 2.0f); + Vector2 b = new Vector2(1.0f, 2.0f); + + Single t = 0.5f; + + Vector2 expected = new Vector2(1.0f, 2.0f); + Vector2 actual = Vector2.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector2, Vector2, Single) + // Lerp test with values known to be innacurate with the old lerp impl + [Fact] + public void Vector2SingleLerpTest7() + { + Vector2 a = new Vector2(0.44728136f); + Vector2 b = new Vector2(0.46345946f); + + Single t = 0.26402435f; + + Vector2 expected = new Vector2(0.45155275f); + Vector2 actual = Vector2.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector2, Vector2, Single) + // Lerp test with values known to be innacurate with the old lerp impl + // (Old code incorrectly gets 0.33333588) + [Fact] + public void Vector2SingleLerpTest8() + { + Vector2 a = new Vector2(-100); + Vector2 b = new Vector2(0.33333334f); + + Single t = 1f; + + Vector2 expected = new Vector2(0.33333334f); + Vector2 actual = Vector2.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Lerp did not return the expected value."); + } + + // // A test for Transform(Vector2, Matrix4x4) + // [Fact] + // public void Vector2SingleTransformTest() + // { + // Vector2 v = new Vector2(1.0f, 2.0f); + // Matrix4x4 m = + // Matrix4x4.CreateRotationX(MathHelper.ToRadians(30.0f)) * + // Matrix4x4.CreateRotationY(MathHelper.ToRadians(30.0f)) * + // Matrix4x4.CreateRotationZ(MathHelper.ToRadians(30.0f)); + // m.M41 = 10.0f; + // m.M42 = 20.0f; + // m.M43 = 30.0f; + + // Vector2 expected = new Vector2(10.316987f, 22.183012f); + // Vector2 actual; + + // actual = Vector2.Transform(v, m); + // Assert.True(MathHelper.Equal(expected, actual), "Vector2.Transform did not return the expected value."); + // } + + // // A test for Transform(Vector2, Matrix3x2) + // [Fact] + // public void Vector2SingleTransform3x2Test() + // { + // Vector2 v = new Vector2(1.0f, 2.0f); + // Matrix3x2 m = Matrix3x2.CreateRotation(MathHelper.ToRadians(30.0f)); + // m.M31 = 10.0f; + // m.M32 = 20.0f; + + // Vector2 expected = new Vector2(9.866025f, 22.23205f); + // Vector2 actual; + + // actual = Vector2.Transform(v, m); + // Assert.True(MathHelper.Equal(expected, actual), "Vector2.Transform did not return the expected value."); + // } + + // // A test for TransformNormal (Vector2, Matrix4x4) + // [Fact] + // public void Vector2SingleTransformNormalTest() + // { + // Vector2 v = new Vector2(1.0f, 2.0f); + // Matrix4x4 m = + // Matrix4x4.CreateRotationX(MathHelper.ToRadians(30.0f)) * + // Matrix4x4.CreateRotationY(MathHelper.ToRadians(30.0f)) * + // Matrix4x4.CreateRotationZ(MathHelper.ToRadians(30.0f)); + // m.M41 = 10.0f; + // m.M42 = 20.0f; + // m.M43 = 30.0f; + + // Vector2 expected = new Vector2(0.3169873f, 2.18301272f); + // Vector2 actual; + + // actual = Vector2.TransformNormal(v, m); + // Assert.True(MathHelper.Equal(expected, actual), "Vector2.Tranform did not return the expected value."); + // } + + // // A test for TransformNormal (Vector2, Matrix3x2) + // [Fact] + // public void Vector2SingleTransformNormal3x2Test() + // { + // Vector2 v = new Vector2(1.0f, 2.0f); + // Matrix3x2 m = Matrix3x2.CreateRotation(MathHelper.ToRadians(30.0f)); + // m.M31 = 10.0f; + // m.M32 = 20.0f; + + // Vector2 expected = new Vector2(-0.133974612f, 2.232051f); + // Vector2 actual; + + // actual = Vector2.TransformNormal(v, m); + // Assert.True(MathHelper.Equal(expected, actual), "Vector2.Transform did not return the expected value."); + // } + + // // A test for Transform (Vector2, Quaternion) + // [Fact] + // public void Vector2SingleTransformByQuaternionTest() + // { + // Vector2 v = new Vector2(1.0f, 2.0f); + + // Matrix4x4 m = + // Matrix4x4.CreateRotationX(MathHelper.ToRadians(30.0f)) * + // Matrix4x4.CreateRotationY(MathHelper.ToRadians(30.0f)) * + // Matrix4x4.CreateRotationZ(MathHelper.ToRadians(30.0f)); + // Quaternion q = Quaternion.CreateFromRotationMatrix(m); + + // Vector2 expected = Vector2.Transform(v, m); + // Vector2 actual = Vector2.Transform(v, q); + // Assert.True(MathHelper.Equal(expected, actual), "Vector2.Transform did not return the expected value."); + // } + + // // A test for Transform (Vector2, Quaternion) + // // Transform Vector2 with zero quaternion + // [Fact] + // public void Vector2SingleTransformByQuaternionTest1() + // { + // Vector2 v = new Vector2(1.0f, 2.0f); + // Quaternion q = new Quaternion(); + // Vector2 expected = v; + + // Vector2 actual = Vector2.Transform(v, q); + // Assert.True(MathHelper.Equal(expected, actual), "Vector2.Transform did not return the expected value."); + // } + + // // A test for Transform (Vector2, Quaternion) + // // Transform Vector2 with identity quaternion + // [Fact] + // public void Vector2SingleTransformByQuaternionTest2() + // { + // Vector2 v = new Vector2(1.0f, 2.0f); + // Quaternion q = Quaternion.Identity; + // Vector2 expected = v; + + // Vector2 actual = Vector2.Transform(v, q); + // Assert.True(MathHelper.Equal(expected, actual), "Vector2.Transform did not return the expected value."); + // } + + // A test for Normalize (Vector2) + [Fact] + public void Vector2SingleNormalizeTest() + { + Vector2 a = new Vector2(2.0f, 3.0f); + Vector2 expected = new Vector2(0.554700196225229122018341733457f, 0.8320502943378436830275126001855f); + Vector2 actual; + + actual = Vector2.Normalize(a); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Normalize did not return the expected value."); + } + + // A test for Normalize (Vector2) + // Normalize zero length vector + [Fact] + public void Vector2SingleNormalizeTest1() + { + Vector2 a = new Vector2(); // no parameter, default to 0.0f + Vector2 actual = Vector2.Normalize(a); + Assert.True(Single.IsNaN(actual.X) && Single.IsNaN(actual.Y), "Vector2.Normalize did not return the expected value."); + } + + // A test for Normalize (Vector2) + // Normalize infinite length vector + [Fact] + public void Vector2SingleNormalizeTest2() + { + Vector2 a = new Vector2(Single.MaxValue, Single.MaxValue); + Vector2 actual = Vector2.Normalize(a); + Vector2 expected = new Vector2(0, 0); + Assert.Equal(expected, actual); + } + + // A test for operator - (Vector2) + [Fact] + public void Vector2SingleUnaryNegationTest() + { + Vector2 a = new Vector2(1.0f, 2.0f); + + Vector2 expected = new Vector2(-1.0f, -2.0f); + Vector2 actual; + + actual = -a; + + Assert.True(MathHelper.Equal(expected, actual), "Vector2.operator - did not return the expected value."); + } + + + + // A test for operator - (Vector2) + // Negate test with special Single value + [Fact] + public void Vector2SingleUnaryNegationTest1() + { + Vector2 a = new Vector2(Single.PositiveInfinity, Single.NegativeInfinity); + + Vector2 actual = -a; + + Assert.True(Single.IsNegativeInfinity(actual.X), "Vector2.operator - did not return the expected value."); + Assert.True(Single.IsPositiveInfinity(actual.Y), "Vector2.operator - did not return the expected value."); + } + + // A test for operator - (Vector2) + // Negate test with special Single value + [Fact] + public void Vector2SingleUnaryNegationTest2() + { + Vector2 a = new Vector2(Single.NaN, 0.0f); + Vector2 actual = -a; + + Assert.True(Single.IsNaN(actual.X), "Vector2.operator - did not return the expected value."); + Assert.True(Single.Equals(0.0f, actual.Y), "Vector2.operator - did not return the expected value."); + } + + // A test for operator - (Vector2, Vector2) + [Fact] + public void Vector2SingleSubtractionTest() + { + Vector2 a = new Vector2(1.0f, 3.0f); + Vector2 b = new Vector2(2.0f, 1.5f); + + Vector2 expected = new Vector2(-1.0f, 1.5f); + Vector2 actual; + + actual = a - b; + + Assert.True(MathHelper.Equal(expected, actual), "Vector2.operator - did not return the expected value."); + } + + // A test for operator * (Vector2, Single) + [Fact] + public void Vector2SingleMultiplyOperatorTest() + { + Vector2 a = new Vector2(2.0f, 3.0f); + const Single factor = 2.0f; + + Vector2 expected = new Vector2(4.0f, 6.0f); + Vector2 actual; + + actual = a * factor; + Assert.True(MathHelper.Equal(expected, actual), "Vector2.operator * did not return the expected value."); + } + + // A test for operator * (Single, Vector2) + [Fact] + public void Vector2SingleMultiplyOperatorTest2() + { + Vector2 a = new Vector2(2.0f, 3.0f); + const Single factor = 2.0f; + + Vector2 expected = new Vector2(4.0f, 6.0f); + Vector2 actual; + + actual = factor * a; + Assert.True(MathHelper.Equal(expected, actual), "Vector2.operator * did not return the expected value."); + } + + // A test for operator * (Vector2, Vector2) + [Fact] + public void Vector2SingleMultiplyOperatorTest3() + { + Vector2 a = new Vector2(2.0f, 3.0f); + Vector2 b = new Vector2(4.0f, 5.0f); + + Vector2 expected = new Vector2(8.0f, 15.0f); + Vector2 actual; + + actual = a * b; + + Assert.True(MathHelper.Equal(expected, actual), "Vector2.operator * did not return the expected value."); + } + + // A test for operator / (Vector2, Single) + [Fact] + public void Vector2SingleDivisionTest() + { + Vector2 a = new Vector2(2.0f, 3.0f); + + Single div = 2.0f; + + Vector2 expected = new Vector2(1.0f, 1.5f); + Vector2 actual; + + actual = a / div; + + Assert.True(MathHelper.Equal(expected, actual), "Vector2.operator / did not return the expected value."); + } + + // A test for operator / (Vector2, Vector2) + [Fact] + public void Vector2SingleDivisionTest1() + { + Vector2 a = new Vector2(2.0f, 3.0f); + Vector2 b = new Vector2(4.0f, 5.0f); + + Vector2 expected = new Vector2(2.0f / 4.0f, 3.0f / 5.0f); + Vector2 actual; + + actual = a / b; + + Assert.True(MathHelper.Equal(expected, actual), "Vector2.operator / did not return the expected value."); + } + + // A test for operator / (Vector2, Single) + // Divide by zero + [Fact] + public void Vector2SingleDivisionTest2() + { + Vector2 a = new Vector2(-2.0f, 3.0f); + + Single div = 0.0f; + + Vector2 actual = a / div; + + Assert.True(Single.IsNegativeInfinity(actual.X), "Vector2.operator / did not return the expected value."); + Assert.True(Single.IsPositiveInfinity(actual.Y), "Vector2.operator / did not return the expected value."); + } + + // A test for operator / (Vector2, Vector2) + // Divide by zero + [Fact] + public void Vector2SingleDivisionTest3() + { + Vector2 a = new Vector2(0.047f, -3.0f); + Vector2 b = new Vector2(); + + Vector2 actual = a / b; + + Assert.True(Single.IsInfinity(actual.X), "Vector2.operator / did not return the expected value."); + Assert.True(Single.IsInfinity(actual.Y), "Vector2.operator / did not return the expected value."); + } + + // A test for operator + (Vector2, Vector2) + [Fact] + public void Vector2SingleAdditionTest() + { + Vector2 a = new Vector2(1.0f, 2.0f); + Vector2 b = new Vector2(3.0f, 4.0f); + + Vector2 expected = new Vector2(4.0f, 6.0f); + Vector2 actual; + + actual = a + b; + + Assert.True(MathHelper.Equal(expected, actual), "Vector2.operator + did not return the expected value."); + } + + // A test for Vector2 (Single, Single) + [Fact] + public void Vector2SingleConstructorTest() + { + Single x = 1.0f; + Single y = 2.0f; + + Vector2 target = new Vector2(x, y); + Assert.True(MathHelper.Equal(target.X, x) && MathHelper.Equal(target.Y, y), "Vector2(x,y) constructor did not return the expected value."); + } + + // A test for Vector2 () + // Constructor with no parameter + [Fact] + public void Vector2SingleConstructorTest2() + { + Vector2 target = new Vector2(); + Assert.Equal(0.0f, target.X); + Assert.Equal(0.0f, target.Y); + } + + // A test for Vector2 (Single, Single) + // Constructor with special Singleing values + [Fact] + public void Vector2SingleConstructorTest3() + { + Vector2 target = new Vector2(Single.NaN, Single.MaxValue); + Assert.Equal(target.X, Single.NaN); + Assert.Equal(target.Y, Single.MaxValue); + } + + // A test for Vector2 (Single) + [Fact] + public void Vector2SingleConstructorTest4() + { + Single value = 1.0f; + Vector2 target = new Vector2(value); + + Vector2 expected = new Vector2(value, value); + Assert.Equal(expected, target); + + value = 2.0f; + target = new Vector2(value); + expected = new Vector2(value, value); + Assert.Equal(expected, target); + } + + // A test for Add (Vector2, Vector2) + [Fact] + public void Vector2SingleAddTest() + { + Vector2 a = new Vector2(1.0f, 2.0f); + Vector2 b = new Vector2(5.0f, 6.0f); + + Vector2 expected = new Vector2(6.0f, 8.0f); + Vector2 actual; + + actual = Vector2.Add(a, b); + Assert.Equal(expected, actual); + } + + // A test for Divide (Vector2, Single) + [Fact] + public void Vector2SingleDivideTest() + { + Vector2 a = new Vector2(1.0f, 2.0f); + Single div = 2.0f; + Vector2 expected = new Vector2(0.5f, 1.0f); + Vector2 actual; + actual = Vector2.Divide(a, div); + Assert.Equal(expected, actual); + } + + // A test for Divide (Vector2, Vector2) + [Fact] + public void Vector2SingleDivideTest1() + { + Vector2 a = new Vector2(1.0f, 6.0f); + Vector2 b = new Vector2(5.0f, 2.0f); + + Vector2 expected = new Vector2(1.0f / 5.0f, 6.0f / 2.0f); + Vector2 actual; + + actual = Vector2.Divide(a, b); + Assert.Equal(expected, actual); + } + + // A test for Equals (object) + [Fact] + public void Vector2SingleEqualsTest() + { + Vector2 a = new Vector2(1.0f, 2.0f); + Vector2 b = new Vector2(1.0f, 2.0f); + + // case 1: compare between same values + object obj = b; + + bool expected = true; + bool actual = a.Equals(obj); + Assert.Equal(expected, actual); + + // case 2: compare between different values + b = new Vector2(b.X, 10); + obj = b; + expected = false; + actual = a.Equals(obj); + Assert.Equal(expected, actual); + + // case 3: compare between different types. + obj = new Quaternion(); + expected = false; + actual = a.Equals(obj); + Assert.Equal(expected, actual); + + // case 3: compare against null. + obj = null; + expected = false; + actual = a.Equals(obj); + Assert.Equal(expected, actual); + } + + // A test for Multiply (Vector2, Single) + [Fact] + public void Vector2SingleMultiplyTest() + { + Vector2 a = new Vector2(1.0f, 2.0f); + const Single factor = 2.0f; + Vector2 expected = new Vector2(2.0f, 4.0f); + Vector2 actual = Vector2.Multiply(a, factor); + Assert.Equal(expected, actual); + } + + // A test for Multiply (Single, Vector2) + [Fact] + public void Vector2SingleMultiplyTest2() + { + Vector2 a = new Vector2(1.0f, 2.0f); + const Single factor = 2.0f; + Vector2 expected = new Vector2(2.0f, 4.0f); + Vector2 actual = Vector2.Multiply(factor, a); + Assert.Equal(expected, actual); + } + + // A test for Multiply (Vector2, Vector2) + [Fact] + public void Vector2SingleMultiplyTest3() + { + Vector2 a = new Vector2(1.0f, 2.0f); + Vector2 b = new Vector2(5.0f, 6.0f); + + Vector2 expected = new Vector2(5.0f, 12.0f); + Vector2 actual; + + actual = Vector2.Multiply(a, b); + Assert.Equal(expected, actual); + } + + // A test for Negate (Vector2) + [Fact] + public void Vector2SingleNegateTest() + { + Vector2 a = new Vector2(1.0f, 2.0f); + + Vector2 expected = new Vector2(-1.0f, -2.0f); + Vector2 actual; + + actual = Vector2.Negate(a); + Assert.Equal(expected, actual); + } + + // A test for operator != (Vector2, Vector2) + [Fact] + public void Vector2SingleInequalityTest() + { + Vector2 a = new Vector2(1.0f, 2.0f); + Vector2 b = new Vector2(1.0f, 2.0f); + + // case 1: compare between same values + bool expected = false; + bool actual = a != b; + Assert.Equal(expected, actual); + + // case 2: compare between different values + b = new Vector2(b.X, 10); + expected = true; + actual = a != b; + Assert.Equal(expected, actual); + } + + // A test for operator == (Vector2, Vector2) + [Fact] + public void Vector2SingleEqualityTest() + { + Vector2 a = new Vector2(1.0f, 2.0f); + Vector2 b = new Vector2(1.0f, 2.0f); + + // case 1: compare between same values + bool expected = true; + bool actual = a == b; + Assert.Equal(expected, actual); + + // case 2: compare between different values + b = new Vector2(b.X, 10); + expected = false; + actual = a == b; + Assert.Equal(expected, actual); + } + + // A test for Subtract (Vector2, Vector2) + [Fact] + public void Vector2SingleSubtractTest() + { + Vector2 a = new Vector2(1.0f, 6.0f); + Vector2 b = new Vector2(5.0f, 2.0f); + + Vector2 expected = new Vector2(-4.0f, 4.0f); + Vector2 actual; + + actual = Vector2.Subtract(a, b); + Assert.Equal(expected, actual); + } + + // A test for UnitX + [Fact] + public void Vector2SingleUnitXTest() + { + Vector2 val = new Vector2(1.0f, 0.0f); + Assert.Equal(val, Vector2.UnitX); + } + + // A test for UnitY + [Fact] + public void Vector2SingleUnitYTest() + { + Vector2 val = new Vector2(0.0f, 1.0f); + Assert.Equal(val, Vector2.UnitY); + } + + // A test for One + [Fact] + public void Vector2SingleOneTest() + { + Vector2 val = new Vector2(1.0f, 1.0f); + Assert.Equal(val, Vector2.One); + } + + // A test for Zero + [Fact] + public void Vector2SingleZeroTest() + { + Vector2 val = new Vector2(0.0f, 0.0f); + Assert.Equal(val, Vector2.Zero); + } + + // A test for Equals (Vector2) + [Fact] + public void Vector2SingleEqualsTest1() + { + Vector2 a = new Vector2(1.0f, 2.0f); + Vector2 b = new Vector2(1.0f, 2.0f); + + // case 1: compare between same values + bool expected = true; + bool actual = a.Equals(b); + Assert.Equal(expected, actual); + + // case 2: compare between different values + b = new Vector2(b.X, 10); + expected = false; + actual = a.Equals(b); + Assert.Equal(expected, actual); + } + + // A test for Vector2 comparison involving NaN values + [Fact] + public void Vector2SingleEqualsNanTest() + { + Vector2 a = new Vector2(Single.NaN, 0); + Vector2 b = new Vector2(0, Single.NaN); + + Assert.False(a == Vector2.Zero); + Assert.False(b == Vector2.Zero); + + Assert.True(a != Vector2.Zero); + Assert.True(b != Vector2.Zero); + + Assert.False(a.Equals(Vector2.Zero)); + Assert.False(b.Equals(Vector2.Zero)); + + // Counterintuitive result - IEEE rules for NaN comparison are weird! + Assert.False(a.Equals(a)); + Assert.False(b.Equals(b)); + } + + // A test for Reflect (Vector2, Vector2) + [Fact] + public void Vector2SingleReflectTest() + { + Vector2 a = Vector2.Normalize(new Vector2(1.0f, 1.0f)); + + // Reflect on XZ plane. + Vector2 n = new Vector2(0.0f, 1.0f); + Vector2 expected = new Vector2(a.X, -a.Y); + Vector2 actual = Vector2.Reflect(a, n); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Reflect did not return the expected value."); + + // Reflect on XY plane. + n = new Vector2(0.0f, 0.0f); + expected = new Vector2(a.X, a.Y); + actual = Vector2.Reflect(a, n); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Reflect did not return the expected value."); + + // Reflect on YZ plane. + n = new Vector2(1.0f, 0.0f); + expected = new Vector2(-a.X, a.Y); + actual = Vector2.Reflect(a, n); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Reflect did not return the expected value."); + } + + // A test for Reflect (Vector2, Vector2) + // Reflection when normal and source are the same + [Fact] + public void Vector2SingleReflectTest1() + { + Vector2 n = new Vector2(0.45f, 1.28f); + n = Vector2.Normalize(n); + Vector2 a = n; + + Vector2 expected = -n; + Vector2 actual = Vector2.Reflect(a, n); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Reflect did not return the expected value."); + } + + // A test for Reflect (Vector2, Vector2) + // Reflection when normal and source are negation + [Fact] + public void Vector2SingleReflectTest2() + { + Vector2 n = new Vector2(0.45f, 1.28f); + n = Vector2.Normalize(n); + Vector2 a = -n; + + Vector2 expected = n; + Vector2 actual = Vector2.Reflect(a, n); + Assert.True(MathHelper.Equal(expected, actual), "Vector2.Reflect did not return the expected value."); + } + + [Fact] + public void Vector2SingleAbsTest() + { + Vector2 v1 = new Vector2(-2.5f, 2.0f); + Vector2 v3 = Vector2.Abs(new Vector2(0.0f, Single.NegativeInfinity)); + Vector2 v = Vector2.Abs(v1); + Assert.Equal(2.5f, v.X); + Assert.Equal(2.0f, v.Y); + Assert.Equal(0.0f, v3.X); + Assert.Equal(Single.PositiveInfinity, v3.Y); + } + + [Fact] + public void Vector2SingleSqrtTest() + { + Vector2 v1 = new Vector2(-2.5f, 2.0f); + Vector2 v2 = new Vector2(5.5f, 4.5f); + Assert.Equal(2, (int)Vector2.SquareRoot(v2).X); + Assert.Equal(2, (int)Vector2.SquareRoot(v2).Y); + Assert.Equal(Single.NaN, Vector2.SquareRoot(v1).X); + } + + #pragma warning disable xUnit2000 // 'sizeof(constant) should be argument 'expected'' error + // A test to make sure these types are blittable directly into GPU buffer memory layouts + [Fact] + public unsafe void Vector2SingleSizeofTest() + { + Assert.Equal(sizeof(Single) * 2, sizeof(Vector2)); + Assert.Equal(sizeof(Single) * 2 * 2, sizeof(Vector2_2x)); + Assert.Equal(sizeof(Single) * 2 + sizeof(Single), sizeof(Vector2PlusSingle)); + Assert.Equal((sizeof(Single) * 2 + sizeof(Single)) * 2, sizeof(Vector2PlusSingle_2x)); + } + #pragma warning restore xUnit2000 // 'sizeof(constant) should be argument 'expected'' error + + [StructLayout(LayoutKind.Sequential)] + struct Vector2_2x + { + private Vector2 _a; + private Vector2 _b; + } + + [StructLayout(LayoutKind.Sequential)] + struct Vector2PlusSingle + { + private Vector2 _v; + private Single _f; + } + + [StructLayout(LayoutKind.Sequential)] + struct Vector2PlusSingle_2x + { + private Vector2PlusSingle _a; + private Vector2PlusSingle _b; + } + [Fact] +public void SetFieldsTest() +{ + Vector2 v3 = new Vector2(4f, 5f); + v3 = v3.WithX(1.0f); + v3 = v3.WithY(2.0f); + Assert.Equal(1.0f, v3.X); + Assert.Equal(2.0f, v3.Y); + Vector2 v4 = v3; + v4 = v4.WithY(0.5f); + Assert.Equal(1.0f, v4.X); + Assert.Equal(0.5f, v4.Y); + Assert.Equal(2.0f, v3.Y); +} + +[Fact] +public void EmbeddedVectorSetFields() +{ + EmbeddedVectorObject evo = new EmbeddedVectorObject(); + evo.FieldVector = evo.FieldVector.WithX(5.0f); + evo.FieldVector = evo.FieldVector.WithY(5.0f); + Assert.Equal(5.0f, evo.FieldVector.X); + Assert.Equal(5.0f, evo.FieldVector.Y); +} + +private class EmbeddedVectorObject +{ + public Vector2 FieldVector; +} + } +} \ No newline at end of file diff --git a/src/libraries/System.Numerics.Vectors/tests/Vector3Tests.cs b/src/libraries/System.Numerics.Vectors/tests/Vector3Tests.cs index ec930798c362d3..88233488606bb8 100644 --- a/src/libraries/System.Numerics.Vectors/tests/Vector3Tests.cs +++ b/src/libraries/System.Numerics.Vectors/tests/Vector3Tests.cs @@ -8,27 +8,18 @@ namespace System.Numerics.Tests { - public class Vector3Tests + public partial class Vector3Tests { - [Fact] - public void Vector3MarshalSizeTest() - { - Assert.Equal(12, Marshal.SizeOf()); - Assert.Equal(12, Marshal.SizeOf(new Vector3())); - } - [Fact] public void Vector3CopyToTest() { Vector3 v1 = new Vector3(2.0f, 3.0f, 3.3f); - float[] a = new float[4]; - float[] b = new float[3]; + var a = new Single[4]; + var b = new Single[3]; - Assert.Throws(() => v1.CopyTo(null, 0)); Assert.Throws(() => v1.CopyTo(a, -1)); Assert.Throws(() => v1.CopyTo(a, a.Length)); - AssertExtensions.Throws(null, () => v1.CopyTo(a, a.Length - 2)); v1.CopyTo(a, 1); v1.CopyTo(b); @@ -132,11 +123,11 @@ public void Vector3DistanceTest() Vector3 a = new Vector3(1.0f, 2.0f, 3.0f); Vector3 b = new Vector3(4.0f, 5.0f, 6.0f); - float expected = (float)System.Math.Sqrt(27); - float actual; + Single expected = (Single)System.Math.Sqrt(27); + Single actual; actual = Vector3.Distance(a, b); - Assert.True(MathHelper.Equal(expected, actual), "Vector3f.Distance did not return the expected value."); + Assert.True(MathHelper.EqualScalar(expected, actual), "Vector3f.Distance did not return the expected value."); } // A test for Distance (Vector3f, Vector3f) @@ -149,7 +140,7 @@ public void Vector3DistanceTest1() b.Y = 2.05f; b.Z = 3.478f; - float actual = Vector3.Distance(a, b); + Single actual = Vector3.Distance(a, b); Assert.Equal(0.0f, actual); } @@ -160,11 +151,11 @@ public void Vector3DistanceSquaredTest() Vector3 a = new Vector3(1.0f, 2.0f, 3.0f); Vector3 b = new Vector3(4.0f, 5.0f, 6.0f); - float expected = 27.0f; - float actual; + Single expected = 27.0f; + Single actual; actual = Vector3.DistanceSquared(a, b); - Assert.True(MathHelper.Equal(expected, actual), "Vector3f.DistanceSquared did not return the expected value."); + Assert.True(MathHelper.EqualScalar(expected, actual), "Vector3f.DistanceSquared did not return the expected value."); } // A test for Dot (Vector3f, Vector3f) @@ -174,11 +165,11 @@ public void Vector3DotTest() Vector3 a = new Vector3(1.0f, 2.0f, 3.0f); Vector3 b = new Vector3(4.0f, 5.0f, 6.0f); - float expected = 32.0f; - float actual; + Single expected = 32.0f; + Single actual; actual = Vector3.Dot(a, b); - Assert.True(MathHelper.Equal(expected, actual), "Vector3f.Dot did not return the expected value."); + Assert.True(MathHelper.EqualScalar(expected, actual), "Vector3f.Dot did not return the expected value."); } // A test for Dot (Vector3f, Vector3f) @@ -190,11 +181,11 @@ public void Vector3DotTest1() Vector3 b = new Vector3(2.5f, 3, 1.5f); Vector3 c = Vector3.Cross(a, b); - float expected = 0.0f; - float actual1 = Vector3.Dot(a, c); - float actual2 = Vector3.Dot(b, c); - Assert.True(MathHelper.Equal(expected, actual1), "Vector3f.Dot did not return the expected value."); - Assert.True(MathHelper.Equal(expected, actual2), "Vector3f.Dot did not return the expected value."); + Single expected = 0.0f; + Single actual1 = Vector3.Dot(a, c); + Single actual2 = Vector3.Dot(b, c); + Assert.True(MathHelper.EqualScalar(expected, actual1), "Vector3f.Dot did not return the expected value."); + Assert.True(MathHelper.EqualScalar(expected, actual2), "Vector3f.Dot did not return the expected value."); } // A test for Length () @@ -203,15 +194,15 @@ public void Vector3LengthTest() { Vector2 a = new Vector2(1.0f, 2.0f); - float z = 3.0f; + Single z = 3.0f; Vector3 target = new Vector3(a, z); - float expected = (float)System.Math.Sqrt(14.0f); - float actual; + Single expected = (Single)System.Math.Sqrt(14.0f); + Single actual; actual = target.Length(); - Assert.True(MathHelper.Equal(expected, actual), "Vector3f.Length did not return the expected value."); + Assert.True(MathHelper.EqualScalar(expected, actual), "Vector3f.Length did not return the expected value."); } // A test for Length () @@ -221,9 +212,9 @@ public void Vector3LengthTest1() { Vector3 target = new Vector3(); - float expected = 0.0f; - float actual = target.Length(); - Assert.True(MathHelper.Equal(expected, actual), "Vector3f.Length did not return the expected value."); + Single expected = 0.0f; + Single actual = target.Length(); + Assert.True(MathHelper.EqualScalar(expected, actual), "Vector3f.Length did not return the expected value."); } // A test for LengthSquared () @@ -232,15 +223,15 @@ public void Vector3LengthSquaredTest() { Vector2 a = new Vector2(1.0f, 2.0f); - float z = 3.0f; + Single z = 3.0f; Vector3 target = new Vector3(a, z); - float expected = 14.0f; - float actual; + Single expected = 14.0f; + Single actual; actual = target.LengthSquared(); - Assert.True(MathHelper.Equal(expected, actual), "Vector3f.LengthSquared did not return the expected value."); + Assert.True(MathHelper.EqualScalar(expected, actual), "Vector3f.LengthSquared did not return the expected value."); } // A test for Min (Vector3f, Vector3f) @@ -291,14 +282,14 @@ public void Vector3MinMaxCodeCoverageTest() Assert.Equal(actual, max); } - // A test for Lerp (Vector3f, Vector3f, float) + // A test for Lerp (Vector3f, Vector3f, Single) [Fact] public void Vector3LerpTest() { Vector3 a = new Vector3(1.0f, 2.0f, 3.0f); Vector3 b = new Vector3(4.0f, 5.0f, 6.0f); - float t = 0.5f; + Single t = 0.5f; Vector3 expected = new Vector3(2.5f, 3.5f, 4.5f); Vector3 actual; @@ -307,7 +298,7 @@ public void Vector3LerpTest() Assert.True(MathHelper.Equal(expected, actual), "Vector3f.Lerp did not return the expected value."); } - // A test for Lerp (Vector3f, Vector3f, float) + // A test for Lerp (Vector3f, Vector3f, Single) // Lerp test with factor zero [Fact] public void Vector3LerpTest1() @@ -315,13 +306,13 @@ public void Vector3LerpTest1() Vector3 a = new Vector3(1.0f, 2.0f, 3.0f); Vector3 b = new Vector3(4.0f, 5.0f, 6.0f); - float t = 0.0f; + Single t = 0.0f; Vector3 expected = new Vector3(1.0f, 2.0f, 3.0f); Vector3 actual = Vector3.Lerp(a, b, t); Assert.True(MathHelper.Equal(expected, actual), "Vector3f.Lerp did not return the expected value."); } - // A test for Lerp (Vector3f, Vector3f, float) + // A test for Lerp (Vector3f, Vector3f, Single) // Lerp test with factor one [Fact] public void Vector3LerpTest2() @@ -329,13 +320,13 @@ public void Vector3LerpTest2() Vector3 a = new Vector3(1.0f, 2.0f, 3.0f); Vector3 b = new Vector3(4.0f, 5.0f, 6.0f); - float t = 1.0f; + Single t = 1.0f; Vector3 expected = new Vector3(4.0f, 5.0f, 6.0f); Vector3 actual = Vector3.Lerp(a, b, t); Assert.True(MathHelper.Equal(expected, actual), "Vector3f.Lerp did not return the expected value."); } - // A test for Lerp (Vector3f, Vector3f, float) + // A test for Lerp (Vector3f, Vector3f, Single) // Lerp test with factor > 1 [Fact] public void Vector3LerpTest3() @@ -343,13 +334,13 @@ public void Vector3LerpTest3() Vector3 a = new Vector3(0.0f, 0.0f, 0.0f); Vector3 b = new Vector3(4.0f, 5.0f, 6.0f); - float t = 2.0f; + Single t = 2.0f; Vector3 expected = new Vector3(8.0f, 10.0f, 12.0f); Vector3 actual = Vector3.Lerp(a, b, t); Assert.True(MathHelper.Equal(expected, actual), "Vector3f.Lerp did not return the expected value."); } - // A test for Lerp (Vector3f, Vector3f, float) + // A test for Lerp (Vector3f, Vector3f, Single) // Lerp test with factor < 0 [Fact] public void Vector3LerpTest4() @@ -357,13 +348,13 @@ public void Vector3LerpTest4() Vector3 a = new Vector3(0.0f, 0.0f, 0.0f); Vector3 b = new Vector3(4.0f, 5.0f, 6.0f); - float t = -2.0f; + Single t = -2.0f; Vector3 expected = new Vector3(-8.0f, -10.0f, -12.0f); Vector3 actual = Vector3.Lerp(a, b, t); Assert.True(MathHelper.Equal(expected, actual), "Vector3f.Lerp did not return the expected value."); } - // A test for Lerp (Vector3f, Vector3f, float) + // A test for Lerp (Vector3f, Vector3f, Single) // Lerp test from the same point [Fact] public void Vector3LerpTest5() @@ -371,7 +362,7 @@ public void Vector3LerpTest5() Vector3 a = new Vector3(1.68f, 2.34f, 5.43f); Vector3 b = a; - float t = 0.18f; + Single t = 0.18f; Vector3 expected = new Vector3(1.68f, 2.34f, 5.43f); Vector3 actual = Vector3.Lerp(a, b, t); Assert.True(MathHelper.Equal(expected, actual), "Vector3f.Lerp did not return the expected value."); @@ -622,7 +613,7 @@ public void Vector3NormalizeTest2() Vector3 expected = new Vector3(0.0f, 0.0f, 0.0f); Vector3 actual = Vector3.Normalize(a); - Assert.True(float.IsNaN(actual.X) && float.IsNaN(actual.Y) && float.IsNaN(actual.Z), "Vector3f.Normalize did not return the expected value."); + Assert.True(Single.IsNaN(actual.X) && Single.IsNaN(actual.Y) && Single.IsNaN(actual.Z), "Vector3f.Normalize did not return the expected value."); } // A test for operator - (Vector3f) @@ -642,11 +633,11 @@ public void Vector3UnaryNegationTest() [Fact] public void Vector3UnaryNegationTest1() { - Vector3 a = -new Vector3(float.NaN, float.PositiveInfinity, float.NegativeInfinity); + Vector3 a = -new Vector3(Single.NaN, Single.PositiveInfinity, Single.NegativeInfinity); Vector3 b = -new Vector3(0.0f, 0.0f, 0.0f); - Assert.Equal(float.NaN, a.X); - Assert.Equal(float.NegativeInfinity, a.Y); - Assert.Equal(float.PositiveInfinity, a.Z); + Assert.Equal(Single.NaN, a.X); + Assert.Equal(Single.NegativeInfinity, a.Y); + Assert.Equal(Single.PositiveInfinity, a.Z); Assert.Equal(0.0f, b.X); Assert.Equal(0.0f, b.Y); Assert.Equal(0.0f, b.Z); @@ -668,13 +659,13 @@ public void Vector3SubtractionTest() Assert.True(MathHelper.Equal(expected, actual), "Vector3f.operator - did not return the expected value."); } - // A test for operator * (Vector3f, float) + // A test for operator * (Vector3f, Single) [Fact] public void Vector3MultiplyOperatorTest() { Vector3 a = new Vector3(1.0f, 2.0f, 3.0f); - float factor = 2.0f; + Single factor = 2.0f; Vector3 expected = new Vector3(2.0f, 4.0f, 6.0f); Vector3 actual; @@ -684,13 +675,13 @@ public void Vector3MultiplyOperatorTest() Assert.True(MathHelper.Equal(expected, actual), "Vector3f.operator * did not return the expected value."); } - // A test for operator * (float, Vector3f) + // A test for operator * (Single, Vector3f) [Fact] public void Vector3MultiplyOperatorTest2() { Vector3 a = new Vector3(1.0f, 2.0f, 3.0f); - const float factor = 2.0f; + const Single factor = 2.0f; Vector3 expected = new Vector3(2.0f, 4.0f, 6.0f); Vector3 actual; @@ -716,13 +707,13 @@ public void Vector3MultiplyOperatorTest3() Assert.True(MathHelper.Equal(expected, actual), "Vector3f.operator * did not return the expected value."); } - // A test for operator / (Vector3f, float) + // A test for operator / (Vector3f, Single) [Fact] public void Vector3DivisionTest() { Vector3 a = new Vector3(1.0f, 2.0f, 3.0f); - float div = 2.0f; + Single div = 2.0f; Vector3 expected = new Vector3(0.5f, 1.0f, 1.5f); Vector3 actual; @@ -753,15 +744,15 @@ public void Vector3DivisionTest1() [Fact] public void Vector3DivisionTest2() { - Vector3 a = new Vector3(-2.0f, 3.0f, float.MaxValue); + Vector3 a = new Vector3(-2.0f, 3.0f, Single.MaxValue); - float div = 0.0f; + Single div = 0.0f; Vector3 actual = a / div; - Assert.True(float.IsNegativeInfinity(actual.X), "Vector3f.operator / did not return the expected value."); - Assert.True(float.IsPositiveInfinity(actual.Y), "Vector3f.operator / did not return the expected value."); - Assert.True(float.IsPositiveInfinity(actual.Z), "Vector3f.operator / did not return the expected value."); + Assert.True(Single.IsNegativeInfinity(actual.X), "Vector3f.operator / did not return the expected value."); + Assert.True(Single.IsPositiveInfinity(actual.Y), "Vector3f.operator / did not return the expected value."); + Assert.True(Single.IsPositiveInfinity(actual.Z), "Vector3f.operator / did not return the expected value."); } // A test for operator / (Vector3f, Vector3f) @@ -769,14 +760,14 @@ public void Vector3DivisionTest2() [Fact] public void Vector3DivisionTest3() { - Vector3 a = new Vector3(0.047f, -3.0f, float.NegativeInfinity); + Vector3 a = new Vector3(0.047f, -3.0f, Single.NegativeInfinity); Vector3 b = new Vector3(); Vector3 actual = a / b; - Assert.True(float.IsPositiveInfinity(actual.X), "Vector3f.operator / did not return the expected value."); - Assert.True(float.IsNegativeInfinity(actual.Y), "Vector3f.operator / did not return the expected value."); - Assert.True(float.IsNegativeInfinity(actual.Z), "Vector3f.operator / did not return the expected value."); + Assert.True(Single.IsPositiveInfinity(actual.X), "Vector3f.operator / did not return the expected value."); + Assert.True(Single.IsNegativeInfinity(actual.Y), "Vector3f.operator / did not return the expected value."); + Assert.True(Single.IsNegativeInfinity(actual.Z), "Vector3f.operator / did not return the expected value."); } // A test for operator + (Vector3f, Vector3f) @@ -794,25 +785,25 @@ public void Vector3AdditionTest() Assert.True(MathHelper.Equal(expected, actual), "Vector3f.operator + did not return the expected value."); } - // A test for Vector3f (float, float, float) + // A test for Vector3f (Single, Single, Single) [Fact] public void Vector3ConstructorTest() { - float x = 1.0f; - float y = 2.0f; - float z = 3.0f; + Single x = 1.0f; + Single y = 2.0f; + Single z = 3.0f; Vector3 target = new Vector3(x, y, z); Assert.True(MathHelper.Equal(target.X, x) && MathHelper.Equal(target.Y, y) && MathHelper.Equal(target.Z, z), "Vector3f.constructor (x,y,z) did not return the expected value."); } - // A test for Vector3f (Vector2f, float) + // A test for Vector3f (Vector2f, Single) [Fact] public void Vector3ConstructorTest1() { Vector2 a = new Vector2(1.0f, 2.0f); - float z = 3.0f; + Single z = 3.0f; Vector3 target = new Vector3(a, z); Assert.True(MathHelper.Equal(target.X, a.X) && MathHelper.Equal(target.Y, a.Y) && MathHelper.Equal(target.Z, z), "Vector3f.constructor (Vector2f,z) did not return the expected value."); @@ -830,16 +821,16 @@ public void Vector3ConstructorTest3() Assert.Equal(0.0f, a.Z); } - // A test for Vector2f (float, float) - // Constructor with special floating values + // A test for Vector2f (Single, Single) + // Constructor with special Singleing values [Fact] public void Vector3ConstructorTest4() { - Vector3 target = new Vector3(float.NaN, float.MaxValue, float.PositiveInfinity); + Vector3 target = new Vector3(Single.NaN, Single.MaxValue, Single.PositiveInfinity); - Assert.True(float.IsNaN(target.X), "Vector3f.constructor (Vector3f) did not return the expected value."); - Assert.True(float.Equals(float.MaxValue, target.Y), "Vector3f.constructor (Vector3f) did not return the expected value."); - Assert.True(float.IsPositiveInfinity(target.Z), "Vector3f.constructor (Vector3f) did not return the expected value."); + Assert.True(Single.IsNaN(target.X), "Vector3f.constructor (Vector3f) did not return the expected value."); + Assert.True(Single.Equals(Single.MaxValue, target.Y), "Vector3f.constructor (Vector3f) did not return the expected value."); + Assert.True(Single.IsPositiveInfinity(target.Z), "Vector3f.constructor (Vector3f) did not return the expected value."); } // A test for Add (Vector3f, Vector3f) @@ -856,12 +847,12 @@ public void Vector3AddTest() Assert.Equal(expected, actual); } - // A test for Divide (Vector3f, float) + // A test for Divide (Vector3f, Single) [Fact] public void Vector3DivideTest() { Vector3 a = new Vector3(1.0f, 2.0f, 3.0f); - float div = 2.0f; + Single div = 2.0f; Vector3 expected = new Vector3(0.5f, 1.0f, 1.5f); Vector3 actual; actual = Vector3.Divide(a, div); @@ -897,7 +888,7 @@ public void Vector3EqualsTest() Assert.Equal(expected, actual); // case 2: compare between different values - b.X = 10.0f; + b = new Vector3(b.X, 10); obj = b; expected = false; actual = a.Equals(obj); @@ -916,23 +907,23 @@ public void Vector3EqualsTest() Assert.Equal(expected, actual); } - // A test for Multiply (Vector3f, float) + // A test for Multiply (Vector3f, Single) [Fact] public void Vector3MultiplyTest() { Vector3 a = new Vector3(1.0f, 2.0f, 3.0f); - const float factor = 2.0f; + const Single factor = 2.0f; Vector3 expected = new Vector3(2.0f, 4.0f, 6.0f); Vector3 actual = Vector3.Multiply(a, factor); Assert.Equal(expected, actual); } - // A test for Multiply (float, Vector3f) + // A test for Multiply (Single, Vector3f) [Fact] public static void Vector3MultiplyTest2() { Vector3 a = new Vector3(1.0f, 2.0f, 3.0f); - const float factor = 2.0f; + const Single factor = 2.0f; Vector3 expected = new Vector3(2.0f, 4.0f, 6.0f); Vector3 actual = Vector3.Multiply(factor, a); Assert.Equal(expected, actual); @@ -978,7 +969,7 @@ public void Vector3InequalityTest() Assert.Equal(expected, actual); // case 2: compare between different values - b.X = 10.0f; + b = new Vector3(b.X, 10); expected = true; actual = a != b; Assert.Equal(expected, actual); @@ -997,7 +988,7 @@ public void Vector3EqualityTest() Assert.Equal(expected, actual); // case 2: compare between different values - b.X = 10.0f; + b = new Vector3(b.X, 10); expected = false; actual = a == b; Assert.Equal(expected, actual); @@ -1070,17 +1061,17 @@ public void Vector3EqualsTest1() Assert.Equal(expected, actual); // case 2: compare between different values - b.X = 10.0f; + b = new Vector3(b.X, 10); expected = false; actual = a.Equals(b); Assert.Equal(expected, actual); } - // A test for Vector3f (float) + // A test for Vector3f (Single) [Fact] public void Vector3ConstructorTest5() { - float value = 1.0f; + Single value = 1.0f; Vector3 target = new Vector3(value); Vector3 expected = new Vector3(value, value, value); @@ -1096,9 +1087,9 @@ public void Vector3ConstructorTest5() [Fact] public void Vector3EqualsNanTest() { - Vector3 a = new Vector3(float.NaN, 0, 0); - Vector3 b = new Vector3(0, float.NaN, 0); - Vector3 c = new Vector3(0, 0, float.NaN); + Vector3 a = new Vector3(Single.NaN, 0, 0); + Vector3 b = new Vector3(0, Single.NaN, 0); + Vector3 c = new Vector3(0, 0, Single.NaN); Assert.False(a == Vector3.Zero); Assert.False(b == Vector3.Zero); @@ -1122,14 +1113,14 @@ public void Vector3EqualsNanTest() public void Vector3AbsTest() { Vector3 v1 = new Vector3(-2.5f, 2.0f, 0.5f); - Vector3 v3 = Vector3.Abs(new Vector3(0.0f, float.NegativeInfinity, float.NaN)); + Vector3 v3 = Vector3.Abs(new Vector3(0.0f, Single.NegativeInfinity, Single.NaN)); Vector3 v = Vector3.Abs(v1); Assert.Equal(2.5f, v.X); Assert.Equal(2.0f, v.Y); Assert.Equal(0.5f, v.Z); Assert.Equal(0.0f, v3.X); - Assert.Equal(float.PositiveInfinity, v3.Y); - Assert.Equal(float.NaN, v3.Z); + Assert.Equal(Single.PositiveInfinity, v3.Y); + Assert.Equal(Single.NaN, v3.Z); } [Fact] @@ -1140,7 +1131,7 @@ public void Vector3SqrtTest() Assert.Equal(2, (int)Vector3.SquareRoot(b).X); Assert.Equal(2, (int)Vector3.SquareRoot(b).Y); Assert.Equal(4, (int)Vector3.SquareRoot(b).Z); - Assert.Equal(float.NaN, Vector3.SquareRoot(a).X); + Assert.Equal(Single.NaN, Vector3.SquareRoot(a).X); } // A test to make sure these types are blittable directly into GPU buffer memory layouts @@ -1149,8 +1140,8 @@ public unsafe void Vector3SizeofTest() { Assert.Equal(12, sizeof(Vector3)); Assert.Equal(24, sizeof(Vector3_2x)); - Assert.Equal(16, sizeof(Vector3PlusFloat)); - Assert.Equal(32, sizeof(Vector3PlusFloat_2x)); + Assert.Equal(16, sizeof(Vector3PlusSingle)); + Assert.Equal(32, sizeof(Vector3PlusSingle_2x)); } [StructLayout(LayoutKind.Sequential)] @@ -1161,58 +1152,17 @@ struct Vector3_2x } [StructLayout(LayoutKind.Sequential)] - struct Vector3PlusFloat + struct Vector3PlusSingle { private Vector3 _v; - private float _f; + private Single _f; } [StructLayout(LayoutKind.Sequential)] - struct Vector3PlusFloat_2x - { - private Vector3PlusFloat _a; - private Vector3PlusFloat _b; - } - - [Fact] - public void SetFieldsTest() - { - Vector3 v3 = new Vector3(4f, 5f, 6f); - v3.X = 1.0f; - v3.Y = 2.0f; - v3.Z = 3.0f; - Assert.Equal(1.0f, v3.X); - Assert.Equal(2.0f, v3.Y); - Assert.Equal(3.0f, v3.Z); - Vector3 v4 = v3; - v4.Y = 0.5f; - v4.Z = 2.2f; - Assert.Equal(1.0f, v4.X); - Assert.Equal(0.5f, v4.Y); - Assert.Equal(2.2f, v4.Z); - Assert.Equal(2.0f, v3.Y); - - Vector3 before = new Vector3(1f, 2f, 3f); - Vector3 after = before; - after.X = 500.0f; - Assert.NotEqual(before, after); - } - - [Fact] - public void EmbeddedVectorSetFields() - { - EmbeddedVectorObject evo = new EmbeddedVectorObject(); - evo.FieldVector.X = 5.0f; - evo.FieldVector.Y = 5.0f; - evo.FieldVector.Z = 5.0f; - Assert.Equal(5.0f, evo.FieldVector.X); - Assert.Equal(5.0f, evo.FieldVector.Y); - Assert.Equal(5.0f, evo.FieldVector.Z); - } - - private class EmbeddedVectorObject + struct Vector3PlusSingle_2x { - public Vector3 FieldVector; + private Vector3PlusSingle _a; + private Vector3PlusSingle _b; } } -} +} \ No newline at end of file diff --git a/src/libraries/System.Numerics.Vectors/tests/Vector3Tests_NonGeneric.cs b/src/libraries/System.Numerics.Vectors/tests/Vector3Tests_NonGeneric.cs new file mode 100644 index 00000000000000..a93ae5bd9bd4d9 --- /dev/null +++ b/src/libraries/System.Numerics.Vectors/tests/Vector3Tests_NonGeneric.cs @@ -0,0 +1,61 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Globalization; +using System.Runtime.InteropServices; +using Xunit; + +namespace System.Numerics.Tests +{ + public partial class Vector3Tests + { + [Fact] + public void Vector3MarshalSizeTest() + { + Assert.Equal(12, Marshal.SizeOf()); + Assert.Equal(12, Marshal.SizeOf(new Vector3())); + } + + [Fact] + public void SetFieldsTest() + { + Vector3 v3 = new Vector3(4f, 5f, 6f); + v3.X = 1.0f; + v3.Y = 2.0f; + v3.Z = 3.0f; + Assert.Equal(1.0f, v3.X); + Assert.Equal(2.0f, v3.Y); + Assert.Equal(3.0f, v3.Z); + Vector3 v4 = v3; + v4.Y = 0.5f; + v4.Z = 2.2f; + Assert.Equal(1.0f, v4.X); + Assert.Equal(0.5f, v4.Y); + Assert.Equal(2.2f, v4.Z); + Assert.Equal(2.0f, v3.Y); + + Vector3 before = new Vector3(1f, 2f, 3f); + Vector3 after = before; + after.X = 500.0f; + Assert.NotEqual(before, after); + } + + [Fact] + public void EmbeddedVectorSetFields() + { + EmbeddedVectorObject evo = new EmbeddedVectorObject(); + evo.FieldVector.X = 5.0f; + evo.FieldVector.Y = 5.0f; + evo.FieldVector.Z = 5.0f; + Assert.Equal(5.0f, evo.FieldVector.X); + Assert.Equal(5.0f, evo.FieldVector.Y); + Assert.Equal(5.0f, evo.FieldVector.Z); + } + + private class EmbeddedVectorObject + { + public Vector3 FieldVector; + } + } +} diff --git a/src/libraries/System.Numerics.Vectors/tests/Vector3_DoubleTests.cs b/src/libraries/System.Numerics.Vectors/tests/Vector3_DoubleTests.cs new file mode 100644 index 00000000000000..520430725f5c1d --- /dev/null +++ b/src/libraries/System.Numerics.Vectors/tests/Vector3_DoubleTests.cs @@ -0,0 +1,1209 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Globalization; +using System.Runtime.InteropServices; +using Xunit; + +namespace System.Numerics.Tests +{ + public partial class Vector3DoubleTests + { + [Fact] + public void Vector3DoublesCopyToTest() + { + Vector3 v1 = new Vector3(2.0d, 3.0d, 3.3d); + + var a = new Double[4]; + var b = new Double[3]; + + Assert.Throws(() => v1.CopyTo(a, -1)); + Assert.Throws(() => v1.CopyTo(a, a.Length)); + + v1.CopyTo(a, 1); + v1.CopyTo(b); + Assert.Equal(0.0d, a[0]); + Assert.Equal(2.0d, a[1]); + Assert.Equal(3.0d, a[2]); + Assert.Equal(3.3d, a[3]); + Assert.Equal(2.0d, b[0]); + Assert.Equal(3.0d, b[1]); + Assert.Equal(3.3d, b[2]); + } + + [Fact] + public void Vector3DoublesGetHashCodeTest() + { + Vector3 v1 = new Vector3(2.0d, 3.0d, 3.3d); + Vector3 v2 = new Vector3(2.0d, 3.0d, 3.3d); + Vector3 v3 = new Vector3(2.0d, 3.0d, 3.3d); + Vector3 v5 = new Vector3(3.0d, 2.0d, 3.3d); + Assert.Equal(v1.GetHashCode(), v1.GetHashCode()); + Assert.Equal(v1.GetHashCode(), v2.GetHashCode()); + Assert.NotEqual(v1.GetHashCode(), v5.GetHashCode()); + Assert.Equal(v1.GetHashCode(), v3.GetHashCode()); + Vector3 v4 = new Vector3(0.0d, 0.0d, 0.0d); + Vector3 v6 = new Vector3(1.0d, 0.0d, 0.0d); + Vector3 v7 = new Vector3(0.0d, 1.0d, 0.0d); + Vector3 v8 = new Vector3(1.0d, 1.0d, 1.0d); + Vector3 v9 = new Vector3(1.0d, 1.0d, 0.0d); + Assert.NotEqual(v4.GetHashCode(), v6.GetHashCode()); + Assert.NotEqual(v4.GetHashCode(), v7.GetHashCode()); + Assert.NotEqual(v4.GetHashCode(), v8.GetHashCode()); + Assert.NotEqual(v7.GetHashCode(), v6.GetHashCode()); + Assert.NotEqual(v8.GetHashCode(), v6.GetHashCode()); + Assert.NotEqual(v8.GetHashCode(), v9.GetHashCode()); + Assert.NotEqual(v7.GetHashCode(), v9.GetHashCode()); + } + + [Fact] + public void Vector3DoublesToStringTest() + { + string separator = CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator; + CultureInfo enUsCultureInfo = new CultureInfo("en-US"); + + Vector3 v1 = new Vector3(2.0d, 3.0d, 3.3d); + string v1str = v1.ToString(); + string expectedv1 = string.Format(CultureInfo.CurrentCulture + , "<{1:G}{0} {2:G}{0} {3:G}>" + , separator, 2, 3, 3.3); + Assert.Equal(expectedv1, v1str); + + string v1strformatted = v1.ToString("c", CultureInfo.CurrentCulture); + string expectedv1dormatted = string.Format(CultureInfo.CurrentCulture + , "<{1:c}{0} {2:c}{0} {3:c}>" + , separator, 2, 3, 3.3); + Assert.Equal(expectedv1dormatted, v1strformatted); + + string v2strformatted = v1.ToString("c", enUsCultureInfo); + string expectedv2dormatted = string.Format(enUsCultureInfo + , "<{1:c}{0} {2:c}{0} {3:c}>" + , enUsCultureInfo.NumberFormat.NumberGroupSeparator, 2, 3, 3.3); + Assert.Equal(expectedv2dormatted, v2strformatted); + + string v3strformatted = v1.ToString("c"); + string expectedv3dormatted = string.Format(CultureInfo.CurrentCulture + , "<{1:c}{0} {2:c}{0} {3:c}>" + , separator, 2, 3, 3.3); + Assert.Equal(expectedv3dormatted, v3strformatted); + } + + // A test for Cross (Vector3d, Vector3d) + [Fact] + public void Vector3DoublesCrossTest() + { + Vector3 a = new Vector3(1.0d, 0.0d, 0.0d); + Vector3 b = new Vector3(0.0d, 1.0d, 0.0d); + + Vector3 expected = new Vector3(0.0d, 0.0d, 1.0d); + Vector3 actual; + + actual = Vector3.Cross(a, b); + Assert.True(MathHelper.Equal(expected, actual), "Vector3d.Cross did not return the expected value."); + } + + // A test for Cross (Vector3d, Vector3d) + // Cross test of the same vector + [Fact] + public void Vector3DoublesCrossTest1() + { + Vector3 a = new Vector3(0.0d, 1.0d, 0.0d); + Vector3 b = new Vector3(0.0d, 1.0d, 0.0d); + + Vector3 expected = new Vector3(0.0d, 0.0d, 0.0d); + Vector3 actual = Vector3.Cross(a, b); + Assert.True(MathHelper.Equal(expected, actual), "Vector3d.Cross did not return the expected value."); + } + + // A test for Distance (Vector3d, Vector3d) + [Fact] + public void Vector3DoublesDistanceTest() + { + Vector3 a = new Vector3(1.0d, 2.0d, 3.0d); + Vector3 b = new Vector3(4.0d, 5.0d, 6.0d); + + Double expected = (Double)System.Math.Sqrt(27); + Double actual; + + actual = Vector3.Distance(a, b); + Assert.True(MathHelper.EqualScalar(expected, actual), "Vector3d.Distance did not return the expected value."); + } + + // A test for Distance (Vector3d, Vector3d) + // Distance from the same point + [Fact] + public void Vector3DoublesDistanceTest1() + { + Vector3 a = new Vector3(1.051d, 2.05d, 3.478d); + Vector3 b = new Vector3(new Vector2(1.051d, 0.0d), 1); + b.Y = 2.05d; + b.Z = 3.478d; + + Double actual = Vector3.Distance(a, b); + Assert.Equal(0.0d, actual); + } + + // A test for DistanceSquared (Vector3d, Vector3d) + [Fact] + public void Vector3DoublesDistanceSquaredTest() + { + Vector3 a = new Vector3(1.0d, 2.0d, 3.0d); + Vector3 b = new Vector3(4.0d, 5.0d, 6.0d); + + Double expected = 27.0d; + Double actual; + + actual = Vector3.DistanceSquared(a, b); + Assert.True(MathHelper.EqualScalar(expected, actual), "Vector3d.DistanceSquared did not return the expected value."); + } + + // A test for Dot (Vector3d, Vector3d) + [Fact] + public void Vector3DoublesDotTest() + { + Vector3 a = new Vector3(1.0d, 2.0d, 3.0d); + Vector3 b = new Vector3(4.0d, 5.0d, 6.0d); + + Double expected = 32.0d; + Double actual; + + actual = Vector3.Dot(a, b); + Assert.True(MathHelper.EqualScalar(expected, actual), "Vector3d.Dot did not return the expected value."); + } + + // A test for Dot (Vector3d, Vector3d) + // Dot test for perpendicular vector + [Fact] + public void Vector3DoublesDotTest1() + { + Vector3 a = new Vector3(1.55d, 1.55d, 1); + Vector3 b = new Vector3(2.5d, 3, 1.5d); + Vector3 c = Vector3.Cross(a, b); + + Double expected = 0.0d; + Double actual1 = Vector3.Dot(a, c); + Double actual2 = Vector3.Dot(b, c); + Assert.True(MathHelper.EqualScalar(expected, actual1), "Vector3d.Dot did not return the expected value."); + Assert.True(MathHelper.EqualScalar(expected, actual2), "Vector3d.Dot did not return the expected value."); + } + + // A test for Length () + [Fact] + public void Vector3DoublesLengthTest() + { + Vector2 a = new Vector2(1.0d, 2.0d); + + Double z = 3.0d; + + Vector3 target = new Vector3(a, z); + + Double expected = (Double)System.Math.Sqrt(14.0d); + Double actual; + + actual = target.Length(); + Assert.True(MathHelper.EqualScalar(expected, actual), "Vector3d.Length did not return the expected value."); + } + + // A test for Length () + // Length test where length is zero + [Fact] + public void Vector3DoublesLengthTest1() + { + Vector3 target = new Vector3(); + + Double expected = 0.0d; + Double actual = target.Length(); + Assert.True(MathHelper.EqualScalar(expected, actual), "Vector3d.Length did not return the expected value."); + } + + // A test for LengthSquared () + [Fact] + public void Vector3DoublesLengthSquaredTest() + { + Vector2 a = new Vector2(1.0d, 2.0d); + + Double z = 3.0d; + + Vector3 target = new Vector3(a, z); + + Double expected = 14.0d; + Double actual; + + actual = target.LengthSquared(); + Assert.True(MathHelper.EqualScalar(expected, actual), "Vector3d.LengthSquared did not return the expected value."); + } + + // A test for Min (Vector3d, Vector3d) + [Fact] + public void Vector3DoublesMinTest() + { + Vector3 a = new Vector3(-1.0d, 4.0d, -3.0d); + Vector3 b = new Vector3(2.0d, 1.0d, -1.0d); + + Vector3 expected = new Vector3(-1.0d, 1.0d, -3.0d); + Vector3 actual; + actual = Vector3.Min(a, b); + Assert.True(MathHelper.Equal(expected, actual), "Vector3d.Min did not return the expected value."); + } + + // A test for Max (Vector3d, Vector3d) + [Fact] + public void Vector3DoublesMaxTest() + { + Vector3 a = new Vector3(-1.0d, 4.0d, -3.0d); + Vector3 b = new Vector3(2.0d, 1.0d, -1.0d); + + Vector3 expected = new Vector3(2.0d, 4.0d, -1.0d); + Vector3 actual; + actual = Vector3.Max(a, b); + Assert.True(MathHelper.Equal(expected, actual), "Vector3.Max did not return the expected value."); + } + + [Fact] + public void Vector3DoublesMinMaxCodeCoverageTest() + { + Vector3 min = Vector3.Zero; + Vector3 max = Vector3.One; + Vector3 actual; + + // Min. + actual = Vector3.Min(min, max); + Assert.Equal(actual, min); + + actual = Vector3.Min(max, min); + Assert.Equal(actual, min); + + // Max. + actual = Vector3.Max(min, max); + Assert.Equal(actual, max); + + actual = Vector3.Max(max, min); + Assert.Equal(actual, max); + } + + // A test for Lerp (Vector3d, Vector3d, Double) + [Fact] + public void Vector3DoublesLerpTest() + { + Vector3 a = new Vector3(1.0d, 2.0d, 3.0d); + Vector3 b = new Vector3(4.0d, 5.0d, 6.0d); + + Double t = 0.5d; + + Vector3 expected = new Vector3(2.5d, 3.5d, 4.5d); + Vector3 actual; + + actual = Vector3.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector3d.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector3d, Vector3d, Double) + // Lerp test with factor zero + [Fact] + public void Vector3DoublesLerpTest1() + { + Vector3 a = new Vector3(1.0d, 2.0d, 3.0d); + Vector3 b = new Vector3(4.0d, 5.0d, 6.0d); + + Double t = 0.0d; + Vector3 expected = new Vector3(1.0d, 2.0d, 3.0d); + Vector3 actual = Vector3.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector3d.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector3d, Vector3d, Double) + // Lerp test with factor one + [Fact] + public void Vector3DoublesLerpTest2() + { + Vector3 a = new Vector3(1.0d, 2.0d, 3.0d); + Vector3 b = new Vector3(4.0d, 5.0d, 6.0d); + + Double t = 1.0d; + Vector3 expected = new Vector3(4.0d, 5.0d, 6.0d); + Vector3 actual = Vector3.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector3d.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector3d, Vector3d, Double) + // Lerp test with factor > 1 + [Fact] + public void Vector3DoublesLerpTest3() + { + Vector3 a = new Vector3(0.0d, 0.0d, 0.0d); + Vector3 b = new Vector3(4.0d, 5.0d, 6.0d); + + Double t = 2.0d; + Vector3 expected = new Vector3(8.0d, 10.0d, 12.0d); + Vector3 actual = Vector3.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector3d.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector3d, Vector3d, Double) + // Lerp test with factor < 0 + [Fact] + public void Vector3DoublesLerpTest4() + { + Vector3 a = new Vector3(0.0d, 0.0d, 0.0d); + Vector3 b = new Vector3(4.0d, 5.0d, 6.0d); + + Double t = -2.0d; + Vector3 expected = new Vector3(-8.0d, -10.0d, -12.0d); + Vector3 actual = Vector3.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector3d.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector3d, Vector3d, Double) + // Lerp test from the same point + [Fact] + public void Vector3DoublesLerpTest5() + { + Vector3 a = new Vector3(1.68d, 2.34d, 5.43d); + Vector3 b = a; + + Double t = 0.18d; + Vector3 expected = new Vector3(1.68d, 2.34d, 5.43d); + Vector3 actual = Vector3.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector3d.Lerp did not return the expected value."); + } + + // A test for Reflect (Vector3d, Vector3d) + [Fact] + public void Vector3DoublesReflectTest() + { + Vector3 a = Vector3.Normalize(new Vector3(1.0d, 1.0d, 1.0d)); + + // Reflect on XZ plane. + Vector3 n = new Vector3(0.0d, 1.0d, 0.0d); + Vector3 expected = new Vector3(a.X, -a.Y, a.Z); + Vector3 actual = Vector3.Reflect(a, n); + Assert.True(MathHelper.Equal(expected, actual), "Vector3d.Reflect did not return the expected value."); + + // Reflect on XY plane. + n = new Vector3(0.0d, 0.0d, 1.0d); + expected = new Vector3(a.X, a.Y, -a.Z); + actual = Vector3.Reflect(a, n); + Assert.True(MathHelper.Equal(expected, actual), "Vector3d.Reflect did not return the expected value."); + + // Reflect on YZ plane. + n = new Vector3(1.0d, 0.0d, 0.0d); + expected = new Vector3(-a.X, a.Y, a.Z); + actual = Vector3.Reflect(a, n); + Assert.True(MathHelper.Equal(expected, actual), "Vector3d.Reflect did not return the expected value."); + } + + // A test for Reflect (Vector3d, Vector3d) + // Reflection when normal and source are the same + [Fact] + public void Vector3DoublesReflectTest1() + { + Vector3 n = new Vector3(0.45d, 1.28d, 0.86d); + n = Vector3.Normalize(n); + Vector3 a = n; + + Vector3 expected = -n; + Vector3 actual = Vector3.Reflect(a, n); + Assert.True(MathHelper.Equal(expected, actual), "Vector3d.Reflect did not return the expected value."); + } + + // A test for Reflect (Vector3d, Vector3d) + // Reflection when normal and source are negation + [Fact] + public void Vector3DoublesReflectTest2() + { + Vector3 n = new Vector3(0.45d, 1.28d, 0.86d); + n = Vector3.Normalize(n); + Vector3 a = -n; + + Vector3 expected = n; + Vector3 actual = Vector3.Reflect(a, n); + Assert.True(MathHelper.Equal(expected, actual), "Vector3d.Reflect did not return the expected value."); + } + + // A test for Reflect (Vector3d, Vector3d) + // Reflection when normal and source are perpendicular (a dot n = 0) + [Fact] + public void Vector3DoublesReflectTest3() + { + Vector3 n = new Vector3(0.45d, 1.28d, 0.86d); + Vector3 void Vector3Doubles = new Vector3(1.28d, 0.45d, 0.01d); + // find a perpendicular vector of n + Vector3 a = Vector3.Cross(void Vector3Doubles, n); + + Vector3 expected = a; + Vector3 actual = Vector3.Reflect(a, n); + Assert.True(MathHelper.Equal(expected, actual), "Vector3d.Reflect did not return the expected value."); + } + + // A test for Transform(Vector3d, Matrix4x4) + [Fact] + public void Vector3DoublesTransformTest() + { + Vector3 v = new Vector3(1.0d, 2.0d, 3.0d); + Matrix4x4 m = + Matrix4x4.CreateRotationX(MathHelper.ToRadians(30.0d)) * + Matrix4x4.CreateRotationY(MathHelper.ToRadians(30.0d)) * + Matrix4x4.CreateRotationZ(MathHelper.ToRadians(30.0d)); + m.M41 = 10.0d; + m.M42 = 20.0d; + m.M43 = 30.0d; + + Vector3 expected = new Vector3(12.191987d, 21.533493d, 32.616024d); + Vector3 actual; + + actual = Vector3.Transform(v, m); + Assert.True(MathHelper.Equal(expected, actual), "Vector3d.Transform did not return the expected value."); + } + + // A test for Clamp (Vector3d, Vector3d, Vector3d) + [Fact] + public void Vector3DoublesClampTest() + { + Vector3 a = new Vector3(0.5d, 0.3d, 0.33d); + Vector3 min = new Vector3(0.0d, 0.1d, 0.13d); + Vector3 max = new Vector3(1.0d, 1.1d, 1.13d); + + // Normal case. + // Case N1: specified value is in the range. + Vector3 expected = new Vector3(0.5d, 0.3d, 0.33d); + Vector3 actual = Vector3.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector3d.Clamp did not return the expected value."); + + // Normal case. + // Case N2: specified value is bigger than max value. + a = new Vector3(2.0d, 3.0d, 4.0d); + expected = max; + actual = Vector3.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector3d.Clamp did not return the expected value."); + + // Case N3: specified value is smaller than max value. + a = new Vector3(-2.0d, -3.0d, -4.0d); + expected = min; + actual = Vector3.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector3d.Clamp did not return the expected value."); + + // Case N4: combination case. + a = new Vector3(-2.0d, 0.5d, 4.0d); + expected = new Vector3(min.X, a.Y, max.Z); + actual = Vector3.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector3d.Clamp did not return the expected value."); + + // User specified min value is bigger than max value. + max = new Vector3(0.0d, 0.1d, 0.13d); + min = new Vector3(1.0d, 1.1d, 1.13d); + + // Case W1: specified value is in the range. + a = new Vector3(0.5d, 0.3d, 0.33d); + expected = max; + actual = Vector3.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector3d.Clamp did not return the expected value."); + + // Normal case. + // Case W2: specified value is bigger than max and min value. + a = new Vector3(2.0d, 3.0d, 4.0d); + expected = max; + actual = Vector3.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector3d.Clamp did not return the expected value."); + + // Case W3: specified value is smaller than min and max value. + a = new Vector3(-2.0d, -3.0d, -4.0d); + expected = max; + actual = Vector3.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector3d.Clamp did not return the expected value."); + } + + // A test for TransformNormal (Vector3d, Matrix4x4) + [Fact] + public void Vector3DoublesTransformNormalTest() + { + Vector3 v = new Vector3(1.0d, 2.0d, 3.0d); + Matrix4x4 m = + Matrix4x4.CreateRotationX(MathHelper.ToRadians(30.0d)) * + Matrix4x4.CreateRotationY(MathHelper.ToRadians(30.0d)) * + Matrix4x4.CreateRotationZ(MathHelper.ToRadians(30.0d)); + m.M41 = 10.0d; + m.M42 = 20.0d; + m.M43 = 30.0d; + + Vector3 expected = new Vector3(2.19198728d, 1.53349364d, 2.61602545d); + Vector3 actual; + + actual = Vector3.TransformNormal(v, m); + Assert.True(MathHelper.Equal(expected, actual), "Vector3d.TransformNormal did not return the expected value."); + } + + // A test for Transform (Vector3d, Quaternion) + [Fact] + public void Vector3DoublesTransformByQuaternionTest() + { + Vector3 v = new Vector3(1.0d, 2.0d, 3.0d); + + Matrix4x4 m = + Matrix4x4.CreateRotationX(MathHelper.ToRadians(30.0d)) * + Matrix4x4.CreateRotationY(MathHelper.ToRadians(30.0d)) * + Matrix4x4.CreateRotationZ(MathHelper.ToRadians(30.0d)); + Quaternion q = Quaternion.CreateFromRotationMatrix(m); + + Vector3 expected = Vector3.Transform(v, m); + Vector3 actual = Vector3.Transform(v, q); + Assert.True(MathHelper.Equal(expected, actual), "Vector3d.Transform did not return the expected value."); + } + + // A test for Transform (Vector3d, Quaternion) + // Transform Vector3 with zero quaternion + [Fact] + public void Vector3DoublesTransformByQuaternionTest1() + { + Vector3 v = new Vector3(1.0d, 2.0d, 3.0d); + Quaternion q = new Quaternion(); + Vector3 expected = v; + + Vector3 actual = Vector3.Transform(v, q); + Assert.True(MathHelper.Equal(expected, actual), "Vector3d.Transform did not return the expected value."); + } + + // A test for Transform (Vector3d, Quaternion) + // Transform Vector3 with identity quaternion + [Fact] + public void Vector3DoublesTransformByQuaternionTest2() + { + Vector3 v = new Vector3(1.0d, 2.0d, 3.0d); + Quaternion q = Quaternion.Identity; + Vector3 expected = v; + + Vector3 actual = Vector3.Transform(v, q); + Assert.True(MathHelper.Equal(expected, actual), "Vector3d.Transform did not return the expected value."); + } + + // A test for Normalize (Vector3d) + [Fact] + public void Vector3DoublesNormalizeTest() + { + Vector3 a = new Vector3(1.0d, 2.0d, 3.0d); + + Vector3 expected = new Vector3( + 0.26726124191242438468455348087975d, + 0.53452248382484876936910696175951d, + 0.80178372573727315405366044263926d); + Vector3 actual; + + actual = Vector3.Normalize(a); + Assert.True(MathHelper.Equal(expected, actual), "Vector3d.Normalize did not return the expected value."); + } + + // A test for Normalize (Vector3d) + // Normalize vector of length one + [Fact] + public void Vector3DoublesNormalizeTest1() + { + Vector3 a = new Vector3(1.0d, 0.0d, 0.0d); + + Vector3 expected = new Vector3(1.0d, 0.0d, 0.0d); + Vector3 actual = Vector3.Normalize(a); + Assert.True(MathHelper.Equal(expected, actual), "Vector3d.Normalize did not return the expected value."); + } + + // A test for Normalize (Vector3d) + // Normalize vector of length zero + [Fact] + public void Vector3DoublesNormalizeTest2() + { + Vector3 a = new Vector3(0.0d, 0.0d, 0.0d); + + Vector3 expected = new Vector3(0.0d, 0.0d, 0.0d); + Vector3 actual = Vector3.Normalize(a); + Assert.True(Double.IsNaN(actual.X) && Double.IsNaN(actual.Y) && Double.IsNaN(actual.Z), "Vector3d.Normalize did not return the expected value."); + } + + // A test for operator - (Vector3d) + [Fact] + public void Vector3DoublesUnaryNegationTest() + { + Vector3 a = new Vector3(1.0d, 2.0d, 3.0d); + + Vector3 expected = new Vector3(-1.0d, -2.0d, -3.0d); + Vector3 actual; + + actual = -a; + + Assert.True(MathHelper.Equal(expected, actual), "Vector3d.operator - did not return the expected value."); + } + + [Fact] + public void Vector3DoublesUnaryNegationTest1() + { + Vector3 a = -new Vector3(Double.NaN, Double.PositiveInfinity, Double.NegativeInfinity); + Vector3 b = -new Vector3(0.0d, 0.0d, 0.0d); + Assert.Equal(Double.NaN, a.X); + Assert.Equal(Double.NegativeInfinity, a.Y); + Assert.Equal(Double.PositiveInfinity, a.Z); + Assert.Equal(0.0d, b.X); + Assert.Equal(0.0d, b.Y); + Assert.Equal(0.0d, b.Z); + } + + // A test for operator - (Vector3d, Vector3d) + [Fact] + public void Vector3DoublesSubtractionTest() + { + Vector3 a = new Vector3(4.0d, 2.0d, 3.0d); + + Vector3 b = new Vector3(1.0d, 5.0d, 7.0d); + + Vector3 expected = new Vector3(3.0d, -3.0d, -4.0d); + Vector3 actual; + + actual = a - b; + + Assert.True(MathHelper.Equal(expected, actual), "Vector3d.operator - did not return the expected value."); + } + + // A test for operator * (Vector3d, Double) + [Fact] + public void Vector3DoublesMultiplyOperatorTest() + { + Vector3 a = new Vector3(1.0d, 2.0d, 3.0d); + + Double factor = 2.0d; + + Vector3 expected = new Vector3(2.0d, 4.0d, 6.0d); + Vector3 actual; + + actual = a * factor; + + Assert.True(MathHelper.Equal(expected, actual), "Vector3d.operator * did not return the expected value."); + } + + // A test for operator * (Double, Vector3d) + [Fact] + public void Vector3DoublesMultiplyOperatorTest2() + { + Vector3 a = new Vector3(1.0d, 2.0d, 3.0d); + + const Double factor = 2.0d; + + Vector3 expected = new Vector3(2.0d, 4.0d, 6.0d); + Vector3 actual; + + actual = factor * a; + + Assert.True(MathHelper.Equal(expected, actual), "Vector3d.operator * did not return the expected value."); + } + + // A test for operator * (Vector3d, Vector3d) + [Fact] + public void Vector3DoublesMultiplyOperatorTest3() + { + Vector3 a = new Vector3(1.0d, 2.0d, 3.0d); + + Vector3 b = new Vector3(4.0d, 5.0d, 6.0d); + + Vector3 expected = new Vector3(4.0d, 10.0d, 18.0d); + Vector3 actual; + + actual = a * b; + + Assert.True(MathHelper.Equal(expected, actual), "Vector3d.operator * did not return the expected value."); + } + + // A test for operator / (Vector3d, Double) + [Fact] + public void Vector3DoublesDivisionTest() + { + Vector3 a = new Vector3(1.0d, 2.0d, 3.0d); + + Double div = 2.0d; + + Vector3 expected = new Vector3(0.5d, 1.0d, 1.5d); + Vector3 actual; + + actual = a / div; + + Assert.True(MathHelper.Equal(expected, actual), "Vector3d.operator / did not return the expected value."); + } + + // A test for operator / (Vector3d, Vector3d) + [Fact] + public void Vector3DoublesDivisionTest1() + { + Vector3 a = new Vector3(4.0d, 2.0d, 3.0d); + + Vector3 b = new Vector3(1.0d, 5.0d, 6.0d); + + Vector3 expected = new Vector3(4.0d, 0.4d, 0.5d); + Vector3 actual; + + actual = a / b; + + Assert.True(MathHelper.Equal(expected, actual), "Vector3d.operator / did not return the expected value."); + } + + // A test for operator / (Vector3d, Vector3d) + // Divide by zero + [Fact] + public void Vector3DoublesDivisionTest2() + { + Vector3 a = new Vector3(-2.0d, 3.0d, Double.MaxValue); + + Double div = 0.0d; + + Vector3 actual = a / div; + + Assert.True(Double.IsNegativeInfinity(actual.X), "Vector3d.operator / did not return the expected value."); + Assert.True(Double.IsPositiveInfinity(actual.Y), "Vector3d.operator / did not return the expected value."); + Assert.True(Double.IsPositiveInfinity(actual.Z), "Vector3d.operator / did not return the expected value."); + } + + // A test for operator / (Vector3d, Vector3d) + // Divide by zero + [Fact] + public void Vector3DoublesDivisionTest3() + { + Vector3 a = new Vector3(0.047d, -3.0d, Double.NegativeInfinity); + Vector3 b = new Vector3(); + + Vector3 actual = a / b; + + Assert.True(Double.IsPositiveInfinity(actual.X), "Vector3d.operator / did not return the expected value."); + Assert.True(Double.IsNegativeInfinity(actual.Y), "Vector3d.operator / did not return the expected value."); + Assert.True(Double.IsNegativeInfinity(actual.Z), "Vector3d.operator / did not return the expected value."); + } + + // A test for operator + (Vector3d, Vector3d) + [Fact] + public void Vector3DoublesAdditionTest() + { + Vector3 a = new Vector3(1.0d, 2.0d, 3.0d); + Vector3 b = new Vector3(4.0d, 5.0d, 6.0d); + + Vector3 expected = new Vector3(5.0d, 7.0d, 9.0d); + Vector3 actual; + + actual = a + b; + + Assert.True(MathHelper.Equal(expected, actual), "Vector3d.operator + did not return the expected value."); + } + + // A test for Vector3d (Double, Double, Double) + [Fact] + public void Vector3DoublesConstructorTest() + { + Double x = 1.0d; + Double y = 2.0d; + Double z = 3.0d; + + Vector3 target = new Vector3(x, y, z); + Assert.True(MathHelper.Equal(target.X, x) && MathHelper.Equal(target.Y, y) && MathHelper.Equal(target.Z, z), "Vector3d.constructor (x,y,z) did not return the expected value."); + } + + // A test for Vector3d (Vector2d, Double) + [Fact] + public void Vector3DoublesConstructorTest1() + { + Vector2 a = new Vector2(1.0d, 2.0d); + + Double z = 3.0d; + + Vector3 target = new Vector3(a, z); + Assert.True(MathHelper.Equal(target.X, a.X) && MathHelper.Equal(target.Y, a.Y) && MathHelper.Equal(target.Z, z), "Vector3d.constructor (Vector2d,z) did not return the expected value."); + } + + // A test for Vector3d () + // Constructor with no parameter + [Fact] + public void Vector3DoublesConstructorTest3() + { + Vector3 a = new Vector3(); + + Assert.Equal(0.0d, a.X); + Assert.Equal(0.0d, a.Y); + Assert.Equal(0.0d, a.Z); + } + + // A test for Vector2d (Double, Double) + // Constructor with special Doubleing values + [Fact] + public void Vector3DoublesConstructorTest4() + { + Vector3 target = new Vector3(Double.NaN, Double.MaxValue, Double.PositiveInfinity); + + Assert.True(Double.IsNaN(target.X), "Vector3d.constructor (Vector3d) did not return the expected value."); + Assert.True(Double.Equals(Double.MaxValue, target.Y), "Vector3d.constructor (Vector3d) did not return the expected value."); + Assert.True(Double.IsPositiveInfinity(target.Z), "Vector3d.constructor (Vector3d) did not return the expected value."); + } + + // A test for Add (Vector3d, Vector3d) + [Fact] + public void Vector3DoublesAddTest() + { + Vector3 a = new Vector3(1.0d, 2.0d, 3.0d); + Vector3 b = new Vector3(5.0d, 6.0d, 7.0d); + + Vector3 expected = new Vector3(6.0d, 8.0d, 10.0d); + Vector3 actual; + + actual = Vector3.Add(a, b); + Assert.Equal(expected, actual); + } + + // A test for Divide (Vector3d, Double) + [Fact] + public void Vector3DoublesDivideTest() + { + Vector3 a = new Vector3(1.0d, 2.0d, 3.0d); + Double div = 2.0d; + Vector3 expected = new Vector3(0.5d, 1.0d, 1.5d); + Vector3 actual; + actual = Vector3.Divide(a, div); + Assert.Equal(expected, actual); + } + + // A test for Divide (Vector3d, Vector3d) + [Fact] + public void Vector3DoublesDivideTest1() + { + Vector3 a = new Vector3(1.0d, 6.0d, 7.0d); + Vector3 b = new Vector3(5.0d, 2.0d, 3.0d); + + Vector3 expected = new Vector3(1.0d / 5.0d, 6.0d / 2.0d, 7.0d / 3.0d); + Vector3 actual; + + actual = Vector3.Divide(a, b); + Assert.Equal(expected, actual); + } + + // A test for Equals (object) + [Fact] + public void Vector3DoublesEqualsTest() + { + Vector3 a = new Vector3(1.0d, 2.0d, 3.0d); + Vector3 b = new Vector3(1.0d, 2.0d, 3.0d); + + // case 1: compare between same values + object obj = b; + + bool expected = true; + bool actual = a.Equals(obj); + Assert.Equal(expected, actual); + + // case 2: compare between different values + b = new Vector3Double(b.X, 10); + obj = b; + expected = false; + actual = a.Equals(obj); + Assert.Equal(expected, actual); + + // case 3: compare between different types. + obj = new Quaternion(); + expected = false; + actual = a.Equals(obj); + Assert.Equal(expected, actual); + + // case 3: compare against null. + obj = null; + expected = false; + actual = a.Equals(obj); + Assert.Equal(expected, actual); + } + + // A test for Multiply (Vector3d, Double) + [Fact] + public void Vector3DoublesMultiplyTest() + { + Vector3 a = new Vector3(1.0d, 2.0d, 3.0d); + const Double factor = 2.0d; + Vector3 expected = new Vector3(2.0d, 4.0d, 6.0d); + Vector3 actual = Vector3.Multiply(a, factor); + Assert.Equal(expected, actual); + } + + // A test for Multiply (Double, Vector3d) + [Fact] + public static void Vector3DoublesMultiplyTest2() + { + Vector3 a = new Vector3(1.0d, 2.0d, 3.0d); + const Double factor = 2.0d; + Vector3 expected = new Vector3(2.0d, 4.0d, 6.0d); + Vector3 actual = Vector3.Multiply(factor, a); + Assert.Equal(expected, actual); + } + + // A test for Multiply (Vector3d, Vector3d) + [Fact] + public void Vector3DoublesMultiplyTest3() + { + Vector3 a = new Vector3(1.0d, 2.0d, 3.0d); + Vector3 b = new Vector3(5.0d, 6.0d, 7.0d); + + Vector3 expected = new Vector3(5.0d, 12.0d, 21.0d); + Vector3 actual; + + actual = Vector3.Multiply(a, b); + Assert.Equal(expected, actual); + } + + // A test for Negate (Vector3d) + [Fact] + public void Vector3DoublesNegateTest() + { + Vector3 a = new Vector3(1.0d, 2.0d, 3.0d); + + Vector3 expected = new Vector3(-1.0d, -2.0d, -3.0d); + Vector3 actual; + + actual = Vector3.Negate(a); + Assert.Equal(expected, actual); + } + + // A test for operator != (Vector3d, Vector3d) + [Fact] + public void Vector3DoublesInequalityTest() + { + Vector3 a = new Vector3(1.0d, 2.0d, 3.0d); + Vector3 b = new Vector3(1.0d, 2.0d, 3.0d); + + // case 1: compare between same values + bool expected = false; + bool actual = a != b; + Assert.Equal(expected, actual); + + // case 2: compare between different values + b = new Vector3Double(b.X, 10); + expected = true; + actual = a != b; + Assert.Equal(expected, actual); + } + + // A test for operator == (Vector3d, Vector3d) + [Fact] + public void Vector3DoublesEqualityTest() + { + Vector3 a = new Vector3(1.0d, 2.0d, 3.0d); + Vector3 b = new Vector3(1.0d, 2.0d, 3.0d); + + // case 1: compare between same values + bool expected = true; + bool actual = a == b; + Assert.Equal(expected, actual); + + // case 2: compare between different values + b = new Vector3Double(b.X, 10); + expected = false; + actual = a == b; + Assert.Equal(expected, actual); + } + + // A test for Subtract (Vector3d, Vector3d) + [Fact] + public void Vector3DoublesSubtractTest() + { + Vector3 a = new Vector3(1.0d, 6.0d, 3.0d); + Vector3 b = new Vector3(5.0d, 2.0d, 3.0d); + + Vector3 expected = new Vector3(-4.0d, 4.0d, 0.0d); + Vector3 actual; + + actual = Vector3.Subtract(a, b); + Assert.Equal(expected, actual); + } + + // A test for One + [Fact] + public void Vector3DoublesOneTest() + { + Vector3 val = new Vector3(1.0d, 1.0d, 1.0d); + Assert.Equal(val, Vector3.One); + } + + // A test for UnitX + [Fact] + public void Vector3DoublesUnitXTest() + { + Vector3 val = new Vector3(1.0d, 0.0d, 0.0d); + Assert.Equal(val, Vector3.UnitX); + } + + // A test for UnitY + [Fact] + public void Vector3DoublesUnitYTest() + { + Vector3 val = new Vector3(0.0d, 1.0d, 0.0d); + Assert.Equal(val, Vector3.UnitY); + } + + // A test for UnitZ + [Fact] + public void Vector3DoublesUnitZTest() + { + Vector3 val = new Vector3(0.0d, 0.0d, 1.0d); + Assert.Equal(val, Vector3.UnitZ); + } + + // A test for Zero + [Fact] + public void Vector3DoublesZeroTest() + { + Vector3 val = new Vector3(0.0d, 0.0d, 0.0d); + Assert.Equal(val, Vector3.Zero); + } + + // A test for Equals (Vector3d) + [Fact] + public void Vector3DoublesEqualsTest1() + { + Vector3 a = new Vector3(1.0d, 2.0d, 3.0d); + Vector3 b = new Vector3(1.0d, 2.0d, 3.0d); + + // case 1: compare between same values + bool expected = true; + bool actual = a.Equals(b); + Assert.Equal(expected, actual); + + // case 2: compare between different values + b = new Vector3Double(b.X, 10); + expected = false; + actual = a.Equals(b); + Assert.Equal(expected, actual); + } + + // A test for Vector3d (Double) + [Fact] + public void Vector3DoublesConstructorTest5() + { + Double value = 1.0d; + Vector3 target = new Vector3(value); + + Vector3 expected = new Vector3(value, value, value); + Assert.Equal(expected, target); + + value = 2.0d; + target = new Vector3(value); + expected = new Vector3(value, value, value); + Assert.Equal(expected, target); + } + + // A test for Vector3d comparison involving NaN values + [Fact] + public void Vector3DoublesEqualsNanTest() + { + Vector3 a = new Vector3(Double.NaN, 0, 0); + Vector3 b = new Vector3(0, Double.NaN, 0); + Vector3 c = new Vector3(0, 0, Double.NaN); + + Assert.False(a == Vector3.Zero); + Assert.False(b == Vector3.Zero); + Assert.False(c == Vector3.Zero); + + Assert.True(a != Vector3.Zero); + Assert.True(b != Vector3.Zero); + Assert.True(c != Vector3.Zero); + + Assert.False(a.Equals(Vector3.Zero)); + Assert.False(b.Equals(Vector3.Zero)); + Assert.False(c.Equals(Vector3.Zero)); + + // Counterintuitive result - IEEE rules for NaN comparison are weird! + Assert.False(a.Equals(a)); + Assert.False(b.Equals(b)); + Assert.False(c.Equals(c)); + } + + [Fact] + public void Vector3DoublesAbsTest() + { + Vector3 v1 = new Vector3(-2.5d, 2.0d, 0.5d); + Vector3 v3 = Vector3.Abs(new Vector3(0.0d, Double.NegativeInfinity, Double.NaN)); + Vector3 v = Vector3.Abs(v1); + Assert.Equal(2.5d, v.X); + Assert.Equal(2.0d, v.Y); + Assert.Equal(0.5d, v.Z); + Assert.Equal(0.0d, v3.X); + Assert.Equal(Double.PositiveInfinity, v3.Y); + Assert.Equal(Double.NaN, v3.Z); + } + + [Fact] + public void Vector3DoublesSqrtTest() + { + Vector3 a = new Vector3(-2.5d, 2.0d, 0.5d); + Vector3 b = new Vector3(5.5d, 4.5d, 16.5d); + Assert.Equal(2, (int)Vector3.SquareRoot(b).X); + Assert.Equal(2, (int)Vector3.SquareRoot(b).Y); + Assert.Equal(4, (int)Vector3.SquareRoot(b).Z); + Assert.Equal(Double.NaN, Vector3.SquareRoot(a).X); + } + + // A test to make sure these types are blittable directly into GPU buffer memory layouts + [Fact] + public unsafe void Vector3DoublesSizeofTest() + { + Assert.Equal(12, sizeof(Vector3)); + Assert.Equal(24, sizeof(Vector3Double_2x)); + Assert.Equal(16, sizeof(Vector3DoublePlusDouble)); + Assert.Equal(32, sizeof(Vector3DoublePlusDouble_2x)); + } + + [StructLayout(LayoutKind.Sequential)] + struct Vector3Double_2x + { + private Vector3 _a; + private Vector3 _b; + } + + [StructLayout(LayoutKind.Sequential)] + struct Vector3DoublePlusDouble + { + private Vector3 _v; + private double _f; + } + + [StructLayout(LayoutKind.Sequential)] + struct Vector3DoublePlusDouble_2x + { + private Vector3DoublePlusDouble _a; + private Vector3DoublePlusDouble _b; + } + + [Fact] + public void SetFieldsTest() + { + Vector3 v3 = new Vector3(4f, 5f, 6f); + v3 = v3.WithX(1.0d); + v3 = v3.WithY(2.0d); + v3 = v3.WithZ(3.0d); + Assert.Equal(1.0f, v3.X); + Assert.Equal(2.0f, v3.Y); + Assert.Equal(3.0f, v3.Z); + Vector3 v4 = v3; + v4 = v4.WithY(0.5d); + v4 = v4.WithZ(2.2d); + Assert.Equal(1.0f, v4.X); + Assert.Equal(0.5f, v4.Y); + Assert.Equal(2.2f, v4.Z); + Assert.Equal(2.0f, v3.Y); + + Vector3 before = new Vector3(1f, 2f, 3f); + Vector3 after = before; + after = after.WithX(500.0d); + Assert.NotEqual(before, after); + } + + [Fact] + public void EmbeddedVectorSetFields() + { + EmbeddedVectorObject evo = new EmbeddedVectorObject(); + evo.FieldVector = evo.FieldVector.WithX(5.0d); + evo.FieldVector = evo.FieldVector.WithY(5.0d); + evo.FieldVector = evo.FieldVector.WithZ(5.0d); + Assert.Equal(5.0f, evo.FieldVector.X); + Assert.Equal(5.0f, evo.FieldVector.Y); + Assert.Equal(5.0f, evo.FieldVector.Z); + } + + private class EmbeddedVectorObject + { + public Vector3Double FieldVector; + } + } +} \ No newline at end of file diff --git a/src/libraries/System.Numerics.Vectors/tests/Vector3_SingleTests.cs b/src/libraries/System.Numerics.Vectors/tests/Vector3_SingleTests.cs new file mode 100644 index 00000000000000..647459ceab8b74 --- /dev/null +++ b/src/libraries/System.Numerics.Vectors/tests/Vector3_SingleTests.cs @@ -0,0 +1,1209 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Globalization; +using System.Runtime.InteropServices; +using Xunit; + +namespace System.Numerics.Tests +{ + public partial class Vector3SingleTests + { + [Fact] + public TEMPCopyToTest() + { + Vector3 v1 = new Vector3(2.0f, 3.0f, 3.3f); + + var a = new Single[4]; + var b = new Single[3]; + + Assert.Throws(() => v1.CopyTo(a, -1)); + Assert.Throws(() => v1.CopyTo(a, a.Length)); + + v1.CopyTo(a, 1); + v1.CopyTo(b); + Assert.Equal(0.0f, a[0]); + Assert.Equal(2.0f, a[1]); + Assert.Equal(3.0f, a[2]); + Assert.Equal(3.3f, a[3]); + Assert.Equal(2.0f, b[0]); + Assert.Equal(3.0f, b[1]); + Assert.Equal(3.3f, b[2]); + } + + [Fact] + public TEMPGetHashCodeTest() + { + Vector3 v1 = new Vector3(2.0f, 3.0f, 3.3f); + Vector3 v2 = new Vector3(2.0f, 3.0f, 3.3f); + Vector3 v3 = new Vector3(2.0f, 3.0f, 3.3f); + Vector3 v5 = new Vector3(3.0f, 2.0f, 3.3f); + Assert.Equal(v1.GetHashCode(), v1.GetHashCode()); + Assert.Equal(v1.GetHashCode(), v2.GetHashCode()); + Assert.NotEqual(v1.GetHashCode(), v5.GetHashCode()); + Assert.Equal(v1.GetHashCode(), v3.GetHashCode()); + Vector3 v4 = new Vector3(0.0f, 0.0f, 0.0f); + Vector3 v6 = new Vector3(1.0f, 0.0f, 0.0f); + Vector3 v7 = new Vector3(0.0f, 1.0f, 0.0f); + Vector3 v8 = new Vector3(1.0f, 1.0f, 1.0f); + Vector3 v9 = new Vector3(1.0f, 1.0f, 0.0f); + Assert.NotEqual(v4.GetHashCode(), v6.GetHashCode()); + Assert.NotEqual(v4.GetHashCode(), v7.GetHashCode()); + Assert.NotEqual(v4.GetHashCode(), v8.GetHashCode()); + Assert.NotEqual(v7.GetHashCode(), v6.GetHashCode()); + Assert.NotEqual(v8.GetHashCode(), v6.GetHashCode()); + Assert.NotEqual(v8.GetHashCode(), v9.GetHashCode()); + Assert.NotEqual(v7.GetHashCode(), v9.GetHashCode()); + } + + [Fact] + public TEMPToStringTest() + { + string separator = CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator; + CultureInfo enUsCultureInfo = new CultureInfo("en-US"); + + Vector3 v1 = new Vector3(2.0f, 3.0f, 3.3f); + string v1str = v1.ToString(); + string expectedv1 = string.Format(CultureInfo.CurrentCulture + , "<{1:G}{0} {2:G}{0} {3:G}>" + , separator, 2, 3, 3.3); + Assert.Equal(expectedv1, v1str); + + string v1strformatted = v1.ToString("c", CultureInfo.CurrentCulture); + string expectedv1formatted = string.Format(CultureInfo.CurrentCulture + , "<{1:c}{0} {2:c}{0} {3:c}>" + , separator, 2, 3, 3.3); + Assert.Equal(expectedv1formatted, v1strformatted); + + string v2strformatted = v1.ToString("c", enUsCultureInfo); + string expectedv2formatted = string.Format(enUsCultureInfo + , "<{1:c}{0} {2:c}{0} {3:c}>" + , enUsCultureInfo.NumberFormat.NumberGroupSeparator, 2, 3, 3.3); + Assert.Equal(expectedv2formatted, v2strformatted); + + string v3strformatted = v1.ToString("c"); + string expectedv3formatted = string.Format(CultureInfo.CurrentCulture + , "<{1:c}{0} {2:c}{0} {3:c}>" + , separator, 2, 3, 3.3); + Assert.Equal(expectedv3formatted, v3strformatted); + } + + // A test for Cross (Vector3f, Vector3f) + [Fact] + public TEMPCrossTest() + { + Vector3 a = new Vector3(1.0f, 0.0f, 0.0f); + Vector3 b = new Vector3(0.0f, 1.0f, 0.0f); + + Vector3 expected = new Vector3(0.0f, 0.0f, 1.0f); + Vector3 actual; + + actual = Vector3.Cross(a, b); + Assert.True(MathHelper.Equal(expected, actual), "Vector3f.Cross did not return the expected value."); + } + + // A test for Cross (Vector3f, Vector3f) + // Cross test of the same vector + [Fact] + public TEMPCrossTest1() + { + Vector3 a = new Vector3(0.0f, 1.0f, 0.0f); + Vector3 b = new Vector3(0.0f, 1.0f, 0.0f); + + Vector3 expected = new Vector3(0.0f, 0.0f, 0.0f); + Vector3 actual = Vector3.Cross(a, b); + Assert.True(MathHelper.Equal(expected, actual), "Vector3f.Cross did not return the expected value."); + } + + // A test for Distance (Vector3f, Vector3f) + [Fact] + public TEMPDistanceTest() + { + Vector3 a = new Vector3(1.0f, 2.0f, 3.0f); + Vector3 b = new Vector3(4.0f, 5.0f, 6.0f); + + Single expected = (Single)System.Math.Sqrt(27); + Single actual; + + actual = Vector3.Distance(a, b); + Assert.True(MathHelper.EqualScalar(expected, actual), "Vector3f.Distance did not return the expected value."); + } + + // A test for Distance (Vector3f, Vector3f) + // Distance from the same point + [Fact] + public TEMPDistanceTest1() + { + Vector3 a = new Vector3(1.051f, 2.05f, 3.478f); + Vector3 b = new Vector3(new Vector2(1.051f, 0.0f), 1); + b.Y = 2.05f; + b.Z = 3.478f; + + Single actual = Vector3.Distance(a, b); + Assert.Equal(0.0f, actual); + } + + // A test for DistanceSquared (Vector3f, Vector3f) + [Fact] + public TEMPDistanceSquaredTest() + { + Vector3 a = new Vector3(1.0f, 2.0f, 3.0f); + Vector3 b = new Vector3(4.0f, 5.0f, 6.0f); + + Single expected = 27.0f; + Single actual; + + actual = Vector3.DistanceSquared(a, b); + Assert.True(MathHelper.EqualScalar(expected, actual), "Vector3f.DistanceSquared did not return the expected value."); + } + + // A test for Dot (Vector3f, Vector3f) + [Fact] + public TEMPDotTest() + { + Vector3 a = new Vector3(1.0f, 2.0f, 3.0f); + Vector3 b = new Vector3(4.0f, 5.0f, 6.0f); + + Single expected = 32.0f; + Single actual; + + actual = Vector3.Dot(a, b); + Assert.True(MathHelper.EqualScalar(expected, actual), "Vector3f.Dot did not return the expected value."); + } + + // A test for Dot (Vector3f, Vector3f) + // Dot test for perpendicular vector + [Fact] + public TEMPDotTest1() + { + Vector3 a = new Vector3(1.55f, 1.55f, 1); + Vector3 b = new Vector3(2.5f, 3, 1.5f); + Vector3 c = Vector3.Cross(a, b); + + Single expected = 0.0f; + Single actual1 = Vector3.Dot(a, c); + Single actual2 = Vector3.Dot(b, c); + Assert.True(MathHelper.EqualScalar(expected, actual1), "Vector3f.Dot did not return the expected value."); + Assert.True(MathHelper.EqualScalar(expected, actual2), "Vector3f.Dot did not return the expected value."); + } + + // A test for Length () + [Fact] + public TEMPLengthTest() + { + Vector2 a = new Vector2(1.0f, 2.0f); + + Single z = 3.0f; + + Vector3 target = new Vector3(a, z); + + Single expected = (Single)System.Math.Sqrt(14.0f); + Single actual; + + actual = target.Length(); + Assert.True(MathHelper.EqualScalar(expected, actual), "Vector3f.Length did not return the expected value."); + } + + // A test for Length () + // Length test where length is zero + [Fact] + public TEMPLengthTest1() + { + Vector3 target = new Vector3(); + + Single expected = 0.0f; + Single actual = target.Length(); + Assert.True(MathHelper.EqualScalar(expected, actual), "Vector3f.Length did not return the expected value."); + } + + // A test for LengthSquared () + [Fact] + public TEMPLengthSquaredTest() + { + Vector2 a = new Vector2(1.0f, 2.0f); + + Single z = 3.0f; + + Vector3 target = new Vector3(a, z); + + Single expected = 14.0f; + Single actual; + + actual = target.LengthSquared(); + Assert.True(MathHelper.EqualScalar(expected, actual), "Vector3f.LengthSquared did not return the expected value."); + } + + // A test for Min (Vector3f, Vector3f) + [Fact] + public TEMPMinTest() + { + Vector3 a = new Vector3(-1.0f, 4.0f, -3.0f); + Vector3 b = new Vector3(2.0f, 1.0f, -1.0f); + + Vector3 expected = new Vector3(-1.0f, 1.0f, -3.0f); + Vector3 actual; + actual = Vector3.Min(a, b); + Assert.True(MathHelper.Equal(expected, actual), "Vector3f.Min did not return the expected value."); + } + + // A test for Max (Vector3f, Vector3f) + [Fact] + public TEMPMaxTest() + { + Vector3 a = new Vector3(-1.0f, 4.0f, -3.0f); + Vector3 b = new Vector3(2.0f, 1.0f, -1.0f); + + Vector3 expected = new Vector3(2.0f, 4.0f, -1.0f); + Vector3 actual; + actual = Vector3.Max(a, b); + Assert.True(MathHelper.Equal(expected, actual), "Vector3.Max did not return the expected value."); + } + + [Fact] + public TEMPMinMaxCodeCoverageTest() + { + Vector3 min = Vector3.Zero; + Vector3 max = Vector3.One; + Vector3 actual; + + // Min. + actual = Vector3.Min(min, max); + Assert.Equal(actual, min); + + actual = Vector3.Min(max, min); + Assert.Equal(actual, min); + + // Max. + actual = Vector3.Max(min, max); + Assert.Equal(actual, max); + + actual = Vector3.Max(max, min); + Assert.Equal(actual, max); + } + + // A test for Lerp (Vector3f, Vector3f, Single) + [Fact] + public TEMPLerpTest() + { + Vector3 a = new Vector3(1.0f, 2.0f, 3.0f); + Vector3 b = new Vector3(4.0f, 5.0f, 6.0f); + + Single t = 0.5f; + + Vector3 expected = new Vector3(2.5f, 3.5f, 4.5f); + Vector3 actual; + + actual = Vector3.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector3f.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector3f, Vector3f, Single) + // Lerp test with factor zero + [Fact] + public TEMPLerpTest1() + { + Vector3 a = new Vector3(1.0f, 2.0f, 3.0f); + Vector3 b = new Vector3(4.0f, 5.0f, 6.0f); + + Single t = 0.0f; + Vector3 expected = new Vector3(1.0f, 2.0f, 3.0f); + Vector3 actual = Vector3.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector3f.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector3f, Vector3f, Single) + // Lerp test with factor one + [Fact] + public TEMPLerpTest2() + { + Vector3 a = new Vector3(1.0f, 2.0f, 3.0f); + Vector3 b = new Vector3(4.0f, 5.0f, 6.0f); + + Single t = 1.0f; + Vector3 expected = new Vector3(4.0f, 5.0f, 6.0f); + Vector3 actual = Vector3.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector3f.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector3f, Vector3f, Single) + // Lerp test with factor > 1 + [Fact] + public TEMPLerpTest3() + { + Vector3 a = new Vector3(0.0f, 0.0f, 0.0f); + Vector3 b = new Vector3(4.0f, 5.0f, 6.0f); + + Single t = 2.0f; + Vector3 expected = new Vector3(8.0f, 10.0f, 12.0f); + Vector3 actual = Vector3.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector3f.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector3f, Vector3f, Single) + // Lerp test with factor < 0 + [Fact] + public TEMPLerpTest4() + { + Vector3 a = new Vector3(0.0f, 0.0f, 0.0f); + Vector3 b = new Vector3(4.0f, 5.0f, 6.0f); + + Single t = -2.0f; + Vector3 expected = new Vector3(-8.0f, -10.0f, -12.0f); + Vector3 actual = Vector3.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector3f.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector3f, Vector3f, Single) + // Lerp test from the same point + [Fact] + public TEMPLerpTest5() + { + Vector3 a = new Vector3(1.68f, 2.34f, 5.43f); + Vector3 b = a; + + Single t = 0.18f; + Vector3 expected = new Vector3(1.68f, 2.34f, 5.43f); + Vector3 actual = Vector3.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector3f.Lerp did not return the expected value."); + } + + // A test for Reflect (Vector3f, Vector3f) + [Fact] + public TEMPReflectTest() + { + Vector3 a = Vector3.Normalize(new Vector3(1.0f, 1.0f, 1.0f)); + + // Reflect on XZ plane. + Vector3 n = new Vector3(0.0f, 1.0f, 0.0f); + Vector3 expected = new Vector3(a.X, -a.Y, a.Z); + Vector3 actual = Vector3.Reflect(a, n); + Assert.True(MathHelper.Equal(expected, actual), "Vector3f.Reflect did not return the expected value."); + + // Reflect on XY plane. + n = new Vector3(0.0f, 0.0f, 1.0f); + expected = new Vector3(a.X, a.Y, -a.Z); + actual = Vector3.Reflect(a, n); + Assert.True(MathHelper.Equal(expected, actual), "Vector3f.Reflect did not return the expected value."); + + // Reflect on YZ plane. + n = new Vector3(1.0f, 0.0f, 0.0f); + expected = new Vector3(-a.X, a.Y, a.Z); + actual = Vector3.Reflect(a, n); + Assert.True(MathHelper.Equal(expected, actual), "Vector3f.Reflect did not return the expected value."); + } + + // A test for Reflect (Vector3f, Vector3f) + // Reflection when normal and source are the same + [Fact] + public TEMPReflectTest1() + { + Vector3 n = new Vector3(0.45f, 1.28f, 0.86f); + n = Vector3.Normalize(n); + Vector3 a = n; + + Vector3 expected = -n; + Vector3 actual = Vector3.Reflect(a, n); + Assert.True(MathHelper.Equal(expected, actual), "Vector3f.Reflect did not return the expected value."); + } + + // A test for Reflect (Vector3f, Vector3f) + // Reflection when normal and source are negation + [Fact] + public TEMPReflectTest2() + { + Vector3 n = new Vector3(0.45f, 1.28f, 0.86f); + n = Vector3.Normalize(n); + Vector3 a = -n; + + Vector3 expected = n; + Vector3 actual = Vector3.Reflect(a, n); + Assert.True(MathHelper.Equal(expected, actual), "Vector3f.Reflect did not return the expected value."); + } + + // A test for Reflect (Vector3f, Vector3f) + // Reflection when normal and source are perpendicular (a dot n = 0) + [Fact] + public TEMPReflectTest3() + { + Vector3 n = new Vector3(0.45f, 1.28f, 0.86f); + Vector3 temp = new Vector3(1.28f, 0.45f, 0.01f); + // find a perpendicular vector of n + Vector3 a = Vector3.Cross(temp, n); + + Vector3 expected = a; + Vector3 actual = Vector3.Reflect(a, n); + Assert.True(MathHelper.Equal(expected, actual), "Vector3f.Reflect did not return the expected value."); + } + + // A test for Transform(Vector3f, Matrix4x4) + [Fact] + public TEMPTransformTest() + { + Vector3 v = new Vector3(1.0f, 2.0f, 3.0f); + Matrix4x4 m = + Matrix4x4.CreateRotationX(MathHelper.ToRadians(30.0f)) * + Matrix4x4.CreateRotationY(MathHelper.ToRadians(30.0f)) * + Matrix4x4.CreateRotationZ(MathHelper.ToRadians(30.0f)); + m.M41 = 10.0f; + m.M42 = 20.0f; + m.M43 = 30.0f; + + Vector3 expected = new Vector3(12.191987f, 21.533493f, 32.616024f); + Vector3 actual; + + actual = Vector3.Transform(v, m); + Assert.True(MathHelper.Equal(expected, actual), "Vector3f.Transform did not return the expected value."); + } + + // A test for Clamp (Vector3f, Vector3f, Vector3f) + [Fact] + public TEMPClampTest() + { + Vector3 a = new Vector3(0.5f, 0.3f, 0.33f); + Vector3 min = new Vector3(0.0f, 0.1f, 0.13f); + Vector3 max = new Vector3(1.0f, 1.1f, 1.13f); + + // Normal case. + // Case N1: specified value is in the range. + Vector3 expected = new Vector3(0.5f, 0.3f, 0.33f); + Vector3 actual = Vector3.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector3f.Clamp did not return the expected value."); + + // Normal case. + // Case N2: specified value is bigger than max value. + a = new Vector3(2.0f, 3.0f, 4.0f); + expected = max; + actual = Vector3.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector3f.Clamp did not return the expected value."); + + // Case N3: specified value is smaller than max value. + a = new Vector3(-2.0f, -3.0f, -4.0f); + expected = min; + actual = Vector3.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector3f.Clamp did not return the expected value."); + + // Case N4: combination case. + a = new Vector3(-2.0f, 0.5f, 4.0f); + expected = new Vector3(min.X, a.Y, max.Z); + actual = Vector3.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector3f.Clamp did not return the expected value."); + + // User specified min value is bigger than max value. + max = new Vector3(0.0f, 0.1f, 0.13f); + min = new Vector3(1.0f, 1.1f, 1.13f); + + // Case W1: specified value is in the range. + a = new Vector3(0.5f, 0.3f, 0.33f); + expected = max; + actual = Vector3.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector3f.Clamp did not return the expected value."); + + // Normal case. + // Case W2: specified value is bigger than max and min value. + a = new Vector3(2.0f, 3.0f, 4.0f); + expected = max; + actual = Vector3.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector3f.Clamp did not return the expected value."); + + // Case W3: specified value is smaller than min and max value. + a = new Vector3(-2.0f, -3.0f, -4.0f); + expected = max; + actual = Vector3.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector3f.Clamp did not return the expected value."); + } + + // A test for TransformNormal (Vector3f, Matrix4x4) + [Fact] + public TEMPTransformNormalTest() + { + Vector3 v = new Vector3(1.0f, 2.0f, 3.0f); + Matrix4x4 m = + Matrix4x4.CreateRotationX(MathHelper.ToRadians(30.0f)) * + Matrix4x4.CreateRotationY(MathHelper.ToRadians(30.0f)) * + Matrix4x4.CreateRotationZ(MathHelper.ToRadians(30.0f)); + m.M41 = 10.0f; + m.M42 = 20.0f; + m.M43 = 30.0f; + + Vector3 expected = new Vector3(2.19198728f, 1.53349364f, 2.61602545f); + Vector3 actual; + + actual = Vector3.TransformNormal(v, m); + Assert.True(MathHelper.Equal(expected, actual), "Vector3f.TransformNormal did not return the expected value."); + } + + // A test for Transform (Vector3f, Quaternion) + [Fact] + public TEMPTransformByQuaternionTest() + { + Vector3 v = new Vector3(1.0f, 2.0f, 3.0f); + + Matrix4x4 m = + Matrix4x4.CreateRotationX(MathHelper.ToRadians(30.0f)) * + Matrix4x4.CreateRotationY(MathHelper.ToRadians(30.0f)) * + Matrix4x4.CreateRotationZ(MathHelper.ToRadians(30.0f)); + Quaternion q = Quaternion.CreateFromRotationMatrix(m); + + Vector3 expected = Vector3.Transform(v, m); + Vector3 actual = Vector3.Transform(v, q); + Assert.True(MathHelper.Equal(expected, actual), "Vector3f.Transform did not return the expected value."); + } + + // A test for Transform (Vector3f, Quaternion) + // Transform Vector3 with zero quaternion + [Fact] + public TEMPTransformByQuaternionTest1() + { + Vector3 v = new Vector3(1.0f, 2.0f, 3.0f); + Quaternion q = new Quaternion(); + Vector3 expected = v; + + Vector3 actual = Vector3.Transform(v, q); + Assert.True(MathHelper.Equal(expected, actual), "Vector3f.Transform did not return the expected value."); + } + + // A test for Transform (Vector3f, Quaternion) + // Transform Vector3 with identity quaternion + [Fact] + public TEMPTransformByQuaternionTest2() + { + Vector3 v = new Vector3(1.0f, 2.0f, 3.0f); + Quaternion q = Quaternion.Identity; + Vector3 expected = v; + + Vector3 actual = Vector3.Transform(v, q); + Assert.True(MathHelper.Equal(expected, actual), "Vector3f.Transform did not return the expected value."); + } + + // A test for Normalize (Vector3f) + [Fact] + public TEMPNormalizeTest() + { + Vector3 a = new Vector3(1.0f, 2.0f, 3.0f); + + Vector3 expected = new Vector3( + 0.26726124191242438468455348087975f, + 0.53452248382484876936910696175951f, + 0.80178372573727315405366044263926f); + Vector3 actual; + + actual = Vector3.Normalize(a); + Assert.True(MathHelper.Equal(expected, actual), "Vector3f.Normalize did not return the expected value."); + } + + // A test for Normalize (Vector3f) + // Normalize vector of length one + [Fact] + public TEMPNormalizeTest1() + { + Vector3 a = new Vector3(1.0f, 0.0f, 0.0f); + + Vector3 expected = new Vector3(1.0f, 0.0f, 0.0f); + Vector3 actual = Vector3.Normalize(a); + Assert.True(MathHelper.Equal(expected, actual), "Vector3f.Normalize did not return the expected value."); + } + + // A test for Normalize (Vector3f) + // Normalize vector of length zero + [Fact] + public TEMPNormalizeTest2() + { + Vector3 a = new Vector3(0.0f, 0.0f, 0.0f); + + Vector3 expected = new Vector3(0.0f, 0.0f, 0.0f); + Vector3 actual = Vector3.Normalize(a); + Assert.True(Single.IsNaN(actual.X) && Single.IsNaN(actual.Y) && Single.IsNaN(actual.Z), "Vector3f.Normalize did not return the expected value."); + } + + // A test for operator - (Vector3f) + [Fact] + public TEMPUnaryNegationTest() + { + Vector3 a = new Vector3(1.0f, 2.0f, 3.0f); + + Vector3 expected = new Vector3(-1.0f, -2.0f, -3.0f); + Vector3 actual; + + actual = -a; + + Assert.True(MathHelper.Equal(expected, actual), "Vector3f.operator - did not return the expected value."); + } + + [Fact] + public TEMPUnaryNegationTest1() + { + Vector3 a = -new Vector3(Single.NaN, Single.PositiveInfinity, Single.NegativeInfinity); + Vector3 b = -new Vector3(0.0f, 0.0f, 0.0f); + Assert.Equal(Single.NaN, a.X); + Assert.Equal(Single.NegativeInfinity, a.Y); + Assert.Equal(Single.PositiveInfinity, a.Z); + Assert.Equal(0.0f, b.X); + Assert.Equal(0.0f, b.Y); + Assert.Equal(0.0f, b.Z); + } + + // A test for operator - (Vector3f, Vector3f) + [Fact] + public TEMPSubtractionTest() + { + Vector3 a = new Vector3(4.0f, 2.0f, 3.0f); + + Vector3 b = new Vector3(1.0f, 5.0f, 7.0f); + + Vector3 expected = new Vector3(3.0f, -3.0f, -4.0f); + Vector3 actual; + + actual = a - b; + + Assert.True(MathHelper.Equal(expected, actual), "Vector3f.operator - did not return the expected value."); + } + + // A test for operator * (Vector3f, Single) + [Fact] + public TEMPMultiplyOperatorTest() + { + Vector3 a = new Vector3(1.0f, 2.0f, 3.0f); + + Single factor = 2.0f; + + Vector3 expected = new Vector3(2.0f, 4.0f, 6.0f); + Vector3 actual; + + actual = a * factor; + + Assert.True(MathHelper.Equal(expected, actual), "Vector3f.operator * did not return the expected value."); + } + + // A test for operator * (Single, Vector3f) + [Fact] + public TEMPMultiplyOperatorTest2() + { + Vector3 a = new Vector3(1.0f, 2.0f, 3.0f); + + const Single factor = 2.0f; + + Vector3 expected = new Vector3(2.0f, 4.0f, 6.0f); + Vector3 actual; + + actual = factor * a; + + Assert.True(MathHelper.Equal(expected, actual), "Vector3f.operator * did not return the expected value."); + } + + // A test for operator * (Vector3f, Vector3f) + [Fact] + public TEMPMultiplyOperatorTest3() + { + Vector3 a = new Vector3(1.0f, 2.0f, 3.0f); + + Vector3 b = new Vector3(4.0f, 5.0f, 6.0f); + + Vector3 expected = new Vector3(4.0f, 10.0f, 18.0f); + Vector3 actual; + + actual = a * b; + + Assert.True(MathHelper.Equal(expected, actual), "Vector3f.operator * did not return the expected value."); + } + + // A test for operator / (Vector3f, Single) + [Fact] + public TEMPDivisionTest() + { + Vector3 a = new Vector3(1.0f, 2.0f, 3.0f); + + Single div = 2.0f; + + Vector3 expected = new Vector3(0.5f, 1.0f, 1.5f); + Vector3 actual; + + actual = a / div; + + Assert.True(MathHelper.Equal(expected, actual), "Vector3f.operator / did not return the expected value."); + } + + // A test for operator / (Vector3f, Vector3f) + [Fact] + public TEMPDivisionTest1() + { + Vector3 a = new Vector3(4.0f, 2.0f, 3.0f); + + Vector3 b = new Vector3(1.0f, 5.0f, 6.0f); + + Vector3 expected = new Vector3(4.0f, 0.4f, 0.5f); + Vector3 actual; + + actual = a / b; + + Assert.True(MathHelper.Equal(expected, actual), "Vector3f.operator / did not return the expected value."); + } + + // A test for operator / (Vector3f, Vector3f) + // Divide by zero + [Fact] + public TEMPDivisionTest2() + { + Vector3 a = new Vector3(-2.0f, 3.0f, Single.MaxValue); + + Single div = 0.0f; + + Vector3 actual = a / div; + + Assert.True(Single.IsNegativeInfinity(actual.X), "Vector3f.operator / did not return the expected value."); + Assert.True(Single.IsPositiveInfinity(actual.Y), "Vector3f.operator / did not return the expected value."); + Assert.True(Single.IsPositiveInfinity(actual.Z), "Vector3f.operator / did not return the expected value."); + } + + // A test for operator / (Vector3f, Vector3f) + // Divide by zero + [Fact] + public TEMPDivisionTest3() + { + Vector3 a = new Vector3(0.047f, -3.0f, Single.NegativeInfinity); + Vector3 b = new Vector3(); + + Vector3 actual = a / b; + + Assert.True(Single.IsPositiveInfinity(actual.X), "Vector3f.operator / did not return the expected value."); + Assert.True(Single.IsNegativeInfinity(actual.Y), "Vector3f.operator / did not return the expected value."); + Assert.True(Single.IsNegativeInfinity(actual.Z), "Vector3f.operator / did not return the expected value."); + } + + // A test for operator + (Vector3f, Vector3f) + [Fact] + public TEMPAdditionTest() + { + Vector3 a = new Vector3(1.0f, 2.0f, 3.0f); + Vector3 b = new Vector3(4.0f, 5.0f, 6.0f); + + Vector3 expected = new Vector3(5.0f, 7.0f, 9.0f); + Vector3 actual; + + actual = a + b; + + Assert.True(MathHelper.Equal(expected, actual), "Vector3f.operator + did not return the expected value."); + } + + // A test for Vector3f (Single, Single, Single) + [Fact] + public TEMPConstructorTest() + { + Single x = 1.0f; + Single y = 2.0f; + Single z = 3.0f; + + Vector3 target = new Vector3(x, y, z); + Assert.True(MathHelper.Equal(target.X, x) && MathHelper.Equal(target.Y, y) && MathHelper.Equal(target.Z, z), "Vector3f.constructor (x,y,z) did not return the expected value."); + } + + // A test for Vector3f (Vector2f, Single) + [Fact] + public TEMPConstructorTest1() + { + Vector2 a = new Vector2(1.0f, 2.0f); + + Single z = 3.0f; + + Vector3 target = new Vector3(a, z); + Assert.True(MathHelper.Equal(target.X, a.X) && MathHelper.Equal(target.Y, a.Y) && MathHelper.Equal(target.Z, z), "Vector3f.constructor (Vector2f,z) did not return the expected value."); + } + + // A test for Vector3f () + // Constructor with no parameter + [Fact] + public TEMPConstructorTest3() + { + Vector3 a = new Vector3(); + + Assert.Equal(0.0f, a.X); + Assert.Equal(0.0f, a.Y); + Assert.Equal(0.0f, a.Z); + } + + // A test for Vector2f (Single, Single) + // Constructor with special Singleing values + [Fact] + public TEMPConstructorTest4() + { + Vector3 target = new Vector3(Single.NaN, Single.MaxValue, Single.PositiveInfinity); + + Assert.True(Single.IsNaN(target.X), "Vector3f.constructor (Vector3f) did not return the expected value."); + Assert.True(Single.Equals(Single.MaxValue, target.Y), "Vector3f.constructor (Vector3f) did not return the expected value."); + Assert.True(Single.IsPositiveInfinity(target.Z), "Vector3f.constructor (Vector3f) did not return the expected value."); + } + + // A test for Add (Vector3f, Vector3f) + [Fact] + public TEMPAddTest() + { + Vector3 a = new Vector3(1.0f, 2.0f, 3.0f); + Vector3 b = new Vector3(5.0f, 6.0f, 7.0f); + + Vector3 expected = new Vector3(6.0f, 8.0f, 10.0f); + Vector3 actual; + + actual = Vector3.Add(a, b); + Assert.Equal(expected, actual); + } + + // A test for Divide (Vector3f, Single) + [Fact] + public TEMPDivideTest() + { + Vector3 a = new Vector3(1.0f, 2.0f, 3.0f); + Single div = 2.0f; + Vector3 expected = new Vector3(0.5f, 1.0f, 1.5f); + Vector3 actual; + actual = Vector3.Divide(a, div); + Assert.Equal(expected, actual); + } + + // A test for Divide (Vector3f, Vector3f) + [Fact] + public TEMPDivideTest1() + { + Vector3 a = new Vector3(1.0f, 6.0f, 7.0f); + Vector3 b = new Vector3(5.0f, 2.0f, 3.0f); + + Vector3 expected = new Vector3(1.0f / 5.0f, 6.0f / 2.0f, 7.0f / 3.0f); + Vector3 actual; + + actual = Vector3.Divide(a, b); + Assert.Equal(expected, actual); + } + + // A test for Equals (object) + [Fact] + public TEMPEqualsTest() + { + Vector3 a = new Vector3(1.0f, 2.0f, 3.0f); + Vector3 b = new Vector3(1.0f, 2.0f, 3.0f); + + // case 1: compare between same values + object obj = b; + + bool expected = true; + bool actual = a.Equals(obj); + Assert.Equal(expected, actual); + + // case 2: compare between different values + b = new Vector3Single(b.X, 10); + obj = b; + expected = false; + actual = a.Equals(obj); + Assert.Equal(expected, actual); + + // case 3: compare between different types. + obj = new Quaternion(); + expected = false; + actual = a.Equals(obj); + Assert.Equal(expected, actual); + + // case 3: compare against null. + obj = null; + expected = false; + actual = a.Equals(obj); + Assert.Equal(expected, actual); + } + + // A test for Multiply (Vector3f, Single) + [Fact] + public TEMPMultiplyTest() + { + Vector3 a = new Vector3(1.0f, 2.0f, 3.0f); + const Single factor = 2.0f; + Vector3 expected = new Vector3(2.0f, 4.0f, 6.0f); + Vector3 actual = Vector3.Multiply(a, factor); + Assert.Equal(expected, actual); + } + + // A test for Multiply (Single, Vector3f) + [Fact] + public static TEMPMultiplyTest2() + { + Vector3 a = new Vector3(1.0f, 2.0f, 3.0f); + const Single factor = 2.0f; + Vector3 expected = new Vector3(2.0f, 4.0f, 6.0f); + Vector3 actual = Vector3.Multiply(factor, a); + Assert.Equal(expected, actual); + } + + // A test for Multiply (Vector3f, Vector3f) + [Fact] + public TEMPMultiplyTest3() + { + Vector3 a = new Vector3(1.0f, 2.0f, 3.0f); + Vector3 b = new Vector3(5.0f, 6.0f, 7.0f); + + Vector3 expected = new Vector3(5.0f, 12.0f, 21.0f); + Vector3 actual; + + actual = Vector3.Multiply(a, b); + Assert.Equal(expected, actual); + } + + // A test for Negate (Vector3f) + [Fact] + public TEMPNegateTest() + { + Vector3 a = new Vector3(1.0f, 2.0f, 3.0f); + + Vector3 expected = new Vector3(-1.0f, -2.0f, -3.0f); + Vector3 actual; + + actual = Vector3.Negate(a); + Assert.Equal(expected, actual); + } + + // A test for operator != (Vector3f, Vector3f) + [Fact] + public TEMPInequalityTest() + { + Vector3 a = new Vector3(1.0f, 2.0f, 3.0f); + Vector3 b = new Vector3(1.0f, 2.0f, 3.0f); + + // case 1: compare between same values + bool expected = false; + bool actual = a != b; + Assert.Equal(expected, actual); + + // case 2: compare between different values + b = new Vector3Single(b.X, 10); + expected = true; + actual = a != b; + Assert.Equal(expected, actual); + } + + // A test for operator == (Vector3f, Vector3f) + [Fact] + public TEMPEqualityTest() + { + Vector3 a = new Vector3(1.0f, 2.0f, 3.0f); + Vector3 b = new Vector3(1.0f, 2.0f, 3.0f); + + // case 1: compare between same values + bool expected = true; + bool actual = a == b; + Assert.Equal(expected, actual); + + // case 2: compare between different values + b = new Vector3Single(b.X, 10); + expected = false; + actual = a == b; + Assert.Equal(expected, actual); + } + + // A test for Subtract (Vector3f, Vector3f) + [Fact] + public TEMPSubtractTest() + { + Vector3 a = new Vector3(1.0f, 6.0f, 3.0f); + Vector3 b = new Vector3(5.0f, 2.0f, 3.0f); + + Vector3 expected = new Vector3(-4.0f, 4.0f, 0.0f); + Vector3 actual; + + actual = Vector3.Subtract(a, b); + Assert.Equal(expected, actual); + } + + // A test for One + [Fact] + public TEMPOneTest() + { + Vector3 val = new Vector3(1.0f, 1.0f, 1.0f); + Assert.Equal(val, Vector3.One); + } + + // A test for UnitX + [Fact] + public TEMPUnitXTest() + { + Vector3 val = new Vector3(1.0f, 0.0f, 0.0f); + Assert.Equal(val, Vector3.UnitX); + } + + // A test for UnitY + [Fact] + public TEMPUnitYTest() + { + Vector3 val = new Vector3(0.0f, 1.0f, 0.0f); + Assert.Equal(val, Vector3.UnitY); + } + + // A test for UnitZ + [Fact] + public TEMPUnitZTest() + { + Vector3 val = new Vector3(0.0f, 0.0f, 1.0f); + Assert.Equal(val, Vector3.UnitZ); + } + + // A test for Zero + [Fact] + public TEMPZeroTest() + { + Vector3 val = new Vector3(0.0f, 0.0f, 0.0f); + Assert.Equal(val, Vector3.Zero); + } + + // A test for Equals (Vector3f) + [Fact] + public TEMPEqualsTest1() + { + Vector3 a = new Vector3(1.0f, 2.0f, 3.0f); + Vector3 b = new Vector3(1.0f, 2.0f, 3.0f); + + // case 1: compare between same values + bool expected = true; + bool actual = a.Equals(b); + Assert.Equal(expected, actual); + + // case 2: compare between different values + b = new Vector3Single(b.X, 10); + expected = false; + actual = a.Equals(b); + Assert.Equal(expected, actual); + } + + // A test for Vector3f (Single) + [Fact] + public TEMPConstructorTest5() + { + Single value = 1.0f; + Vector3 target = new Vector3(value); + + Vector3 expected = new Vector3(value, value, value); + Assert.Equal(expected, target); + + value = 2.0f; + target = new Vector3(value); + expected = new Vector3(value, value, value); + Assert.Equal(expected, target); + } + + // A test for Vector3f comparison involving NaN values + [Fact] + public TEMPEqualsNanTest() + { + Vector3 a = new Vector3(Single.NaN, 0, 0); + Vector3 b = new Vector3(0, Single.NaN, 0); + Vector3 c = new Vector3(0, 0, Single.NaN); + + Assert.False(a == Vector3.Zero); + Assert.False(b == Vector3.Zero); + Assert.False(c == Vector3.Zero); + + Assert.True(a != Vector3.Zero); + Assert.True(b != Vector3.Zero); + Assert.True(c != Vector3.Zero); + + Assert.False(a.Equals(Vector3.Zero)); + Assert.False(b.Equals(Vector3.Zero)); + Assert.False(c.Equals(Vector3.Zero)); + + // Counterintuitive result - IEEE rules for NaN comparison are weird! + Assert.False(a.Equals(a)); + Assert.False(b.Equals(b)); + Assert.False(c.Equals(c)); + } + + [Fact] + public TEMPAbsTest() + { + Vector3 v1 = new Vector3(-2.5f, 2.0f, 0.5f); + Vector3 v3 = Vector3.Abs(new Vector3(0.0f, Single.NegativeInfinity, Single.NaN)); + Vector3 v = Vector3.Abs(v1); + Assert.Equal(2.5f, v.X); + Assert.Equal(2.0f, v.Y); + Assert.Equal(0.5f, v.Z); + Assert.Equal(0.0f, v3.X); + Assert.Equal(Single.PositiveInfinity, v3.Y); + Assert.Equal(Single.NaN, v3.Z); + } + + [Fact] + public TEMPSqrtTest() + { + Vector3 a = new Vector3(-2.5f, 2.0f, 0.5f); + Vector3 b = new Vector3(5.5f, 4.5f, 16.5f); + Assert.Equal(2, (int)Vector3.SquareRoot(b).X); + Assert.Equal(2, (int)Vector3.SquareRoot(b).Y); + Assert.Equal(4, (int)Vector3.SquareRoot(b).Z); + Assert.Equal(Single.NaN, Vector3.SquareRoot(a).X); + } + + // A test to make sure these types are blittable directly into GPU buffer memory layouts + [Fact] + public unsafe TEMPSizeofTest() + { + Assert.Equal(12, sizeof(Vector3)); + Assert.Equal(24, sizeof(Vector3Single_2x)); + Assert.Equal(16, sizeof(Vector3SinglePlusSingle)); + Assert.Equal(32, sizeof(Vector3SinglePlusSingle_2x)); + } + + [StructLayout(LayoutKind.Sequential)] + struct Vector3Single_2x + { + private Vector3 _a; + private Vector3 _b; + } + + [StructLayout(LayoutKind.Sequential)] + struct Vector3SinglePlusSingle + { + private Vector3 _v; + private Single _f; + } + + [StructLayout(LayoutKind.Sequential)] + struct Vector3SinglePlusSingle_2x + { + private Vector3SinglePlusSingle _a; + private Vector3SinglePlusSingle _b; + } + + [Fact] + public void SetFieldsTest() + { + Vector3 v3 = new Vector3(4f, 5f, 6f); + v3 = v3.WithX(1.0f); + v3 = v3.WithY(2.0f); + v3 = v3.WithZ(3.0f); + Assert.Equal(1.0f, v3.X); + Assert.Equal(2.0f, v3.Y); + Assert.Equal(3.0f, v3.Z); + Vector3 v4 = v3; + v4 = v4.WithY(0.5f); + v4 = v4.WithZ(2.2f); + Assert.Equal(1.0f, v4.X); + Assert.Equal(0.5f, v4.Y); + Assert.Equal(2.2f, v4.Z); + Assert.Equal(2.0f, v3.Y); + + Vector3 before = new Vector3(1f, 2f, 3f); + Vector3 after = before; + after = after.WithX(500.0f); + Assert.NotEqual(before, after); + } + + [Fact] + public void EmbeddedVectorSetFields() + { + EmbeddedVectorObject evo = new EmbeddedVectorObject(); + evo.FieldVector = evo.FieldVector.WithX(5.0f); + evo.FieldVector = evo.FieldVector.WithY(5.0f); + evo.FieldVector = evo.FieldVector.WithZ(5.0f); + Assert.Equal(5.0f, evo.FieldVector.X); + Assert.Equal(5.0f, evo.FieldVector.Y); + Assert.Equal(5.0f, evo.FieldVector.Z); + } + + private class EmbeddedVectorObject + { + public Vector3Single FieldVector; + } + } +} \ No newline at end of file diff --git a/src/libraries/System.Numerics.Vectors/tests/Vector4Tests.cs b/src/libraries/System.Numerics.Vectors/tests/Vector4Tests.cs index 3643f20a14345d..bdac47321b3e2f 100644 --- a/src/libraries/System.Numerics.Vectors/tests/Vector4Tests.cs +++ b/src/libraries/System.Numerics.Vectors/tests/Vector4Tests.cs @@ -1,3 +1,9 @@ +/********************************************************************************* + * This file is auto-generated from a template file by the GenerateTests.csx * + * script in tests\src\libraries\System.Numerics.Vectors\tests. In order to make * + * changes, please update the corresponding template and run according to the * + * directions listed in the file. * + *********************************************************************************/ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. @@ -8,27 +14,18 @@ namespace System.Numerics.Tests { - public class Vector4Tests + public partial class Vector4Tests { - [Fact] - public void Vector4MarshalSizeTest() - { - Assert.Equal(16, Marshal.SizeOf()); - Assert.Equal(16, Marshal.SizeOf(new Vector4())); - } - [Fact] public void Vector4CopyToTest() { Vector4 v1 = new Vector4(2.5f, 2.0f, 3.0f, 3.3f); - float[] a = new float[5]; - float[] b = new float[4]; + var a = new Single[5]; + var b = new Single[4]; - Assert.Throws(() => v1.CopyTo(null, 0)); Assert.Throws(() => v1.CopyTo(a, -1)); Assert.Throws(() => v1.CopyTo(a, a.Length)); - AssertExtensions.Throws(null, () => v1.CopyTo(a, a.Length - 2)); v1.CopyTo(a, 1); v1.CopyTo(b); @@ -108,11 +105,11 @@ public void Vector4DistanceSquaredTest() Vector4 a = new Vector4(1.0f, 2.0f, 3.0f, 4.0f); Vector4 b = new Vector4(5.0f, 6.0f, 7.0f, 8.0f); - float expected = 64.0f; - float actual; + Single expected = 64.0f; + Single actual; actual = Vector4.DistanceSquared(a, b); - Assert.True(MathHelper.Equal(expected, actual), "Vector4f.DistanceSquared did not return the expected value."); + Assert.True(MathHelper.EqualScalar(expected, actual), "Vector4f.DistanceSquared did not return the expected value."); } // A test for Distance (Vector4f, Vector4f) @@ -122,11 +119,11 @@ public void Vector4DistanceTest() Vector4 a = new Vector4(1.0f, 2.0f, 3.0f, 4.0f); Vector4 b = new Vector4(5.0f, 6.0f, 7.0f, 8.0f); - float expected = 8.0f; - float actual; + Single expected = 8.0f; + Single actual; actual = Vector4.Distance(a, b); - Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Distance did not return the expected value."); + Assert.True(MathHelper.EqualScalar(expected, actual), "Vector4f.Distance did not return the expected value."); } // A test for Distance (Vector4f, Vector4f) @@ -138,7 +135,7 @@ public void Vector4DistanceTest1() Vector4 b = new Vector4(new Vector3(1.051f, 2.05f, 3.478f), 0.0f); b.W = 1.0f; - float actual = Vector4.Distance(a, b); + Single actual = Vector4.Distance(a, b); Assert.Equal(0.0f, actual); } @@ -149,11 +146,11 @@ public void Vector4DotTest() Vector4 a = new Vector4(1.0f, 2.0f, 3.0f, 4.0f); Vector4 b = new Vector4(5.0f, 6.0f, 7.0f, 8.0f); - float expected = 70.0f; - float actual; + Single expected = 70.0f; + Single actual; actual = Vector4.Dot(a, b); - Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Dot did not return the expected value."); + Assert.True(MathHelper.EqualScalar(expected, actual), "Vector4f.Dot did not return the expected value."); } // A test for Dot (Vector4f, Vector4f) @@ -168,8 +165,8 @@ public void Vector4DotTest1() Vector4 d = new Vector4(a, 0); Vector4 e = new Vector4(c, 0); - float actual = Vector4.Dot(d, e); - Assert.True(MathHelper.Equal(0.0f, actual), "Vector4f.Dot did not return the expected value."); + Single actual = Vector4.Dot(d, e); + Assert.True(MathHelper.EqualScalar(0.0f, actual), "Vector4f.Dot did not return the expected value."); } // A test for Length () @@ -177,12 +174,12 @@ public void Vector4DotTest1() public void Vector4LengthTest() { Vector3 a = new Vector3(1.0f, 2.0f, 3.0f); - float w = 4.0f; + Single w = 4.0f; Vector4 target = new Vector4(a, w); - float expected = (float)System.Math.Sqrt(30.0f); - float actual; + Single expected = (Single)System.Math.Sqrt(30.0f); + Single actual; actual = target.Length(); @@ -196,10 +193,10 @@ public void Vector4LengthTest1() { Vector4 target = new Vector4(); - float expected = 0.0f; - float actual = target.Length(); + Single expected = 0.0f; + Single actual = target.Length(); - Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Length did not return the expected value."); + Assert.True(MathHelper.EqualScalar(expected, actual), "Vector4f.Length did not return the expected value."); } // A test for LengthSquared () @@ -207,16 +204,16 @@ public void Vector4LengthTest1() public void Vector4LengthSquaredTest() { Vector3 a = new Vector3(1.0f, 2.0f, 3.0f); - float w = 4.0f; + Single w = 4.0f; Vector4 target = new Vector4(a, w); - float expected = 30; - float actual; + Single expected = 30; + Single actual; actual = target.LengthSquared(); - Assert.True(MathHelper.Equal(expected, actual), "Vector4f.LengthSquared did not return the expected value."); + Assert.True(MathHelper.EqualScalar(expected, actual), "Vector4f.LengthSquared did not return the expected value."); } // A test for Min (Vector4f, Vector4f) @@ -324,14 +321,14 @@ public void Vector4ClampTest() Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Clamp did not return the expected value."); } - // A test for Lerp (Vector4f, Vector4f, float) + // A test for Lerp (Vector4f, Vector4f, Single) [Fact] public void Vector4LerpTest() { Vector4 a = new Vector4(1.0f, 2.0f, 3.0f, 4.0f); Vector4 b = new Vector4(5.0f, 6.0f, 7.0f, 8.0f); - float t = 0.5f; + Single t = 0.5f; Vector4 expected = new Vector4(3.0f, 4.0f, 5.0f, 6.0f); Vector4 actual; @@ -340,7 +337,7 @@ public void Vector4LerpTest() Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Lerp did not return the expected value."); } - // A test for Lerp (Vector4f, Vector4f, float) + // A test for Lerp (Vector4f, Vector4f, Single) // Lerp test with factor zero [Fact] public void Vector4LerpTest1() @@ -348,13 +345,13 @@ public void Vector4LerpTest1() Vector4 a = new Vector4(new Vector3(1.0f, 2.0f, 3.0f), 4.0f); Vector4 b = new Vector4(4.0f, 5.0f, 6.0f, 7.0f); - float t = 0.0f; + Single t = 0.0f; Vector4 expected = new Vector4(1.0f, 2.0f, 3.0f, 4.0f); Vector4 actual = Vector4.Lerp(a, b, t); Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Lerp did not return the expected value."); } - // A test for Lerp (Vector4f, Vector4f, float) + // A test for Lerp (Vector4f, Vector4f, Single) // Lerp test with factor one [Fact] public void Vector4LerpTest2() @@ -362,13 +359,13 @@ public void Vector4LerpTest2() Vector4 a = new Vector4(new Vector3(1.0f, 2.0f, 3.0f), 4.0f); Vector4 b = new Vector4(4.0f, 5.0f, 6.0f, 7.0f); - float t = 1.0f; + Single t = 1.0f; Vector4 expected = new Vector4(4.0f, 5.0f, 6.0f, 7.0f); Vector4 actual = Vector4.Lerp(a, b, t); Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Lerp did not return the expected value."); } - // A test for Lerp (Vector4f, Vector4f, float) + // A test for Lerp (Vector4f, Vector4f, Single) // Lerp test with factor > 1 [Fact] public void Vector4LerpTest3() @@ -376,13 +373,13 @@ public void Vector4LerpTest3() Vector4 a = new Vector4(new Vector3(0.0f, 0.0f, 0.0f), 0.0f); Vector4 b = new Vector4(4.0f, 5.0f, 6.0f, 7.0f); - float t = 2.0f; + Single t = 2.0f; Vector4 expected = new Vector4(8.0f, 10.0f, 12.0f, 14.0f); Vector4 actual = Vector4.Lerp(a, b, t); Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Lerp did not return the expected value."); } - // A test for Lerp (Vector4f, Vector4f, float) + // A test for Lerp (Vector4f, Vector4f, Single) // Lerp test with factor < 0 [Fact] public void Vector4LerpTest4() @@ -390,13 +387,13 @@ public void Vector4LerpTest4() Vector4 a = new Vector4(new Vector3(0.0f, 0.0f, 0.0f), 0.0f); Vector4 b = new Vector4(4.0f, 5.0f, 6.0f, 7.0f); - float t = -2.0f; + Single t = -2.0f; Vector4 expected = -(b * 2); Vector4 actual = Vector4.Lerp(a, b, t); Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Lerp did not return the expected value."); } - // A test for Lerp (Vector4f, Vector4f, float) + // A test for Lerp (Vector4f, Vector4f, Single) // Lerp test from the same point [Fact] public void Vector4LerpTest5() @@ -404,7 +401,7 @@ public void Vector4LerpTest5() Vector4 a = new Vector4(4.0f, 5.0f, 6.0f, 7.0f); Vector4 b = new Vector4(4.0f, 5.0f, 6.0f, 7.0f); - float t = 0.85f; + Single t = 0.85f; Vector4 expected = a; Vector4 actual = Vector4.Lerp(a, b, t); Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Lerp did not return the expected value."); @@ -814,7 +811,7 @@ public void Vector4NormalizeTest2() Vector4 expected = new Vector4(0.0f, 0.0f, 0.0f, 0.0f); Vector4 actual = Vector4.Normalize(a); - Assert.True(float.IsNaN(actual.X) && float.IsNaN(actual.Y) && float.IsNaN(actual.Z) && float.IsNaN(actual.W), "Vector4f.Normalize did not return the expected value."); + Assert.True(Single.IsNaN(actual.X) && Single.IsNaN(actual.Y) && Single.IsNaN(actual.Z) && Single.IsNaN(actual.W), "Vector4f.Normalize did not return the expected value."); } // A test for operator - (Vector4f) @@ -846,13 +843,13 @@ public void Vector4SubtractionTest() Assert.True(MathHelper.Equal(expected, actual), "Vector4f.operator - did not return the expected value."); } - // A test for operator * (Vector4f, float) + // A test for operator * (Vector4f, Single) [Fact] public void Vector4MultiplyOperatorTest() { Vector4 a = new Vector4(1.0f, 2.0f, 3.0f, 4.0f); - const float factor = 2.0f; + const Single factor = 2.0f; Vector4 expected = new Vector4(2.0f, 4.0f, 6.0f, 8.0f); Vector4 actual; @@ -861,13 +858,13 @@ public void Vector4MultiplyOperatorTest() Assert.True(MathHelper.Equal(expected, actual), "Vector4f.operator * did not return the expected value."); } - // A test for operator * (float, Vector4f) + // A test for operator * (Single, Vector4f) [Fact] public void Vector4MultiplyOperatorTest2() { Vector4 a = new Vector4(1.0f, 2.0f, 3.0f, 4.0f); - const float factor = 2.0f; + const Single factor = 2.0f; Vector4 expected = new Vector4(2.0f, 4.0f, 6.0f, 8.0f); Vector4 actual; @@ -890,13 +887,13 @@ public void Vector4MultiplyOperatorTest3() Assert.True(MathHelper.Equal(expected, actual), "Vector4f.operator * did not return the expected value."); } - // A test for operator / (Vector4f, float) + // A test for operator / (Vector4f, Single) [Fact] public void Vector4DivisionTest() { Vector4 a = new Vector4(1.0f, 2.0f, 3.0f, 4.0f); - float div = 2.0f; + Single div = 2.0f; Vector4 expected = new Vector4(0.5f, 1.0f, 1.5f, 2.0f); Vector4 actual; @@ -926,16 +923,16 @@ public void Vector4DivisionTest1() [Fact] public void Vector4DivisionTest2() { - Vector4 a = new Vector4(-2.0f, 3.0f, float.MaxValue, float.NaN); + Vector4 a = new Vector4(-2.0f, 3.0f, Single.MaxValue, Single.NaN); - float div = 0.0f; + Single div = 0.0f; Vector4 actual = a / div; - Assert.True(float.IsNegativeInfinity(actual.X), "Vector4f.operator / did not return the expected value."); - Assert.True(float.IsPositiveInfinity(actual.Y), "Vector4f.operator / did not return the expected value."); - Assert.True(float.IsPositiveInfinity(actual.Z), "Vector4f.operator / did not return the expected value."); - Assert.True(float.IsNaN(actual.W), "Vector4f.operator / did not return the expected value."); + Assert.True(Single.IsNegativeInfinity(actual.X), "Vector4f.operator / did not return the expected value."); + Assert.True(Single.IsPositiveInfinity(actual.Y), "Vector4f.operator / did not return the expected value."); + Assert.True(Single.IsPositiveInfinity(actual.Z), "Vector4f.operator / did not return the expected value."); + Assert.True(Single.IsNaN(actual.W), "Vector4f.operator / did not return the expected value."); } // A test for operator / (Vector4f, Vector4f) @@ -943,15 +940,15 @@ public void Vector4DivisionTest2() [Fact] public void Vector4DivisionTest3() { - Vector4 a = new Vector4(0.047f, -3.0f, float.NegativeInfinity, float.MinValue); + Vector4 a = new Vector4(0.047f, -3.0f, Single.NegativeInfinity, Single.MinValue); Vector4 b = new Vector4(); Vector4 actual = a / b; - Assert.True(float.IsPositiveInfinity(actual.X), "Vector4f.operator / did not return the expected value."); - Assert.True(float.IsNegativeInfinity(actual.Y), "Vector4f.operator / did not return the expected value."); - Assert.True(float.IsNegativeInfinity(actual.Z), "Vector4f.operator / did not return the expected value."); - Assert.True(float.IsNegativeInfinity(actual.W), "Vector4f.operator / did not return the expected value."); + Assert.True(Single.IsPositiveInfinity(actual.X), "Vector4f.operator / did not return the expected value."); + Assert.True(Single.IsNegativeInfinity(actual.Y), "Vector4f.operator / did not return the expected value."); + Assert.True(Single.IsNegativeInfinity(actual.Z), "Vector4f.operator / did not return the expected value."); + Assert.True(Single.IsNegativeInfinity(actual.W), "Vector4f.operator / did not return the expected value."); } // A test for operator + (Vector4f, Vector4f) @@ -976,7 +973,7 @@ public void OperatorAddTest() Vector4 v2 = new Vector4(5.5f, 4.5f, 6.5f, 7.5f); Vector4 v3 = v1 + v2; - Vector4 v5 = new Vector4(-1.0f, 0.0f, 0.0f, float.NaN); + Vector4 v5 = new Vector4(-1.0f, 0.0f, 0.0f, Single.NaN); Vector4 v4 = v1 + v5; Assert.Equal(8.0f, v3.X); Assert.Equal(6.5f, v3.Y); @@ -985,17 +982,17 @@ public void OperatorAddTest() Assert.Equal(1.5f, v4.X); Assert.Equal(2.0f, v4.Y); Assert.Equal(3.0f, v4.Z); - Assert.Equal(float.NaN, v4.W); + Assert.Equal(Single.NaN, v4.W); } - // A test for Vector4f (float, float, float, float) + // A test for Vector4f (Single, Single, Single, Single) [Fact] public void Vector4ConstructorTest() { - float x = 1.0f; - float y = 2.0f; - float z = 3.0f; - float w = 4.0f; + Single x = 1.0f; + Single y = 2.0f; + Single z = 3.0f; + Single w = 4.0f; Vector4 target = new Vector4(x, y, z, w); @@ -1003,25 +1000,25 @@ public void Vector4ConstructorTest() "Vector4f constructor(x,y,z,w) did not return the expected value."); } - // A test for Vector4f (Vector2f, float, float) + // A test for Vector4f (Vector2f, Single, Single) [Fact] public void Vector4ConstructorTest1() { Vector2 a = new Vector2(1.0f, 2.0f); - float z = 3.0f; - float w = 4.0f; + Single z = 3.0f; + Single w = 4.0f; Vector4 target = new Vector4(a, z, w); Assert.True(MathHelper.Equal(target.X, a.X) && MathHelper.Equal(target.Y, a.Y) && MathHelper.Equal(target.Z, z) && MathHelper.Equal(target.W, w), "Vector4f constructor(Vector2f,z,w) did not return the expected value."); } - // A test for Vector4f (Vector3f, float) + // A test for Vector4f (Vector3f, Single) [Fact] public void Vector4ConstructorTest2() { Vector3 a = new Vector3(1.0f, 2.0f, 3.0f); - float w = 4.0f; + Single w = 4.0f; Vector4 target = new Vector4(a, w); @@ -1043,16 +1040,16 @@ public void Vector4ConstructorTest4() } // A test for Vector4f () - // Constructor with special floating values + // Constructor with special Singleing values [Fact] public void Vector4ConstructorTest5() { - Vector4 target = new Vector4(float.NaN, float.MaxValue, float.PositiveInfinity, float.Epsilon); + Vector4 target = new Vector4(Single.NaN, Single.MaxValue, Single.PositiveInfinity, Single.Epsilon); - Assert.True(float.IsNaN(target.X), "Vector4f.constructor (float, float, float, float) did not return the expected value."); - Assert.True(float.Equals(float.MaxValue, target.Y), "Vector4f.constructor (float, float, float, float) did not return the expected value."); - Assert.True(float.IsPositiveInfinity(target.Z), "Vector4f.constructor (float, float, float, float) did not return the expected value."); - Assert.True(float.Equals(float.Epsilon, target.W), "Vector4f.constructor (float, float, float, float) did not return the expected value."); + Assert.True(Single.IsNaN(target.X), "Vector4f.constructor (Single, Single, Single, Single) did not return the expected value."); + Assert.True(Single.Equals(Single.MaxValue, target.Y), "Vector4f.constructor (Single, Single, Single, Single) did not return the expected value."); + Assert.True(Single.IsPositiveInfinity(target.Z), "Vector4f.constructor (Single, Single, Single, Single) did not return the expected value."); + Assert.True(Single.Equals(Single.Epsilon, target.W), "Vector4f.constructor (Single, Single, Single, Single) did not return the expected value."); } // A test for Add (Vector4f, Vector4f) @@ -1069,12 +1066,12 @@ public void Vector4AddTest() Assert.Equal(expected, actual); } - // A test for Divide (Vector4f, float) + // A test for Divide (Vector4f, Single) [Fact] public void Vector4DivideTest() { Vector4 a = new Vector4(1.0f, 2.0f, 3.0f, 4.0f); - float div = 2.0f; + Single div = 2.0f; Vector4 expected = new Vector4(0.5f, 1.0f, 1.5f, 2.0f); Vector4 actual; actual = Vector4.Divide(a, div); @@ -1129,23 +1126,23 @@ public void Vector4EqualsTest() Assert.Equal(expected, actual); } - // A test for Multiply (float, Vector4f) + // A test for Multiply (Single, Vector4f) [Fact] public void Vector4MultiplyTest() { Vector4 a = new Vector4(1.0f, 2.0f, 3.0f, 4.0f); - const float factor = 2.0f; + const Single factor = 2.0f; Vector4 expected = new Vector4(2.0f, 4.0f, 6.0f, 8.0f); Vector4 actual = Vector4.Multiply(factor, a); Assert.Equal(expected, actual); } - // A test for Multiply (Vector4f, float) + // A test for Multiply (Vector4f, Single) [Fact] public void Vector4MultiplyTest2() { Vector4 a = new Vector4(1.0f, 2.0f, 3.0f, 4.0f); - const float factor = 2.0f; + const Single factor = 2.0f; Vector4 expected = new Vector4(2.0f, 4.0f, 6.0f, 8.0f); Vector4 actual = Vector4.Multiply(a, factor); Assert.Equal(expected, actual); @@ -1294,11 +1291,11 @@ public void Vector4EqualsTest1() Assert.False(a.Equals(b)); } - // A test for Vector4f (float) + // A test for Vector4f (Single) [Fact] public void Vector4ConstructorTest6() { - float value = 1.0f; + Single value = 1.0f; Vector4 target = new Vector4(value); Vector4 expected = new Vector4(value, value, value, value); @@ -1314,10 +1311,10 @@ public void Vector4ConstructorTest6() [Fact] public void Vector4EqualsNanTest() { - Vector4 a = new Vector4(float.NaN, 0, 0, 0); - Vector4 b = new Vector4(0, float.NaN, 0, 0); - Vector4 c = new Vector4(0, 0, float.NaN, 0); - Vector4 d = new Vector4(0, 0, 0, float.NaN); + Vector4 a = new Vector4(Single.NaN, 0, 0, 0); + Vector4 b = new Vector4(0, Single.NaN, 0, 0); + Vector4 c = new Vector4(0, 0, Single.NaN, 0); + Vector4 d = new Vector4(0, 0, 0, Single.NaN); Assert.False(a == Vector4.Zero); Assert.False(b == Vector4.Zero); @@ -1345,16 +1342,16 @@ public void Vector4EqualsNanTest() public void Vector4AbsTest() { Vector4 v1 = new Vector4(-2.5f, 2.0f, 3.0f, 3.3f); - Vector4 v3 = Vector4.Abs(new Vector4(float.PositiveInfinity, 0.0f, float.NegativeInfinity, float.NaN)); + Vector4 v3 = Vector4.Abs(new Vector4(Single.PositiveInfinity, 0.0f, Single.NegativeInfinity, Single.NaN)); Vector4 v = Vector4.Abs(v1); Assert.Equal(2.5f, v.X); Assert.Equal(2.0f, v.Y); Assert.Equal(3.0f, v.Z); Assert.Equal(3.3f, v.W); - Assert.Equal(float.PositiveInfinity, v3.X); + Assert.Equal(Single.PositiveInfinity, v3.X); Assert.Equal(0.0f, v3.Y); - Assert.Equal(float.PositiveInfinity, v3.Z); - Assert.Equal(float.NaN, v3.W); + Assert.Equal(Single.PositiveInfinity, v3.Z); + Assert.Equal(Single.NaN, v3.W); } [Fact] @@ -1366,7 +1363,7 @@ public void Vector4SqrtTest() Assert.Equal(2, (int)Vector4.SquareRoot(v2).Y); Assert.Equal(2, (int)Vector4.SquareRoot(v2).Z); Assert.Equal(2, (int)Vector4.SquareRoot(v2).W); - Assert.Equal(float.NaN, Vector4.SquareRoot(v1).X); + Assert.Equal(Single.NaN, Vector4.SquareRoot(v1).X); } // A test to make sure these types are blittable directly into GPU buffer memory layouts @@ -1375,8 +1372,8 @@ public unsafe void Vector4SizeofTest() { Assert.Equal(16, sizeof(Vector4)); Assert.Equal(32, sizeof(Vector4_2x)); - Assert.Equal(20, sizeof(Vector4PlusFloat)); - Assert.Equal(40, sizeof(Vector4PlusFloat_2x)); + Assert.Equal(20, sizeof(Vector4PlusSingle)); + Assert.Equal(40, sizeof(Vector4PlusSingle_2x)); } [StructLayout(LayoutKind.Sequential)] @@ -1387,199 +1384,17 @@ struct Vector4_2x } [StructLayout(LayoutKind.Sequential)] - struct Vector4PlusFloat + struct Vector4PlusSingle { private Vector4 _v; - private float _f; + private Single _f; } [StructLayout(LayoutKind.Sequential)] - struct Vector4PlusFloat_2x - { - private Vector4PlusFloat _a; - private Vector4PlusFloat _b; - } - - [Fact] - public void SetFieldsTest() - { - Vector4 v3 = new Vector4(4f, 5f, 6f, 7f); - v3.X = 1.0f; - v3.Y = 2.0f; - v3.Z = 3.0f; - v3.W = 4.0f; - Assert.Equal(1.0f, v3.X); - Assert.Equal(2.0f, v3.Y); - Assert.Equal(3.0f, v3.Z); - Assert.Equal(4.0f, v3.W); - Vector4 v4 = v3; - v4.Y = 0.5f; - v4.Z = 2.2f; - v4.W = 3.5f; - Assert.Equal(1.0f, v4.X); - Assert.Equal(0.5f, v4.Y); - Assert.Equal(2.2f, v4.Z); - Assert.Equal(3.5f, v4.W); - Assert.Equal(2.0f, v3.Y); - } - - [Fact] - public void EmbeddedVectorSetFields() - { - EmbeddedVectorObject evo = new EmbeddedVectorObject(); - evo.FieldVector.X = 5.0f; - evo.FieldVector.Y = 5.0f; - evo.FieldVector.Z = 5.0f; - evo.FieldVector.W = 5.0f; - Assert.Equal(5.0f, evo.FieldVector.X); - Assert.Equal(5.0f, evo.FieldVector.Y); - Assert.Equal(5.0f, evo.FieldVector.Z); - Assert.Equal(5.0f, evo.FieldVector.W); - } - - [Fact] - public void DeeplyEmbeddedObjectTest() - { - DeeplyEmbeddedClass obj = new DeeplyEmbeddedClass(); - obj.L0.L1.L2.L3.L4.L5.L6.L7.EmbeddedVector.X = 5f; - Assert.Equal(5f, obj.RootEmbeddedObject.X); - Assert.Equal(5f, obj.RootEmbeddedObject.Y); - Assert.Equal(1f, obj.RootEmbeddedObject.Z); - Assert.Equal(-5f, obj.RootEmbeddedObject.W); - obj.L0.L1.L2.L3.L4.L5.L6.L7.EmbeddedVector = new Vector4(1, 2, 3, 4); - Assert.Equal(1f, obj.RootEmbeddedObject.X); - Assert.Equal(2f, obj.RootEmbeddedObject.Y); - Assert.Equal(3f, obj.RootEmbeddedObject.Z); - Assert.Equal(4f, obj.RootEmbeddedObject.W); - } - - [Fact] - public void DeeplyEmbeddedStructTest() - { - DeeplyEmbeddedStruct obj = DeeplyEmbeddedStruct.Create(); - obj.L0.L1.L2.L3.L4.L5.L6.L7.EmbeddedVector.X = 5f; - Assert.Equal(5f, obj.RootEmbeddedObject.X); - Assert.Equal(5f, obj.RootEmbeddedObject.Y); - Assert.Equal(1f, obj.RootEmbeddedObject.Z); - Assert.Equal(-5f, obj.RootEmbeddedObject.W); - obj.L0.L1.L2.L3.L4.L5.L6.L7.EmbeddedVector = new Vector4(1, 2, 3, 4); - Assert.Equal(1f, obj.RootEmbeddedObject.X); - Assert.Equal(2f, obj.RootEmbeddedObject.Y); - Assert.Equal(3f, obj.RootEmbeddedObject.Z); - Assert.Equal(4f, obj.RootEmbeddedObject.W); - } - - private class EmbeddedVectorObject - { - public Vector4 FieldVector; - } - - private class DeeplyEmbeddedClass - { - public readonly Level0 L0 = new Level0(); - public Vector4 RootEmbeddedObject { get { return L0.L1.L2.L3.L4.L5.L6.L7.EmbeddedVector; } } - public class Level0 - { - public readonly Level1 L1 = new Level1(); - public class Level1 - { - public readonly Level2 L2 = new Level2(); - public class Level2 - { - public readonly Level3 L3 = new Level3(); - public class Level3 - { - public readonly Level4 L4 = new Level4(); - public class Level4 - { - public readonly Level5 L5 = new Level5(); - public class Level5 - { - public readonly Level6 L6 = new Level6(); - public class Level6 - { - public readonly Level7 L7 = new Level7(); - public class Level7 - { - public Vector4 EmbeddedVector = new Vector4(1, 5, 1, -5); - } - } - } - } - } - } - } - } - } - - // Contrived test for strangely-sized and shaped embedded structures, with unused buffer fields. -#pragma warning disable 0169 - private struct DeeplyEmbeddedStruct - { - public static DeeplyEmbeddedStruct Create() - { - var obj = new DeeplyEmbeddedStruct(); - obj.L0 = new Level0(); - obj.L0.L1 = new Level0.Level1(); - obj.L0.L1.L2 = new Level0.Level1.Level2(); - obj.L0.L1.L2.L3 = new Level0.Level1.Level2.Level3(); - obj.L0.L1.L2.L3.L4 = new Level0.Level1.Level2.Level3.Level4(); - obj.L0.L1.L2.L3.L4.L5 = new Level0.Level1.Level2.Level3.Level4.Level5(); - obj.L0.L1.L2.L3.L4.L5.L6 = new Level0.Level1.Level2.Level3.Level4.Level5.Level6(); - obj.L0.L1.L2.L3.L4.L5.L6.L7 = new Level0.Level1.Level2.Level3.Level4.Level5.Level6.Level7(); - obj.L0.L1.L2.L3.L4.L5.L6.L7.EmbeddedVector = new Vector4(1, 5, 1, -5); - - return obj; - } - - public Level0 L0; - public Vector4 RootEmbeddedObject { get { return L0.L1.L2.L3.L4.L5.L6.L7.EmbeddedVector; } } - public struct Level0 - { - private float _buffer0, _buffer1; - public Level1 L1; - private float _buffer2; - public struct Level1 - { - private float _buffer0, _buffer1; - public Level2 L2; - private byte _buffer2; - public struct Level2 - { - public Level3 L3; - private float _buffer0; - private byte _buffer1; - public struct Level3 - { - public Level4 L4; - public struct Level4 - { - private float _buffer0; - public Level5 L5; - private long _buffer1; - private byte _buffer2; - private double _buffer3; - public struct Level5 - { - private byte _buffer0; - public Level6 L6; - public struct Level6 - { - private byte _buffer0; - public Level7 L7; - private byte _buffer1, _buffer2; - public struct Level7 - { - public Vector4 EmbeddedVector; - } - } - } - } - } - } - } - } - } -#pragma warning restore 0169 + struct Vector4PlusSingle_2x + { + private Vector4PlusSingle _a; + private Vector4PlusSingle _b; + } } -} +} \ No newline at end of file diff --git a/src/libraries/System.Numerics.Vectors/tests/Vector4Tests_NonGeneric.cs b/src/libraries/System.Numerics.Vectors/tests/Vector4Tests_NonGeneric.cs new file mode 100644 index 00000000000000..69f8bf04c42b12 --- /dev/null +++ b/src/libraries/System.Numerics.Vectors/tests/Vector4Tests_NonGeneric.cs @@ -0,0 +1,202 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Globalization; +using System.Runtime.InteropServices; +using Xunit; + +namespace System.Numerics.Tests +{ + public partial class Vector4Tests + { + [Fact] + public void Vector4MarshalSizeTest() + { + Assert.Equal(16, Marshal.SizeOf()); + Assert.Equal(16, Marshal.SizeOf(new Vector4())); + } + + [Fact] + public void SetFieldsTest() + { + Vector4 v3 = new Vector4(4f, 5f, 6f, 7f); + v3.X = 1.0f; + v3.Y = 2.0f; + v3.Z = 3.0f; + v3.W = 4.0f; + Assert.Equal(1.0f, v3.X); + Assert.Equal(2.0f, v3.Y); + Assert.Equal(3.0f, v3.Z); + Assert.Equal(4.0f, v3.W); + Vector4 v4 = v3; + v4.Y = 0.5f; + v4.Z = 2.2f; + v4.W = 3.5f; + Assert.Equal(1.0f, v4.X); + Assert.Equal(0.5f, v4.Y); + Assert.Equal(2.2f, v4.Z); + Assert.Equal(3.5f, v4.W); + Assert.Equal(2.0f, v3.Y); + } + + [Fact] + public void EmbeddedVectorSetFields() + { + EmbeddedVectorObject evo = new EmbeddedVectorObject(); + evo.FieldVector.X = 5.0f; + evo.FieldVector.Y = 5.0f; + evo.FieldVector.Z = 5.0f; + evo.FieldVector.W = 5.0f; + Assert.Equal(5.0f, evo.FieldVector.X); + Assert.Equal(5.0f, evo.FieldVector.Y); + Assert.Equal(5.0f, evo.FieldVector.Z); + Assert.Equal(5.0f, evo.FieldVector.W); + } + + [Fact] + public void DeeplyEmbeddedObjectTest() + { + DeeplyEmbeddedClass obj = new DeeplyEmbeddedClass(); + obj.L0.L1.L2.L3.L4.L5.L6.L7.EmbeddedVector.X = 5f; + Assert.Equal(5f, obj.RootEmbeddedObject.X); + Assert.Equal(5f, obj.RootEmbeddedObject.Y); + Assert.Equal(1f, obj.RootEmbeddedObject.Z); + Assert.Equal(-5f, obj.RootEmbeddedObject.W); + obj.L0.L1.L2.L3.L4.L5.L6.L7.EmbeddedVector = new Vector4(1, 2, 3, 4); + Assert.Equal(1f, obj.RootEmbeddedObject.X); + Assert.Equal(2f, obj.RootEmbeddedObject.Y); + Assert.Equal(3f, obj.RootEmbeddedObject.Z); + Assert.Equal(4f, obj.RootEmbeddedObject.W); + } + + [Fact] + public void DeeplyEmbeddedStructTest() + { + DeeplyEmbeddedStruct obj = DeeplyEmbeddedStruct.Create(); + obj.L0.L1.L2.L3.L4.L5.L6.L7.EmbeddedVector.X = 5f; + Assert.Equal(5f, obj.RootEmbeddedObject.X); + Assert.Equal(5f, obj.RootEmbeddedObject.Y); + Assert.Equal(1f, obj.RootEmbeddedObject.Z); + Assert.Equal(-5f, obj.RootEmbeddedObject.W); + obj.L0.L1.L2.L3.L4.L5.L6.L7.EmbeddedVector = new Vector4(1, 2, 3, 4); + Assert.Equal(1f, obj.RootEmbeddedObject.X); + Assert.Equal(2f, obj.RootEmbeddedObject.Y); + Assert.Equal(3f, obj.RootEmbeddedObject.Z); + Assert.Equal(4f, obj.RootEmbeddedObject.W); + } + + private class EmbeddedVectorObject + { + public Vector4 FieldVector; + } + + private class DeeplyEmbeddedClass + { + public readonly Level0 L0 = new Level0(); + public Vector4 RootEmbeddedObject { get { return L0.L1.L2.L3.L4.L5.L6.L7.EmbeddedVector; } } + public class Level0 + { + public readonly Level1 L1 = new Level1(); + public class Level1 + { + public readonly Level2 L2 = new Level2(); + public class Level2 + { + public readonly Level3 L3 = new Level3(); + public class Level3 + { + public readonly Level4 L4 = new Level4(); + public class Level4 + { + public readonly Level5 L5 = new Level5(); + public class Level5 + { + public readonly Level6 L6 = new Level6(); + public class Level6 + { + public readonly Level7 L7 = new Level7(); + public class Level7 + { + public Vector4 EmbeddedVector = new Vector4(1, 5, 1, -5); + } + } + } + } + } + } + } + } + } + + // Contrived test for strangely-sized and shaped embedded structures, with unused buffer fields. +#pragma warning disable 0169 + private struct DeeplyEmbeddedStruct + { + public static DeeplyEmbeddedStruct Create() + { + var obj = new DeeplyEmbeddedStruct(); + obj.L0 = new Level0(); + obj.L0.L1 = new Level0.Level1(); + obj.L0.L1.L2 = new Level0.Level1.Level2(); + obj.L0.L1.L2.L3 = new Level0.Level1.Level2.Level3(); + obj.L0.L1.L2.L3.L4 = new Level0.Level1.Level2.Level3.Level4(); + obj.L0.L1.L2.L3.L4.L5 = new Level0.Level1.Level2.Level3.Level4.Level5(); + obj.L0.L1.L2.L3.L4.L5.L6 = new Level0.Level1.Level2.Level3.Level4.Level5.Level6(); + obj.L0.L1.L2.L3.L4.L5.L6.L7 = new Level0.Level1.Level2.Level3.Level4.Level5.Level6.Level7(); + obj.L0.L1.L2.L3.L4.L5.L6.L7.EmbeddedVector = new Vector4(1, 5, 1, -5); + + return obj; + } + + public Level0 L0; + public Vector4 RootEmbeddedObject { get { return L0.L1.L2.L3.L4.L5.L6.L7.EmbeddedVector; } } + public struct Level0 + { + private float _buffer0, _buffer1; + public Level1 L1; + private float _buffer2; + public struct Level1 + { + private float _buffer0, _buffer1; + public Level2 L2; + private byte _buffer2; + public struct Level2 + { + public Level3 L3; + private float _buffer0; + private byte _buffer1; + public struct Level3 + { + public Level4 L4; + public struct Level4 + { + private float _buffer0; + public Level5 L5; + private long _buffer1; + private byte _buffer2; + private double _buffer3; + public struct Level5 + { + private byte _buffer0; + public Level6 L6; + public struct Level6 + { + private byte _buffer0; + public Level7 L7; + private byte _buffer1, _buffer2; + public struct Level7 + { + public Vector4 EmbeddedVector; + } + } + } + } + } + } + } + } + } +#pragma warning restore 0169 + } +} diff --git a/src/libraries/System.Numerics.Vectors/tests/Vector4_DoubleTests.cs b/src/libraries/System.Numerics.Vectors/tests/Vector4_DoubleTests.cs new file mode 100644 index 00000000000000..db2e0aaab5009f --- /dev/null +++ b/src/libraries/System.Numerics.Vectors/tests/Vector4_DoubleTests.cs @@ -0,0 +1,1577 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Globalization; +using System.Runtime.InteropServices; +using Xunit; + +namespace System.Numerics.Tests +{ + public partial class Vector4DoubleDoubleTests + { + [Fact] + public void Vector4DoubleCopyToTest() + { + Vector4 v1 = new Vector4(2.5d, 2.0d, 3.0d, 3.3d); + + var a = new Double[5]; + var b = new Double[4]; + + Assert.Throws(() => v1.CopyTo(a, -1)); + Assert.Throws(() => v1.CopyTo(a, a.Length)); + + v1.CopyTo(a, 1); + v1.CopyTo(b); + Assert.Equal(0.0d, a[0]); + Assert.Equal(2.5d, a[1]); + Assert.Equal(2.0d, a[2]); + Assert.Equal(3.0d, a[3]); + Assert.Equal(3.3d, a[4]); + Assert.Equal(2.5d, b[0]); + Assert.Equal(2.0d, b[1]); + Assert.Equal(3.0d, b[2]); + Assert.Equal(3.3d, b[3]); + } + + [Fact] + public void Vector4DoubleGetHashCodeTest() + { + Vector4 v1 = new Vector4(2.5d, 2.0d, 3.0d, 3.3d); + Vector4 v2 = new Vector4(2.5d, 2.0d, 3.0d, 3.3d); + Vector4 v3 = new Vector4(2.5d, 2.0d, 3.0d, 3.3d); + Vector4 v5 = new Vector4(3.3d, 3.0d, 2.0d, 2.5d); + Assert.Equal(v1.GetHashCode(), v1.GetHashCode()); + Assert.Equal(v1.GetHashCode(), v2.GetHashCode()); + Assert.NotEqual(v1.GetHashCode(), v5.GetHashCode()); + Assert.Equal(v1.GetHashCode(), v3.GetHashCode()); + Vector4 v4 = new Vector4(0.0d, 0.0d, 0.0d, 0.0d); + Vector4 v6 = new Vector4(1.0d, 0.0d, 0.0d, 0.0d); + Vector4 v7 = new Vector4(0.0d, 1.0d, 0.0d, 0.0d); + Vector4 v8 = new Vector4(1.0d, 1.0d, 1.0d, 1.0d); + Vector4 v9 = new Vector4(1.0d, 1.0d, 0.0d, 0.0d); + Assert.NotEqual(v4.GetHashCode(), v6.GetHashCode()); + Assert.NotEqual(v4.GetHashCode(), v7.GetHashCode()); + Assert.NotEqual(v4.GetHashCode(), v8.GetHashCode()); + Assert.NotEqual(v7.GetHashCode(), v6.GetHashCode()); + Assert.NotEqual(v8.GetHashCode(), v6.GetHashCode()); + Assert.NotEqual(v8.GetHashCode(), v7.GetHashCode()); + Assert.NotEqual(v9.GetHashCode(), v7.GetHashCode()); + } + + [Fact] + public void Vector4DoubleToStringTest() + { + string separator = CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator; + CultureInfo enUsCultureInfo = new CultureInfo("en-US"); + + Vector4 v1 = new Vector4(2.5d, 2.0d, 3.0d, 3.3d); + + string v1str = v1.ToString(); + string expectedv1 = string.Format(CultureInfo.CurrentCulture + , "<{1:G}{0} {2:G}{0} {3:G}{0} {4:G}>" + , separator, 2.5, 2, 3, 3.3); + Assert.Equal(expectedv1, v1str); + + string v1strformatted = v1.ToString("c", CultureInfo.CurrentCulture); + string expectedv1dormatted = string.Format(CultureInfo.CurrentCulture + , "<{1:c}{0} {2:c}{0} {3:c}{0} {4:c}>" + , separator, 2.5, 2, 3, 3.3); + Assert.Equal(expectedv1dormatted, v1strformatted); + + string v2strformatted = v1.ToString("c", enUsCultureInfo); + string expectedv2dormatted = string.Format(enUsCultureInfo + , "<{1:c}{0} {2:c}{0} {3:c}{0} {4:c}>" + , enUsCultureInfo.NumberFormat.NumberGroupSeparator, 2.5, 2, 3, 3.3); + Assert.Equal(expectedv2dormatted, v2strformatted); + + string v3strformatted = v1.ToString("c"); + string expectedv3dormatted = string.Format(CultureInfo.CurrentCulture + , "<{1:c}{0} {2:c}{0} {3:c}{0} {4:c}>" + , separator, 2.5, 2, 3, 3.3); + Assert.Equal(expectedv3dormatted, v3strformatted); + } + + // A test for DistanceSquared (Vector4d, Vector4d) + [Fact] + public void Vector4DoubleDistanceSquaredTest() + { + Vector4 a = new Vector4(1.0d, 2.0d, 3.0d, 4.0d); + Vector4 b = new Vector4(5.0d, 6.0d, 7.0d, 8.0d); + + Double expected = 64.0d; + Double actual; + + actual = Vector4.DistanceSquared(a, b); + Assert.True(MathHelper.EqualScalar(expected, actual), "Vector4d.DistanceSquared did not return the expected value."); + } + + // A test for Distance (Vector4d, Vector4d) + [Fact] + public void Vector4DoubleDistanceTest() + { + Vector4 a = new Vector4(1.0d, 2.0d, 3.0d, 4.0d); + Vector4 b = new Vector4(5.0d, 6.0d, 7.0d, 8.0d); + + Double expected = 8.0d; + Double actual; + + actual = Vector4.Distance(a, b); + Assert.True(MathHelper.EqualScalar(expected, actual), "Vector4d.Distance did not return the expected value."); + } + + // A test for Distance (Vector4d, Vector4d) + // Distance from the same point + [Fact] + public void Vector4DoubleDistanceTest1() + { + Vector4 a = new Vector4(new Vector2(1.051d, 2.05d), 3.478d, 1.0d); + Vector4 b = new Vector4(new Vector3(1.051d, 2.05d, 3.478d), 0.0d); + b.W = 1.0d; + + Double actual = Vector4.Distance(a, b); + Assert.Equal(0.0d, actual); + } + + // A test for Dot (Vector4d, Vector4d) + [Fact] + public void Vector4DoubleDotTest() + { + Vector4 a = new Vector4(1.0d, 2.0d, 3.0d, 4.0d); + Vector4 b = new Vector4(5.0d, 6.0d, 7.0d, 8.0d); + + Double expected = 70.0d; + Double actual; + + actual = Vector4.Dot(a, b); + Assert.True(MathHelper.EqualScalar(expected, actual), "Vector4d.Dot did not return the expected value."); + } + + // A test for Dot (Vector4d, Vector4d) + // Dot test for perpendicular vector + [Fact] + public void Vector4DoubleDotTest1() + { + Vector3 a = new Vector3(1.55d, 1.55d, 1); + Vector3 b = new Vector3(2.5d, 3, 1.5d); + Vector3 c = Vector3.Cross(a, b); + + Vector4 d = new Vector4(a, 0); + Vector4 e = new Vector4(c, 0); + + Double actual = Vector4.Dot(d, e); + Assert.True(MathHelper.EqualScalar(0.0d, actual), "Vector4d.Dot did not return the expected value."); + } + + // A test for Length () + [Fact] + public void Vector4DoubleLengthTest() + { + Vector3 a = new Vector3(1.0d, 2.0d, 3.0d); + Double w = 4.0d; + + Vector4 target = new Vector4(a, w); + + Double expected = (Double)System.Math.Sqrt(30.0d); + Double actual; + + actual = target.Length(); + + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.Length did not return the expected value."); + } + + // A test for Length () + // Length test where length is zero + [Fact] + public void Vector4DoubleLengthTest1() + { + Vector4 target = new Vector4(); + + Double expected = 0.0d; + Double actual = target.Length(); + + Assert.True(MathHelper.EqualScalar(expected, actual), "Vector4d.Length did not return the expected value."); + } + + // A test for LengthSquared () + [Fact] + public void Vector4DoubleLengthSquaredTest() + { + Vector3 a = new Vector3(1.0d, 2.0d, 3.0d); + Double w = 4.0d; + + Vector4 target = new Vector4(a, w); + + Double expected = 30; + Double actual; + + actual = target.LengthSquared(); + + Assert.True(MathHelper.EqualScalar(expected, actual), "Vector4d.LengthSquared did not return the expected value."); + } + + // A test for Min (Vector4d, Vector4d) + [Fact] + public void Vector4DoubleMinTest() + { + Vector4 a = new Vector4(-1.0d, 4.0d, -3.0d, 1000.0d); + Vector4 b = new Vector4(2.0d, 1.0d, -1.0d, 0.0d); + + Vector4 expected = new Vector4(-1.0d, 1.0d, -3.0d, 0.0d); + Vector4 actual; + actual = Vector4.Min(a, b); + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.Min did not return the expected value."); + } + + // A test for Max (Vector4d, Vector4d) + [Fact] + public void Vector4DoubleMaxTest() + { + Vector4 a = new Vector4(-1.0d, 4.0d, -3.0d, 1000.0d); + Vector4 b = new Vector4(2.0d, 1.0d, -1.0d, 0.0d); + + Vector4 expected = new Vector4(2.0d, 4.0d, -1.0d, 1000.0d); + Vector4 actual; + actual = Vector4.Max(a, b); + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.Max did not return the expected value."); + } + + [Fact] + public void Vector4DoubleMinMaxCodeCoverageTest() + { + Vector4 min = Vector4.Zero; + Vector4 max = Vector4.One; + Vector4 actual; + + // Min. + actual = Vector4.Min(min, max); + Assert.Equal(actual, min); + + actual = Vector4.Min(max, min); + Assert.Equal(actual, min); + + // Max. + actual = Vector4.Max(min, max); + Assert.Equal(actual, max); + + actual = Vector4.Max(max, min); + Assert.Equal(actual, max); + } + + // A test for Clamp (Vector4d, Vector4d, Vector4d) + [Fact] + public void Vector4DoubleClampTest() + { + Vector4 a = new Vector4(0.5d, 0.3d, 0.33d, 0.44d); + Vector4 min = new Vector4(0.0d, 0.1d, 0.13d, 0.14d); + Vector4 max = new Vector4(1.0d, 1.1d, 1.13d, 1.14d); + + // Normal case. + // Case N1: specified value is in the range. + Vector4 expected = new Vector4(0.5d, 0.3d, 0.33d, 0.44d); + Vector4 actual = Vector4.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.Clamp did not return the expected value."); + + // Normal case. + // Case N2: specified value is bigger than max value. + a = new Vector4(2.0d, 3.0d, 4.0d, 5.0d); + expected = max; + actual = Vector4.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.Clamp did not return the expected value."); + + // Case N3: specified value is smaller than max value. + a = new Vector4(-2.0d, -3.0d, -4.0d, -5.0d); + expected = min; + actual = Vector4.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.Clamp did not return the expected value."); + + // Case N4: combination case. + a = new Vector4(-2.0d, 0.5d, 4.0d, -5.0d); + expected = new Vector4(min.X, a.Y, max.Z, min.W); + actual = Vector4.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.Clamp did not return the expected value."); + + // User specified min value is bigger than max value. + max = new Vector4(0.0d, 0.1d, 0.13d, 0.14d); + min = new Vector4(1.0d, 1.1d, 1.13d, 1.14d); + + // Case W1: specified value is in the range. + a = new Vector4(0.5d, 0.3d, 0.33d, 0.44d); + expected = max; + actual = Vector4.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.Clamp did not return the expected value."); + + // Normal case. + // Case W2: specified value is bigger than max and min value. + a = new Vector4(2.0d, 3.0d, 4.0d, 5.0d); + expected = max; + actual = Vector4.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.Clamp did not return the expected value."); + + // Case W3: specified value is smaller than min and max value. + a = new Vector4(-2.0d, -3.0d, -4.0d, -5.0d); + expected = max; + actual = Vector4.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.Clamp did not return the expected value."); + } + + // A test for Lerp (Vector4d, Vector4d, Double) + [Fact] + public void Vector4DoubleLerpTest() + { + Vector4 a = new Vector4(1.0d, 2.0d, 3.0d, 4.0d); + Vector4 b = new Vector4(5.0d, 6.0d, 7.0d, 8.0d); + + Double t = 0.5d; + + Vector4 expected = new Vector4(3.0d, 4.0d, 5.0d, 6.0d); + Vector4 actual; + + actual = Vector4.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector4d, Vector4d, Double) + // Lerp test with factor zero + [Fact] + public void Vector4DoubleLerpTest1() + { + Vector4 a = new Vector4(new Vector3(1.0d, 2.0d, 3.0d), 4.0d); + Vector4 b = new Vector4(4.0d, 5.0d, 6.0d, 7.0d); + + Double t = 0.0d; + Vector4 expected = new Vector4(1.0d, 2.0d, 3.0d, 4.0d); + Vector4 actual = Vector4.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector4d, Vector4d, Double) + // Lerp test with factor one + [Fact] + public void Vector4DoubleLerpTest2() + { + Vector4 a = new Vector4(new Vector3(1.0d, 2.0d, 3.0d), 4.0d); + Vector4 b = new Vector4(4.0d, 5.0d, 6.0d, 7.0d); + + Double t = 1.0d; + Vector4 expected = new Vector4(4.0d, 5.0d, 6.0d, 7.0d); + Vector4 actual = Vector4.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector4d, Vector4d, Double) + // Lerp test with factor > 1 + [Fact] + public void Vector4DoubleLerpTest3() + { + Vector4 a = new Vector4(new Vector3(0.0d, 0.0d, 0.0d), 0.0d); + Vector4 b = new Vector4(4.0d, 5.0d, 6.0d, 7.0d); + + Double t = 2.0d; + Vector4 expected = new Vector4(8.0d, 10.0d, 12.0d, 14.0d); + Vector4 actual = Vector4.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector4d, Vector4d, Double) + // Lerp test with factor < 0 + [Fact] + public void Vector4DoubleLerpTest4() + { + Vector4 a = new Vector4(new Vector3(0.0d, 0.0d, 0.0d), 0.0d); + Vector4 b = new Vector4(4.0d, 5.0d, 6.0d, 7.0d); + + Double t = -2.0d; + Vector4 expected = -(b * 2); + Vector4 actual = Vector4.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector4d, Vector4d, Double) + // Lerp test from the same point + [Fact] + public void Vector4DoubleLerpTest5() + { + Vector4 a = new Vector4(4.0d, 5.0d, 6.0d, 7.0d); + Vector4 b = new Vector4(4.0d, 5.0d, 6.0d, 7.0d); + + Double t = 0.85d; + Vector4 expected = a; + Vector4 actual = Vector4.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.Lerp did not return the expected value."); + } + + // A test for Transform (Vector2d, Matrix4x4) + [Fact] + public void Vector4DoubleTransformTest1() + { + Vector2 v = new Vector2(1.0d, 2.0d); + + Matrix4x4 m = + Matrix4x4.CreateRotationX(MathHelper.ToRadians(30.0d)) * + Matrix4x4.CreateRotationY(MathHelper.ToRadians(30.0d)) * + Matrix4x4.CreateRotationZ(MathHelper.ToRadians(30.0d)); + m.M41 = 10.0d; + m.M42 = 20.0d; + m.M43 = 30.0d; + + Vector4 expected = new Vector4(10.316987d, 22.183012d, 30.3660259d, 1.0d); + Vector4 actual; + + actual = Vector4.Transform(v, m); + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.Transform did not return the expected value."); + } + + // A test for Transform (Vector3d, Matrix4x4) + [Fact] + public void Vector4DoubleTransformTest2() + { + Vector3 v = new Vector3(1.0d, 2.0d, 3.0d); + + Matrix4x4 m = + Matrix4x4.CreateRotationX(MathHelper.ToRadians(30.0d)) * + Matrix4x4.CreateRotationY(MathHelper.ToRadians(30.0d)) * + Matrix4x4.CreateRotationZ(MathHelper.ToRadians(30.0d)); + m.M41 = 10.0d; + m.M42 = 20.0d; + m.M43 = 30.0d; + + Vector4 expected = new Vector4(12.19198728d, 21.53349376d, 32.61602545d, 1.0d); + Vector4 actual; + + actual = Vector4.Transform(v, m); + Assert.True(MathHelper.Equal(expected, actual), "Vector4.Transform did not return the expected value."); + } + + // A test for Transform (Vector4d, Matrix4x4) + [Fact] + public void Vector4DoubleTransformVector4Test() + { + Vector4 v = new Vector4(1.0d, 2.0d, 3.0d, 0.0d); + + Matrix4x4 m = + Matrix4x4.CreateRotationX(MathHelper.ToRadians(30.0d)) * + Matrix4x4.CreateRotationY(MathHelper.ToRadians(30.0d)) * + Matrix4x4.CreateRotationZ(MathHelper.ToRadians(30.0d)); + m.M41 = 10.0d; + m.M42 = 20.0d; + m.M43 = 30.0d; + + Vector4 expected = new Vector4(2.19198728d, 1.53349376d, 2.61602545d, 0.0d); + Vector4 actual; + + actual = Vector4.Transform(v, m); + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.Transform did not return the expected value."); + + // + v.W = 1.0d; + + expected = new Vector4(12.19198728d, 21.53349376d, 32.61602545d, 1.0d); + actual = Vector4.Transform(v, m); + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.Transform did not return the expected value."); + } + + // A test for Transform (Vector4d, Matrix4x4) + // Transform Vector4 with zero matrix + [Fact] + public void Vector4DoubleTransformVector4Test1() + { + Vector4 v = new Vector4(1.0d, 2.0d, 3.0d, 0.0d); + Matrix4x4 m = new Matrix4x4(); + Vector4 expected = new Vector4(0, 0, 0, 0); + + Vector4 actual = Vector4.Transform(v, m); + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.Transform did not return the expected value."); + } + + // A test for Transform (Vector4d, Matrix4x4) + // Transform Vector4 with identity matrix + [Fact] + public void Vector4DoubleTransformVector4Test2() + { + Vector4 v = new Vector4(1.0d, 2.0d, 3.0d, 0.0d); + Matrix4x4 m = Matrix4x4.Identity; + Vector4 expected = new Vector4(1.0d, 2.0d, 3.0d, 0.0d); + + Vector4 actual = Vector4.Transform(v, m); + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.Transform did not return the expected value."); + } + + // A test for Transform (Vector3d, Matrix4x4) + // Transform Vector3d test + [Fact] + public void Vector4DoubleTransformVector3Test() + { + Vector3 v = new Vector3(1.0d, 2.0d, 3.0d); + + Matrix4x4 m = + Matrix4x4.CreateRotationX(MathHelper.ToRadians(30.0d)) * + Matrix4x4.CreateRotationY(MathHelper.ToRadians(30.0d)) * + Matrix4x4.CreateRotationZ(MathHelper.ToRadians(30.0d)); + m.M41 = 10.0d; + m.M42 = 20.0d; + m.M43 = 30.0d; + + Vector4 expected = Vector4.Transform(new Vector4(v, 1.0d), m); + Vector4 actual = Vector4.Transform(v, m); + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.Transform did not return the expected value."); + } + + // A test for Transform (Vector3d, Matrix4x4) + // Transform vector3 with zero matrix + [Fact] + public void Vector4DoubleTransformVector3Test1() + { + Vector3 v = new Vector3(1.0d, 2.0d, 3.0d); + Matrix4x4 m = new Matrix4x4(); + Vector4 expected = new Vector4(0, 0, 0, 0); + + Vector4 actual = Vector4.Transform(v, m); + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.Transform did not return the expected value."); + } + + // A test for Transform (Vector3d, Matrix4x4) + // Transform vector3 with identity matrix + [Fact] + public void Vector4DoubleTransformVector3Test2() + { + Vector3 v = new Vector3(1.0d, 2.0d, 3.0d); + Matrix4x4 m = Matrix4x4.Identity; + Vector4 expected = new Vector4(1.0d, 2.0d, 3.0d, 1.0d); + + Vector4 actual = Vector4.Transform(v, m); + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.Transform did not return the expected value."); + } + + // A test for Transform (Vector2d, Matrix4x4) + // Transform Vector2d test + [Fact] + public void Vector4DoubleTransformVector2Test() + { + Vector2 v = new Vector2(1.0d, 2.0d); + + Matrix4x4 m = + Matrix4x4.CreateRotationX(MathHelper.ToRadians(30.0d)) * + Matrix4x4.CreateRotationY(MathHelper.ToRadians(30.0d)) * + Matrix4x4.CreateRotationZ(MathHelper.ToRadians(30.0d)); + m.M41 = 10.0d; + m.M42 = 20.0d; + m.M43 = 30.0d; + + Vector4 expected = Vector4.Transform(new Vector4(v, 0.0d, 1.0d), m); + Vector4 actual = Vector4.Transform(v, m); + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.Transform did not return the expected value."); + } + + // A test for Transform (Vector2d, Matrix4x4) + // Transform Vector2d with zero matrix + [Fact] + public void Vector4DoubleTransformVector2Test1() + { + Vector2 v = new Vector2(1.0d, 2.0d); + Matrix4x4 m = new Matrix4x4(); + Vector4 expected = new Vector4(0, 0, 0, 0); + + Vector4 actual = Vector4.Transform(v, m); + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.Transform did not return the expected value."); + } + + // A test for Transform (Vector2d, Matrix4x4) + // Transform vector2 with identity matrix + [Fact] + public void Vector4DoubleTransformVector2Test2() + { + Vector2 v = new Vector2(1.0d, 2.0d); + Matrix4x4 m = Matrix4x4.Identity; + Vector4 expected = new Vector4(1.0d, 2.0d, 0, 1.0d); + + Vector4 actual = Vector4.Transform(v, m); + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.Transform did not return the expected value."); + } + + // A test for Transform (Vector2d, Quaternion) + [Fact] + public void Vector4DoubleTransformVector2QuatanionTest() + { + Vector2 v = new Vector2(1.0d, 2.0d); + + Matrix4x4 m = + Matrix4x4.CreateRotationX(MathHelper.ToRadians(30.0d)) * + Matrix4x4.CreateRotationY(MathHelper.ToRadians(30.0d)) * + Matrix4x4.CreateRotationZ(MathHelper.ToRadians(30.0d)); + + Quaternion q = Quaternion.CreateFromRotationMatrix(m); + + Vector4 expected = Vector4.Transform(v, m); + Vector4 actual; + + actual = Vector4.Transform(v, q); + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.Transform did not return the expected value."); + } + + // A test for Transform (Vector3d, Quaternion) + [Fact] + public void Vector4DoubleTransformVector3Quaternion() + { + Vector3 v = new Vector3(1.0d, 2.0d, 3.0d); + + Matrix4x4 m = + Matrix4x4.CreateRotationX(MathHelper.ToRadians(30.0d)) * + Matrix4x4.CreateRotationY(MathHelper.ToRadians(30.0d)) * + Matrix4x4.CreateRotationZ(MathHelper.ToRadians(30.0d)); + Quaternion q = Quaternion.CreateFromRotationMatrix(m); + + Vector4 expected = Vector4.Transform(v, m); + Vector4 actual; + + actual = Vector4.Transform(v, q); + Assert.True(MathHelper.Equal(expected, actual), "Vector4.Transform did not return the expected value."); + } + + // A test for Transform (Vector4d, Quaternion) + [Fact] + public void Vector4DoubleTransformVector4QuaternionTest() + { + Vector4 v = new Vector4(1.0d, 2.0d, 3.0d, 0.0d); + + Matrix4x4 m = + Matrix4x4.CreateRotationX(MathHelper.ToRadians(30.0d)) * + Matrix4x4.CreateRotationY(MathHelper.ToRadians(30.0d)) * + Matrix4x4.CreateRotationZ(MathHelper.ToRadians(30.0d)); + Quaternion q = Quaternion.CreateFromRotationMatrix(m); + + Vector4 expected = Vector4.Transform(v, m); + Vector4 actual; + + actual = Vector4.Transform(v, q); + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.Transform did not return the expected value."); + + // + v.W = 1.0d; + expected.W = 1.0d; + actual = Vector4.Transform(v, q); + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.Transform did not return the expected value."); + } + + // A test for Transform (Vector4d, Quaternion) + // Transform Vector4 with zero quaternion + [Fact] + public void Vector4DoubleTransformVector4QuaternionTest1() + { + Vector4 v = new Vector4(1.0d, 2.0d, 3.0d, 0.0d); + Quaternion q = new Quaternion(); + Vector4 expected = v; + + Vector4 actual = Vector4.Transform(v, q); + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.Transform did not return the expected value."); + } + + // A test for Transform (Vector4d, Quaternion) + // Transform Vector4 with identity matrix + [Fact] + public void Vector4DoubleTransformVector4QuaternionTest2() + { + Vector4 v = new Vector4(1.0d, 2.0d, 3.0d, 0.0d); + Quaternion q = Quaternion.Identity; + Vector4 expected = new Vector4(1.0d, 2.0d, 3.0d, 0.0d); + + Vector4 actual = Vector4.Transform(v, q); + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.Transform did not return the expected value."); + } + + // A test for Transform (Vector3d, Quaternion) + // Transform Vector3d test + [Fact] + public void Vector4DoubleTransformVector3QuaternionTest() + { + Vector3 v = new Vector3(1.0d, 2.0d, 3.0d); + + Matrix4x4 m = + Matrix4x4.CreateRotationX(MathHelper.ToRadians(30.0d)) * + Matrix4x4.CreateRotationY(MathHelper.ToRadians(30.0d)) * + Matrix4x4.CreateRotationZ(MathHelper.ToRadians(30.0d)); + Quaternion q = Quaternion.CreateFromRotationMatrix(m); + + Vector4 expected = Vector4.Transform(v, m); + Vector4 actual = Vector4.Transform(v, q); + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.Transform did not return the expected value."); + } + + // A test for Transform (Vector3d, Quaternion) + // Transform vector3 with zero quaternion + [Fact] + public void Vector4DoubleTransformVector3QuaternionTest1() + { + Vector3 v = new Vector3(1.0d, 2.0d, 3.0d); + Quaternion q = new Quaternion(); + Vector4 expected = new Vector4(v, 1.0d); + + Vector4 actual = Vector4.Transform(v, q); + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.Transform did not return the expected value."); + } + + // A test for Transform (Vector3d, Quaternion) + // Transform vector3 with identity quaternion + [Fact] + public void Vector4DoubleTransformVector3QuaternionTest2() + { + Vector3 v = new Vector3(1.0d, 2.0d, 3.0d); + Quaternion q = Quaternion.Identity; + Vector4 expected = new Vector4(1.0d, 2.0d, 3.0d, 1.0d); + + Vector4 actual = Vector4.Transform(v, q); + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.Transform did not return the expected value."); + } + + // A test for Transform (Vector2d, Quaternion) + // Transform Vector2d by quaternion test + [Fact] + public void Vector4DoubleTransformVector2QuaternionTest() + { + Vector2 v = new Vector2(1.0d, 2.0d); + + Matrix4x4 m = + Matrix4x4.CreateRotationX(MathHelper.ToRadians(30.0d)) * + Matrix4x4.CreateRotationY(MathHelper.ToRadians(30.0d)) * + Matrix4x4.CreateRotationZ(MathHelper.ToRadians(30.0d)); + Quaternion q = Quaternion.CreateFromRotationMatrix(m); + + Vector4 expected = Vector4.Transform(v, m); + Vector4 actual = Vector4.Transform(v, q); + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.Transform did not return the expected value."); + } + + // A test for Transform (Vector2d, Quaternion) + // Transform Vector2d with zero quaternion + [Fact] + public void Vector4DoubleTransformVector2QuaternionTest1() + { + Vector2 v = new Vector2(1.0d, 2.0d); + Quaternion q = new Quaternion(); + Vector4 expected = new Vector4(1.0d, 2.0d, 0, 1.0d); + + Vector4 actual = Vector4.Transform(v, q); + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.Transform did not return the expected value."); + } + + // A test for Transform (Vector2d, Matrix4x4) + // Transform vector2 with identity Quaternion + [Fact] + public void Vector4DoubleTransformVector2QuaternionTest2() + { + Vector2 v = new Vector2(1.0d, 2.0d); + Quaternion q = Quaternion.Identity; + Vector4 expected = new Vector4(1.0d, 2.0d, 0, 1.0d); + + Vector4 actual = Vector4.Transform(v, q); + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.Transform did not return the expected value."); + } + + // A test for Normalize (Vector4d) + [Fact] + public void Vector4DoubleNormalizeTest() + { + Vector4 a = new Vector4(1.0d, 2.0d, 3.0d, 4.0d); + + Vector4 expected = new Vector4( + 0.1825741858350553711523232609336d, + 0.3651483716701107423046465218672d, + 0.5477225575051661134569697828008d, + 0.7302967433402214846092930437344d); + Vector4 actual; + + actual = Vector4.Normalize(a); + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.Normalize did not return the expected value."); + } + + // A test for Normalize (Vector4d) + // Normalize vector of length one + [Fact] + public void Vector4DoubleNormalizeTest1() + { + Vector4 a = new Vector4(1.0d, 0.0d, 0.0d, 0.0d); + + Vector4 expected = new Vector4(1.0d, 0.0d, 0.0d, 0.0d); + Vector4 actual = Vector4.Normalize(a); + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.Normalize did not return the expected value."); + } + + // A test for Normalize (Vector4d) + // Normalize vector of length zero + [Fact] + public void Vector4DoubleNormalizeTest2() + { + Vector4 a = new Vector4(0.0d, 0.0d, 0.0d, 0.0d); + + Vector4 expected = new Vector4(0.0d, 0.0d, 0.0d, 0.0d); + Vector4 actual = Vector4.Normalize(a); + Assert.True(Double.IsNaN(actual.X) && Double.IsNaN(actual.Y) && Double.IsNaN(actual.Z) && Double.IsNaN(actual.W), "Vector4d.Normalize did not return the expected value."); + } + + // A test for operator - (Vector4d) + [Fact] + public void Vector4DoubleUnaryNegationTest() + { + Vector4 a = new Vector4(1.0d, 2.0d, 3.0d, 4.0d); + + Vector4 expected = new Vector4(-1.0d, -2.0d, -3.0d, -4.0d); + Vector4 actual; + + actual = -a; + + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.operator - did not return the expected value."); + } + + // A test for operator - (Vector4d, Vector4d) + [Fact] + public void Vector4DoubleSubtractionTest() + { + Vector4 a = new Vector4(1.0d, 6.0d, 3.0d, 4.0d); + Vector4 b = new Vector4(5.0d, 2.0d, 3.0d, 9.0d); + + Vector4 expected = new Vector4(-4.0d, 4.0d, 0.0d, -5.0d); + Vector4 actual; + + actual = a - b; + + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.operator - did not return the expected value."); + } + + // A test for operator * (Vector4d, Double) + [Fact] + public void Vector4DoubleMultiplyOperatorTest() + { + Vector4 a = new Vector4(1.0d, 2.0d, 3.0d, 4.0d); + + const Double factor = 2.0d; + + Vector4 expected = new Vector4(2.0d, 4.0d, 6.0d, 8.0d); + Vector4 actual; + + actual = a * factor; + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.operator * did not return the expected value."); + } + + // A test for operator * (Double, Vector4d) + [Fact] + public void Vector4DoubleMultiplyOperatorTest2() + { + Vector4 a = new Vector4(1.0d, 2.0d, 3.0d, 4.0d); + + const Double factor = 2.0d; + Vector4 expected = new Vector4(2.0d, 4.0d, 6.0d, 8.0d); + Vector4 actual; + + actual = factor * a; + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.operator * did not return the expected value."); + } + + // A test for operator * (Vector4d, Vector4d) + [Fact] + public void Vector4DoubleMultiplyOperatorTest3() + { + Vector4 a = new Vector4(1.0d, 2.0d, 3.0d, 4.0d); + Vector4 b = new Vector4(5.0d, 6.0d, 7.0d, 8.0d); + + Vector4 expected = new Vector4(5.0d, 12.0d, 21.0d, 32.0d); + Vector4 actual; + + actual = a * b; + + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.operator * did not return the expected value."); + } + + // A test for operator / (Vector4d, Double) + [Fact] + public void Vector4DoubleDivisionTest() + { + Vector4 a = new Vector4(1.0d, 2.0d, 3.0d, 4.0d); + + Double div = 2.0d; + + Vector4 expected = new Vector4(0.5d, 1.0d, 1.5d, 2.0d); + Vector4 actual; + + actual = a / div; + + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.operator / did not return the expected value."); + } + + // A test for operator / (Vector4d, Vector4d) + [Fact] + public void Vector4DoubleDivisionTest1() + { + Vector4 a = new Vector4(1.0d, 6.0d, 7.0d, 4.0d); + Vector4 b = new Vector4(5.0d, 2.0d, 3.0d, 8.0d); + + Vector4 expected = new Vector4(1.0d / 5.0d, 6.0d / 2.0d, 7.0d / 3.0d, 4.0d / 8.0d); + Vector4 actual; + + actual = a / b; + + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.operator / did not return the expected value."); + } + + // A test for operator / (Vector4d, Vector4d) + // Divide by zero + [Fact] + public void Vector4DoubleDivisionTest2() + { + Vector4 a = new Vector4(-2.0d, 3.0d, Double.MaxValue, Double.NaN); + + Double div = 0.0d; + + Vector4 actual = a / div; + + Assert.True(Double.IsNegativeInfinity(actual.X), "Vector4d.operator / did not return the expected value."); + Assert.True(Double.IsPositiveInfinity(actual.Y), "Vector4d.operator / did not return the expected value."); + Assert.True(Double.IsPositiveInfinity(actual.Z), "Vector4d.operator / did not return the expected value."); + Assert.True(Double.IsNaN(actual.W), "Vector4d.operator / did not return the expected value."); + } + + // A test for operator / (Vector4d, Vector4d) + // Divide by zero + [Fact] + public void Vector4DoubleDivisionTest3() + { + Vector4 a = new Vector4(0.047d, -3.0d, Double.NegativeInfinity, Double.MinValue); + Vector4 b = new Vector4(); + + Vector4 actual = a / b; + + Assert.True(Double.IsPositiveInfinity(actual.X), "Vector4d.operator / did not return the expected value."); + Assert.True(Double.IsNegativeInfinity(actual.Y), "Vector4d.operator / did not return the expected value."); + Assert.True(Double.IsNegativeInfinity(actual.Z), "Vector4d.operator / did not return the expected value."); + Assert.True(Double.IsNegativeInfinity(actual.W), "Vector4d.operator / did not return the expected value."); + } + + // A test for operator + (Vector4d, Vector4d) + [Fact] + public void Vector4DoubleAdditionTest() + { + Vector4 a = new Vector4(1.0d, 2.0d, 3.0d, 4.0d); + Vector4 b = new Vector4(5.0d, 6.0d, 7.0d, 8.0d); + + Vector4 expected = new Vector4(6.0d, 8.0d, 10.0d, 12.0d); + Vector4 actual; + + actual = a + b; + + Assert.True(MathHelper.Equal(expected, actual), "Vector4d.operator + did not return the expected value."); + } + + [Fact] + public void OperatorAddTest() + { + Vector4 v1 = new Vector4(2.5d, 2.0d, 3.0d, 3.3d); + Vector4 v2 = new Vector4(5.5d, 4.5d, 6.5d, 7.5d); + + Vector4 v3 = v1 + v2; + Vector4 v5 = new Vector4(-1.0d, 0.0d, 0.0d, Double.NaN); + Vector4 v4 = v1 + v5; + Assert.Equal(8.0d, v3.X); + Assert.Equal(6.5d, v3.Y); + Assert.Equal(9.5d, v3.Z); + Assert.Equal(10.8d, v3.W); + Assert.Equal(1.5d, v4.X); + Assert.Equal(2.0d, v4.Y); + Assert.Equal(3.0d, v4.Z); + Assert.Equal(Double.NaN, v4.W); + } + + // A test for Vector4d (Double, Double, Double, Double) + [Fact] + public void Vector4DoubleConstructorTest() + { + Double x = 1.0d; + Double y = 2.0d; + Double z = 3.0d; + Double w = 4.0d; + + Vector4 target = new Vector4(x, y, z, w); + + Assert.True(MathHelper.Equal(target.X, x) && MathHelper.Equal(target.Y, y) && MathHelper.Equal(target.Z, z) && MathHelper.Equal(target.W, w), + "Vector4d constructor(x,y,z,w) did not return the expected value."); + } + + // A test for Vector4d (Vector2d, Double, Double) + [Fact] + public void Vector4DoubleConstructorTest1() + { + Vector2 a = new Vector2(1.0d, 2.0d); + Double z = 3.0d; + Double w = 4.0d; + + Vector4 target = new Vector4(a, z, w); + Assert.True(MathHelper.Equal(target.X, a.X) && MathHelper.Equal(target.Y, a.Y) && MathHelper.Equal(target.Z, z) && MathHelper.Equal(target.W, w), + "Vector4d constructor(Vector2d,z,w) did not return the expected value."); + } + + // A test for Vector4d (Vector3d, Double) + [Fact] + public void Vector4DoubleConstructorTest2() + { + Vector3 a = new Vector3(1.0d, 2.0d, 3.0d); + Double w = 4.0d; + + Vector4 target = new Vector4(a, w); + + Assert.True(MathHelper.Equal(target.X, a.X) && MathHelper.Equal(target.Y, a.Y) && MathHelper.Equal(target.Z, a.Z) && MathHelper.Equal(target.W, w), + "Vector4d constructor(Vector3d,w) did not return the expected value."); + } + + // A test for Vector4d () + // Constructor with no parameter + [Fact] + public void Vector4DoubleConstructorTest4() + { + Vector4 a = new Vector4(); + + Assert.Equal(0.0d, a.X); + Assert.Equal(0.0d, a.Y); + Assert.Equal(0.0d, a.Z); + Assert.Equal(0.0d, a.W); + } + + // A test for Vector4d () + // Constructor with special Doubleing values + [Fact] + public void Vector4DoubleConstructorTest5() + { + Vector4 target = new Vector4(Double.NaN, Double.MaxValue, Double.PositiveInfinity, Double.Epsilon); + + Assert.True(Double.IsNaN(target.X), "Vector4d.constructor (Double, Double, Double, Double) did not return the expected value."); + Assert.True(Double.Equals(Double.MaxValue, target.Y), "Vector4d.constructor (Double, Double, Double, Double) did not return the expected value."); + Assert.True(Double.IsPositiveInfinity(target.Z), "Vector4d.constructor (Double, Double, Double, Double) did not return the expected value."); + Assert.True(Double.Equals(Double.Epsilon, target.W), "Vector4d.constructor (Double, Double, Double, Double) did not return the expected value."); + } + + // A test for Add (Vector4d, Vector4d) + [Fact] + public void Vector4DoubleAddTest() + { + Vector4 a = new Vector4(1.0d, 2.0d, 3.0d, 4.0d); + Vector4 b = new Vector4(5.0d, 6.0d, 7.0d, 8.0d); + + Vector4 expected = new Vector4(6.0d, 8.0d, 10.0d, 12.0d); + Vector4 actual; + + actual = Vector4.Add(a, b); + Assert.Equal(expected, actual); + } + + // A test for Divide (Vector4d, Double) + [Fact] + public void Vector4DoubleDivideTest() + { + Vector4 a = new Vector4(1.0d, 2.0d, 3.0d, 4.0d); + Double div = 2.0d; + Vector4 expected = new Vector4(0.5d, 1.0d, 1.5d, 2.0d); + Vector4 actual; + actual = Vector4.Divide(a, div); + Assert.Equal(expected, actual); + } + + // A test for Divide (Vector4d, Vector4d) + [Fact] + public void Vector4DoubleDivideTest1() + { + Vector4 a = new Vector4(1.0d, 6.0d, 7.0d, 4.0d); + Vector4 b = new Vector4(5.0d, 2.0d, 3.0d, 8.0d); + + Vector4 expected = new Vector4(1.0d / 5.0d, 6.0d / 2.0d, 7.0d / 3.0d, 4.0d / 8.0d); + Vector4 actual; + + actual = Vector4.Divide(a, b); + Assert.Equal(expected, actual); + } + + // A test for Equals (object) + [Fact] + public void Vector4DoubleEqualsTest() + { + Vector4 a = new Vector4(1.0d, 2.0d, 3.0d, 4.0d); + Vector4 b = new Vector4(1.0d, 2.0d, 3.0d, 4.0d); + + // case 1: compare between same values + object obj = b; + + bool expected = true; + bool actual = a.Equals(obj); + Assert.Equal(expected, actual); + + // case 2: compare between different values + b.X = 10.0d; + obj = b; + expected = false; + actual = a.Equals(obj); + Assert.Equal(expected, actual); + + // case 3: compare between different types. + obj = new Quaternion(); + expected = false; + actual = a.Equals(obj); + Assert.Equal(expected, actual); + + // case 3: compare against null. + obj = null; + expected = false; + actual = a.Equals(obj); + Assert.Equal(expected, actual); + } + + // A test for Multiply (Double, Vector4d) + [Fact] + public void Vector4DoubleMultiplyTest() + { + Vector4 a = new Vector4(1.0d, 2.0d, 3.0d, 4.0d); + const Double factor = 2.0d; + Vector4 expected = new Vector4(2.0d, 4.0d, 6.0d, 8.0d); + Vector4 actual = Vector4.Multiply(factor, a); + Assert.Equal(expected, actual); + } + + // A test for Multiply (Vector4d, Double) + [Fact] + public void Vector4DoubleMultiplyTest2() + { + Vector4 a = new Vector4(1.0d, 2.0d, 3.0d, 4.0d); + const Double factor = 2.0d; + Vector4 expected = new Vector4(2.0d, 4.0d, 6.0d, 8.0d); + Vector4 actual = Vector4.Multiply(a, factor); + Assert.Equal(expected, actual); + } + + // A test for Multiply (Vector4d, Vector4d) + [Fact] + public void Vector4DoubleMultiplyTest3() + { + Vector4 a = new Vector4(1.0d, 2.0d, 3.0d, 4.0d); + Vector4 b = new Vector4(5.0d, 6.0d, 7.0d, 8.0d); + + Vector4 expected = new Vector4(5.0d, 12.0d, 21.0d, 32.0d); + Vector4 actual; + + actual = Vector4.Multiply(a, b); + Assert.Equal(expected, actual); + } + + // A test for Negate (Vector4d) + [Fact] + public void Vector4DoubleNegateTest() + { + Vector4 a = new Vector4(1.0d, 2.0d, 3.0d, 4.0d); + + Vector4 expected = new Vector4(-1.0d, -2.0d, -3.0d, -4.0d); + Vector4 actual; + + actual = Vector4.Negate(a); + Assert.Equal(expected, actual); + } + + // A test for operator != (Vector4d, Vector4d) + [Fact] + public void Vector4DoubleInequalityTest() + { + Vector4 a = new Vector4(1.0d, 2.0d, 3.0d, 4.0d); + Vector4 b = new Vector4(1.0d, 2.0d, 3.0d, 4.0d); + + // case 1: compare between same values + bool expected = false; + bool actual = a != b; + Assert.Equal(expected, actual); + + // case 2: compare between different values + b.X = 10.0d; + expected = true; + actual = a != b; + Assert.Equal(expected, actual); + } + + // A test for operator == (Vector4d, Vector4d) + [Fact] + public void Vector4DoubleEqualityTest() + { + Vector4 a = new Vector4(1.0d, 2.0d, 3.0d, 4.0d); + Vector4 b = new Vector4(1.0d, 2.0d, 3.0d, 4.0d); + + // case 1: compare between same values + bool expected = true; + bool actual = a == b; + Assert.Equal(expected, actual); + + // case 2: compare between different values + b.X = 10.0d; + expected = false; + actual = a == b; + Assert.Equal(expected, actual); + } + + // A test for Subtract (Vector4d, Vector4d) + [Fact] + public void Vector4DoubleSubtractTest() + { + Vector4 a = new Vector4(1.0d, 6.0d, 3.0d, 4.0d); + Vector4 b = new Vector4(5.0d, 2.0d, 3.0d, 9.0d); + + Vector4 expected = new Vector4(-4.0d, 4.0d, 0.0d, -5.0d); + Vector4 actual; + + actual = Vector4.Subtract(a, b); + + Assert.Equal(expected, actual); + } + + // A test for UnitW + [Fact] + public void Vector4DoubleUnitWTest() + { + Vector4 val = new Vector4(0.0d, 0.0d, 0.0d, 1.0d); + Assert.Equal(val, Vector4.UnitW); + } + + // A test for UnitX + [Fact] + public void Vector4DoubleUnitXTest() + { + Vector4 val = new Vector4(1.0d, 0.0d, 0.0d, 0.0d); + Assert.Equal(val, Vector4.UnitX); + } + + // A test for UnitY + [Fact] + public void Vector4DoubleUnitYTest() + { + Vector4 val = new Vector4(0.0d, 1.0d, 0.0d, 0.0d); + Assert.Equal(val, Vector4.UnitY); + } + + // A test for UnitZ + [Fact] + public void Vector4DoubleUnitZTest() + { + Vector4 val = new Vector4(0.0d, 0.0d, 1.0d, 0.0d); + Assert.Equal(val, Vector4.UnitZ); + } + + // A test for One + [Fact] + public void Vector4DoubleOneTest() + { + Vector4 val = new Vector4(1.0d, 1.0d, 1.0d, 1.0d); + Assert.Equal(val, Vector4.One); + } + + // A test for Zero + [Fact] + public void Vector4DoubleZeroTest() + { + Vector4 val = new Vector4(0.0d, 0.0d, 0.0d, 0.0d); + Assert.Equal(val, Vector4.Zero); + } + + // A test for Equals (Vector4d) + [Fact] + public void Vector4DoubleEqualsTest1() + { + Vector4 a = new Vector4(1.0d, 2.0d, 3.0d, 4.0d); + Vector4 b = new Vector4(1.0d, 2.0d, 3.0d, 4.0d); + + // case 1: compare between same values + Assert.True(a.Equals(b)); + + // case 2: compare between different values + b.X = 10.0d; + Assert.False(a.Equals(b)); + } + + // A test for Vector4d (Double) + [Fact] + public void Vector4DoubleConstructorTest6() + { + Double value = 1.0d; + Vector4 target = new Vector4(value); + + Vector4 expected = new Vector4(value, value, value, value); + Assert.Equal(expected, target); + + value = 2.0d; + target = new Vector4(value); + expected = new Vector4(value, value, value, value); + Assert.Equal(expected, target); + } + + // A test for Vector4d comparison involving NaN values + [Fact] + public void Vector4DoubleEqualsNanTest() + { + Vector4 a = new Vector4(Double.NaN, 0, 0, 0); + Vector4 b = new Vector4(0, Double.NaN, 0, 0); + Vector4 c = new Vector4(0, 0, Double.NaN, 0); + Vector4 d = new Vector4(0, 0, 0, Double.NaN); + + Assert.False(a == Vector4.Zero); + Assert.False(b == Vector4.Zero); + Assert.False(c == Vector4.Zero); + Assert.False(d == Vector4.Zero); + + Assert.True(a != Vector4.Zero); + Assert.True(b != Vector4.Zero); + Assert.True(c != Vector4.Zero); + Assert.True(d != Vector4.Zero); + + Assert.False(a.Equals(Vector4.Zero)); + Assert.False(b.Equals(Vector4.Zero)); + Assert.False(c.Equals(Vector4.Zero)); + Assert.False(d.Equals(Vector4.Zero)); + + // Counterintuitive result - IEEE rules for NaN comparison are weird! + Assert.False(a.Equals(a)); + Assert.False(b.Equals(b)); + Assert.False(c.Equals(c)); + Assert.False(d.Equals(d)); + } + + [Fact] + public void Vector4DoubleAbsTest() + { + Vector4 v1 = new Vector4(-2.5d, 2.0d, 3.0d, 3.3d); + Vector4 v3 = Vector4.Abs(new Vector4(Double.PositiveInfinity, 0.0d, Double.NegativeInfinity, Double.NaN)); + Vector4 v = Vector4.Abs(v1); + Assert.Equal(2.5d, v.X); + Assert.Equal(2.0d, v.Y); + Assert.Equal(3.0d, v.Z); + Assert.Equal(3.3d, v.W); + Assert.Equal(Double.PositiveInfinity, v3.X); + Assert.Equal(0.0d, v3.Y); + Assert.Equal(Double.PositiveInfinity, v3.Z); + Assert.Equal(Double.NaN, v3.W); + } + + [Fact] + public void Vector4DoubleSqrtTest() + { + Vector4 v1 = new Vector4(-2.5d, 2.0d, 3.0d, 3.3d); + Vector4 v2 = new Vector4(5.5d, 4.5d, 6.5d, 7.5d); + Assert.Equal(2, (int)Vector4.SquareRoot(v2).X); + Assert.Equal(2, (int)Vector4.SquareRoot(v2).Y); + Assert.Equal(2, (int)Vector4.SquareRoot(v2).Z); + Assert.Equal(2, (int)Vector4.SquareRoot(v2).W); + Assert.Equal(Double.NaN, Vector4.SquareRoot(v1).X); + } + + // A test to make sure these types are blittable directly into GPU buffer memory layouts + [Fact] + public unsafe void Vector4DoubleSizeofTest() + { + Assert.Equal(16, sizeof(Vector4)); + Assert.Equal(32, sizeof(Vector4Double_2x)); + Assert.Equal(20, sizeof(Vector4DoublePlusDouble)); + Assert.Equal(40, sizeof(Vector4DoublePlusDouble_2x)); + } + + [StructLayout(LayoutKind.Sequential)] + struct Vector4Double_2x + { + private Vector4 _a; + private Vector4 _b; + } + + [StructLayout(LayoutKind.Sequential)] + struct Vector4DoublePlusDouble + { + private Vector4 _v; + private Double _f; + } + + [StructLayout(LayoutKind.Sequential)] + struct Vector4DoublePlusDouble_2x + { + private Vector4PlusDouble _a; + private Vector4PlusDouble _b; + } + + + [Fact] + public void SetFieldsTest() + { + Vector4Double v3 = new Vector4Double(4f, 5f, 6f, 7f); + v3 = v3.WithX(1.0d); + v3 = v3.WithY(2.0d); + v3 = v3.WithZ(3.0d); + v3 = v3.WithW(4.0d); + Assert.Equal(1.0f, v3.X); + Assert.Equal(2.0f, v3.Y); + Assert.Equal(3.0f, v3.Z); + Assert.Equal(4.0f, v3.W); + Vector4Double v4 = v3; + v4 = v4.WithY(0.5d); + v4 = v4.WithZ(2.2d); + v4 = v4.WithW(3.5d); + Assert.Equal(1.0f, v4.X); + Assert.Equal(0.5f, v4.Y); + Assert.Equal(2.2f, v4.Z); + Assert.Equal(3.5f, v4.W); + Assert.Equal(2.0f, v3.Y); + } + + [Fact] + public void EmbeddedVectorSetFields() + { + EmbeddedVectorObject evo = new EmbeddedVectorObject(); + evo.FieldVector = evo.FieldVector.WithX(5.0d); + evo.FieldVector = evo.FieldVector.WithY(5.0d); + evo.FieldVector = evo.FieldVector.WithZ(5.0d); + evo.FieldVector = evo.FieldVector.WithW(5.0d); + Assert.Equal(5.0f, evo.FieldVector.X); + Assert.Equal(5.0f, evo.FieldVector.Y); + Assert.Equal(5.0f, evo.FieldVector.Z); + Assert.Equal(5.0f, evo.FieldVector.W); + } + + [Fact] + public void DeeplyEmbeddedObjectTest() + { + DeeplyEmbeddedClass obj = new DeeplyEmbeddedClass(); + obj.L0.L1.L2.L3.L4.L5.L6.L7.EmbeddedVector = obj.L0.L1.L2.L3.L4.L5.L6.L7.EmbeddedVector.WithX(5d); + Assert.Equal(5f, obj.RootEmbeddedObject.X); + Assert.Equal(5f, obj.RootEmbeddedObject.Y); + Assert.Equal(1f, obj.RootEmbeddedObject.Z); + Assert.Equal(-5f, obj.RootEmbeddedObject.W); + obj.L0.L1.L2.L3.L4.L5.L6.L7.EmbeddedVector = new Vector4(1, 2, 3, 4); + Assert.Equal(1f, obj.RootEmbeddedObject.X); + Assert.Equal(2f, obj.RootEmbeddedObject.Y); + Assert.Equal(3f, obj.RootEmbeddedObject.Z); + Assert.Equal(4f, obj.RootEmbeddedObject.W); + } + + [Fact] + public void DeeplyEmbeddedStructTest() + { + DeeplyEmbeddedStruct obj = DeeplyEmbeddedStruct.Create(); + obj.L0.L1.L2.L3.L4.L5.L6.L7.EmbeddedVector = obj.L0.L1.L2.L3.L4.L5.L6.L7.EmbeddedVector.WithX(5d); + Assert.Equal(5f, obj.RootEmbeddedObject.X); + Assert.Equal(5f, obj.RootEmbeddedObject.Y); + Assert.Equal(1f, obj.RootEmbeddedObject.Z); + Assert.Equal(-5f, obj.RootEmbeddedObject.W); + obj.L0.L1.L2.L3.L4.L5.L6.L7.EmbeddedVector = new Vector4(1, 2, 3, 4); + Assert.Equal(1f, obj.RootEmbeddedObject.X); + Assert.Equal(2f, obj.RootEmbeddedObject.Y); + Assert.Equal(3f, obj.RootEmbeddedObject.Z); + Assert.Equal(4f, obj.RootEmbeddedObject.W); + } + + private class EmbeddedVectorObject + { + public Vector4 FieldVector; + } + + private class DeeplyEmbeddedClass + { + public readonly Level0 L0 = new Level0(); + public Vector4 RootEmbeddedObject { get { return L0.L1.L2.L3.L4.L5.L6.L7.EmbeddedVector; } } + public class Level0 + { + public readonly Level1 L1 = new Level1(); + public class Level1 + { + public readonly Level2 L2 = new Level2(); + public class Level2 + { + public readonly Level3 L3 = new Level3(); + public class Level3 + { + public readonly Level4 L4 = new Level4(); + public class Level4 + { + public readonly Level5 L5 = new Level5(); + public class Level5 + { + public readonly Level6 L6 = new Level6(); + public class Level6 + { + public readonly Level7 L7 = new Level7(); + public class Level7 + { + public Vector4Double EmbeddedVector = new Vector4(1, 5, 1, -5); + } + } + } + } + } + } + } + } + } + + // Contrived test for strangely-sized and shaped embedded structures, with unused buffer fields. + #pragma warning disable 0169 + private struct DeeplyEmbeddedStruct + { + public static DeeplyEmbeddedStruct Create() + { + var obj = new DeeplyEmbeddedStruct(); + obj.L0 = new Level0(); + obj.L0.L1 = new Level0.Level1(); + obj.L0.L1.L2 = new Level0.Level1.Level2(); + obj.L0.L1.L2.L3 = new Level0.Level1.Level2.Level3(); + obj.L0.L1.L2.L3.L4 = new Level0.Level1.Level2.Level3.Level4(); + obj.L0.L1.L2.L3.L4.L5 = new Level0.Level1.Level2.Level3.Level4.Level5(); + obj.L0.L1.L2.L3.L4.L5.L6 = new Level0.Level1.Level2.Level3.Level4.Level5.Level6(); + obj.L0.L1.L2.L3.L4.L5.L6.L7 = new Level0.Level1.Level2.Level3.Level4.Level5.Level6.Level7(); + obj.L0.L1.L2.L3.L4.L5.L6.L7.EmbeddedVector = new Vector4(1, 5, 1, -5); + + return obj; + } + + public Level0 L0; + public Vector4 RootEmbeddedObject { get { return L0.L1.L2.L3.L4.L5.L6.L7.EmbeddedVector; } } + public struct Level0 + { + private float _buffer0, _buffer1; + public Level1 L1; + private float _buffer2; + public struct Level1 + { + private float _buffer0, _buffer1; + public Level2 L2; + private byte _buffer2; + public struct Level2 + { + public Level3 L3; + private float _buffer0; + private byte _buffer1; + public struct Level3 + { + public Level4 L4; + public struct Level4 + { + private float _buffer0; + public Level5 L5; + private long _buffer1; + private byte _buffer2; + private double _buffer3; + public struct Level5 + { + private byte _buffer0; + public Level6 L6; + public struct Level6 + { + private byte _buffer0; + public Level7 L7; + private byte _buffer1, _buffer2; + public struct Level7 + { + public Vector4 EmbeddedVector; + } + } + } + } + } + } + } + } + } + #pragma warning restore 0169 + } +} \ No newline at end of file diff --git a/src/libraries/System.Numerics.Vectors/tests/Vector4_SingleTests.cs b/src/libraries/System.Numerics.Vectors/tests/Vector4_SingleTests.cs new file mode 100644 index 00000000000000..847a831472af48 --- /dev/null +++ b/src/libraries/System.Numerics.Vectors/tests/Vector4_SingleTests.cs @@ -0,0 +1,1576 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Globalization; +using System.Runtime.InteropServices; +using Xunit; + +namespace System.Numerics.Tests +{ + public partial class Vector4SingleTests + { + [Fact] + public void Vector4SingleCopyToTest() + { + Vector4 v1 = new Vector4(2.5f, 2.0f, 3.0f, 3.3f); + + var a = new Single[5]; + var b = new Single[4]; + + Assert.Throws(() => v1.CopyTo(a, -1)); + Assert.Throws(() => v1.CopyTo(a, a.Length)); + + v1.CopyTo(a, 1); + v1.CopyTo(b); + Assert.Equal(0.0f, a[0]); + Assert.Equal(2.5f, a[1]); + Assert.Equal(2.0f, a[2]); + Assert.Equal(3.0f, a[3]); + Assert.Equal(3.3f, a[4]); + Assert.Equal(2.5f, b[0]); + Assert.Equal(2.0f, b[1]); + Assert.Equal(3.0f, b[2]); + Assert.Equal(3.3f, b[3]); + } + + [Fact] + public void Vector4SingleGetHashCodeTest() + { + Vector4 v1 = new Vector4(2.5f, 2.0f, 3.0f, 3.3f); + Vector4 v2 = new Vector4(2.5f, 2.0f, 3.0f, 3.3f); + Vector4 v3 = new Vector4(2.5f, 2.0f, 3.0f, 3.3f); + Vector4 v5 = new Vector4(3.3f, 3.0f, 2.0f, 2.5f); + Assert.Equal(v1.GetHashCode(), v1.GetHashCode()); + Assert.Equal(v1.GetHashCode(), v2.GetHashCode()); + Assert.NotEqual(v1.GetHashCode(), v5.GetHashCode()); + Assert.Equal(v1.GetHashCode(), v3.GetHashCode()); + Vector4 v4 = new Vector4(0.0f, 0.0f, 0.0f, 0.0f); + Vector4 v6 = new Vector4(1.0f, 0.0f, 0.0f, 0.0f); + Vector4 v7 = new Vector4(0.0f, 1.0f, 0.0f, 0.0f); + Vector4 v8 = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); + Vector4 v9 = new Vector4(1.0f, 1.0f, 0.0f, 0.0f); + Assert.NotEqual(v4.GetHashCode(), v6.GetHashCode()); + Assert.NotEqual(v4.GetHashCode(), v7.GetHashCode()); + Assert.NotEqual(v4.GetHashCode(), v8.GetHashCode()); + Assert.NotEqual(v7.GetHashCode(), v6.GetHashCode()); + Assert.NotEqual(v8.GetHashCode(), v6.GetHashCode()); + Assert.NotEqual(v8.GetHashCode(), v7.GetHashCode()); + Assert.NotEqual(v9.GetHashCode(), v7.GetHashCode()); + } + + [Fact] + public void Vector4SingleToStringTest() + { + string separator = CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator; + CultureInfo enUsCultureInfo = new CultureInfo("en-US"); + + Vector4 v1 = new Vector4(2.5f, 2.0f, 3.0f, 3.3f); + + string v1str = v1.ToString(); + string expectedv1 = string.Format(CultureInfo.CurrentCulture + , "<{1:G}{0} {2:G}{0} {3:G}{0} {4:G}>" + , separator, 2.5, 2, 3, 3.3); + Assert.Equal(expectedv1, v1str); + + string v1strformatted = v1.ToString("c", CultureInfo.CurrentCulture); + string expectedv1formatted = string.Format(CultureInfo.CurrentCulture + , "<{1:c}{0} {2:c}{0} {3:c}{0} {4:c}>" + , separator, 2.5, 2, 3, 3.3); + Assert.Equal(expectedv1formatted, v1strformatted); + + string v2strformatted = v1.ToString("c", enUsCultureInfo); + string expectedv2formatted = string.Format(enUsCultureInfo + , "<{1:c}{0} {2:c}{0} {3:c}{0} {4:c}>" + , enUsCultureInfo.NumberFormat.NumberGroupSeparator, 2.5, 2, 3, 3.3); + Assert.Equal(expectedv2formatted, v2strformatted); + + string v3strformatted = v1.ToString("c"); + string expectedv3formatted = string.Format(CultureInfo.CurrentCulture + , "<{1:c}{0} {2:c}{0} {3:c}{0} {4:c}>" + , separator, 2.5, 2, 3, 3.3); + Assert.Equal(expectedv3formatted, v3strformatted); + } + + // A test for DistanceSquared (Vector4f, Vector4f) + [Fact] + public void Vector4SingleDistanceSquaredTest() + { + Vector4 a = new Vector4(1.0f, 2.0f, 3.0f, 4.0f); + Vector4 b = new Vector4(5.0f, 6.0f, 7.0f, 8.0f); + + Single expected = 64.0f; + Single actual; + + actual = Vector4.DistanceSquared(a, b); + Assert.True(MathHelper.EqualScalar(expected, actual), "Vector4f.DistanceSquared did not return the expected value."); + } + + // A test for Distance (Vector4f, Vector4f) + [Fact] + public void Vector4SingleDistanceTest() + { + Vector4 a = new Vector4(1.0f, 2.0f, 3.0f, 4.0f); + Vector4 b = new Vector4(5.0f, 6.0f, 7.0f, 8.0f); + + Single expected = 8.0f; + Single actual; + + actual = Vector4.Distance(a, b); + Assert.True(MathHelper.EqualScalar(expected, actual), "Vector4f.Distance did not return the expected value."); + } + + // A test for Distance (Vector4f, Vector4f) + // Distance from the same point + [Fact] + public void Vector4SingleDistanceTest1() + { + Vector4 a = new Vector4(new Vector2(1.051f, 2.05f), 3.478f, 1.0f); + Vector4 b = new Vector4(new Vector3(1.051f, 2.05f, 3.478f), 0.0f); + b.W = 1.0f; + + Single actual = Vector4.Distance(a, b); + Assert.Equal(0.0f, actual); + } + + // A test for Dot (Vector4f, Vector4f) + [Fact] + public void Vector4SingleDotTest() + { + Vector4 a = new Vector4(1.0f, 2.0f, 3.0f, 4.0f); + Vector4 b = new Vector4(5.0f, 6.0f, 7.0f, 8.0f); + + Single expected = 70.0f; + Single actual; + + actual = Vector4.Dot(a, b); + Assert.True(MathHelper.EqualScalar(expected, actual), "Vector4f.Dot did not return the expected value."); + } + + // A test for Dot (Vector4f, Vector4f) + // Dot test for perpendicular vector + [Fact] + public void Vector4SingleDotTest1() + { + Vector3 a = new Vector3(1.55f, 1.55f, 1); + Vector3 b = new Vector3(2.5f, 3, 1.5f); + Vector3 c = Vector3.Cross(a, b); + + Vector4 d = new Vector4(a, 0); + Vector4 e = new Vector4(c, 0); + + Single actual = Vector4.Dot(d, e); + Assert.True(MathHelper.EqualScalar(0.0f, actual), "Vector4f.Dot did not return the expected value."); + } + + // A test for Length () + [Fact] + public void Vector4SingleLengthTest() + { + Vector3 a = new Vector3(1.0f, 2.0f, 3.0f); + Single w = 4.0f; + + Vector4 target = new Vector4(a, w); + + Single expected = (Single)System.Math.Sqrt(30.0f); + Single actual; + + actual = target.Length(); + + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Length did not return the expected value."); + } + + // A test for Length () + // Length test where length is zero + [Fact] + public void Vector4SingleLengthTest1() + { + Vector4 target = new Vector4(); + + Single expected = 0.0f; + Single actual = target.Length(); + + Assert.True(MathHelper.EqualScalar(expected, actual), "Vector4f.Length did not return the expected value."); + } + + // A test for LengthSquared () + [Fact] + public void Vector4SingleLengthSquaredTest() + { + Vector3 a = new Vector3(1.0f, 2.0f, 3.0f); + Single w = 4.0f; + + Vector4 target = new Vector4(a, w); + + Single expected = 30; + Single actual; + + actual = target.LengthSquared(); + + Assert.True(MathHelper.EqualScalar(expected, actual), "Vector4f.LengthSquared did not return the expected value."); + } + + // A test for Min (Vector4f, Vector4f) + [Fact] + public void Vector4SingleMinTest() + { + Vector4 a = new Vector4(-1.0f, 4.0f, -3.0f, 1000.0f); + Vector4 b = new Vector4(2.0f, 1.0f, -1.0f, 0.0f); + + Vector4 expected = new Vector4(-1.0f, 1.0f, -3.0f, 0.0f); + Vector4 actual; + actual = Vector4.Min(a, b); + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Min did not return the expected value."); + } + + // A test for Max (Vector4f, Vector4f) + [Fact] + public void Vector4SingleMaxTest() + { + Vector4 a = new Vector4(-1.0f, 4.0f, -3.0f, 1000.0f); + Vector4 b = new Vector4(2.0f, 1.0f, -1.0f, 0.0f); + + Vector4 expected = new Vector4(2.0f, 4.0f, -1.0f, 1000.0f); + Vector4 actual; + actual = Vector4.Max(a, b); + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Max did not return the expected value."); + } + + [Fact] + public void Vector4SingleMinMaxCodeCoverageTest() + { + Vector4 min = Vector4.Zero; + Vector4 max = Vector4.One; + Vector4 actual; + + // Min. + actual = Vector4.Min(min, max); + Assert.Equal(actual, min); + + actual = Vector4.Min(max, min); + Assert.Equal(actual, min); + + // Max. + actual = Vector4.Max(min, max); + Assert.Equal(actual, max); + + actual = Vector4.Max(max, min); + Assert.Equal(actual, max); + } + + // A test for Clamp (Vector4f, Vector4f, Vector4f) + [Fact] + public void Vector4SingleClampTest() + { + Vector4 a = new Vector4(0.5f, 0.3f, 0.33f, 0.44f); + Vector4 min = new Vector4(0.0f, 0.1f, 0.13f, 0.14f); + Vector4 max = new Vector4(1.0f, 1.1f, 1.13f, 1.14f); + + // Normal case. + // Case N1: specified value is in the range. + Vector4 expected = new Vector4(0.5f, 0.3f, 0.33f, 0.44f); + Vector4 actual = Vector4.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Clamp did not return the expected value."); + + // Normal case. + // Case N2: specified value is bigger than max value. + a = new Vector4(2.0f, 3.0f, 4.0f, 5.0f); + expected = max; + actual = Vector4.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Clamp did not return the expected value."); + + // Case N3: specified value is smaller than max value. + a = new Vector4(-2.0f, -3.0f, -4.0f, -5.0f); + expected = min; + actual = Vector4.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Clamp did not return the expected value."); + + // Case N4: combination case. + a = new Vector4(-2.0f, 0.5f, 4.0f, -5.0f); + expected = new Vector4(min.X, a.Y, max.Z, min.W); + actual = Vector4.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Clamp did not return the expected value."); + + // User specified min value is bigger than max value. + max = new Vector4(0.0f, 0.1f, 0.13f, 0.14f); + min = new Vector4(1.0f, 1.1f, 1.13f, 1.14f); + + // Case W1: specified value is in the range. + a = new Vector4(0.5f, 0.3f, 0.33f, 0.44f); + expected = max; + actual = Vector4.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Clamp did not return the expected value."); + + // Normal case. + // Case W2: specified value is bigger than max and min value. + a = new Vector4(2.0f, 3.0f, 4.0f, 5.0f); + expected = max; + actual = Vector4.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Clamp did not return the expected value."); + + // Case W3: specified value is smaller than min and max value. + a = new Vector4(-2.0f, -3.0f, -4.0f, -5.0f); + expected = max; + actual = Vector4.Clamp(a, min, max); + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Clamp did not return the expected value."); + } + + // A test for Lerp (Vector4f, Vector4f, Single) + [Fact] + public void Vector4SingleLerpTest() + { + Vector4 a = new Vector4(1.0f, 2.0f, 3.0f, 4.0f); + Vector4 b = new Vector4(5.0f, 6.0f, 7.0f, 8.0f); + + Single t = 0.5f; + + Vector4 expected = new Vector4(3.0f, 4.0f, 5.0f, 6.0f); + Vector4 actual; + + actual = Vector4.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector4f, Vector4f, Single) + // Lerp test with factor zero + [Fact] + public void Vector4SingleLerpTest1() + { + Vector4 a = new Vector4(new Vector3(1.0f, 2.0f, 3.0f), 4.0f); + Vector4 b = new Vector4(4.0f, 5.0f, 6.0f, 7.0f); + + Single t = 0.0f; + Vector4 expected = new Vector4(1.0f, 2.0f, 3.0f, 4.0f); + Vector4 actual = Vector4.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector4f, Vector4f, Single) + // Lerp test with factor one + [Fact] + public void Vector4SingleLerpTest2() + { + Vector4 a = new Vector4(new Vector3(1.0f, 2.0f, 3.0f), 4.0f); + Vector4 b = new Vector4(4.0f, 5.0f, 6.0f, 7.0f); + + Single t = 1.0f; + Vector4 expected = new Vector4(4.0f, 5.0f, 6.0f, 7.0f); + Vector4 actual = Vector4.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector4f, Vector4f, Single) + // Lerp test with factor > 1 + [Fact] + public void Vector4SingleLerpTest3() + { + Vector4 a = new Vector4(new Vector3(0.0f, 0.0f, 0.0f), 0.0f); + Vector4 b = new Vector4(4.0f, 5.0f, 6.0f, 7.0f); + + Single t = 2.0f; + Vector4 expected = new Vector4(8.0f, 10.0f, 12.0f, 14.0f); + Vector4 actual = Vector4.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector4f, Vector4f, Single) + // Lerp test with factor < 0 + [Fact] + public void Vector4SingleLerpTest4() + { + Vector4 a = new Vector4(new Vector3(0.0f, 0.0f, 0.0f), 0.0f); + Vector4 b = new Vector4(4.0f, 5.0f, 6.0f, 7.0f); + + Single t = -2.0f; + Vector4 expected = -(b * 2); + Vector4 actual = Vector4.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Lerp did not return the expected value."); + } + + // A test for Lerp (Vector4f, Vector4f, Single) + // Lerp test from the same point + [Fact] + public void Vector4SingleLerpTest5() + { + Vector4 a = new Vector4(4.0f, 5.0f, 6.0f, 7.0f); + Vector4 b = new Vector4(4.0f, 5.0f, 6.0f, 7.0f); + + Single t = 0.85f; + Vector4 expected = a; + Vector4 actual = Vector4.Lerp(a, b, t); + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Lerp did not return the expected value."); + } + + // A test for Transform (Vector2f, Matrix4x4) + [Fact] + public void Vector4SingleTransformTest1() + { + Vector2 v = new Vector2(1.0f, 2.0f); + + Matrix4x4 m = + Matrix4x4.CreateRotationX(MathHelper.ToRadians(30.0f)) * + Matrix4x4.CreateRotationY(MathHelper.ToRadians(30.0f)) * + Matrix4x4.CreateRotationZ(MathHelper.ToRadians(30.0f)); + m.M41 = 10.0f; + m.M42 = 20.0f; + m.M43 = 30.0f; + + Vector4 expected = new Vector4(10.316987f, 22.183012f, 30.3660259f, 1.0f); + Vector4 actual; + + actual = Vector4.Transform(v, m); + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Transform did not return the expected value."); + } + + // A test for Transform (Vector3f, Matrix4x4) + [Fact] + public void Vector4SingleTransformTest2() + { + Vector3 v = new Vector3(1.0f, 2.0f, 3.0f); + + Matrix4x4 m = + Matrix4x4.CreateRotationX(MathHelper.ToRadians(30.0f)) * + Matrix4x4.CreateRotationY(MathHelper.ToRadians(30.0f)) * + Matrix4x4.CreateRotationZ(MathHelper.ToRadians(30.0f)); + m.M41 = 10.0f; + m.M42 = 20.0f; + m.M43 = 30.0f; + + Vector4 expected = new Vector4(12.19198728f, 21.53349376f, 32.61602545f, 1.0f); + Vector4 actual; + + actual = Vector4.Transform(v, m); + Assert.True(MathHelper.Equal(expected, actual), "Vector4.Transform did not return the expected value."); + } + + // A test for Transform (Vector4f, Matrix4x4) + [Fact] + public void Vector4SingleTransformVector4Test() + { + Vector4 v = new Vector4(1.0f, 2.0f, 3.0f, 0.0f); + + Matrix4x4 m = + Matrix4x4.CreateRotationX(MathHelper.ToRadians(30.0f)) * + Matrix4x4.CreateRotationY(MathHelper.ToRadians(30.0f)) * + Matrix4x4.CreateRotationZ(MathHelper.ToRadians(30.0f)); + m.M41 = 10.0f; + m.M42 = 20.0f; + m.M43 = 30.0f; + + Vector4 expected = new Vector4(2.19198728f, 1.53349376f, 2.61602545f, 0.0f); + Vector4 actual; + + actual = Vector4.Transform(v, m); + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Transform did not return the expected value."); + + // + v.W = 1.0f; + + expected = new Vector4(12.19198728f, 21.53349376f, 32.61602545f, 1.0f); + actual = Vector4.Transform(v, m); + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Transform did not return the expected value."); + } + + // A test for Transform (Vector4f, Matrix4x4) + // Transform Vector4 with zero matrix + [Fact] + public void Vector4SingleTransformVector4Test1() + { + Vector4 v = new Vector4(1.0f, 2.0f, 3.0f, 0.0f); + Matrix4x4 m = new Matrix4x4(); + Vector4 expected = new Vector4(0, 0, 0, 0); + + Vector4 actual = Vector4.Transform(v, m); + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Transform did not return the expected value."); + } + + // A test for Transform (Vector4f, Matrix4x4) + // Transform Vector4 with identity matrix + [Fact] + public void Vector4SingleTransformVector4Test2() + { + Vector4 v = new Vector4(1.0f, 2.0f, 3.0f, 0.0f); + Matrix4x4 m = Matrix4x4.Identity; + Vector4 expected = new Vector4(1.0f, 2.0f, 3.0f, 0.0f); + + Vector4 actual = Vector4.Transform(v, m); + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Transform did not return the expected value."); + } + + // A test for Transform (Vector3f, Matrix4x4) + // Transform Vector3f test + [Fact] + public void Vector4SingleTransformVector3Test() + { + Vector3 v = new Vector3(1.0f, 2.0f, 3.0f); + + Matrix4x4 m = + Matrix4x4.CreateRotationX(MathHelper.ToRadians(30.0f)) * + Matrix4x4.CreateRotationY(MathHelper.ToRadians(30.0f)) * + Matrix4x4.CreateRotationZ(MathHelper.ToRadians(30.0f)); + m.M41 = 10.0f; + m.M42 = 20.0f; + m.M43 = 30.0f; + + Vector4 expected = Vector4.Transform(new Vector4(v, 1.0f), m); + Vector4 actual = Vector4.Transform(v, m); + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Transform did not return the expected value."); + } + + // A test for Transform (Vector3f, Matrix4x4) + // Transform vector3 with zero matrix + [Fact] + public void Vector4SingleTransformVector3Test1() + { + Vector3 v = new Vector3(1.0f, 2.0f, 3.0f); + Matrix4x4 m = new Matrix4x4(); + Vector4 expected = new Vector4(0, 0, 0, 0); + + Vector4 actual = Vector4.Transform(v, m); + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Transform did not return the expected value."); + } + + // A test for Transform (Vector3f, Matrix4x4) + // Transform vector3 with identity matrix + [Fact] + public void Vector4SingleTransformVector3Test2() + { + Vector3 v = new Vector3(1.0f, 2.0f, 3.0f); + Matrix4x4 m = Matrix4x4.Identity; + Vector4 expected = new Vector4(1.0f, 2.0f, 3.0f, 1.0f); + + Vector4 actual = Vector4.Transform(v, m); + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Transform did not return the expected value."); + } + + // A test for Transform (Vector2f, Matrix4x4) + // Transform Vector2f test + [Fact] + public void Vector4SingleTransformVector2Test() + { + Vector2 v = new Vector2(1.0f, 2.0f); + + Matrix4x4 m = + Matrix4x4.CreateRotationX(MathHelper.ToRadians(30.0f)) * + Matrix4x4.CreateRotationY(MathHelper.ToRadians(30.0f)) * + Matrix4x4.CreateRotationZ(MathHelper.ToRadians(30.0f)); + m.M41 = 10.0f; + m.M42 = 20.0f; + m.M43 = 30.0f; + + Vector4 expected = Vector4.Transform(new Vector4(v, 0.0f, 1.0f), m); + Vector4 actual = Vector4.Transform(v, m); + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Transform did not return the expected value."); + } + + // A test for Transform (Vector2f, Matrix4x4) + // Transform Vector2f with zero matrix + [Fact] + public void Vector4SingleTransformVector2Test1() + { + Vector2 v = new Vector2(1.0f, 2.0f); + Matrix4x4 m = new Matrix4x4(); + Vector4 expected = new Vector4(0, 0, 0, 0); + + Vector4 actual = Vector4.Transform(v, m); + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Transform did not return the expected value."); + } + + // A test for Transform (Vector2f, Matrix4x4) + // Transform vector2 with identity matrix + [Fact] + public void Vector4SingleTransformVector2Test2() + { + Vector2 v = new Vector2(1.0f, 2.0f); + Matrix4x4 m = Matrix4x4.Identity; + Vector4 expected = new Vector4(1.0f, 2.0f, 0, 1.0f); + + Vector4 actual = Vector4.Transform(v, m); + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Transform did not return the expected value."); + } + + // A test for Transform (Vector2f, Quaternion) + [Fact] + public void Vector4SingleTransformVector2QuatanionTest() + { + Vector2 v = new Vector2(1.0f, 2.0f); + + Matrix4x4 m = + Matrix4x4.CreateRotationX(MathHelper.ToRadians(30.0f)) * + Matrix4x4.CreateRotationY(MathHelper.ToRadians(30.0f)) * + Matrix4x4.CreateRotationZ(MathHelper.ToRadians(30.0f)); + + Quaternion q = Quaternion.CreateFromRotationMatrix(m); + + Vector4 expected = Vector4.Transform(v, m); + Vector4 actual; + + actual = Vector4.Transform(v, q); + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Transform did not return the expected value."); + } + + // A test for Transform (Vector3f, Quaternion) + [Fact] + public void Vector4SingleTransformVector3Quaternion() + { + Vector3 v = new Vector3(1.0f, 2.0f, 3.0f); + + Matrix4x4 m = + Matrix4x4.CreateRotationX(MathHelper.ToRadians(30.0f)) * + Matrix4x4.CreateRotationY(MathHelper.ToRadians(30.0f)) * + Matrix4x4.CreateRotationZ(MathHelper.ToRadians(30.0f)); + Quaternion q = Quaternion.CreateFromRotationMatrix(m); + + Vector4 expected = Vector4.Transform(v, m); + Vector4 actual; + + actual = Vector4.Transform(v, q); + Assert.True(MathHelper.Equal(expected, actual), "Vector4.Transform did not return the expected value."); + } + + // A test for Transform (Vector4f, Quaternion) + [Fact] + public void Vector4SingleTransformVector4QuaternionTest() + { + Vector4 v = new Vector4(1.0f, 2.0f, 3.0f, 0.0f); + + Matrix4x4 m = + Matrix4x4.CreateRotationX(MathHelper.ToRadians(30.0f)) * + Matrix4x4.CreateRotationY(MathHelper.ToRadians(30.0f)) * + Matrix4x4.CreateRotationZ(MathHelper.ToRadians(30.0f)); + Quaternion q = Quaternion.CreateFromRotationMatrix(m); + + Vector4 expected = Vector4.Transform(v, m); + Vector4 actual; + + actual = Vector4.Transform(v, q); + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Transform did not return the expected value."); + + // + v.W = 1.0f; + expected.W = 1.0f; + actual = Vector4.Transform(v, q); + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Transform did not return the expected value."); + } + + // A test for Transform (Vector4f, Quaternion) + // Transform Vector4 with zero quaternion + [Fact] + public void Vector4SingleTransformVector4QuaternionTest1() + { + Vector4 v = new Vector4(1.0f, 2.0f, 3.0f, 0.0f); + Quaternion q = new Quaternion(); + Vector4 expected = v; + + Vector4 actual = Vector4.Transform(v, q); + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Transform did not return the expected value."); + } + + // A test for Transform (Vector4f, Quaternion) + // Transform Vector4 with identity matrix + [Fact] + public void Vector4SingleTransformVector4QuaternionTest2() + { + Vector4 v = new Vector4(1.0f, 2.0f, 3.0f, 0.0f); + Quaternion q = Quaternion.Identity; + Vector4 expected = new Vector4(1.0f, 2.0f, 3.0f, 0.0f); + + Vector4 actual = Vector4.Transform(v, q); + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Transform did not return the expected value."); + } + + // A test for Transform (Vector3f, Quaternion) + // Transform Vector3f test + [Fact] + public void Vector4SingleTransformVector3QuaternionTest() + { + Vector3 v = new Vector3(1.0f, 2.0f, 3.0f); + + Matrix4x4 m = + Matrix4x4.CreateRotationX(MathHelper.ToRadians(30.0f)) * + Matrix4x4.CreateRotationY(MathHelper.ToRadians(30.0f)) * + Matrix4x4.CreateRotationZ(MathHelper.ToRadians(30.0f)); + Quaternion q = Quaternion.CreateFromRotationMatrix(m); + + Vector4 expected = Vector4.Transform(v, m); + Vector4 actual = Vector4.Transform(v, q); + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Transform did not return the expected value."); + } + + // A test for Transform (Vector3f, Quaternion) + // Transform vector3 with zero quaternion + [Fact] + public void Vector4SingleTransformVector3QuaternionTest1() + { + Vector3 v = new Vector3(1.0f, 2.0f, 3.0f); + Quaternion q = new Quaternion(); + Vector4 expected = new Vector4(v, 1.0f); + + Vector4 actual = Vector4.Transform(v, q); + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Transform did not return the expected value."); + } + + // A test for Transform (Vector3f, Quaternion) + // Transform vector3 with identity quaternion + [Fact] + public void Vector4SingleTransformVector3QuaternionTest2() + { + Vector3 v = new Vector3(1.0f, 2.0f, 3.0f); + Quaternion q = Quaternion.Identity; + Vector4 expected = new Vector4(1.0f, 2.0f, 3.0f, 1.0f); + + Vector4 actual = Vector4.Transform(v, q); + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Transform did not return the expected value."); + } + + // A test for Transform (Vector2f, Quaternion) + // Transform Vector2f by quaternion test + [Fact] + public void Vector4SingleTransformVector2QuaternionTest() + { + Vector2 v = new Vector2(1.0f, 2.0f); + + Matrix4x4 m = + Matrix4x4.CreateRotationX(MathHelper.ToRadians(30.0f)) * + Matrix4x4.CreateRotationY(MathHelper.ToRadians(30.0f)) * + Matrix4x4.CreateRotationZ(MathHelper.ToRadians(30.0f)); + Quaternion q = Quaternion.CreateFromRotationMatrix(m); + + Vector4 expected = Vector4.Transform(v, m); + Vector4 actual = Vector4.Transform(v, q); + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Transform did not return the expected value."); + } + + // A test for Transform (Vector2f, Quaternion) + // Transform Vector2f with zero quaternion + [Fact] + public void Vector4SingleTransformVector2QuaternionTest1() + { + Vector2 v = new Vector2(1.0f, 2.0f); + Quaternion q = new Quaternion(); + Vector4 expected = new Vector4(1.0f, 2.0f, 0, 1.0f); + + Vector4 actual = Vector4.Transform(v, q); + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Transform did not return the expected value."); + } + + // A test for Transform (Vector2f, Matrix4x4) + // Transform vector2 with identity Quaternion + [Fact] + public void Vector4SingleTransformVector2QuaternionTest2() + { + Vector2 v = new Vector2(1.0f, 2.0f); + Quaternion q = Quaternion.Identity; + Vector4 expected = new Vector4(1.0f, 2.0f, 0, 1.0f); + + Vector4 actual = Vector4.Transform(v, q); + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Transform did not return the expected value."); + } + + // A test for Normalize (Vector4f) + [Fact] + public void Vector4SingleNormalizeTest() + { + Vector4 a = new Vector4(1.0f, 2.0f, 3.0f, 4.0f); + + Vector4 expected = new Vector4( + 0.1825741858350553711523232609336f, + 0.3651483716701107423046465218672f, + 0.5477225575051661134569697828008f, + 0.7302967433402214846092930437344f); + Vector4 actual; + + actual = Vector4.Normalize(a); + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Normalize did not return the expected value."); + } + + // A test for Normalize (Vector4f) + // Normalize vector of length one + [Fact] + public void Vector4SingleNormalizeTest1() + { + Vector4 a = new Vector4(1.0f, 0.0f, 0.0f, 0.0f); + + Vector4 expected = new Vector4(1.0f, 0.0f, 0.0f, 0.0f); + Vector4 actual = Vector4.Normalize(a); + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.Normalize did not return the expected value."); + } + + // A test for Normalize (Vector4f) + // Normalize vector of length zero + [Fact] + public void Vector4SingleNormalizeTest2() + { + Vector4 a = new Vector4(0.0f, 0.0f, 0.0f, 0.0f); + + Vector4 expected = new Vector4(0.0f, 0.0f, 0.0f, 0.0f); + Vector4 actual = Vector4.Normalize(a); + Assert.True(Single.IsNaN(actual.X) && Single.IsNaN(actual.Y) && Single.IsNaN(actual.Z) && Single.IsNaN(actual.W), "Vector4f.Normalize did not return the expected value."); + } + + // A test for operator - (Vector4f) + [Fact] + public void Vector4SingleUnaryNegationTest() + { + Vector4 a = new Vector4(1.0f, 2.0f, 3.0f, 4.0f); + + Vector4 expected = new Vector4(-1.0f, -2.0f, -3.0f, -4.0f); + Vector4 actual; + + actual = -a; + + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.operator - did not return the expected value."); + } + + // A test for operator - (Vector4f, Vector4f) + [Fact] + public void Vector4SingleSubtractionTest() + { + Vector4 a = new Vector4(1.0f, 6.0f, 3.0f, 4.0f); + Vector4 b = new Vector4(5.0f, 2.0f, 3.0f, 9.0f); + + Vector4 expected = new Vector4(-4.0f, 4.0f, 0.0f, -5.0f); + Vector4 actual; + + actual = a - b; + + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.operator - did not return the expected value."); + } + + // A test for operator * (Vector4f, Single) + [Fact] + public void Vector4SingleMultiplyOperatorTest() + { + Vector4 a = new Vector4(1.0f, 2.0f, 3.0f, 4.0f); + + const Single factor = 2.0f; + + Vector4 expected = new Vector4(2.0f, 4.0f, 6.0f, 8.0f); + Vector4 actual; + + actual = a * factor; + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.operator * did not return the expected value."); + } + + // A test for operator * (Single, Vector4f) + [Fact] + public void Vector4SingleMultiplyOperatorTest2() + { + Vector4 a = new Vector4(1.0f, 2.0f, 3.0f, 4.0f); + + const Single factor = 2.0f; + Vector4 expected = new Vector4(2.0f, 4.0f, 6.0f, 8.0f); + Vector4 actual; + + actual = factor * a; + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.operator * did not return the expected value."); + } + + // A test for operator * (Vector4f, Vector4f) + [Fact] + public void Vector4SingleMultiplyOperatorTest3() + { + Vector4 a = new Vector4(1.0f, 2.0f, 3.0f, 4.0f); + Vector4 b = new Vector4(5.0f, 6.0f, 7.0f, 8.0f); + + Vector4 expected = new Vector4(5.0f, 12.0f, 21.0f, 32.0f); + Vector4 actual; + + actual = a * b; + + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.operator * did not return the expected value."); + } + + // A test for operator / (Vector4f, Single) + [Fact] + public void Vector4SingleDivisionTest() + { + Vector4 a = new Vector4(1.0f, 2.0f, 3.0f, 4.0f); + + Single div = 2.0f; + + Vector4 expected = new Vector4(0.5f, 1.0f, 1.5f, 2.0f); + Vector4 actual; + + actual = a / div; + + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.operator / did not return the expected value."); + } + + // A test for operator / (Vector4f, Vector4f) + [Fact] + public void Vector4SingleDivisionTest1() + { + Vector4 a = new Vector4(1.0f, 6.0f, 7.0f, 4.0f); + Vector4 b = new Vector4(5.0f, 2.0f, 3.0f, 8.0f); + + Vector4 expected = new Vector4(1.0f / 5.0f, 6.0f / 2.0f, 7.0f / 3.0f, 4.0f / 8.0f); + Vector4 actual; + + actual = a / b; + + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.operator / did not return the expected value."); + } + + // A test for operator / (Vector4f, Vector4f) + // Divide by zero + [Fact] + public void Vector4SingleDivisionTest2() + { + Vector4 a = new Vector4(-2.0f, 3.0f, Single.MaxValue, Single.NaN); + + Single div = 0.0f; + + Vector4 actual = a / div; + + Assert.True(Single.IsNegativeInfinity(actual.X), "Vector4f.operator / did not return the expected value."); + Assert.True(Single.IsPositiveInfinity(actual.Y), "Vector4f.operator / did not return the expected value."); + Assert.True(Single.IsPositiveInfinity(actual.Z), "Vector4f.operator / did not return the expected value."); + Assert.True(Single.IsNaN(actual.W), "Vector4f.operator / did not return the expected value."); + } + + // A test for operator / (Vector4f, Vector4f) + // Divide by zero + [Fact] + public void Vector4SingleDivisionTest3() + { + Vector4 a = new Vector4(0.047f, -3.0f, Single.NegativeInfinity, Single.MinValue); + Vector4 b = new Vector4(); + + Vector4 actual = a / b; + + Assert.True(Single.IsPositiveInfinity(actual.X), "Vector4f.operator / did not return the expected value."); + Assert.True(Single.IsNegativeInfinity(actual.Y), "Vector4f.operator / did not return the expected value."); + Assert.True(Single.IsNegativeInfinity(actual.Z), "Vector4f.operator / did not return the expected value."); + Assert.True(Single.IsNegativeInfinity(actual.W), "Vector4f.operator / did not return the expected value."); + } + + // A test for operator + (Vector4f, Vector4f) + [Fact] + public void Vector4SingleAdditionTest() + { + Vector4 a = new Vector4(1.0f, 2.0f, 3.0f, 4.0f); + Vector4 b = new Vector4(5.0f, 6.0f, 7.0f, 8.0f); + + Vector4 expected = new Vector4(6.0f, 8.0f, 10.0f, 12.0f); + Vector4 actual; + + actual = a + b; + + Assert.True(MathHelper.Equal(expected, actual), "Vector4f.operator + did not return the expected value."); + } + + [Fact] + public void OperatorAddTest() + { + Vector4 v1 = new Vector4(2.5f, 2.0f, 3.0f, 3.3f); + Vector4 v2 = new Vector4(5.5f, 4.5f, 6.5f, 7.5f); + + Vector4 v3 = v1 + v2; + Vector4 v5 = new Vector4(-1.0f, 0.0f, 0.0f, Single.NaN); + Vector4 v4 = v1 + v5; + Assert.Equal(8.0f, v3.X); + Assert.Equal(6.5f, v3.Y); + Assert.Equal(9.5f, v3.Z); + Assert.Equal(10.8f, v3.W); + Assert.Equal(1.5f, v4.X); + Assert.Equal(2.0f, v4.Y); + Assert.Equal(3.0f, v4.Z); + Assert.Equal(Single.NaN, v4.W); + } + + // A test for Vector4f (Single, Single, Single, Single) + [Fact] + public void Vector4SingleConstructorTest() + { + Single x = 1.0f; + Single y = 2.0f; + Single z = 3.0f; + Single w = 4.0f; + + Vector4 target = new Vector4(x, y, z, w); + + Assert.True(MathHelper.Equal(target.X, x) && MathHelper.Equal(target.Y, y) && MathHelper.Equal(target.Z, z) && MathHelper.Equal(target.W, w), + "Vector4f constructor(x,y,z,w) did not return the expected value."); + } + + // A test for Vector4f (Vector2f, Single, Single) + [Fact] + public void Vector4SingleConstructorTest1() + { + Vector2 a = new Vector2(1.0f, 2.0f); + Single z = 3.0f; + Single w = 4.0f; + + Vector4 target = new Vector4(a, z, w); + Assert.True(MathHelper.Equal(target.X, a.X) && MathHelper.Equal(target.Y, a.Y) && MathHelper.Equal(target.Z, z) && MathHelper.Equal(target.W, w), + "Vector4f constructor(Vector2f,z,w) did not return the expected value."); + } + + // A test for Vector4f (Vector3f, Single) + [Fact] + public void Vector4SingleConstructorTest2() + { + Vector3 a = new Vector3(1.0f, 2.0f, 3.0f); + Single w = 4.0f; + + Vector4 target = new Vector4(a, w); + + Assert.True(MathHelper.Equal(target.X, a.X) && MathHelper.Equal(target.Y, a.Y) && MathHelper.Equal(target.Z, a.Z) && MathHelper.Equal(target.W, w), + "Vector4f constructor(Vector3f,w) did not return the expected value."); + } + + // A test for Vector4f () + // Constructor with no parameter + [Fact] + public void Vector4SingleConstructorTest4() + { + Vector4 a = new Vector4(); + + Assert.Equal(0.0f, a.X); + Assert.Equal(0.0f, a.Y); + Assert.Equal(0.0f, a.Z); + Assert.Equal(0.0f, a.W); + } + + // A test for Vector4f () + // Constructor with special Singleing values + [Fact] + public void Vector4SingleConstructorTest5() + { + Vector4 target = new Vector4(Single.NaN, Single.MaxValue, Single.PositiveInfinity, Single.Epsilon); + + Assert.True(Single.IsNaN(target.X), "Vector4f.constructor (Single, Single, Single, Single) did not return the expected value."); + Assert.True(Single.Equals(Single.MaxValue, target.Y), "Vector4f.constructor (Single, Single, Single, Single) did not return the expected value."); + Assert.True(Single.IsPositiveInfinity(target.Z), "Vector4f.constructor (Single, Single, Single, Single) did not return the expected value."); + Assert.True(Single.Equals(Single.Epsilon, target.W), "Vector4f.constructor (Single, Single, Single, Single) did not return the expected value."); + } + + // A test for Add (Vector4f, Vector4f) + [Fact] + public void Vector4SingleAddTest() + { + Vector4 a = new Vector4(1.0f, 2.0f, 3.0f, 4.0f); + Vector4 b = new Vector4(5.0f, 6.0f, 7.0f, 8.0f); + + Vector4 expected = new Vector4(6.0f, 8.0f, 10.0f, 12.0f); + Vector4 actual; + + actual = Vector4.Add(a, b); + Assert.Equal(expected, actual); + } + + // A test for Divide (Vector4f, Single) + [Fact] + public void Vector4SingleDivideTest() + { + Vector4 a = new Vector4(1.0f, 2.0f, 3.0f, 4.0f); + Single div = 2.0f; + Vector4 expected = new Vector4(0.5f, 1.0f, 1.5f, 2.0f); + Vector4 actual; + actual = Vector4.Divide(a, div); + Assert.Equal(expected, actual); + } + + // A test for Divide (Vector4f, Vector4f) + [Fact] + public void Vector4SingleDivideTest1() + { + Vector4 a = new Vector4(1.0f, 6.0f, 7.0f, 4.0f); + Vector4 b = new Vector4(5.0f, 2.0f, 3.0f, 8.0f); + + Vector4 expected = new Vector4(1.0f / 5.0f, 6.0f / 2.0f, 7.0f / 3.0f, 4.0f / 8.0f); + Vector4 actual; + + actual = Vector4.Divide(a, b); + Assert.Equal(expected, actual); + } + + // A test for Equals (object) + [Fact] + public void Vector4SingleEqualsTest() + { + Vector4 a = new Vector4(1.0f, 2.0f, 3.0f, 4.0f); + Vector4 b = new Vector4(1.0f, 2.0f, 3.0f, 4.0f); + + // case 1: compare between same values + object obj = b; + + bool expected = true; + bool actual = a.Equals(obj); + Assert.Equal(expected, actual); + + // case 2: compare between different values + b.X = 10.0f; + obj = b; + expected = false; + actual = a.Equals(obj); + Assert.Equal(expected, actual); + + // case 3: compare between different types. + obj = new Quaternion(); + expected = false; + actual = a.Equals(obj); + Assert.Equal(expected, actual); + + // case 3: compare against null. + obj = null; + expected = false; + actual = a.Equals(obj); + Assert.Equal(expected, actual); + } + + // A test for Multiply (Single, Vector4f) + [Fact] + public void Vector4SingleMultiplyTest() + { + Vector4 a = new Vector4(1.0f, 2.0f, 3.0f, 4.0f); + const Single factor = 2.0f; + Vector4 expected = new Vector4(2.0f, 4.0f, 6.0f, 8.0f); + Vector4 actual = Vector4.Multiply(factor, a); + Assert.Equal(expected, actual); + } + + // A test for Multiply (Vector4f, Single) + [Fact] + public void Vector4SingleMultiplyTest2() + { + Vector4 a = new Vector4(1.0f, 2.0f, 3.0f, 4.0f); + const Single factor = 2.0f; + Vector4 expected = new Vector4(2.0f, 4.0f, 6.0f, 8.0f); + Vector4 actual = Vector4.Multiply(a, factor); + Assert.Equal(expected, actual); + } + + // A test for Multiply (Vector4f, Vector4f) + [Fact] + public void Vector4SingleMultiplyTest3() + { + Vector4 a = new Vector4(1.0f, 2.0f, 3.0f, 4.0f); + Vector4 b = new Vector4(5.0f, 6.0f, 7.0f, 8.0f); + + Vector4 expected = new Vector4(5.0f, 12.0f, 21.0f, 32.0f); + Vector4 actual; + + actual = Vector4.Multiply(a, b); + Assert.Equal(expected, actual); + } + + // A test for Negate (Vector4f) + [Fact] + public void Vector4SingleNegateTest() + { + Vector4 a = new Vector4(1.0f, 2.0f, 3.0f, 4.0f); + + Vector4 expected = new Vector4(-1.0f, -2.0f, -3.0f, -4.0f); + Vector4 actual; + + actual = Vector4.Negate(a); + Assert.Equal(expected, actual); + } + + // A test for operator != (Vector4f, Vector4f) + [Fact] + public void Vector4SingleInequalityTest() + { + Vector4 a = new Vector4(1.0f, 2.0f, 3.0f, 4.0f); + Vector4 b = new Vector4(1.0f, 2.0f, 3.0f, 4.0f); + + // case 1: compare between same values + bool expected = false; + bool actual = a != b; + Assert.Equal(expected, actual); + + // case 2: compare between different values + b.X = 10.0f; + expected = true; + actual = a != b; + Assert.Equal(expected, actual); + } + + // A test for operator == (Vector4f, Vector4f) + [Fact] + public void Vector4SingleEqualityTest() + { + Vector4 a = new Vector4(1.0f, 2.0f, 3.0f, 4.0f); + Vector4 b = new Vector4(1.0f, 2.0f, 3.0f, 4.0f); + + // case 1: compare between same values + bool expected = true; + bool actual = a == b; + Assert.Equal(expected, actual); + + // case 2: compare between different values + b.X = 10.0f; + expected = false; + actual = a == b; + Assert.Equal(expected, actual); + } + + // A test for Subtract (Vector4f, Vector4f) + [Fact] + public void Vector4SingleSubtractTest() + { + Vector4 a = new Vector4(1.0f, 6.0f, 3.0f, 4.0f); + Vector4 b = new Vector4(5.0f, 2.0f, 3.0f, 9.0f); + + Vector4 expected = new Vector4(-4.0f, 4.0f, 0.0f, -5.0f); + Vector4 actual; + + actual = Vector4.Subtract(a, b); + + Assert.Equal(expected, actual); + } + + // A test for UnitW + [Fact] + public void Vector4SingleUnitWTest() + { + Vector4 val = new Vector4(0.0f, 0.0f, 0.0f, 1.0f); + Assert.Equal(val, Vector4.UnitW); + } + + // A test for UnitX + [Fact] + public void Vector4SingleUnitXTest() + { + Vector4 val = new Vector4(1.0f, 0.0f, 0.0f, 0.0f); + Assert.Equal(val, Vector4.UnitX); + } + + // A test for UnitY + [Fact] + public void Vector4SingleUnitYTest() + { + Vector4 val = new Vector4(0.0f, 1.0f, 0.0f, 0.0f); + Assert.Equal(val, Vector4.UnitY); + } + + // A test for UnitZ + [Fact] + public void Vector4SingleUnitZTest() + { + Vector4 val = new Vector4(0.0f, 0.0f, 1.0f, 0.0f); + Assert.Equal(val, Vector4.UnitZ); + } + + // A test for One + [Fact] + public void Vector4SingleOneTest() + { + Vector4 val = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); + Assert.Equal(val, Vector4.One); + } + + // A test for Zero + [Fact] + public void Vector4SingleZeroTest() + { + Vector4 val = new Vector4(0.0f, 0.0f, 0.0f, 0.0f); + Assert.Equal(val, Vector4.Zero); + } + + // A test for Equals (Vector4f) + [Fact] + public void Vector4SingleEqualsTest1() + { + Vector4 a = new Vector4(1.0f, 2.0f, 3.0f, 4.0f); + Vector4 b = new Vector4(1.0f, 2.0f, 3.0f, 4.0f); + + // case 1: compare between same values + Assert.True(a.Equals(b)); + + // case 2: compare between different values + b.X = 10.0f; + Assert.False(a.Equals(b)); + } + + // A test for Vector4f (Single) + [Fact] + public void Vector4SingleConstructorTest6() + { + Single value = 1.0f; + Vector4 target = new Vector4(value); + + Vector4 expected = new Vector4(value, value, value, value); + Assert.Equal(expected, target); + + value = 2.0f; + target = new Vector4(value); + expected = new Vector4(value, value, value, value); + Assert.Equal(expected, target); + } + + // A test for Vector4f comparison involving NaN values + [Fact] + public void Vector4SingleEqualsNanTest() + { + Vector4 a = new Vector4(Single.NaN, 0, 0, 0); + Vector4 b = new Vector4(0, Single.NaN, 0, 0); + Vector4 c = new Vector4(0, 0, Single.NaN, 0); + Vector4 d = new Vector4(0, 0, 0, Single.NaN); + + Assert.False(a == Vector4.Zero); + Assert.False(b == Vector4.Zero); + Assert.False(c == Vector4.Zero); + Assert.False(d == Vector4.Zero); + + Assert.True(a != Vector4.Zero); + Assert.True(b != Vector4.Zero); + Assert.True(c != Vector4.Zero); + Assert.True(d != Vector4.Zero); + + Assert.False(a.Equals(Vector4.Zero)); + Assert.False(b.Equals(Vector4.Zero)); + Assert.False(c.Equals(Vector4.Zero)); + Assert.False(d.Equals(Vector4.Zero)); + + // Counterintuitive result - IEEE rules for NaN comparison are weird! + Assert.False(a.Equals(a)); + Assert.False(b.Equals(b)); + Assert.False(c.Equals(c)); + Assert.False(d.Equals(d)); + } + + [Fact] + public void Vector4SingleAbsTest() + { + Vector4 v1 = new Vector4(-2.5f, 2.0f, 3.0f, 3.3f); + Vector4 v3 = Vector4.Abs(new Vector4(Single.PositiveInfinity, 0.0f, Single.NegativeInfinity, Single.NaN)); + Vector4 v = Vector4.Abs(v1); + Assert.Equal(2.5f, v.X); + Assert.Equal(2.0f, v.Y); + Assert.Equal(3.0f, v.Z); + Assert.Equal(3.3f, v.W); + Assert.Equal(Single.PositiveInfinity, v3.X); + Assert.Equal(0.0f, v3.Y); + Assert.Equal(Single.PositiveInfinity, v3.Z); + Assert.Equal(Single.NaN, v3.W); + } + + [Fact] + public void Vector4SingleSqrtTest() + { + Vector4 v1 = new Vector4(-2.5f, 2.0f, 3.0f, 3.3f); + Vector4 v2 = new Vector4(5.5f, 4.5f, 6.5f, 7.5f); + Assert.Equal(2, (int)Vector4.SquareRoot(v2).X); + Assert.Equal(2, (int)Vector4.SquareRoot(v2).Y); + Assert.Equal(2, (int)Vector4.SquareRoot(v2).Z); + Assert.Equal(2, (int)Vector4.SquareRoot(v2).W); + Assert.Equal(Single.NaN, Vector4.SquareRoot(v1).X); + } + + // A test to make sure these types are blittable directly into GPU buffer memory layouts + [Fact] + public unsafe void Vector4SingleSizeofTest() + { + Assert.Equal(16, sizeof(Vector4)); + Assert.Equal(32, sizeof(Vector4Single_2x)); + Assert.Equal(20, sizeof(Vector4SinglePlusSingle)); + Assert.Equal(40, sizeof(Vector4SinglePlusSingle_2x)); + } + + [StructLayout(LayoutKind.Sequential)] + struct Vector4Single_2x + { + private Vector4 _a; + private Vector4 _b; + } + + [StructLayout(LayoutKind.Sequential)] + struct Vector4SinglePlusSingle + { + private Vector4 _v; + private Single _f; + } + + [StructLayout(LayoutKind.Sequential)] + struct Vector4SinglePlusSingle_2x + { + private Vector4SinglePlusSingle _a; + private Vector4SinglePlusSingle _b; + } + + [Fact] + public void SetFieldsTest() + { + Vector4 v3 = new Vector4(4f, 5f, 6f, 7f); + v3 = v3.WithX(1.0f); + v3 = v3.WithY(2.0f); + v3 = v3.WithZ(3.0f); + v3 = v3.WithW(4.0f); + Assert.Equal(1.0f, v3.X); + Assert.Equal(2.0f, v3.Y); + Assert.Equal(3.0f, v3.Z); + Assert.Equal(4.0f, v3.W); + Vector4 v4 = v3; + v4 = v4.WithY(0.5f); + v4 = v4.WithZ(2.2f); + v4 = v4.WithW(3.5f); + Assert.Equal(1.0f, v4.X); + Assert.Equal(0.5f, v4.Y); + Assert.Equal(2.2f, v4.Z); + Assert.Equal(3.5f, v4.W); + Assert.Equal(2.0f, v3.Y); + } + + [Fact] + public void EmbeddedVectorSetFields() + { + EmbeddedVectorObject evo = new EmbeddedVectorObject(); + evo.FieldVector = evo.FieldVector.WithX(5.0f); + evo.FieldVector = evo.FieldVector.WithY(5.0f); + evo.FieldVector = evo.FieldVector.WithZ(5.0f); + evo.FieldVector = evo.FieldVector.WithW(5.0f); + Assert.Equal(5.0f, evo.FieldVector.X); + Assert.Equal(5.0f, evo.FieldVector.Y); + Assert.Equal(5.0f, evo.FieldVector.Z); + Assert.Equal(5.0f, evo.FieldVector.W); + } + + [Fact] + public void DeeplyEmbeddedObjectTest() + { + DeeplyEmbeddedClass obj = new DeeplyEmbeddedClass(); + obj.L0.L1.L2.L3.L4.L5.L6.L7.EmbeddedVector = obj.L0.L1.L2.L3.L4.L5.L6.L7.EmbeddedVector.WithX(5f); + Assert.Equal(5f, obj.RootEmbeddedObject.X); + Assert.Equal(5f, obj.RootEmbeddedObject.Y); + Assert.Equal(1f, obj.RootEmbeddedObject.Z); + Assert.Equal(-5f, obj.RootEmbeddedObject.W); + obj.L0.L1.L2.L3.L4.L5.L6.L7.EmbeddedVector = new Vector4Single(1, 2, 3, 4); + Assert.Equal(1f, obj.RootEmbeddedObject.X); + Assert.Equal(2f, obj.RootEmbeddedObject.Y); + Assert.Equal(3f, obj.RootEmbeddedObject.Z); + Assert.Equal(4f, obj.RootEmbeddedObject.W); + } + + [Fact] + public void DeeplyEmbeddedStructTest() + { + DeeplyEmbeddedStruct obj = DeeplyEmbeddedStruct.Create(); + obj.L0.L1.L2.L3.L4.L5.L6.L7.EmbeddedVector = obj.L0.L1.L2.L3.L4.L5.L6.L7.EmbeddedVector.WithX(5f); + Assert.Equal(5f, obj.RootEmbeddedObject.X); + Assert.Equal(5f, obj.RootEmbeddedObject.Y); + Assert.Equal(1f, obj.RootEmbeddedObject.Z); + Assert.Equal(-5f, obj.RootEmbeddedObject.W); + obj.L0.L1.L2.L3.L4.L5.L6.L7.EmbeddedVector = new Vector4Single(1, 2, 3, 4); + Assert.Equal(1f, obj.RootEmbeddedObject.X); + Assert.Equal(2f, obj.RootEmbeddedObject.Y); + Assert.Equal(3f, obj.RootEmbeddedObject.Z); + Assert.Equal(4f, obj.RootEmbeddedObject.W); + } + + private class EmbeddedVectorObject + { + public Vector4Single FieldVector; + } + + private class DeeplyEmbeddedClass + { + public readonly Level0 L0 = new Level0(); + public Vector4Single RootEmbeddedObject { get { return L0.L1.L2.L3.L4.L5.L6.L7.EmbeddedVector; } } + public class Level0 + { + public readonly Level1 L1 = new Level1(); + public class Level1 + { + public readonly Level2 L2 = new Level2(); + public class Level2 + { + public readonly Level3 L3 = new Level3(); + public class Level3 + { + public readonly Level4 L4 = new Level4(); + public class Level4 + { + public readonly Level5 L5 = new Level5(); + public class Level5 + { + public readonly Level6 L6 = new Level6(); + public class Level6 + { + public readonly Level7 L7 = new Level7(); + public class Level7 + { + public Vector4Single EmbeddedVector = new Vector4Single(1, 5, 1, -5); + } + } + } + } + } + } + } + } + } + + // Contrived test for strangely-sized and shaped embedded structures, with unused buffer fields. + #pragma warning disable 0169 + private struct DeeplyEmbeddedStruct + { + public static DeeplyEmbeddedStruct Create() + { + var obj = new DeeplyEmbeddedStruct(); + obj.L0 = new Level0(); + obj.L0.L1 = new Level0.Level1(); + obj.L0.L1.L2 = new Level0.Level1.Level2(); + obj.L0.L1.L2.L3 = new Level0.Level1.Level2.Level3(); + obj.L0.L1.L2.L3.L4 = new Level0.Level1.Level2.Level3.Level4(); + obj.L0.L1.L2.L3.L4.L5 = new Level0.Level1.Level2.Level3.Level4.Level5(); + obj.L0.L1.L2.L3.L4.L5.L6 = new Level0.Level1.Level2.Level3.Level4.Level5.Level6(); + obj.L0.L1.L2.L3.L4.L5.L6.L7 = new Level0.Level1.Level2.Level3.Level4.Level5.Level6.Level7(); + obj.L0.L1.L2.L3.L4.L5.L6.L7.EmbeddedVector = new Vector4Single(1, 5, 1, -5); + + return obj; + } + + public Level0 L0; + public Vector4Single RootEmbeddedObject { get { return L0.L1.L2.L3.L4.L5.L6.L7.EmbeddedVector; } } + public struct Level0 + { + private float _buffer0, _buffer1; + public Level1 L1; + private float _buffer2; + public struct Level1 + { + private float _buffer0, _buffer1; + public Level2 L2; + private byte _buffer2; + public struct Level2 + { + public Level3 L3; + private float _buffer0; + private byte _buffer1; + public struct Level3 + { + public Level4 L4; + public struct Level4 + { + private float _buffer0; + public Level5 L5; + private long _buffer1; + private byte _buffer2; + private double _buffer3; + public struct Level5 + { + private byte _buffer0; + public Level6 L6; + public struct Level6 + { + private byte _buffer0; + public Level7 L7; + private byte _buffer1, _buffer2; + public struct Level7 + { + public Vector4Single EmbeddedVector; + } + } + } + } + } + } + } + } + } + #pragma warning restore 0169 + } +} \ No newline at end of file diff --git a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems index f3971d1fc235e5..b04631b7dafc55 100644 --- a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems +++ b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems @@ -438,6 +438,7 @@ + diff --git a/src/libraries/System.Private.CoreLib/src/System/Numerics/Vector2_1.cs b/src/libraries/System.Private.CoreLib/src/System/Numerics/Vector2_1.cs new file mode 100644 index 00000000000000..a0a730c6309e3b --- /dev/null +++ b/src/libraries/System.Private.CoreLib/src/System/Numerics/Vector2_1.cs @@ -0,0 +1,639 @@ +using System; +using System.Diagnostics; +using System.Globalization; +using System.Runtime.CompilerServices; +using System.Text; + +namespace System.Numerics +{ + public readonly struct Vector2 : IEquatable>, IFormattable + where T : struct + { + // Fields + + public T X { get; } + public T Y { get; } + + // Constructors + + public Vector2(T value) + { + ThrowForUnsupportedVectorBaseType(); + + X = value; + Y = value; + } + + public Vector2(T x, T y) + { + ThrowForUnsupportedVectorBaseType(); + + X = x; + Y = y; + } + + public Vector2(T[] value) : this(value.AsSpan()) + { + } + + public Vector2(T[] value, int offset) : this(value.AsSpan(offset)) + { + } + + public Vector2(ReadOnlySpan value) + { + ThrowForUnsupportedVectorBaseType(); + + if (value.Length < 2) + { + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.value, ExceptionResource.ArgumentOutOfRange_SmallCapacity); + } + + Y = value[1]; + X = value[0]; + } + + // Static Properties + + private static T OneT + { + get + { + ThrowForUnsupportedVectorBaseType(); + + if (typeof(T) == typeof(float)) + { + return 1.0f; + } + else if (typeof(T) == typeof(double)) + { + return 1.0; + } + + return default; + } + } + + public static Vector2 One => new Vector2(OneT); + public static Vector2 UnitX => new Vector2(OneT, default); + public static Vector2 UnitY => new Vector2(default, OneT); + public static Vector2 Zero => new Vector2(default, default); + + // With methods + public Vector2 WithX(T x) => new Vector2(x, Y); + public Vector2 WithY(T y) => new Vector2(X, y); + + // Operators + + public static bool operator ==(Vector2 left, Vector2 right) + { + ThrowForUnsupportedVectorBaseType(); + + if (typeof(T) == typeof(float)) + { + return (float)(object)left.X == (float)(object)right.X + && (float)(object)left.Y == (float)(object)right.Y; + } + else if (typeof(T) == typeof(double)) + { + return (double)(object)left.X == (double)(object)right.X + && (double)(object)left.Y == (double)(object)right.Y; + } + + return default; + } + + public static bool operator !=(Vector2 left, Vector2 right) => !(left == right); + + public static Vector2 operator +(Vector2 value) => value; + public static Vector2 operator -(Vector2 value) + { + ThrowForUnsupportedVectorBaseType(); + + if (typeof(T) == typeof(float)) + { + var negX = -(float)(object)value.X; + var negY = -(float)(object)value.Y; + return new Vector2((T)(object)negX, (T)(object)negY); + } + else if (typeof(T) == typeof(double)) + { + var negX = -(double)(object)value.X; + var negY = -(double)(object)value.Y; + return new Vector2((T)(object)negX, (T)(object)negY); + } + + return default; + } + + public static Vector2 operator +(Vector2 left, Vector2 right) + { + ThrowForUnsupportedVectorBaseType(); + + if (typeof(T) == typeof(float)) + { + var x = (float)(object)left.X + (float)(object)right.X; + var y = (float)(object)left.Y + (float)(object)right.Y; + return new Vector2((T)(object)x, (T)(object)y); + } + else if (typeof(T) == typeof(double)) + { + var x = (double)(object)left.X + (double)(object)right.X; + var y = (double)(object)left.Y + (double)(object)right.Y; + return new Vector2((T)(object)x, (T)(object)y); + } + + return default; + } + + public static Vector2 operator -(Vector2 left, Vector2 right) + { + ThrowForUnsupportedVectorBaseType(); + + if (typeof(T) == typeof(float)) + { + var x = (float)(object)left.X - (float)(object)right.X; + var y = (float)(object)left.Y - (float)(object)right.Y; + return new Vector2((T)(object)x, (T)(object)y); + } + else if (typeof(T) == typeof(double)) + { + var x = (double)(object)left.X - (double)(object)right.X; + var y = (double)(object)left.Y - (double)(object)right.Y; + return new Vector2((T)(object)x, (T)(object)y); + } + + return default; + } + + public static Vector2 operator *(Vector2 left, Vector2 right) + { + ThrowForUnsupportedVectorBaseType(); + + if (typeof(T) == typeof(float)) + { + var x = (float)(object)left.X * (float)(object)right.X; + var y = (float)(object)left.Y * (float)(object)right.Y; + return new Vector2((T)(object)x, (T)(object)y); + } + else if (typeof(T) == typeof(double)) + { + var x = (double)(object)left.X * (double)(object)right.X; + var y = (double)(object)left.Y * (double)(object)right.Y; + return new Vector2((T)(object)x, (T)(object)y); + } + + return default; + } + + public static Vector2 operator /(Vector2 left, Vector2 right) + { + ThrowForUnsupportedVectorBaseType(); + + if (typeof(T) == typeof(float)) + { + var x = (float)(object)left.X / (float)(object)right.X; + var y = (float)(object)left.Y / (float)(object)right.Y; + return new Vector2((T)(object)x, (T)(object)y); + } + else if (typeof(T) == typeof(double)) + { + var x = (double)(object)left.X / (double)(object)right.X; + var y = (double)(object)left.Y / (double)(object)right.Y; + return new Vector2((T)(object)x, (T)(object)y); + } + + return default; + } + + public static Vector2 operator *(Vector2 left, T right) + { + ThrowForUnsupportedVectorBaseType(); + + if (typeof(T) == typeof(float)) + { + var scalar = (float)(object)right; + var x = (float)(object)left.X * scalar; + var y = (float)(object)left.Y * scalar; + return new Vector2((T)(object)x, (T)(object)y); + } + else if (typeof(T) == typeof(double)) + { + var scalar = (double)(object)right; + var x = (double)(object)left.X * scalar; + var y = (double)(object)left.Y * scalar; + return new Vector2((T)(object)x, (T)(object)y); + } + + return default; + } + + public static Vector2 operator /(Vector2 left, T right) + { + ThrowForUnsupportedVectorBaseType(); + + if (typeof(T) == typeof(float)) + { + var scalar = (float)(object)right; + var x = (float)(object)left.X / scalar; + var y = (float)(object)left.Y / scalar; + return new Vector2((T)(object)x, (T)(object)y); + } + else if (typeof(T) == typeof(double)) + { + var scalar = (double)(object)right; + var x = (double)(object)left.X / scalar; + var y = (double)(object)left.Y / scalar; + return new Vector2((T)(object)x, (T)(object)y); + } + + return default; + } + + public static Vector2 operator *(T left, Vector2 right) => right * left; + + // "Friendly" Operators + + public static Vector2 Plus(Vector2 value) => value; // unary plus is nop + public static Vector2 Negate(Vector2 value) => -value; + + public static Vector2 Add(Vector2 left, Vector2 right) => left + right; + public static Vector2 Subtract(Vector2 left, Vector2 right) => left - right; + + public static Vector2 Multiply(Vector2 left, Vector2 right) => left * right; + public static Vector2 Divide(Vector2 left, Vector2 right) => left / right; + + public static Vector2 Multiply(Vector2 left, T right) => left * right; + public static Vector2 Divide(Vector2 left, T right) => left / right; + + public static Vector2 Multiply(T left, Vector2 right) => left * right; + + // Static Methods + + public static Vector2 Abs(Vector2 value) + { + ThrowForUnsupportedVectorBaseType(); + + if (typeof(T) == typeof(float)) + { + var x = MathF.Abs((float)(object)value.X); + var y = MathF.Abs((float)(object)value.Y); + return new Vector2((T)(object)x, (T)(object)y); + } + else if (typeof(T) == typeof(double)) + { + var x = Math.Abs((double)(object)value.X); + var y = Math.Abs((double)(object)value.Y); + return new Vector2((T)(object)x, (T)(object)y); + } + + return default; + } + + public static Vector2 Clamp(Vector2 value, Vector2 min, Vector2 max) => Min(Max(value, min), max); // Keeps style of old Vector2 method (HLSL style) + + public static T Distance(Vector2 left, Vector2 right) + { + return (left - right).Length(); + } + + public static T DistanceSquared(Vector2 left, Vector2 right) => (left - right).LengthSquared(); + + public static T Dot(Vector2 left, Vector2 right) + { + ThrowForUnsupportedVectorBaseType(); + + if (typeof(T) == typeof(float)) + { + var x = (float)(object)left.X * (float)(object)right.X; + var y = (float)(object)left.Y * (float)(object)right.Y; + + return (T)(object)(x + y); + } + else if (typeof(T) == typeof(double)) + { + var x = (double)(object)left.X * (double)(object)right.X; + var y = (double)(object)left.Y * (double)(object)right.Y; + + return (T)(object)(x + y); + } + + return default; + } + + public static Vector2 Lerp(Vector2 min, Vector2 max, T amount) => (min * (One - new Vector2(amount))) + (max * amount); + + public static Vector2 Min(Vector2 left, Vector2 right) + { + ThrowForUnsupportedVectorBaseType(); + + if (typeof(T) == typeof(float)) + { + var x = Math.Min((float)(object)left.X, (float)(object)right.X); + var y = Math.Min((float)(object)left.Y, (float)(object)right.Y); + return new Vector2((T)(object)x, (T)(object)y); + } + else if (typeof(T) == typeof(double)) + { + var x = Math.Min((double)(object)left.X, (double)(object)right.X); + var y = Math.Min((double)(object)left.Y, (double)(object)right.Y); + return new Vector2((T)(object)x, (T)(object)y); + } + + return default; + } + + public static Vector2 Max(Vector2 left, Vector2 right) + { + ThrowForUnsupportedVectorBaseType(); + + if (typeof(T) == typeof(float)) + { + var x = Math.Max((float)(object)left.X, (float)(object)right.X); + var y = Math.Max((float)(object)left.Y, (float)(object)right.Y); + return new Vector2((T)(object)x, (T)(object)y); + } + else if (typeof(T) == typeof(double)) + { + var x = Math.Max((double)(object)left.X, (double)(object)right.X); + var y = Math.Max((double)(object)left.Y, (double)(object)right.Y); + return new Vector2((T)(object)x, (T)(object)y); + } + + return default; + } + + public static Vector2 Normalize(Vector2 value) => value / value.Length(); + + public static Vector2 Reflect(Vector2 incident, Vector2 normal) + { + // reflection = incident - (2 * Dot(incident, normal)) * normal + var dp = Dot(incident, normal); + + + if (typeof(T) == typeof(float)) + { + var asFloat = (float)(object)dp; + var splatDp = new Vector2((T)(object)(asFloat + asFloat)); + return incident - splatDp * normal; + } + else if (typeof(T) == typeof(double)) + { + var asDouble = (double)(object)dp; + var splatDp = new Vector2((T)(object)(asDouble + asDouble)); + return incident - splatDp * normal; + } + + return default; + } + + public static Vector2 SquareRoot(Vector2 value) + { + ThrowForUnsupportedVectorBaseType(); + + if (typeof(T) == typeof(float)) + { + var x = MathF.Sqrt((float)(object)value.X); + var y = MathF.Sqrt((float)(object)value.Y); + return new Vector2((T)(object)x, (T)(object)y); + } + else if (typeof(T) == typeof(double)) + { + var x = Math.Sqrt((double)(object)value.X); + var y = Math.Sqrt((double)(object)value.Y); + return new Vector2((T)(object)x, (T)(object)y); + } + + return default; + } + + // public static Vector2 Transform(Vector2 position, Matrix3x2 matrix) + // { + // ThrowForUnsupportedVectorBaseType(); + + // if (typeof(T) == typeof(float)) + // { + // var posX = (float)(object)position.X; + // var posY = (float)(object)position.X; + // var x = posX * (float)(object)matrix.M11 + posY * (float)(object)matrix.M21 + (float)(object)matrix.M31; + // var y = posX * (float)(object)matrix.M12 + posY * (float)(object)matrix.M22 + (float)(object)matrix.M32; + // return new Vector2((T)(object)x, (T)(object)y); + // } + // else if (typeof(T) == typeof(double)) + // { + // var posX = (double)(object)position.X; + // var posY = (double)(object)position.X; + // var x = posX * (double)(object)matrix.M11 + posY * (double)(object)matrix.M21 + (double)(object)matrix.M31; + // var y = posX * (double)(object)matrix.M12 + posY * (double)(object)matrix.M22 + (double)(object)matrix.M32; + // return new Vector2((T)(object)x, (T)(object)y); + // } + + // return default; + // } + + // public static Vector2 Transform(Vector2 position, Matrix4x4 matrix) + // { + // ThrowForUnsupportedVectorBaseType(); + + // if (typeof(T) == typeof(float)) + // { + // var posX = (float)(object)position.X; + // var posY = (float)(object)position.X; + // var x = posX * (float)(object)matrix.M11 + posY * (float)(object)matrix.M21 + (float)(object)matrix.M41; + // var y = posX * (float)(object)matrix.M12 + posY * (float)(object)matrix.M22 + (float)(object)matrix.M42; + // return new Vector2((T)(object)x, (T)(object)y); + // } + // else if (typeof(T) == typeof(double)) + // { + // var posX = (double)(object)position.X; + // var posY = (double)(object)position.X; + // var x = posX * (double)(object)matrix.M11 + posY * (double)(object)matrix.M21 + (double)(object)matrix.M41; + // var y = posX * (double)(object)matrix.M12 + posY * (double)(object)matrix.M22 + (double)(object)matrix.M42; + // return new Vector2((T)(object)x, (T)(object)y); + // } + + // return default; + // } + + // public static Vector2 Transform(Vector2 position, Quaternion rotation) + // { + // ThrowForUnsupportedVectorBaseType(); + + // if (typeof(T) == typeof(float)) + // { + // var posX = (float)(object)position.X; + // var posY = (float)(object)position.X; + + // var mul2 = rotation + rotation; + + // var x2 = (float)(object)mul2.X; + // var y2 = (float)(object)mul2.Y; + // var z2 = (float)(object)mul2.Z; + + // var wz2 = (float)(object)rotation.W * z2; + // var xx2 = (float)(object)rotation.X * x2; + // var xy2 = (float)(object)rotation.X * y2; + // var yy2 = (float)(object)rotation.Y * y2; + // var zz2 = (float)(object)rotation.Z * z2; + + // var x = posX * (1.0f - yy2 - zz2) + posY* (xy2 - wz2); + // var y = posX * (xy2 + wz2) + posY * (1.0f - xx2 - zz2); + + // return new Vector2((T)(object)x, (T)(object)y); + // } + // else if (typeof(T) == typeof(double)) + // { + // var posX = (double)(object)position.X; + // var posY = (double)(object)position.X; + + // var mul2 = rotation + rotation; + + // var x2 = (double)(object)mul2.X; + // var y2 = (double)(object)mul2.Y; + // var z2 = (double)(object)mul2.Z; + + // var wz2 = (double)(object)rotation.W * z2; + // var xx2 = (double)(object)rotation.X * x2; + // var xy2 = (double)(object)rotation.X * y2; + // var yy2 = (double)(object)rotation.Y * y2; + // var zz2 = (double)(object)rotation.Z * z2; + + // var x = posX * (1.0 - yy2 - zz2) + posY* (xy2 - wz2); + // var y = posX * (xy2 + wz2) + posY * (1.0 - xx2 - zz2); + + // return new Vector2((T)(object)x, (T)(object)y); + // } + + // return default; + // } + + // public static Vector2 TransformNormal(Vector2 normal, Matrix3x2 matrix) + // { + // ThrowForUnsupportedVectorBaseType(); + + // if (typeof(T) == typeof(float)) + // { + // var normalX = (float)(object)normal.X; + // var normalY = (float)(object)normal.X; + // var x = normalX * (float)(object)matrix.M11 + normalY * (float)(object)matrix.M21; + // var y = normalX * (float)(object)matrix.M12 + normalY * (float)(object)matrix.M22; + // return new Vector2((T)(object)x, (T)(object)y); + // } + // else if (typeof(T) == typeof(double)) + // { + // var normalX = (double)(object)normal.X; + // var normalY = (double)(object)normal.X; + // var x = normalX * (double)(object)matrix.M11 + normalY * (double)(object)matrix.M21; + // var y = normalX * (double)(object)matrix.M12 + normalY * (double)(object)matrix.M22; + // return new Vector2((T)(object)x, (T)(object)y); + // } + + // return default; + // } + + // public static Vector2 TransformNormal(Vector2 normal, Matrix4x4 matrix) + // { + // ThrowForUnsupportedVectorBaseType(); + + // if (typeof(T) == typeof(float)) + // { + // var normalX = (float)(object)normal.X; + // var normalY = (float)(object)normal.X; + // var x = normalX * (float)(object)matrix.M11 + normalY * (float)(object)matrix.M21; + // var y = normalX * (float)(object)matrix.M12 + normalY * (float)(object)matrix.M22; + // return new Vector2((T)(object)x, (T)(object)y); + // } + // else if (typeof(T) == typeof(double)) + // { + // var normalX = (double)(object)normal.X; + // var normalY = (double)(object)normal.X; + // var x = normalX * (double)(object)matrix.M11 + normalY * (double)(object)matrix.M21; + // var y = normalX * (double)(object)matrix.M12 + normalY * (double)(object)matrix.M22; + // return new Vector2((T)(object)x, (T)(object)y); + // } + + // return default; + // } + + // Methods + + public readonly void CopyTo(T[] array) => CopyTo(array.AsSpan()); + + public readonly void CopyTo(T[] array, int index) => CopyTo(array.AsSpan(index)); + + public readonly void CopyTo(Span destination) + { + if (destination.Length < 2) + { + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.destination, ExceptionResource.ArgumentOutOfRange_SmallCapacity); + } + + destination[1] = Y; + destination[0] = X; + } + + public override readonly bool Equals(object? obj) => obj is Vector2 other && Equals(other); + public readonly bool Equals(Vector2 other) => this == other; + + public override readonly int GetHashCode() => HashCode.Combine(X, Y); + + public readonly T Length() + { + if (typeof(T) == typeof(float)) + { + var squared = LengthSquared(); + return (T)(object)MathF.Sqrt((float)(object)squared); + } + else if (typeof(T) == typeof(double)) + { + var squared = LengthSquared(); + return (T)(object)Math.Sqrt((double)(object)squared); + } + + return default; + } + + public readonly T LengthSquared() => Dot(this, this); + + public readonly override string ToString() => ToString("G"); + + public readonly string ToString(string? format) => ToString(format, CultureInfo.CurrentCulture); + + public readonly string ToString(string? format, IFormatProvider? formatProvider) + { + ThrowForUnsupportedVectorBaseType(); + + static string ToString(T val, string? format, IFormatProvider? formatProvider) + { + if (typeof(T) == typeof(float)) + { + return ((float)(object)val).ToString(format, formatProvider); + } + else if (typeof(T) == typeof(double)) + { + return ((double)(object)val).ToString(format, formatProvider); + } + + return default!; + } + + StringBuilder sb = new StringBuilder(); + string separator = NumberFormatInfo.GetInstance(formatProvider).NumberGroupSeparator; + sb.Append('<'); + sb.Append(ToString(X, format, formatProvider)); + sb.Append(separator); + sb.Append(' '); + sb.Append(ToString(Y, format, formatProvider)); + sb.Append('>'); + return sb.ToString(); + } + + internal static void ThrowForUnsupportedVectorBaseType() + { + if (typeof(T) != typeof(float) && typeof(T) != typeof(double)) + { + ThrowHelper.ThrowNotSupportedException(ExceptionResource.Arg_TypeNotSupported); + } + } + } +} \ No newline at end of file diff --git a/src/libraries/System.Private.CoreLib/src/System/ThrowHelper.cs b/src/libraries/System.Private.CoreLib/src/System/ThrowHelper.cs index 817a86d3c7df76..82bef47f762cd7 100644 --- a/src/libraries/System.Private.CoreLib/src/System/ThrowHelper.cs +++ b/src/libraries/System.Private.CoreLib/src/System/ThrowHelper.cs @@ -700,6 +700,10 @@ private static string GetArgumentName(ExceptionArgument argument) return "prefix"; case ExceptionArgument.suffix: return "suffix"; + case ExceptionArgument.offset: + return "offset"; + case ExceptionArgument.destination: + return "destination"; default: Debug.Fail("The enum value is not defined, please check the ExceptionArgument Enum."); return ""; @@ -958,6 +962,8 @@ internal enum ExceptionArgument options, prefix, suffix, + offset, + destination } //