Skip to content

Commit e784f4d

Browse files
committed
[Core.Mathematics.Tests] Modernize code
1 parent a4b149f commit e784f4d

7 files changed

Lines changed: 241 additions & 244 deletions

File tree

sources/core/Stride.Core.Mathematics.Tests/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
22
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
3-
using System.Reflection;
3+
44
using System.Runtime.InteropServices;
55

66
// General Information about an assembly is controlled through the following

sources/core/Stride.Core.Mathematics.Tests/Stride.Core.Mathematics.Tests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
<PropertyGroup>
44
<TargetFramework>net8.0</TargetFramework>
55
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<LangVersion>latest</LangVersion>
8+
<Nullable>enable</Nullable>
69
<StrideAssemblyProcessor>true</StrideAssemblyProcessor>
710
<StrideAssemblyProcessorOptions>--auto-module-initializer --serialization</StrideAssemblyProcessorOptions>
811
<StrideBuildTags>Windows</StrideBuildTags>

sources/core/Stride.Core.Mathematics.Tests/TestColor.cs

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,37 @@
22
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
33
using Xunit;
44

5-
namespace Stride.Core.Mathematics.Tests
5+
namespace Stride.Core.Mathematics.Tests;
6+
7+
public class TestColor
68
{
7-
public class TestColor
9+
[Fact]
10+
public void TestRGB2HSVConversion()
811
{
9-
[Fact]
10-
public void TestRGB2HSVConversion()
11-
{
12-
Assert.Equal(new ColorHSV(312, 1, 1, 1), ColorHSV.FromColor(new Color4(1, 0, 0.8f, 1)));
13-
Assert.Equal(new ColorHSV(0, 0, 0, 1), ColorHSV.FromColor(Color.Black));
14-
Assert.Equal(new ColorHSV(0, 0, 1, 1), ColorHSV.FromColor(Color.White));
15-
Assert.Equal(new ColorHSV(0, 1, 1, 1), ColorHSV.FromColor(Color.Red));
16-
Assert.Equal(new ColorHSV(120, 1, 1, 1), ColorHSV.FromColor(Color.Lime));
17-
Assert.Equal(new ColorHSV(240, 1, 1, 1), ColorHSV.FromColor(Color.Blue));
18-
Assert.Equal(new ColorHSV(60, 1, 1, 1), ColorHSV.FromColor(Color.Yellow));
19-
Assert.Equal(new ColorHSV(180, 1, 1, 1), ColorHSV.FromColor(Color.Cyan));
20-
Assert.Equal(new ColorHSV(300, 1, 1, 1), ColorHSV.FromColor(Color.Magenta));
21-
Assert.Equal(new ColorHSV(0, 0, 0.7529412f, 1), ColorHSV.FromColor(Color.Silver));
22-
Assert.Equal(new ColorHSV(0, 0, 0.5019608f, 1), ColorHSV.FromColor(Color.Gray));
23-
Assert.Equal(new ColorHSV(0, 1, 0.5019608f, 1), ColorHSV.FromColor(Color.Maroon));
24-
}
12+
Assert.Equal(new ColorHSV(312, 1, 1, 1), ColorHSV.FromColor(new Color4(1, 0, 0.8f, 1)));
13+
Assert.Equal(new ColorHSV(0, 0, 0, 1), ColorHSV.FromColor(Color.Black));
14+
Assert.Equal(new ColorHSV(0, 0, 1, 1), ColorHSV.FromColor(Color.White));
15+
Assert.Equal(new ColorHSV(0, 1, 1, 1), ColorHSV.FromColor(Color.Red));
16+
Assert.Equal(new ColorHSV(120, 1, 1, 1), ColorHSV.FromColor(Color.Lime));
17+
Assert.Equal(new ColorHSV(240, 1, 1, 1), ColorHSV.FromColor(Color.Blue));
18+
Assert.Equal(new ColorHSV(60, 1, 1, 1), ColorHSV.FromColor(Color.Yellow));
19+
Assert.Equal(new ColorHSV(180, 1, 1, 1), ColorHSV.FromColor(Color.Cyan));
20+
Assert.Equal(new ColorHSV(300, 1, 1, 1), ColorHSV.FromColor(Color.Magenta));
21+
Assert.Equal(new ColorHSV(0, 0, 0.7529412f, 1), ColorHSV.FromColor(Color.Silver));
22+
Assert.Equal(new ColorHSV(0, 0, 0.5019608f, 1), ColorHSV.FromColor(Color.Gray));
23+
Assert.Equal(new ColorHSV(0, 1, 0.5019608f, 1), ColorHSV.FromColor(Color.Maroon));
24+
}
2525

26-
[Fact]
27-
public void TestHSV2RGBConversion()
28-
{
29-
Assert.Equal(Color.Black.ToColor4(), ColorHSV.FromColor(Color.Black).ToColor());
30-
Assert.Equal(Color.White.ToColor4(), ColorHSV.FromColor(Color.White).ToColor());
31-
Assert.Equal(Color.Red.ToColor4(), ColorHSV.FromColor(Color.Red).ToColor());
32-
Assert.Equal(Color.Lime.ToColor4(), ColorHSV.FromColor(Color.Lime).ToColor());
33-
Assert.Equal(Color.Blue.ToColor4(), ColorHSV.FromColor(Color.Blue).ToColor());
34-
Assert.Equal(Color.Silver.ToColor4(), ColorHSV.FromColor(Color.Silver).ToColor());
35-
Assert.Equal(Color.Maroon.ToColor4(), ColorHSV.FromColor(Color.Maroon).ToColor());
36-
Assert.Equal(new Color(184, 209, 219, 255).ToRgba(), ColorHSV.FromColor(new Color(184, 209, 219, 255)).ToColor().ToRgba());
37-
}
26+
[Fact]
27+
public void TestHSV2RGBConversion()
28+
{
29+
Assert.Equal(Color.Black.ToColor4(), ColorHSV.FromColor(Color.Black).ToColor());
30+
Assert.Equal(Color.White.ToColor4(), ColorHSV.FromColor(Color.White).ToColor());
31+
Assert.Equal(Color.Red.ToColor4(), ColorHSV.FromColor(Color.Red).ToColor());
32+
Assert.Equal(Color.Lime.ToColor4(), ColorHSV.FromColor(Color.Lime).ToColor());
33+
Assert.Equal(Color.Blue.ToColor4(), ColorHSV.FromColor(Color.Blue).ToColor());
34+
Assert.Equal(Color.Silver.ToColor4(), ColorHSV.FromColor(Color.Silver).ToColor());
35+
Assert.Equal(Color.Maroon.ToColor4(), ColorHSV.FromColor(Color.Maroon).ToColor());
36+
Assert.Equal(new Color(184, 209, 219, 255).ToRgba(), ColorHSV.FromColor(new Color(184, 209, 219, 255)).ToColor().ToRgba());
3837
}
3938
}

sources/core/Stride.Core.Mathematics.Tests/TestMatrix.cs

Lines changed: 108 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -2,116 +2,115 @@
22
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
33
using Xunit;
44

5-
namespace Stride.Core.Mathematics.Tests
5+
namespace Stride.Core.Mathematics.Tests;
6+
7+
public class TestMatrix
68
{
7-
public class TestMatrix
9+
/* Note: As seen in the TestCompose* tests, we check both expectedQuat == decompedQuat and expectedQuat == -decompedQuat
10+
* This is because different combinations of yaw/pitch/roll can result in the same *orientation*, which is what we're actually testing.
11+
* This means that decomposing a rotation matrix or quaternion can actually have multiple answers, but we arbitrarily pick
12+
* one result, and this may not have actually been the original yaw/pitch/roll the user chose.
13+
*/
14+
15+
[Theory, ClassData(typeof(TestRotationsData.YRPTestData))]
16+
public void TestDecomposeYawPitchRollFromQuaternionYPR(float yawDegrees, float pitchDegrees, float rollDegrees)
17+
{
18+
var yawRadians = MathUtil.DegreesToRadians(yawDegrees);
19+
var pitchRadians = MathUtil.DegreesToRadians(pitchDegrees);
20+
var rollRadians = MathUtil.DegreesToRadians(rollDegrees);
21+
22+
var rotQuat = Quaternion.RotationYawPitchRoll(yawRadians, pitchRadians, rollRadians);
23+
var rotMatrix = Matrix.RotationQuaternion(rotQuat);
24+
rotMatrix.Decompose(out float decomposedYaw, out float decomposedPitch, out float decomposedRoll);
25+
26+
var expectedQuat = rotQuat;
27+
var decompedQuat = Quaternion.RotationYawPitchRoll(decomposedYaw, decomposedPitch, decomposedRoll);
28+
Assert.True(expectedQuat == decompedQuat || expectedQuat == -decompedQuat, $"Quat not equals: Expected: {expectedQuat} - Actual: {decompedQuat}");
29+
}
30+
31+
[Theory, ClassData(typeof(TestRotationsData.YRPTestData))]
32+
public void TestDecomposeYawPitchRollFromMatrixYPR(float yawDegrees, float pitchDegrees, float rollDegrees)
33+
{
34+
var yawRadians = MathUtil.DegreesToRadians(yawDegrees);
35+
var pitchRadians = MathUtil.DegreesToRadians(pitchDegrees);
36+
var rollRadians = MathUtil.DegreesToRadians(rollDegrees);
37+
38+
var rotMatrix = Matrix.RotationYawPitchRoll(yawRadians, pitchRadians, rollRadians);
39+
rotMatrix.Decompose(out float decomposedYaw, out float decomposedPitch, out float decomposedRoll);
40+
41+
var expectedQuat = Quaternion.RotationYawPitchRoll(yawRadians, pitchRadians, rollRadians);
42+
var decompedQuat = Quaternion.RotationYawPitchRoll(decomposedYaw, decomposedPitch, decomposedRoll);
43+
Assert.True(expectedQuat == decompedQuat || expectedQuat == -decompedQuat, $"Quat not equals: Expected: {expectedQuat} - Actual: {decompedQuat}");
44+
}
45+
46+
[Theory, ClassData(typeof(TestRotationsData.YRPTestData))]
47+
public void TestDecomposeYawPitchRollFromMatricesZXY(float yawDegrees, float pitchDegrees, float rollDegrees)
48+
{
49+
var yawRadians = MathUtil.DegreesToRadians(yawDegrees);
50+
var pitchRadians = MathUtil.DegreesToRadians(pitchDegrees);
51+
var rollRadians = MathUtil.DegreesToRadians(rollDegrees);
52+
53+
// Yaw-Pitch-Roll is the intrinsic rotation order, so extrinsic is the reverse (ie. Z-X-Y)
54+
var rotMatrix = Matrix.RotationZ(rollRadians) * Matrix.RotationX(pitchRadians) * Matrix.RotationY(yawRadians);
55+
rotMatrix.Decompose(out float decomposedYaw, out float decomposedPitch, out float decomposedRoll);
56+
57+
var expectedQuat = Quaternion.RotationYawPitchRoll(yawRadians, pitchRadians, rollRadians);
58+
var decompedQuat = Quaternion.RotationYawPitchRoll(decomposedYaw, decomposedPitch, decomposedRoll);
59+
Assert.True(expectedQuat == decompedQuat || expectedQuat == -decompedQuat, $"Quat not equals: Expected: {expectedQuat} - Actual: {decompedQuat}");
60+
}
61+
62+
[Theory, ClassData(typeof(TestRotationsData.XYZTestData))]
63+
public void TestDecomposeXYZFromMatricesXYZ(float yawDegrees, float pitchDegrees, float rollDegrees)
64+
{
65+
var yawRadians = MathUtil.DegreesToRadians(yawDegrees);
66+
var pitchRadians = MathUtil.DegreesToRadians(pitchDegrees);
67+
var rollRadians = MathUtil.DegreesToRadians(rollDegrees);
68+
69+
var rotMatrix = Matrix.RotationX(pitchRadians) * Matrix.RotationY(yawRadians) * Matrix.RotationZ(rollRadians);
70+
rotMatrix.DecomposeXYZ(out Vector3 eulerAngles);
71+
72+
var decompedRotMatrix = Matrix.RotationX(eulerAngles.X) * Matrix.RotationY(eulerAngles.Y) * Matrix.RotationZ(eulerAngles.Z);
73+
var decompedQuat = Quaternion.RotationMatrix(decompedRotMatrix);
74+
75+
var expectedQuat = Quaternion.RotationX(pitchRadians) * Quaternion.RotationY(yawRadians) * Quaternion.RotationZ(rollRadians);
76+
Assert.True(expectedQuat == decompedQuat || expectedQuat == -decompedQuat, $"Quat not equals: Expected: {expectedQuat} - Actual: {decompedQuat}");
77+
}
78+
79+
[Fact]
80+
public void TestNumericConversion()
81+
{
82+
System.Numerics.Matrix4x4 matrix = new System.Numerics.Matrix4x4(
83+
1, 2, 3, 4,
84+
5, 6, 7, 8,
85+
9, 10, 11, 12,
86+
13, 14, 15, 16);
87+
88+
Matrix baseStrideMatrix = new Matrix(
89+
1, 2, 3, 4,
90+
5, 6, 7, 8,
91+
9, 10, 11, 12,
92+
13, 14, 15, 16);
93+
94+
Matrix strideMatrix = matrix;
95+
Assert.Equal(baseStrideMatrix, strideMatrix);
96+
}
97+
98+
[Fact]
99+
public void TestStrideConversion()
8100
{
9-
/* Note: As seen in the TestCompose* tests, we check both expectedQuat == decompedQuat and expectedQuat == -decompedQuat
10-
* This is because different combinations of yaw/pitch/roll can result in the same *orientation*, which is what we're actually testing.
11-
* This means that decomposing a rotation matrix or quaternion can actually have multiple answers, but we arbitrarily pick
12-
* one result, and this may not have actually been the original yaw/pitch/roll the user chose.
13-
*/
14-
15-
[Theory, ClassData(typeof(TestRotationsData.YRPTestData))]
16-
public void TestDecomposeYawPitchRollFromQuaternionYPR(float yawDegrees, float pitchDegrees, float rollDegrees)
17-
{
18-
var yawRadians = MathUtil.DegreesToRadians(yawDegrees);
19-
var pitchRadians = MathUtil.DegreesToRadians(pitchDegrees);
20-
var rollRadians = MathUtil.DegreesToRadians(rollDegrees);
21-
22-
var rotQuat = Quaternion.RotationYawPitchRoll(yawRadians, pitchRadians, rollRadians);
23-
var rotMatrix = Matrix.RotationQuaternion(rotQuat);
24-
rotMatrix.Decompose(out float decomposedYaw, out float decomposedPitch, out float decomposedRoll);
25-
26-
var expectedQuat = rotQuat;
27-
var decompedQuat = Quaternion.RotationYawPitchRoll(decomposedYaw, decomposedPitch, decomposedRoll);
28-
Assert.True(expectedQuat == decompedQuat || expectedQuat == -decompedQuat, $"Quat not equals: Expected: {expectedQuat} - Actual: {decompedQuat}");
29-
}
30-
31-
[Theory, ClassData(typeof(TestRotationsData.YRPTestData))]
32-
public void TestDecomposeYawPitchRollFromMatrixYPR(float yawDegrees, float pitchDegrees, float rollDegrees)
33-
{
34-
var yawRadians = MathUtil.DegreesToRadians(yawDegrees);
35-
var pitchRadians = MathUtil.DegreesToRadians(pitchDegrees);
36-
var rollRadians = MathUtil.DegreesToRadians(rollDegrees);
37-
38-
var rotMatrix = Matrix.RotationYawPitchRoll(yawRadians, pitchRadians, rollRadians);
39-
rotMatrix.Decompose(out float decomposedYaw, out float decomposedPitch, out float decomposedRoll);
40-
41-
var expectedQuat = Quaternion.RotationYawPitchRoll(yawRadians, pitchRadians, rollRadians);
42-
var decompedQuat = Quaternion.RotationYawPitchRoll(decomposedYaw, decomposedPitch, decomposedRoll);
43-
Assert.True(expectedQuat == decompedQuat || expectedQuat == -decompedQuat, $"Quat not equals: Expected: {expectedQuat} - Actual: {decompedQuat}");
44-
}
45-
46-
[Theory, ClassData(typeof(TestRotationsData.YRPTestData))]
47-
public void TestDecomposeYawPitchRollFromMatricesZXY(float yawDegrees, float pitchDegrees, float rollDegrees)
48-
{
49-
var yawRadians = MathUtil.DegreesToRadians(yawDegrees);
50-
var pitchRadians = MathUtil.DegreesToRadians(pitchDegrees);
51-
var rollRadians = MathUtil.DegreesToRadians(rollDegrees);
52-
53-
// Yaw-Pitch-Roll is the intrinsic rotation order, so extrinsic is the reverse (ie. Z-X-Y)
54-
var rotMatrix = Matrix.RotationZ(rollRadians) * Matrix.RotationX(pitchRadians) * Matrix.RotationY(yawRadians);
55-
rotMatrix.Decompose(out float decomposedYaw, out float decomposedPitch, out float decomposedRoll);
56-
57-
var expectedQuat = Quaternion.RotationYawPitchRoll(yawRadians, pitchRadians, rollRadians);
58-
var decompedQuat = Quaternion.RotationYawPitchRoll(decomposedYaw, decomposedPitch, decomposedRoll);
59-
Assert.True(expectedQuat == decompedQuat || expectedQuat == -decompedQuat, $"Quat not equals: Expected: {expectedQuat} - Actual: {decompedQuat}");
60-
}
61-
62-
[Theory, ClassData(typeof(TestRotationsData.XYZTestData))]
63-
public void TestDecomposeXYZFromMatricesXYZ(float yawDegrees, float pitchDegrees, float rollDegrees)
64-
{
65-
var yawRadians = MathUtil.DegreesToRadians(yawDegrees);
66-
var pitchRadians = MathUtil.DegreesToRadians(pitchDegrees);
67-
var rollRadians = MathUtil.DegreesToRadians(rollDegrees);
68-
69-
var rotMatrix = Matrix.RotationX(pitchRadians) * Matrix.RotationY(yawRadians) * Matrix.RotationZ(rollRadians);
70-
rotMatrix.DecomposeXYZ(out Vector3 eulerAngles);
71-
72-
var decompedRotMatrix = Matrix.RotationX(eulerAngles.X) * Matrix.RotationY(eulerAngles.Y) * Matrix.RotationZ(eulerAngles.Z);
73-
var decompedQuat = Quaternion.RotationMatrix(decompedRotMatrix);
74-
75-
var expectedQuat = Quaternion.RotationX(pitchRadians) * Quaternion.RotationY(yawRadians) * Quaternion.RotationZ(rollRadians);
76-
Assert.True(expectedQuat == decompedQuat || expectedQuat == -decompedQuat, $"Quat not equals: Expected: {expectedQuat} - Actual: {decompedQuat}");
77-
}
78-
79-
[Fact]
80-
public void TestNumericConversion()
81-
{
82-
System.Numerics.Matrix4x4 matrix = new System.Numerics.Matrix4x4(
83-
1, 2, 3, 4,
84-
5, 6, 7, 8,
85-
9, 10, 11, 12,
86-
13, 14, 15, 16);
87-
88-
Matrix baseStrideMatrix = new Matrix(
89-
1, 2, 3, 4,
90-
5, 6, 7, 8,
91-
9, 10, 11, 12,
92-
13, 14, 15, 16);
93-
94-
Matrix strideMatrix = matrix;
95-
Assert.Equal(baseStrideMatrix, strideMatrix);
96-
}
97-
98-
[Fact]
99-
public void TestStrideConversion()
100-
{
101-
Matrix matrix = new(
102-
1, 2, 3, 4,
103-
5, 6, 7, 8,
104-
9, 10, 11, 12,
105-
13, 14, 15, 16);
106-
107-
System.Numerics.Matrix4x4 baseNumericseMatrix = new(
108-
1, 2, 3, 4,
109-
5, 6, 7, 8,
110-
9, 10, 11, 12,
111-
13, 14, 15, 16);
112-
113-
System.Numerics.Matrix4x4 numericsMatrix = matrix;
114-
Assert.Equal(baseNumericseMatrix, numericsMatrix);
115-
}
101+
Matrix matrix = new(
102+
1, 2, 3, 4,
103+
5, 6, 7, 8,
104+
9, 10, 11, 12,
105+
13, 14, 15, 16);
106+
107+
System.Numerics.Matrix4x4 baseNumericseMatrix = new(
108+
1, 2, 3, 4,
109+
5, 6, 7, 8,
110+
9, 10, 11, 12,
111+
13, 14, 15, 16);
112+
113+
System.Numerics.Matrix4x4 numericsMatrix = matrix;
114+
Assert.Equal(baseNumericseMatrix, numericsMatrix);
116115
}
117116
}
Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11

22
using Xunit;
33

4-
namespace Stride.Core.Mathematics.Tests
4+
namespace Stride.Core.Mathematics.Tests;
5+
6+
public class TestPoint
57
{
6-
public class TestPoint
7-
{
8+
readonly Point testPoint1 = new(5, 5);
9+
readonly Point testPoint2 = new(10, 10);
10+
readonly Point testPoint3 = new(5, 5);
811

9-
readonly Point testPoint1 = new Point(5,5);
10-
readonly Point testPoint2 = new Point(10, 10);
11-
readonly Point testPoint3 = new Point(5, 5);
12-
13-
[Fact]
14-
public void TestPointsNotEqual()
15-
{
16-
Assert.NotEqual(testPoint1, testPoint2);
17-
}
12+
[Fact]
13+
public void TestPointsNotEqual()
14+
{
15+
Assert.NotEqual(testPoint1, testPoint2);
16+
}
1817

19-
[Fact]
20-
public void TestPointsEqual()
21-
{
22-
Assert.True(testPoint1.Equals(testPoint3));
23-
}
18+
[Fact]
19+
public void TestPointsEqual()
20+
{
21+
Assert.Equal(testPoint1, testPoint3);
2422
}
2523
}

0 commit comments

Comments
 (0)