From c48a161bb56d73ed6c82c1b4c66d25eb68749444 Mon Sep 17 00:00:00 2001 From: Steve Date: Fri, 22 Nov 2024 02:44:19 +0900 Subject: [PATCH 1/6] Fix Matrix4x4.CreateReflection --- .../tests/Matrix4x4Tests.cs | 32 ++++++++++++++++++- .../src/System/Numerics/Matrix4x4.Impl.cs | 2 +- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Numerics.Vectors/tests/Matrix4x4Tests.cs b/src/libraries/System.Numerics.Vectors/tests/Matrix4x4Tests.cs index 80877852d6378..c7c95d06279da 100644 --- a/src/libraries/System.Numerics.Vectors/tests/Matrix4x4Tests.cs +++ b/src/libraries/System.Numerics.Vectors/tests/Matrix4x4Tests.cs @@ -819,11 +819,41 @@ public void Matrix4x4CreateReflectionTest01() Vector3 v = point - pp; float d = Vector3.Dot(v, plane.Normal); Vector3 vp = point - 2.0f * d * plane.Normal; - Assert.True(MathHelper.Equal(rp, vp), "Matrix4x4.Reflection did not provide expected value."); + Assert.True(MathHelper.Equal(rp, vp), "Matrix4x4.CreateReflection did not provide expected value."); } } } + [Fact] + public void Matrix4x4CreateReflectionTest02() + { + Plane plane = new Plane(0, 1, 0, 60); + Matrix4x4 actual = Matrix4x4.CreateReflection(plane); + + Matrix4x4 expected = new Matrix4x4(); + expected.M11 = 1; + expected.M12 = 0; + expected.M13 = 0; + expected.M14 = 0; + + expected.M21 = 0; + expected.M22 = -1; + expected.M23 = 0; + expected.M24 = 0; + + expected.M31 = 0; + expected.M32 = 0; + expected.M33 = 1; + expected.M34 = 0; + + expected.M41 = -0; + expected.M42 = -120; + expected.M43 = -0; + expected.M44 = 1; + + Assert.True(MathHelper.Equal(expected, actual), "Matrix4x4.CreateReflection did not return the expected value."); + } + // A test for CreateRotationZ (float) [Fact] public void Matrix4x4CreateRotationZTest() diff --git a/src/libraries/System.Private.CoreLib/src/System/Numerics/Matrix4x4.Impl.cs b/src/libraries/System.Private.CoreLib/src/System/Numerics/Matrix4x4.Impl.cs index 3a489f0fc1af0..889311ca07302 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Numerics/Matrix4x4.Impl.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Numerics/Matrix4x4.Impl.cs @@ -620,7 +620,7 @@ public static Impl CreateReflection(in Plane value) // https://github.com/microsoft/DirectXMath/blob/master/Inc/DirectXMathMatrix.inl Vector4 p = Plane.Normalize(value).AsVector4(); - Vector4 s = p * -2.0f; + Vector4 s = p * Vector4.Create(-2.0f, -2.0f, -2.0f, 0.0f); Impl result; From 17d4803abc252bff60dc0253944db907660866e5 Mon Sep 17 00:00:00 2001 From: Steve Date: Fri, 22 Nov 2024 03:27:17 +0900 Subject: [PATCH 2/6] Update tests --- .../tests/Matrix4x4Tests.cs | 62 ++++++++++++------- 1 file changed, 39 insertions(+), 23 deletions(-) diff --git a/src/libraries/System.Numerics.Vectors/tests/Matrix4x4Tests.cs b/src/libraries/System.Numerics.Vectors/tests/Matrix4x4Tests.cs index c7c95d06279da..65b0e517598b2 100644 --- a/src/libraries/System.Numerics.Vectors/tests/Matrix4x4Tests.cs +++ b/src/libraries/System.Numerics.Vectors/tests/Matrix4x4Tests.cs @@ -819,7 +819,26 @@ public void Matrix4x4CreateReflectionTest01() Vector3 v = point - pp; float d = Vector3.Dot(v, plane.Normal); Vector3 vp = point - 2.0f * d * plane.Normal; - Assert.True(MathHelper.Equal(rp, vp), "Matrix4x4.CreateReflection did not provide expected value."); + + Assert.Equal(vp.M11, rp.M11, 0.0f); + Assert.Equal(vp.M12, rp.M12, 0.0f); + Assert.Equal(vp.M13, rp.M13, 0.0f); + Assert.Equal(vp.M14, rp.M14, 0.0f); + + Assert.Equal(vp.M21, rp.M21, 0.0f); + Assert.Equal(vp.M22, rp.M22, 0.0f); + Assert.Equal(vp.M23, rp.M23, 0.0f); + Assert.Equal(vp.M24, rp.M24, 0.0f); + + Assert.Equal(vp.M31, rp.M31, 0.0f); + Assert.Equal(vp.M32, rp.M32, 0.0f); + Assert.Equal(vp.M33, rp.M33, 0.0f); + Assert.Equal(vp.M34, rp.M34, 0.0f); + + Assert.Equal(vp.M41, rp.M41, 0.0f); + Assert.Equal(vp.M42, rp.M42, 0.0f); + Assert.Equal(vp.M43, rp.M43, 0.0f); + Assert.Equal(vp.M44, rp.M44, 0.0f); } } } @@ -830,28 +849,25 @@ public void Matrix4x4CreateReflectionTest02() Plane plane = new Plane(0, 1, 0, 60); Matrix4x4 actual = Matrix4x4.CreateReflection(plane); - Matrix4x4 expected = new Matrix4x4(); - expected.M11 = 1; - expected.M12 = 0; - expected.M13 = 0; - expected.M14 = 0; - - expected.M21 = 0; - expected.M22 = -1; - expected.M23 = 0; - expected.M24 = 0; - - expected.M31 = 0; - expected.M32 = 0; - expected.M33 = 1; - expected.M34 = 0; - - expected.M41 = -0; - expected.M42 = -120; - expected.M43 = -0; - expected.M44 = 1; - - Assert.True(MathHelper.Equal(expected, actual), "Matrix4x4.CreateReflection did not return the expected value."); + Assert.Equal(1.0f, actual.M11, 0.0f); + Assert.Equal(0.0f, actual.M12, 0.0f); + Assert.Equal(0.0f, actual.M13, 0.0f); + Assert.Equal(0.0f, actual.M14, 0.0f); + + Assert.Equal(0.0f, actual.M21, 0.0f); + Assert.Equal(-1.0f, actual.M22, 0.0f); + Assert.Equal(0.0f, actual.M23, 0.0f); + Assert.Equal(0.0f, actual.M24, 0.0f); + + Assert.Equal(0.0f, actual.M31, 0.0f); + Assert.Equal(0.0f, actual.M32, 0.0f); + Assert.Equal(1.0f, actual.M33, 0.0f); + Assert.Equal(0.0f, actual.M34, 0.0f); + + Assert.Equal(-0.0f, actual.M41, 0.0f); + Assert.Equal(-120.0f, actual.M42, 0.0f); + Assert.Equal(-0.0f, actual.M43, 0.0f); + Assert.Equal(1.0f, actual.M44, 0.0f); } // A test for CreateRotationZ (float) From fff7fca5a6f3612554fdeb32b983fdaf49e4c61c Mon Sep 17 00:00:00 2001 From: Steve Date: Fri, 22 Nov 2024 03:57:10 +0900 Subject: [PATCH 3/6] Use AssertExtensions instead --- .../tests/Matrix4x4Tests.cs | 76 +++++++++---------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/src/libraries/System.Numerics.Vectors/tests/Matrix4x4Tests.cs b/src/libraries/System.Numerics.Vectors/tests/Matrix4x4Tests.cs index 65b0e517598b2..090701c092a48 100644 --- a/src/libraries/System.Numerics.Vectors/tests/Matrix4x4Tests.cs +++ b/src/libraries/System.Numerics.Vectors/tests/Matrix4x4Tests.cs @@ -820,25 +820,25 @@ public void Matrix4x4CreateReflectionTest01() float d = Vector3.Dot(v, plane.Normal); Vector3 vp = point - 2.0f * d * plane.Normal; - Assert.Equal(vp.M11, rp.M11, 0.0f); - Assert.Equal(vp.M12, rp.M12, 0.0f); - Assert.Equal(vp.M13, rp.M13, 0.0f); - Assert.Equal(vp.M14, rp.M14, 0.0f); - - Assert.Equal(vp.M21, rp.M21, 0.0f); - Assert.Equal(vp.M22, rp.M22, 0.0f); - Assert.Equal(vp.M23, rp.M23, 0.0f); - Assert.Equal(vp.M24, rp.M24, 0.0f); - - Assert.Equal(vp.M31, rp.M31, 0.0f); - Assert.Equal(vp.M32, rp.M32, 0.0f); - Assert.Equal(vp.M33, rp.M33, 0.0f); - Assert.Equal(vp.M34, rp.M34, 0.0f); - - Assert.Equal(vp.M41, rp.M41, 0.0f); - Assert.Equal(vp.M42, rp.M42, 0.0f); - Assert.Equal(vp.M43, rp.M43, 0.0f); - Assert.Equal(vp.M44, rp.M44, 0.0f); + AssertExtensions.Equal(vp.M11, rp.M11, 0.0f); + AssertExtensions.Equal(vp.M12, rp.M12, 0.0f); + AssertExtensions.Equal(vp.M13, rp.M13, 0.0f); + AssertExtensions.Equal(vp.M14, rp.M14, 0.0f); + + AssertExtensions.Equal(vp.M21, rp.M21, 0.0f); + AssertExtensions.Equal(vp.M22, rp.M22, 0.0f); + AssertExtensions.Equal(vp.M23, rp.M23, 0.0f); + AssertExtensions.Equal(vp.M24, rp.M24, 0.0f); + + AssertExtensions.Equal(vp.M31, rp.M31, 0.0f); + AssertExtensions.Equal(vp.M32, rp.M32, 0.0f); + AssertExtensions.Equal(vp.M33, rp.M33, 0.0f); + AssertExtensions.Equal(vp.M34, rp.M34, 0.0f); + + AssertExtensions.Equal(vp.M41, rp.M41, 0.0f); + AssertExtensions.Equal(vp.M42, rp.M42, 0.0f); + AssertExtensions.Equal(vp.M43, rp.M43, 0.0f); + AssertExtensions.Equal(vp.M44, rp.M44, 0.0f); } } } @@ -849,25 +849,25 @@ public void Matrix4x4CreateReflectionTest02() Plane plane = new Plane(0, 1, 0, 60); Matrix4x4 actual = Matrix4x4.CreateReflection(plane); - Assert.Equal(1.0f, actual.M11, 0.0f); - Assert.Equal(0.0f, actual.M12, 0.0f); - Assert.Equal(0.0f, actual.M13, 0.0f); - Assert.Equal(0.0f, actual.M14, 0.0f); - - Assert.Equal(0.0f, actual.M21, 0.0f); - Assert.Equal(-1.0f, actual.M22, 0.0f); - Assert.Equal(0.0f, actual.M23, 0.0f); - Assert.Equal(0.0f, actual.M24, 0.0f); - - Assert.Equal(0.0f, actual.M31, 0.0f); - Assert.Equal(0.0f, actual.M32, 0.0f); - Assert.Equal(1.0f, actual.M33, 0.0f); - Assert.Equal(0.0f, actual.M34, 0.0f); - - Assert.Equal(-0.0f, actual.M41, 0.0f); - Assert.Equal(-120.0f, actual.M42, 0.0f); - Assert.Equal(-0.0f, actual.M43, 0.0f); - Assert.Equal(1.0f, actual.M44, 0.0f); + AssertExtensions.Equal(1.0f, actual.M11, 0.0f); + AssertExtensions.Equal(0.0f, actual.M12, 0.0f); + AssertExtensions.Equal(0.0f, actual.M13, 0.0f); + AssertExtensions.Equal(0.0f, actual.M14, 0.0f); + + AssertExtensions.Equal(0.0f, actual.M21, 0.0f); + AssertExtensions.Equal(-1.0f, actual.M22, 0.0f); + AssertExtensions.Equal(0.0f, actual.M23, 0.0f); + AssertExtensions.Equal(0.0f, actual.M24, 0.0f); + + AssertExtensions.Equal(0.0f, actual.M31, 0.0f); + AssertExtensions.Equal(0.0f, actual.M32, 0.0f); + AssertExtensions.Equal(1.0f, actual.M33, 0.0f); + AssertExtensions.Equal(0.0f, actual.M34, 0.0f); + + AssertExtensions.Equal(-0.0f, actual.M41, 0.0f); + AssertExtensions.Equal(-120.0f, actual.M42, 0.0f); + AssertExtensions.Equal(-0.0f, actual.M43, 0.0f); + AssertExtensions.Equal(1.0f, actual.M44, 0.0f); } // A test for CreateRotationZ (float) From c8caa3b956b515bffafdc6adb577c21ee5dddb8c Mon Sep 17 00:00:00 2001 From: Steve Date: Fri, 22 Nov 2024 04:42:14 +0900 Subject: [PATCH 4/6] Update Matrix4x4Tests.cs --- .../tests/Matrix4x4Tests.cs | 22 +++---------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/src/libraries/System.Numerics.Vectors/tests/Matrix4x4Tests.cs b/src/libraries/System.Numerics.Vectors/tests/Matrix4x4Tests.cs index 090701c092a48..d4c724e18104d 100644 --- a/src/libraries/System.Numerics.Vectors/tests/Matrix4x4Tests.cs +++ b/src/libraries/System.Numerics.Vectors/tests/Matrix4x4Tests.cs @@ -820,25 +820,9 @@ public void Matrix4x4CreateReflectionTest01() float d = Vector3.Dot(v, plane.Normal); Vector3 vp = point - 2.0f * d * plane.Normal; - AssertExtensions.Equal(vp.M11, rp.M11, 0.0f); - AssertExtensions.Equal(vp.M12, rp.M12, 0.0f); - AssertExtensions.Equal(vp.M13, rp.M13, 0.0f); - AssertExtensions.Equal(vp.M14, rp.M14, 0.0f); - - AssertExtensions.Equal(vp.M21, rp.M21, 0.0f); - AssertExtensions.Equal(vp.M22, rp.M22, 0.0f); - AssertExtensions.Equal(vp.M23, rp.M23, 0.0f); - AssertExtensions.Equal(vp.M24, rp.M24, 0.0f); - - AssertExtensions.Equal(vp.M31, rp.M31, 0.0f); - AssertExtensions.Equal(vp.M32, rp.M32, 0.0f); - AssertExtensions.Equal(vp.M33, rp.M33, 0.0f); - AssertExtensions.Equal(vp.M34, rp.M34, 0.0f); - - AssertExtensions.Equal(vp.M41, rp.M41, 0.0f); - AssertExtensions.Equal(vp.M42, rp.M42, 0.0f); - AssertExtensions.Equal(vp.M43, rp.M43, 0.0f); - AssertExtensions.Equal(vp.M44, rp.M44, 0.0f); + AssertExtensions.Equal(vp.X, rp.X, 0.0f); + AssertExtensions.Equal(vp.Y, rp.Y, 0.0f); + AssertExtensions.Equal(vp.Z, rp.Z, 0.0f); } } } From 4771080e5e151a3ef32f5f287e686f34c62037bf Mon Sep 17 00:00:00 2001 From: Steve Date: Fri, 22 Nov 2024 19:01:18 +0900 Subject: [PATCH 5/6] Update Matrix4x4Tests.cs --- .../tests/Matrix4x4Tests.cs | 45 ++++++++++--------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/src/libraries/System.Numerics.Vectors/tests/Matrix4x4Tests.cs b/src/libraries/System.Numerics.Vectors/tests/Matrix4x4Tests.cs index d4c724e18104d..148c326c7634b 100644 --- a/src/libraries/System.Numerics.Vectors/tests/Matrix4x4Tests.cs +++ b/src/libraries/System.Numerics.Vectors/tests/Matrix4x4Tests.cs @@ -820,9 +820,7 @@ public void Matrix4x4CreateReflectionTest01() float d = Vector3.Dot(v, plane.Normal); Vector3 vp = point - 2.0f * d * plane.Normal; - AssertExtensions.Equal(vp.X, rp.X, 0.0f); - AssertExtensions.Equal(vp.Y, rp.Y, 0.0f); - AssertExtensions.Equal(vp.Z, rp.Z, 0.0f); + Assert.True(MathHelper.Equal(rp, vp), "Matrix4x4.CreateReflection did not provide expected value."); } } } @@ -833,25 +831,28 @@ public void Matrix4x4CreateReflectionTest02() Plane plane = new Plane(0, 1, 0, 60); Matrix4x4 actual = Matrix4x4.CreateReflection(plane); - AssertExtensions.Equal(1.0f, actual.M11, 0.0f); - AssertExtensions.Equal(0.0f, actual.M12, 0.0f); - AssertExtensions.Equal(0.0f, actual.M13, 0.0f); - AssertExtensions.Equal(0.0f, actual.M14, 0.0f); - - AssertExtensions.Equal(0.0f, actual.M21, 0.0f); - AssertExtensions.Equal(-1.0f, actual.M22, 0.0f); - AssertExtensions.Equal(0.0f, actual.M23, 0.0f); - AssertExtensions.Equal(0.0f, actual.M24, 0.0f); - - AssertExtensions.Equal(0.0f, actual.M31, 0.0f); - AssertExtensions.Equal(0.0f, actual.M32, 0.0f); - AssertExtensions.Equal(1.0f, actual.M33, 0.0f); - AssertExtensions.Equal(0.0f, actual.M34, 0.0f); - - AssertExtensions.Equal(-0.0f, actual.M41, 0.0f); - AssertExtensions.Equal(-120.0f, actual.M42, 0.0f); - AssertExtensions.Equal(-0.0f, actual.M43, 0.0f); - AssertExtensions.Equal(1.0f, actual.M44, 0.0f); + Matrix4x4 expected = new Matrix4x4(); + expected.M11 = 1.0f; + expected.M12 = 0.0f; + expected.M13 = 0.0f; + expected.M14 = 0.0f; + + expected.M21 = 0.0f; + expected.M22 = -1.0f; + expected.M23 = 0.0f; + expected.M24 = 0.0f; + + expected.M31 = 0.0f; + expected.M32 = 0.0f; + expected.M33 = 1.0f; + expected.M34 = 0.0f; + + expected.M41 = 0.0f; + expected.M42 = -120.0f; + expected.M43 = 0.0f; + expected.M44 = 1.0f; + + Assert.True(MathHelper.Equal(expected, actual), "Matrix4x4.CreateReflection did not return the expected value."); } // A test for CreateRotationZ (float) From db16a0cda4dad609ec650bdc72da10eb3faff6c2 Mon Sep 17 00:00:00 2001 From: Steve Date: Fri, 22 Nov 2024 23:06:32 +0900 Subject: [PATCH 6/6] Use AssertExtensions for new test only --- .../tests/Matrix4x4Tests.cs | 42 +++++++++---------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/src/libraries/System.Numerics.Vectors/tests/Matrix4x4Tests.cs b/src/libraries/System.Numerics.Vectors/tests/Matrix4x4Tests.cs index 148c326c7634b..2107bcf184f8d 100644 --- a/src/libraries/System.Numerics.Vectors/tests/Matrix4x4Tests.cs +++ b/src/libraries/System.Numerics.Vectors/tests/Matrix4x4Tests.cs @@ -819,7 +819,6 @@ public void Matrix4x4CreateReflectionTest01() Vector3 v = point - pp; float d = Vector3.Dot(v, plane.Normal); Vector3 vp = point - 2.0f * d * plane.Normal; - Assert.True(MathHelper.Equal(rp, vp), "Matrix4x4.CreateReflection did not provide expected value."); } } @@ -831,28 +830,25 @@ public void Matrix4x4CreateReflectionTest02() Plane plane = new Plane(0, 1, 0, 60); Matrix4x4 actual = Matrix4x4.CreateReflection(plane); - Matrix4x4 expected = new Matrix4x4(); - expected.M11 = 1.0f; - expected.M12 = 0.0f; - expected.M13 = 0.0f; - expected.M14 = 0.0f; - - expected.M21 = 0.0f; - expected.M22 = -1.0f; - expected.M23 = 0.0f; - expected.M24 = 0.0f; - - expected.M31 = 0.0f; - expected.M32 = 0.0f; - expected.M33 = 1.0f; - expected.M34 = 0.0f; - - expected.M41 = 0.0f; - expected.M42 = -120.0f; - expected.M43 = 0.0f; - expected.M44 = 1.0f; - - Assert.True(MathHelper.Equal(expected, actual), "Matrix4x4.CreateReflection did not return the expected value."); + AssertExtensions.Equal(1.0f, actual.M11, 0.0f); + AssertExtensions.Equal(0.0f, actual.M12, 0.0f); + AssertExtensions.Equal(0.0f, actual.M13, 0.0f); + AssertExtensions.Equal(0.0f, actual.M14, 0.0f); + + AssertExtensions.Equal(0.0f, actual.M21, 0.0f); + AssertExtensions.Equal(-1.0f, actual.M22, 0.0f); + AssertExtensions.Equal(0.0f, actual.M23, 0.0f); + AssertExtensions.Equal(0.0f, actual.M24, 0.0f); + + AssertExtensions.Equal(0.0f, actual.M31, 0.0f); + AssertExtensions.Equal(0.0f, actual.M32, 0.0f); + AssertExtensions.Equal(1.0f, actual.M33, 0.0f); + AssertExtensions.Equal(0.0f, actual.M34, 0.0f); + + AssertExtensions.Equal(0.0f, actual.M41, 0.0f); + AssertExtensions.Equal(-120.0f, actual.M42, 0.0f); + AssertExtensions.Equal(0.0f, actual.M43, 0.0f); + AssertExtensions.Equal(1.0f, actual.M44, 0.0f); } // A test for CreateRotationZ (float)