Skip to content

Commit cd0ffc2

Browse files
ref: global refactor
1 parent 70df240 commit cd0ffc2

8 files changed

Lines changed: 588 additions & 471 deletions

File tree

src/Hypercube.Mathematics.UnitTests/Matrices/Matrix3x2Tests.cs

Lines changed: 55 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -12,110 +12,100 @@ public sealed class Matrix3x2Tests
1212
public void ZeroMatrix_HasAllZeroes()
1313
{
1414
var matrix = Matrix3x2.Zero;
15-
16-
Assert.Multiple(() =>
15+
using (Assert.EnterMultipleScope())
1716
{
18-
Assert.That(matrix.M00, Is.EqualTo(0f));
19-
Assert.That(matrix.M01, Is.EqualTo(0f));
20-
Assert.That(matrix.M10, Is.EqualTo(0f));
21-
Assert.That(matrix.M11, Is.EqualTo(0f));
22-
Assert.That(matrix.M20, Is.EqualTo(0f));
23-
Assert.That(matrix.M21, Is.EqualTo(0f));
24-
});
17+
Assert.That(matrix.M00, Is.Zero);
18+
Assert.That(matrix.M01, Is.Zero);
19+
Assert.That(matrix.M10, Is.Zero);
20+
Assert.That(matrix.M11, Is.Zero);
21+
Assert.That(matrix.M20, Is.Zero);
22+
Assert.That(matrix.M21, Is.Zero);
23+
}
2524
}
2625

2726
[Test]
28-
public void OneMatrix_HasAllOnes()
27+
public void OneMatrix()
2928
{
3029
var matrix = Matrix3x2.One;
31-
32-
Assert.Multiple(() =>
30+
using (Assert.EnterMultipleScope())
3331
{
34-
Assert.That(matrix.M00, Is.EqualTo(1f));
35-
Assert.That(matrix.M01, Is.EqualTo(1f));
36-
Assert.That(matrix.M10, Is.EqualTo(1f));
37-
Assert.That(matrix.M11, Is.EqualTo(1f));
38-
Assert.That(matrix.M20, Is.EqualTo(1f));
39-
Assert.That(matrix.M21, Is.EqualTo(1f));
40-
});
32+
Assert.That(matrix.M00, Is.EqualTo(1));
33+
Assert.That(matrix.M01, Is.EqualTo(1));
34+
Assert.That(matrix.M10, Is.EqualTo(1));
35+
Assert.That(matrix.M11, Is.EqualTo(1));
36+
Assert.That(matrix.M20, Is.EqualTo(1));
37+
Assert.That(matrix.M21, Is.EqualTo(1));
38+
}
4139
}
4240

4341
[Test]
44-
public void IdentityMatrix_HasCorrectValues()
42+
public void IdentityMatrix()
4543
{
4644
var matrix = Matrix3x2.Identity;
47-
48-
Assert.Multiple(() =>
45+
using (Assert.EnterMultipleScope())
4946
{
50-
Assert.That(matrix.M00, Is.EqualTo(1f));
51-
Assert.That(matrix.M01, Is.EqualTo(0f));
52-
Assert.That(matrix.M10, Is.EqualTo(0f));
53-
Assert.That(matrix.M11, Is.EqualTo(1f));
54-
Assert.That(matrix.M20, Is.EqualTo(0f));
55-
Assert.That(matrix.M21, Is.EqualTo(0f));
56-
});
47+
Assert.That(matrix.M00, Is.EqualTo(1));
48+
Assert.That(matrix.M01, Is.Zero);
49+
Assert.That(matrix.M10, Is.Zero);
50+
Assert.That(matrix.M11, Is.EqualTo(1));
51+
Assert.That(matrix.M20, Is.Zero);
52+
Assert.That(matrix.M21, Is.Zero);
53+
}
5754
}
5855

5956
[Test]
60-
public void CreateTranslation_CorrectMatrix()
57+
public void CreateTranslation()
6158
{
6259
var matrix = Matrix3x2.CreateTranslation(3, 5);
63-
64-
Assert.Multiple(() =>
60+
using (Assert.EnterMultipleScope())
6561
{
66-
Assert.That(matrix.M00, Is.EqualTo(1f));
67-
Assert.That(matrix.M01, Is.EqualTo(0f));
68-
Assert.That(matrix.M10, Is.EqualTo(0f));
69-
Assert.That(matrix.M11, Is.EqualTo(1f));
70-
Assert.That(matrix.M20, Is.EqualTo(3f));
71-
Assert.That(matrix.M21, Is.EqualTo(5f));
72-
});
62+
Assert.That(matrix.M00, Is.EqualTo(1));
63+
Assert.That(matrix.M01, Is.Zero);
64+
Assert.That(matrix.M10, Is.Zero);
65+
Assert.That(matrix.M11, Is.EqualTo(1));
66+
Assert.That(matrix.M20, Is.EqualTo(3));
67+
Assert.That(matrix.M21, Is.EqualTo(5));
68+
}
7369
}
7470

7571
[Test]
76-
public void CreateScale_CorrectMatrix()
72+
public void CreateScale()
7773
{
7874
var matrix = Matrix3x2.CreateScale(2, 3);
79-
80-
Assert.Multiple(() =>
75+
using (Assert.EnterMultipleScope())
8176
{
82-
Assert.That(matrix.M00, Is.EqualTo(2f));
83-
Assert.That(matrix.M01, Is.EqualTo(0f));
84-
Assert.That(matrix.M10, Is.EqualTo(0f));
85-
Assert.That(matrix.M11, Is.EqualTo(3f));
86-
Assert.That(matrix.M20, Is.EqualTo(0f));
87-
Assert.That(matrix.M21, Is.EqualTo(0f));
88-
});
77+
Assert.That(matrix.M00, Is.EqualTo(2));
78+
Assert.That(matrix.M01, Is.Zero);
79+
Assert.That(matrix.M10, Is.Zero);
80+
Assert.That(matrix.M11, Is.EqualTo(3));
81+
Assert.That(matrix.M20, Is.Zero);
82+
Assert.That(matrix.M21, Is.Zero);
83+
}
8984
}
9085

9186
[Test]
92-
public void CreateRotation_90Degrees_RotatesCorrectly()
87+
public void CreateRotation()
9388
{
94-
var angle = Math.PI / 2;
95-
var matrix = Matrix3x2.CreateRotation(angle);
96-
97-
Assert.Multiple(() =>
89+
var matrix = Matrix3x2.CreateRotation(HyperMath.PIOver2);
90+
using (Assert.EnterMultipleScope())
9891
{
9992
Assert.That(matrix.M00, Is.EqualTo(0f).Within(1e-5));
10093
Assert.That(matrix.M01, Is.EqualTo(1f).Within(1e-5));
10194
Assert.That(matrix.M10, Is.EqualTo(-1f).Within(1e-5));
10295
Assert.That(matrix.M11, Is.EqualTo(0f).Within(1e-5));
103-
});
96+
}
10497
}
10598

10699
[Test]
107-
public void Transform_AppliesTransformation()
100+
public void TransformVector2()
108101
{
109-
var matrix = new Matrix3x2(
110-
2, 0, // scale X
111-
0, 3, // scale Y
112-
1, 1 // translation
113-
);
114-
115-
var v = new Vector2(1, 1);
116-
var result = matrix.Transform(v);
102+
var result = new Matrix3x2(
103+
2, 0,
104+
0, 3,
105+
1, 1
106+
) * Vector2.One;
117107

118-
// Expected: (1*2 + 1*0 + 1, 1*0 + 1*3 + 1) = (3, 4)
108+
// Expected: (1 * 2 + 1 * 0 + 1, 1 * 0 + 1 * 3 + 1) = (3, 4)
119109
Assert.That(result, Is.EqualTo(new Vector2(3, 4)));
120110
}
121111
}
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
using Hypercube.Mathematics.Matrices;
3+
using Hypercube.Mathematics.Vectors;
4+
5+
namespace Hypercube.Mathematics.UnitTests.Matrices;
6+
7+
[TestFixture]
8+
[SuppressMessage("ReSharper", "InconsistentNaming")]
9+
public sealed class Matrix3x3Tests
10+
{
11+
[Test]
12+
public void Multiply_IdentityMatrix()
13+
{
14+
var original = new Vector3(1, 2, 3);
15+
var result = Matrix3x3.Identity * original;
16+
AssertAreEqual(original, result);
17+
}
18+
19+
[Test]
20+
public void Multiply_ScaleMatrix_Vector3()
21+
{
22+
var scale = new Matrix3x3(
23+
2, 0, 0,
24+
0, 3, 0,
25+
0, 0, 4
26+
);
27+
28+
var result = scale * new Vector3(1, 2, 3);
29+
AssertAreEqual(new Vector3(2, 6, 12), result);
30+
}
31+
32+
[Test]
33+
public void Multiply_Matrix3x3()
34+
{
35+
var a = new Matrix3x3(
36+
1, 2, 3,
37+
4, 5, 6,
38+
7, 8, 9
39+
);
40+
41+
var b = new Matrix3x3(
42+
9, 8, 7,
43+
6, 5, 4,
44+
3, 2, 1
45+
);
46+
47+
var expected = new Matrix3x3(
48+
30, 24, 18,
49+
84, 69, 54,
50+
138, 114, 90
51+
);
52+
53+
var result = a * b;
54+
AssertAreEqual(expected, result);
55+
}
56+
57+
[Test]
58+
public void Addition_AddTwoMatrices()
59+
{
60+
var a = new Matrix3x3(
61+
1, 2, 3,
62+
4, 5, 6,
63+
7, 8, 9
64+
);
65+
66+
var b = new Matrix3x3(
67+
9, 8, 7,
68+
6, 5, 4,
69+
3, 2, 1
70+
);
71+
72+
var expected = new Matrix3x3(
73+
10, 10, 10,
74+
10, 10, 10,
75+
10, 10, 10
76+
);
77+
78+
var result = a + b;
79+
AssertAreEqual(expected, result);
80+
}
81+
82+
[Test]
83+
public void Multiply_Scalar()
84+
{
85+
var a = new Matrix3x3(
86+
1, 2, 3,
87+
4, 5, 6,
88+
7, 8, 9
89+
);
90+
91+
var expected = new Matrix3x3(
92+
2, 4, 6,
93+
8, 10, 12,
94+
14, 16, 18
95+
);
96+
97+
var result = a * 2;
98+
AssertAreEqual(expected, result);
99+
}
100+
101+
[Test]
102+
public void Equals_SameValues()
103+
{
104+
var a = new Matrix3x3(
105+
1, 2, 3,
106+
4, 5, 6,
107+
7, 8, 9
108+
);
109+
110+
var b = new Matrix3x3(
111+
1, 2, 3,
112+
4, 5, 6,
113+
7, 8, 9
114+
);
115+
116+
Assert.That(a, Is.EqualTo(b));
117+
}
118+
119+
[Test]
120+
public void Equals_DifferentValues()
121+
{
122+
var a = new Matrix3x3(
123+
1, 2, 3,
124+
4, 5, 6,
125+
7, 8, 9
126+
);
127+
128+
var b = new Matrix3x3(
129+
1, 2, 3,
130+
4, 5, 6,
131+
7, 8, 10
132+
);
133+
134+
Assert.That(a, Is.Not.EqualTo(b));
135+
}
136+
137+
[Test]
138+
public void Multiply_Vector3()
139+
{
140+
var m = new Matrix3x3(
141+
1, 0, 10,
142+
0, 1, 20,
143+
0, 0, 1
144+
);
145+
146+
var v = new Vector3(1, 2, 1);
147+
var result = m * v;
148+
149+
AssertAreEqual(new Vector3(11, 22, 1), result);
150+
}
151+
152+
private static void AssertAreEqual(Matrix3x3 expected, Matrix3x3 actual, float delta = HyperMath.FloatTolerance)
153+
{
154+
using (Assert.EnterMultipleScope())
155+
{
156+
Assert.That(actual.M00, Is.EqualTo(expected.M00).Within(delta));
157+
Assert.That(actual.M01, Is.EqualTo(expected.M01).Within(delta));
158+
Assert.That(actual.M02, Is.EqualTo(expected.M02).Within(delta));
159+
Assert.That(actual.M10, Is.EqualTo(expected.M10).Within(delta));
160+
Assert.That(actual.M11, Is.EqualTo(expected.M11).Within(delta));
161+
Assert.That(actual.M12, Is.EqualTo(expected.M12).Within(delta));
162+
Assert.That(actual.M20, Is.EqualTo(expected.M20).Within(delta));
163+
Assert.That(actual.M21, Is.EqualTo(expected.M21).Within(delta));
164+
Assert.That(actual.M22, Is.EqualTo(expected.M22).Within(delta));
165+
}
166+
}
167+
168+
private static void AssertAreEqual(Vector3 expected, Vector3 actual, float delta = HyperMath.FloatTolerance)
169+
{
170+
using (Assert.EnterMultipleScope())
171+
{
172+
Assert.That(actual.X, Is.EqualTo(expected.X).Within(delta));
173+
Assert.That(actual.Y, Is.EqualTo(expected.Y).Within(delta));
174+
Assert.That(actual.Z, Is.EqualTo(expected.Z).Within(delta));
175+
}
176+
}
177+
}

0 commit comments

Comments
 (0)